10#ifndef SMALLSTACK_TYPE_HPP
11#define SMALLSTACK_TYPE_HPP
18template <
typename Titem,
typename Tindex, Tindex Tgrowth_step, Tindex Tmax_size>
21 inline SimplePool() : first_unused(0), first_free(0) {}
27 inline Titem &
Get(Tindex index) {
return this->data[index]; }
35 Tindex index = this->FindFirstFree();
36 if (index < Tmax_size) {
37 this->data[index].valid =
true;
38 this->first_free = index + 1;
39 this->first_unused = std::max(this->first_unused, this->first_free);
50 this->data[index].valid =
false;
51 this->first_free = std::min(this->first_free, index);
56 inline Tindex FindFirstFree()
58 Tindex index = this->first_free;
59 for (; index < this->first_unused; index++) {
60 if (!this->data[index].valid)
return index;
63 if (index >= this->data.size() && index < Tmax_size) {
64 this->data.resize(index + 1);
76 std::vector<SimplePoolPoolItem> data;
83template <
typename Titem,
typename Tindex>
120template <
typename Titem,
typename Tindex, Titem Tinval
id, Tindex Tgrowth_step, Tindex Tmax_size>
146 while (this->
next != Tmax_size) this->
Pop();
162 if (
this == &other)
return *
this;
163 while (this->
next != Tmax_size) this->
Pop();
177 inline void Push(
const Titem &item)
179 if (this->
value != Tinvalid) {
180 Tindex new_item = SmallStack::GetPool().
Create();
181 if (new_item != Tmax_size) {
186 this->
next = new_item;
177 inline void Push(
const Titem &item) {
…}
198 Titem ret = this->
value;
199 if (this->
next == Tmax_size) {
200 this->
value = Tinvalid;
208 if (popped.
next != Tmax_size) {
209 ++(SmallStack::GetPool().
Get(popped.
next).branch_count);
225 return this->
value == Tinvalid && this->
next == Tmax_size;
235 if (item == Tinvalid || item == this->
value)
return true;
236 if (this->
next != Tmax_size) {
240 static_cast<const Item *
>(&SmallStack::GetPool().
Get(in_list->
next)));
241 if (in_list->
value == item)
return true;
242 }
while (in_list->
next != Tmax_size);
248 static SmallStackPool &GetPool()
250 static SmallStackPool pool;
259 if (this->
next != Tmax_size) {
260 ++(SmallStack::GetPool().
Get(this->
next).branch_count);
A simplified pool which stores values instead of pointers and doesn't redefine operator new/delete.
Titem & Get(Tindex index)
Get the item at position index.
Tindex Create()
Create a new item and return its index.
void Destroy(Tindex index)
Destroy (or rather invalidate) the item at the given index.
Minimal stack that uses a pool to avoid pointers.
SmallStack & operator=(const SmallStack &other)
Shallow copy the stack, marking the first item as branched.
bool Contains(const Titem &item) const
Check if the given item is contained in the stack.
void Branch()
Create a branch in the pool if necessary.
Titem Pop()
Pop an item from the stack.
SmallStack(const SmallStack &other)
Shallow copy the stack, marking the first item as branched.
SmallStack(const Titem &value=Tinvalid)
Constructor for a stack with one or two items in it.
bool IsEmpty() const
Check if the stack is empty.
void Push(const Titem &item)
Pushes a new item onto the stack if there is still space in the underlying pool.
~SmallStack()
Remove the head of stack and all other items members that are unique to it.
Base class for SmallStack.
Titem value
Value of current item.
Tindex next
Pool index of next item.
SmallStackItem(const Titem &value, Tindex next)
Create a new item.
SmallStack item that can be kept in a pool.
Tindex branch_count
Number of branches in the tree structure this item is parent of.