1#ifndef NNGN_UTILS_STATIC_VECTOR_H
2#define NNGN_UTILS_STATIC_VECTOR_H
23 static_assert(
sizeof(
entry*) <=
sizeof(
T));
24 static_assert(
alignof(
entry*) <=
alignof(
T));
29 using typename vector_type::iterator;
30 using typename vector_type::const_iterator;
31 using vector_type::back;
32 using vector_type::begin;
33 using vector_type::cbegin;
34 using vector_type::cend;
35 using vector_type::data;
36 using vector_type::capacity;
37 using vector_type::empty;
38 using vector_type::end;
39 using vector_type::front;
40 using vector_type::operator[];
54 std::size_t
size(
void)
const;
73 template<
typename ...Ts>
T &
emplace(Ts &&...ts);
88 return vector_type::size() - this->n_free();
93 return !this->free_head && vector_type::size() == this->capacity();
99 for(
auto *p = this->free_head; p; p = p->next_free)
107 this->free_head = {};
111template<
typename ...Ts>
116 ret = &std::exchange(*
next, (*next)->next_free)->t;
118 ret = &this->emplace_back();
119 return *ret =
T(
FWD(ts)...);
124 assert(&this->front() <= p);
125 assert(p <= &this->back());
126 return this->erase(this->
begin() + (p - this->
data()));
133 if(&*it == &this->back())
134 return this->pop_back();
135 auto &
e =
reinterpret_cast<entry&
>(*it);
137 auto **
const next = &this->free_head;
Fixed-size vector with an embedded free list.
Definition static_vector.h:21
std::size_t size(void) const
Calculates the true size (excluding the entries in the free list).
Definition static_vector.h:87
void erase(T *p)
Removes element p from the vector.
Definition static_vector.h:123
void set_capacity(std::size_t c)
Changes the total number of items of the vector can hold.
Definition static_vector.h:105
static_vector(std::size_t n)
Constructs an empty vector with capacity n.
Definition static_vector.h:49
void erase(iterator it)
Removes the element at *it from the vector.
Definition static_vector.h:130
~static_vector(void)=default
T & insert(T t)
Inserts an element in the first free position.
Definition static_vector.h:68
bool full(void) const
Checks if the vector is full (i.e.
Definition static_vector.h:92
static_vector(void)=default
Constructs an empty vector.
std::vector< T > vector_type
Definition static_vector.h:25
std::size_t n_free(void) const
Calculates the number of entries in the free list.
Definition static_vector.h:97
T & emplace(Ts &&...ts)
Emplace an element in the first free position.
Definition static_vector.h:112
entry * free_head
Pointer to the first entry in the free list.
Definition static_vector.h:27
assert
Definition debug.lua:3
function DEMO text end
Definition demo0.lua:6
map next
Definition demo1.lua:80
local n
Definition dump_lights.lua:5
local c
Definition gamma.lua:11
local data
Definition house0.lua:10
constexpr const R * begin(const T< R > &v)
Definition vec.h:207
void set_capacity(V *v, size_t n)
Definition ranges.h:73
#define FWD(...)
Definition utils.h:18
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
local e
Definition math.lua:4
Definition static_vector.h:22
entry * next_free
Definition static_vector.h:22
T t
Definition static_vector.h:22
std::uintptr_t align
Definition static_vector.h:22