MT Core (C++)
Core library for replacing C++ standard in project usage
|
Represents a generational list where removed items are marked and recycled. More...
#include <gen_list.hpp>
Classes | |
struct | ConstIter |
An iterator that iterates over values (copies) of each stored element. More... | |
struct | HandleIter |
An iterator that iterates over all handles of each stored element. More... | |
struct | PtrIter |
An iterator that iterates over pointers to each stored element. More... | |
Public Member Functions | |
Result< void, AllocationError > | init (Allocator &alloc, size_t initCapacity=64) |
Initializes a generational list. | |
void | deinit (Allocator &alloc) |
Deinitializes a list. | |
Result< Handle, CollectionAddNoAllocationError > | add (const T &item) noexcept |
Adds a new item to the generational list Will first try to recycle removed items Will then try to add a new item if there is remaining capacity Will fail if there is no remaining capacity. | |
Result< Handle, AllocationError > | add (Allocator &alloc, const T &item) noexcept |
Adds a new item to the generational list Will first try to recycle removed items Will then try to add a new item if there is remaining capacity Will reallocate if there is no remaining capacity. | |
const T & | operator[] (const Handle &handle) const noexcept |
References an element by handle. | |
T & | operator[] (const Handle &handle) noexcept |
References an element by handle. | |
Optional< T > | at (const Handle &handle) const noexcept |
Gets an item at a handle If item does not exist, or handle is no longer valid, will return nullopt. | |
bool | is_handle_valid (const Handle &handle) const noexcept |
Checks if a handle is currently valid. | |
size_t | size () const noexcept |
Size of GenList (number of live elements) | |
size_t | capacity () const noexcept |
Capacity of GenList. | |
void | remove (const Handle &handle) noexcept |
Will remove the element at a handle Aborts if handle is no longer valid. | |
PtrIter | ptr_iter () noexcept |
Iterates over pointers of stored elements Allows changing stored elements directly. | |
ConstIter | iter () const noexcept |
Iterates over copied values of stored elements. | |
HandleIter | handles () const noexcept |
Iterates over handles for stored elements Allows accessing and manipulating elements by handles Since removed elements are only marked deleted, it is safe to remove handles while iterating so long as you do not try to access a deleted element by handle once it's been removed Note that adding elements while iterating may Result in undefined behavior. | |
Represents a generational list where removed items are marked and recycled.
Each element is referred to with a handle (index + generation) instead of pointers. This allows elements to be reliably referenced, even after a memory copy (such as reallocation). Additionally, it also allows detection of dead handles (i.e. the referenced element was removed), which may be important when lifetimes are complex.
Note that this data structure is not memory stable as it uses an ArrayList which does memory copies when it runs out of memory.
Accessing elements is \(O(1)\) Adding elements is \(O(N)\) (though it should be fast as we check 64 slots at once) Removing elements is \(O(1)\)
T | Stored element type |
Definition at line 59 of file gen_list.hpp.
|
inlinenoexcept |
Adds a new item to the generational list Will first try to recycle removed items Will then try to add a new item if there is remaining capacity Will reallocate if there is no remaining capacity.
alloc | Allocator to use for growing memory |
item | Item to add |
Definition at line 124 of file gen_list.hpp.
|
inlinenoexcept |
Adds a new item to the generational list Will first try to recycle removed items Will then try to add a new item if there is remaining capacity Will fail if there is no remaining capacity.
item | Item to add |
Definition at line 96 of file gen_list.hpp.
|
inlinenoexcept |
Gets an item at a handle If item does not exist, or handle is no longer valid, will return nullopt.
handle | Handle (from add) to use to access element |
Definition at line 164 of file gen_list.hpp.
|
inlinenodiscardnoexcept |
Capacity of GenList.
Definition at line 197 of file gen_list.hpp.
|
inline |
Deinitializes a list.
alloc | Allocator for memory cleanup |
Definition at line 83 of file gen_list.hpp.
|
inlinenoexcept |
Iterates over handles for stored elements Allows accessing and manipulating elements by handles Since removed elements are only marked deleted, it is safe to remove handles while iterating so long as you do not try to access a deleted element by handle once it's been removed Note that adding elements while iterating may Result in undefined behavior.
Definition at line 300 of file gen_list.hpp.
|
inline |
Initializes a generational list.
alloc | Allocator for memory allocation |
initCapacity | Initial list capacity |
Definition at line 67 of file gen_list.hpp.
|
inlinenodiscardnoexcept |
Checks if a handle is currently valid.
handle | Handle to check |
Definition at line 180 of file gen_list.hpp.
|
inlinenoexcept |
Iterates over copied values of stored elements.
Definition at line 289 of file gen_list.hpp.
|
inlinenoexcept |
References an element by handle.
handle | Handle (from add) to use to access element |
Definition at line 141 of file gen_list.hpp.
|
inlinenoexcept |
References an element by handle.
handle | Handle (from add) to use to access element |
Definition at line 152 of file gen_list.hpp.
|
inlinenoexcept |
Iterates over pointers of stored elements Allows changing stored elements directly.
Definition at line 283 of file gen_list.hpp.
|
inlinenoexcept |
Will remove the element at a handle Aborts if handle is no longer valid.
handle | Handle to remove element at |
Definition at line 204 of file gen_list.hpp.
|
inlinenodiscardnoexcept |
Size of GenList (number of live elements)
Definition at line 194 of file gen_list.hpp.