From b47e177898657e435cb5bc913a4a783fddbe1f3d Mon Sep 17 00:00:00 2001 From: ladevieq Date: Thu, 27 Oct 2022 23:22:55 +0200 Subject: Api prototype --- include/api-test/freelist.hpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 include/api-test/freelist.hpp (limited to 'include/api-test/freelist.hpp') 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 +#include + +template +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 items; + std::vector holes; +}; -- cgit v1.3