10 #ifndef SMALLSTACK_TYPE_HPP 11 #define SMALLSTACK_TYPE_HPP 21 template<
typename Titem,
typename Tindex, Tindex Tgrowth_step, Tindex Tmax_size>
24 inline SimplePool() : first_unused(0), first_free(0) {}
31 inline std::mutex &
GetMutex() {
return this->mutex; }
37 inline Titem &
Get(Tindex index) {
return this->data[index]; }
45 Tindex index = this->FindFirstFree();
46 if (index < Tmax_size) {
47 this->data[index].valid =
true;
48 this->first_free = index + 1;
49 this->first_unused =
max(this->first_unused, this->first_free);
60 this->data[index].valid =
false;
61 this->first_free =
min(this->first_free, index);
66 inline Tindex FindFirstFree()
68 Tindex index = this->first_free;
69 for (; index < this->first_unused; index++) {
70 if (!this->data[index].
valid)
return index;
73 if (index >= this->data.size() && index < Tmax_size) {
74 this->data.resize(index + 1);
87 std::vector<SimplePoolPoolItem> data;
94 template <
typename Titem,
typename Tindex>
105 next(next), value(value) {}
135 template <
typename Titem,
typename Tindex, Titem Tinval
id, Tindex Tgrowth_step, Tindex Tmax_size>
154 inline SmallStack(
const Titem &value = Tinvalid) : Item(value, Tmax_size) {}
162 while (this->next != Tmax_size) this->Pop();
178 if (
this == &other)
return *
this;
179 while (this->next != Tmax_size) this->Pop();
180 this->next = other.
next;
181 this->value = other.
value;
193 inline void Push(
const Titem &item)
195 if (this->value != Tinvalid) {
196 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().
GetMutex());
197 Tindex new_item = SmallStack::GetPool().
Create();
198 if (new_item != Tmax_size) {
200 pushed.
value = this->value;
201 pushed.
next = this->next;
203 this->next = new_item;
215 Titem ret = this->value;
216 if (this->next == Tmax_size) {
217 this->value = Tinvalid;
219 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().
GetMutex());
221 this->value = popped.
value;
223 SmallStack::GetPool().
Destroy(this->next);
227 if (popped.
next != Tmax_size) {
228 ++(SmallStack::GetPool().
Get(popped.
next).branch_count);
235 this->next = popped.
next;
246 return this->value == Tinvalid && this->next == Tmax_size;
256 if (item == Tinvalid || item == this->value)
return true;
257 if (this->next != Tmax_size) {
258 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().
GetMutex());
262 static_cast<const Item *
>(&SmallStack::GetPool().
Get(in_list->
next)));
263 if (in_list->
value == item)
return true;
264 }
while (in_list->
next != Tmax_size);
270 static SmallStackPool &GetPool()
272 static SmallStackPool pool;
281 if (this->next != Tmax_size) {
282 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().
GetMutex());
283 ++(SmallStack::GetPool().
Get(this->next).branch_count);
Minimal stack that uses a pool to avoid pointers.
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
static T max(const T a, const T b)
Returns the maximum of two values.
Tindex next
Pool index of next item.
bool IsEmpty() const
Check if the stack is empty.
~SmallStack()
Remove the head of stack and all other items members that are unique to it.
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
void Destroy(Tindex index)
Destroy (or rather invalidate) the item at the given index.
Titem & Get(Tindex index)
Get the item at position index.
std::mutex lock
synchronization for playback status fields
A simplified pool which stores values instead of pointers and doesn't redefine operator new/delete...
Titem Pop()
Pop an item from the stack.
bool Contains(const Titem &item) const
Check if the given item is contained in the stack.
Tindex Create()
Create a new item and return its index.
Titem value
Value of current item.
void Push(const Titem &item)
Pushes a new item onto the stack if there is still space in the underlying pool.
static T min(const T a, const T b)
Returns the minimum of two values.
SmallStack & operator=(const SmallStack &other)
Shallow copy the stack, marking the first item as branched.
SmallStack item that can be kept in a pool.
Base class for SmallStack.
SmallStackItem(const Titem &value, Tindex next)
Create a new item.
SmallStack(const SmallStack &other)
Shallow copy the stack, marking the first item as branched.
void Branch()
Create a branch in the pool if necessary.
std::mutex & GetMutex()
Get the mutex.
Tindex branch_count
Number of branches in the tree structure this item is parent of.
SmallStack(const Titem &value=Tinvalid)
Constructor for a stack with one or two items in it.