31#ifndef FENNEC_CONTAINERS_OBJECT_POOL_H
32#define FENNEC_CONTAINERS_OBJECT_POOL_H
45template<
typename TypeT,
typename AllocT = allocator<TypeT>>
50 using value_t = TypeT;
84 constexpr size_t size()
const {
107 if (not _freed.
empty()) {
108 next = _freed.
front();
125 assert(i <
capacity(),
"Index out of Bounds!");
126 assert(_table[i],
"Attempted to access null object.")
135 assert(i <
capacity(),
"Index out of Bounds!");
136 assert(_table[i],
"Attempted to access null object.")
153 return this->_insert(fennec::forward<value_t>(x));
160 constexpr size_t insert(
const value_t& x) {
161 return this->_insert(x);
169 template<
typename...ArgsT>
171 return this->_insert(fennec::forward<ArgsT>(args)...);
186 for (
auto& it : _table) {
199 return iterator(
this, 0);
203 return iterator(
this, _size);
206 const_iterator begin()
const {
207 return iterator(
this, 0);
210 const_iterator end()
const {
211 return iterator(
this, _size);
217 : pool(pool), curr(start) {
221 iterator(
const iterator&) =
default;
222 iterator(iterator&&) noexcept = default;
224 ~iterator() = default;
226 iterator& operator=(const iterator&) = default;
227 iterator& operator=(iterator&&) noexcept = default;
229 iterator operator++(
int) {
230 iterator ret = *
this;
236 iterator& operator++() {
242 value_t& operator*()
const {
243 return *pool->_table[curr];
246 value_t* operator->()
const {
247 return *pool->_table[curr];
250 bool operator==(
const iterator& it) {
251 return pool == it.pool and curr == it.curr;
254 bool operator!=(
const iterator& it) {
255 return pool != it.pool or curr != it.curr;
263 while (curr < pool->_size and not pool->_table[curr]) {
269 class const_iterator {
271 const_iterator(
const object_pool* pool,
size_t start)
272 : pool(pool), curr(start) {
276 const_iterator(
const const_iterator&) =
default;
277 const_iterator(const_iterator&&) noexcept = default;
279 ~const_iterator() = default;
281 const_iterator& operator=(const const_iterator&) = default;
282 const_iterator& operator=(const_iterator&&) noexcept = default;
284 const_iterator operator++(
int) {
285 const_iterator ret = *
this;
291 const_iterator& operator++() {
297 const value_t& operator*()
const {
298 return *pool->_table[curr];
301 const value_t* operator->()
const {
302 return *pool->_table[curr];
305 bool operator==(
const const_iterator& it) {
306 return pool == it.pool and curr == it.curr;
309 bool operator!=(
const const_iterator& it) {
310 return pool != it.pool or curr != it.curr;
318 while (curr < pool->_size and not pool->_table[curr]) {
329 size_t _next_free() {
331 if (not _freed.empty()) {
332 next = _freed.front();
339 template<
typename...ArgsT>
340 size_t _insert(ArgsT&&...args) {
341 size_t i = _next_free();
342 if (i >= _table.size()) {
343 _table.resize(fennec::max(_table.size() * 2,
size_t(8)));
345 _table[i].emplace(fennec::forward<ArgsT>(args)...);
A header containing the definition for a dynamically allocated array.
A header containing the definition for a linked list of values.
A header containing the definition for a container with an optionally present variable.
Wrapper for dynamically sized and allocated arrays.
Definition dynarray.h:64
constexpr size_t capacity() const
Definition dynarray.h:337
Data Structure defining lists of elements.
Definition list.h:67
constexpr bool empty() const
Definition list.h:181
constexpr void clear()
Clears the list, destructing all elements in order.
Definition list.h:392
constexpr size_t push_back(const value_t &x)
Push Back Copy.
Definition list.h:342
constexpr value_t & front()
Access Front Element.
Definition list.h:222
Struct which holds a pool of objects associated with ids.
Definition object_pool.h:46
constexpr bool empty() const
Definition object_pool.h:96
constexpr ~object_pool()=default
Default Destructor, destructs objects then releases the allocation.
constexpr size_t emplace(ArgsT &&...args)
Emplacement, constructs a new object using args...
Definition object_pool.h:170
constexpr const value_t & operator[](size_t i) const
Array Const Access Operator.
Definition object_pool.h:134
constexpr void clear()
Clear the object pool.
Definition object_pool.h:185
constexpr size_t capacity() const
Definition object_pool.h:90
constexpr size_t next_id() const
Retrieve the next id i that would be assigned to an object o were it added to the object pool.
Definition object_pool.h:105
constexpr size_t insert(const value_t &x)
Move Insertion, inserts a copy of x into the pool.
Definition object_pool.h:160
constexpr size_t insert(value_t &&x)
Move Insertion, inserts x into the pool.
Definition object_pool.h:152
constexpr void erase(size_t i)
Erase an object from the pool.
Definition object_pool.h:177
constexpr value_t & operator[](size_t i)
Array Access Operator.
Definition object_pool.h:124
constexpr size_t size() const
Definition object_pool.h:84
constexpr object_pool()
Default Constructor, initializes an empty object pool.
Definition object_pool.h:66