Gaia-ECS v1.0.0
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 {
10 template <class T>
13 T* next = nullptr;
17 T** prevs_next = nullptr;
18
21 GAIA_NODISCARD bool linked() const {
22 return next != nullptr || prevs_next != nullptr;
23 }
24 };
25
28 template <class T>
44
47 template <typename T>
52 using value_type = T;
54 using pointer = T*;
56 using reference = T&;
63
64 private:
65 T* m_pNode;
66
67 public:
70 explicit fwd_llist_iterator(T* pNode): m_pNode(pNode) {}
71
75 return *m_pNode;
76 }
80 return m_pNode;
81 }
82
86 auto& list = m_pNode->get_fwd_llist_link();
87 m_pNode = list.next;
88 return *this;
89 }
93 iterator temp(*this);
94 ++*this;
95 return temp;
96 }
97
101 GAIA_NODISCARD bool operator==(const iterator& other) const {
102 return m_pNode == other.m_pNode;
103 }
107 GAIA_NODISCARD bool operator!=(const iterator& other) const {
108 return m_pNode != other.m_pNode;
109 }
110 };
111
117 template <class T>
118 struct fwd_llist {
122 T* first = nullptr;
123
125 void clear() {
126 count = 0;
127 first = nullptr;
128 }
129
132 void link(T* pNode) {
133 GAIA_ASSERT(pNode != nullptr);
134
135 auto& link = pNode->get_fwd_llist_link();
136 link.next = first;
137 if (first != nullptr) {
138 auto& linkFirst = first->get_fwd_llist_link();
139 linkFirst.prevs_next = &(link.next);
140 first = pNode;
141 }
142 link.prevs_next = &first;
143 first = pNode;
144
145 ++count;
146 }
147
150 void unlink(T* pNode) {
151 GAIA_ASSERT(pNode != nullptr);
152
153 auto& link = pNode->get_fwd_llist_link();
154 *(link.prevs_next) = link.next;
155 if (link.next != nullptr) {
156 auto& linkNext = link.next->get_fwd_llist_link();
157 linkNext.prevs_next = link.prevs_next;
158 }
159
160 // Reset the node's link
161 link = {};
162
163 --count;
164 }
165
169 GAIA_NODISCARD bool has(T* pNode) const {
170 GAIA_ASSERT(pNode != nullptr);
171
172 for (auto& curr: *this) {
173 if (&curr == pNode)
174 return true;
175 }
176
177 return false;
178 }
179
182 GAIA_NODISCARD bool empty() const {
183 GAIA_ASSERT(count == 0);
184 return first == nullptr;
185 }
186
189 GAIA_NODISCARD uint32_t size() const {
190 return count;
191 }
192
198
202 return fwd_llist_iterator((const T*)first);
203 }
204
208 return fwd_llist_iterator((const T*)first);
209 }
210
214 return fwd_llist_iterator<T>(nullptr);
215 }
216
220 return fwd_llist_iterator((const T*)nullptr);
221 }
222
226 return fwd_llist_iterator((const T*)nullptr);
227 }
228 };
229 } // namespace cnt
230} // namespace gaia
Array with variable size of elements of type.
Definition darray_impl.h:27
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:29
fwd_llist_link< T > & get_fwd_llist_link()
Returns the mutable intrusive link.
Definition fwd_llist.h:35
const fwd_llist_link< T > & get_fwd_llist_link() const
Returns the immutable intrusive link.
Definition fwd_llist.h:40
fwd_llist_link< T > fwd_link_GAIA
Intrusive link storage used by fwd_llist.
Definition fwd_llist.h:31
Forward iterator over an intrusive fwd_llist.
Definition fwd_llist.h:48
iterator operator++(int)
Advances to the next linked node.
Definition fwd_llist.h:92
GAIA_NODISCARD bool operator==(const iterator &other) const
Compares iterator positions.
Definition fwd_llist.h:101
GAIA_NODISCARD bool operator!=(const iterator &other) const
Compares iterator positions.
Definition fwd_llist.h:107
fwd_llist_iterator(T *pNode)
Constructs an iterator at a node.
Definition fwd_llist.h:70
reference operator*() const
Dereferences the current node.
Definition fwd_llist.h:74
pointer operator->() const
Accesses the current node.
Definition fwd_llist.h:79
iterator & operator++()
Advances to the next linked node.
Definition fwd_llist.h:85
Forward list container. No memory allocation is performed because the list is stored directly inside ...
Definition fwd_llist.h:118
GAIA_NODISCARD bool empty() const
Returns true if the list is empty. False otherwise.
Definition fwd_llist.h:182
void link(T *pNode)
Links the node in the list.
Definition fwd_llist.h:132
fwd_llist_iterator< const T > cbegin() const
Returns an iterator to the first node.
Definition fwd_llist.h:207
T * first
First linked node, or nullptr when empty.
Definition fwd_llist.h:122
GAIA_NODISCARD uint32_t size() const
Returns the number of nodes linked in the list.
Definition fwd_llist.h:189
GAIA_NODISCARD bool has(T *pNode) const
Checks whether a node is linked in this list.
Definition fwd_llist.h:169
fwd_llist_iterator< const T > begin() const
Returns an iterator to the first node.
Definition fwd_llist.h:201
fwd_llist_iterator< const T > cend() const
Returns the const end sentinel.
Definition fwd_llist.h:225
void unlink(T *pNode)
Unlinks the node from the list.
Definition fwd_llist.h:150
fwd_llist_iterator< T > begin()
Returns an iterator to the first node.
Definition fwd_llist.h:195
uint32_t count
Number of linked nodes.
Definition fwd_llist.h:120
fwd_llist_iterator< T > end()
Returns the mutable end sentinel.
Definition fwd_llist.h:213
fwd_llist_iterator< const T > end() const
Returns the const end sentinel.
Definition fwd_llist.h:219
void clear()
Clears the list.
Definition fwd_llist.h:125
Definition iterator.h:12