diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2022-10-27 23:22:55 +0200 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2022-10-27 23:22:55 +0200 |
| commit | b47e177898657e435cb5bc913a4a783fddbe1f3d (patch) | |
| tree | df0a042b5ed9e98d7653462466dd3c6147127bc3 /include/api-test/freelist.hpp | |
| parent | cee4f9c528a91691d94c52ec1849f3ad1245b081 (diff) | |
Api prototype
Diffstat (limited to 'include/api-test/freelist.hpp')
| -rw-r--r-- | include/api-test/freelist.hpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/include/api-test/freelist.hpp b/include/api-test/freelist.hpp new file mode 100644 index 0000000..e5af6f5 --- /dev/null +++ b/include/api-test/freelist.hpp @@ -0,0 +1,32 @@ +#include <numeric> +#include <vector> + +template<typename T, const size_t initial_size = 1024U> +class freelist { + public: + freelist() + : holes(initial_size), + items(initial_size) { + std::iota(holes.begin(), holes.end(), 0); + } + + uint32_t add(const T& item) { + const auto id = holes.back(); + holes.pop_back(); + + items[id] = std::move(item); + + return id; + } + + void remove(uint32_t item_id) { + holes.push_back(item_id); + } + + const T& operator[](uint32_t index) { + return items[index]; + } + + std::vector<T> items; + std::vector<uint32_t> holes; +}; |
