Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
fwd_llist.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include "gaia/core/iterator.h"
5
6namespace gaia {
7 namespace cnt {
8 template <class T>
11 T* next = nullptr;
15 T** prevs_next = nullptr;
16
18 GAIA_NODISCARD bool linked() const {
19 return next != nullptr || prevs_next != nullptr;
20 }
21 };
22
25 template <class T>
27 fwd_llist_link<T> fwd_link_GAIA;
28
29 fwd_llist_link<T>& get_fwd_llist_link() {
30 return fwd_link_GAIA;
31 }
32 const fwd_llist_link<T>& get_fwd_llist_link() const {
33 return fwd_link_GAIA;
34 }
35 };
36
37 template <typename T>
40 using value_type = T;
41 using pointer = T*;
42 using reference = T&;
43 using difference_type = uint32_t;
44 using size_type = uint32_t;
46
47 private:
48 T* m_pNode;
49
50 public:
51 explicit fwd_llist_iterator(T* pNode): m_pNode(pNode) {}
52
53 reference operator*() const {
54 return *m_pNode;
55 }
56 pointer operator->() const {
57 return m_pNode;
58 }
59
60 iterator& operator++() {
61 auto& list = m_pNode->get_fwd_llist_link();
62 m_pNode = list.next;
63 return *this;
64 }
65 iterator operator++(int) {
66 iterator temp(*this);
67 ++*this;
68 return temp;
69 }
70
71 GAIA_NODISCARD bool operator==(const iterator& other) const {
72 return m_pNode == other.m_pNode;
73 }
74 GAIA_NODISCARD bool operator!=(const iterator& other) const {
75 return m_pNode != other.m_pNode;
76 }
77 };
78
84 template <class T>
85 struct fwd_llist {
86 uint32_t count = 0;
87 T* first = nullptr;
88
90 void clear() {
91 count = 0;
92 first = nullptr;
93 }
94
96 void link(T* pNode) {
97 GAIA_ASSERT(pNode != nullptr);
98
99 auto& link = pNode->get_fwd_llist_link();
100 link.next = first;
101 if (first != nullptr) {
102 auto& linkFirst = first->get_fwd_llist_link();
103 linkFirst.prevs_next = &(link.next);
104 first = pNode;
105 }
106 link.prevs_next = &first;
107 first = pNode;
108
109 ++count;
110 }
111
113 void unlink(T* pNode) {
114 GAIA_ASSERT(pNode != nullptr);
115
116 auto& link = pNode->get_fwd_llist_link();
117 *(link.prevs_next) = link.next;
118 if (link.next != nullptr) {
119 auto& linkNext = link.next->get_fwd_llist_link();
120 linkNext.prevs_next = link.prevs_next;
121 }
122
123 // Reset the node's link
124 link = {};
125
126 --count;
127 }
128
130 GAIA_NODISCARD bool has(T* pNode) const {
131 GAIA_ASSERT(pNode != nullptr);
132
133 for (auto& curr: *this) {
134 if (&curr == pNode)
135 return true;
136 }
137
138 return false;
139 }
140
142 GAIA_NODISCARD bool empty() const {
143 GAIA_ASSERT(count == 0);
144 return first == nullptr;
145 }
146
148 GAIA_NODISCARD uint32_t size() const {
149 return count;
150 }
151
152 fwd_llist_iterator<T> begin() {
153 return fwd_llist_iterator<T>(first);
154 }
155
156 fwd_llist_iterator<const T> begin() const {
157 return fwd_llist_iterator((const T*)first);
158 }
159
160 fwd_llist_iterator<const T> cbegin() const {
161 return fwd_llist_iterator((const T*)first);
162 }
163
164 fwd_llist_iterator<T> end() {
165 return fwd_llist_iterator<T>(nullptr);
166 }
167
168 fwd_llist_iterator<const T> end() const {
169 return fwd_llist_iterator((const T*)nullptr);
170 }
171
172 fwd_llist_iterator<const T> cend() const {
173 return fwd_llist_iterator((const T*)nullptr);
174 }
175 };
176 } // namespace cnt
177} // namespace gaia
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Each fwd_llist node either has to inherit from fwd_llist_base or it has to provide get_fwd_llist_link...
Definition fwd_llist.h:26
Definition fwd_llist.h:38
Forward list container. No memory allocation is performed because the list is stored directly inside ...
Definition fwd_llist.h:85
GAIA_NODISCARD bool empty() const
Returns true if the list is empty. False otherwise.
Definition fwd_llist.h:142
void link(T *pNode)
Links the node in the list.
Definition fwd_llist.h:96
GAIA_NODISCARD uint32_t size() const
Returns the number of nodes linked in the list.
Definition fwd_llist.h:148
GAIA_NODISCARD bool has(T *pNode) const
Checks if the node.
Definition fwd_llist.h:130
void unlink(T *pNode)
Unlinks the node from the list.
Definition fwd_llist.h:113
void clear()
Clears the list.
Definition fwd_llist.h:90
Definition iterator.h:12