nngn
Loading...
Searching...
No Matches
static_vector.h
Go to the documentation of this file.
1#ifndef NNGN_UTILS_STATIC_VECTOR_H
2#define NNGN_UTILS_STATIC_VECTOR_H
3
4#include <cassert>
5#include <cstddef>
6#include <vector>
7
8#include "utils/ranges.h"
9#include "utils/utils.h"
10
11namespace nngn {
12
20template<typename T>
21class static_vector : private std::vector<T> {
22 union entry { entry *next_free; T t; std::uintptr_t align; };
23 static_assert(sizeof(entry*) <= sizeof(T));
24 static_assert(alignof(entry*) <= alignof(T));
25 using vector_type = std::vector<T>;
28public:
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[];
42 ~static_vector(void) = default;
47 static_vector(void) = default;
49 explicit static_vector(std::size_t n) { this->set_capacity(n); }
54 std::size_t size(void) const;
56 std::size_t n_free(void) const;
58 bool full(void) const;
63 void set_capacity(std::size_t c);
68 T &insert(T t) { return this->emplace(std::move(t)); }
73 template<typename ...Ts> T &emplace(Ts &&...ts);
78 void erase(T *p);
83 void erase(iterator it);
84};
85
86template<typename T>
87std::size_t static_vector<T>::size(void) const {
88 return vector_type::size() - this->n_free();
89}
90
91template<typename T>
92bool static_vector<T>::full(void) const {
93 return !this->free_head && vector_type::size() == this->capacity();
94}
95
96template<typename T>
97std::size_t static_vector<T>::n_free(void) const {
98 std::size_t ret = {};
99 for(auto *p = this->free_head; p; p = p->next_free)
100 ++ret;
101 return ret;
102}
103
104template<typename T>
106 nngn::set_capacity(static_cast<vector_type*>(this), c);
107 this->free_head = {};
108}
109
110template<typename T>
111template<typename ...Ts>
113 assert(!this->full());
114 T *ret = {};
115 if(entry **next = &this->free_head; *next)
116 ret = &std::exchange(*next, (*next)->next_free)->t;
117 else
118 ret = &this->emplace_back();
119 return *ret = T(FWD(ts)...);
120}
121
122template<typename T>
124 assert(&this->front() <= p);
125 assert(p <= &this->back());
126 return this->erase(this->begin() + (p - this->data()));
127}
128
129template<typename T>
130void static_vector<T>::erase(iterator it) {
131 assert(this->begin() <= it);
132 assert(it < this->end());
133 if(&*it == &this->back())
134 return this->pop_back();
135 auto &e = reinterpret_cast<entry&>(*it);
136 e.t = {};
137 auto **const next = &this->free_head;
138 e.next_free = *next;
139 *next = &e;
140}
141
142}
143
144#endif
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
#define T(f0, f1, f2)
local c
Definition gamma.lua:11
local data
Definition house0.lua:10
Definition audio.cpp:7
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