21#ifndef MTCORE_GEN_LIST_HPP
22#define MTCORE_GEN_LIST_HPP
68 if (
auto res = entries.init(alloc, initCapacity); res.is_error()) {
71 if (
auto res = dead.init(alloc, initCapacity); res.is_error()) {
72 entries.deinit(alloc);
84 entries.deinit(alloc);
98 auto index = dead.first_set();
99 if (index.has_value() && index.value() < entries.size()) {
101 ensure(dead[i],
"BAD INDEX");
102 entries[i].generation += 1;
103 entries[i].item = item;
108 size_t i = entries.size();
109 if (
auto res = entries.push(Gen{item, 0}); res.is_error()) {
126 if (
auto res =
add(item); res.is_success()) {
130 entries.reserve(alloc, entries.capacity() * 2);
131 dead.resize(alloc, entries.capacity(),
true);
133 return add(item).value();
142 auto index = handle.index;
143 ensure(entries[index].generation == handle.generation,
"INVALID HANDLE, BAD GENERATION");
144 return entries[index].item;
153 auto index = handle.index;
154 ensure(entries[index].generation == handle.generation,
"INVALID HANDLE, BAD GENERATION");
155 return entries[index].item;
165 auto index = handle.index;
166 if (index >= entries.size()) {
169 if (entries[index].generation != handle.generation) {
172 return entries[index].item;
181 if (handle.index >= entries.size()) {
184 if (dead[handle.index]) {
187 if (entries[handle.index].generation != handle.generation) {
194 [[nodiscard]]
size_t size() const noexcept {
return entries.size() - dead.pop_count(); }
197 [[nodiscard]]
size_t capacity() const noexcept {
return entries.capacity(); }
206 dead[handle.index] =
true;
308 ArrayList<Gen> entries = {};
constexpr auto nullopt
Placeholder value for an empty Optional.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
Success< void > success()
Creates a successful void Result object.
Core library for C++ with Zig-related functionality.
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
An iterator that iterates over values (copies) of each stored element.
Optional< T > next()
Gets the next element.
An iterator that iterates over all handles of each stored element.
Optional< Handle > next()
Gets the next element.
An iterator that iterates over pointers to each stored element.
Optional< T * > next()
Gets the pointer to the next stored element.
Represents a generational list where removed items are marked and recycled.
PtrIter ptr_iter() noexcept
Iterates over pointers of stored elements Allows changing stored elements directly.
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.
HandleIter handles() const noexcept
Iterates over handles for stored elements Allows accessing and manipulating elements by handles Since...
bool is_handle_valid(const Handle &handle) const noexcept
Checks if a handle is currently valid.
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...
const T & operator[](const Handle &handle) const noexcept
References an element by handle.
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...
size_t size() const noexcept
Size of GenList (number of live elements)
Result< void, AllocationError > init(Allocator &alloc, size_t initCapacity=64)
Initializes a generational list.
ConstIter iter() const noexcept
Iterates over copied values of stored elements.
void deinit(Allocator &alloc)
Deinitializes a list.
size_t capacity() const noexcept
Capacity of GenList.
T & operator[](const Handle &handle) noexcept
References an element by handle.
void remove(const Handle &handle) noexcept
Will remove the element at a handle Aborts if handle is no longer valid.
Handle to an item in the list.
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Represents a Result that may have an error (error code) or a success value A type of "void" means the...