12 #ifndef SMALLSTACK_TYPE_HPP 13 #define SMALLSTACK_TYPE_HPP 16 #include "../thread/thread.h" 23 template<
typename Titem,
typename Tindex, Tindex Tgrowth_step, Tindex Tmax_size>
40 inline Titem &
Get(Tindex index) {
return this->data[index]; }
48 Tindex index = this->FindFirstFree();
49 if (index < Tmax_size) {
50 this->data[index].valid =
true;
51 this->first_free = index + 1;
52 this->first_unused =
max(this->first_unused, this->first_free);
63 this->data[index].valid =
false;
64 this->first_free =
min(this->first_free, index);
69 inline Tindex FindFirstFree()
71 Tindex index = this->first_free;
72 for (; index < this->first_unused; index++) {
73 if (!this->data[index].
valid)
return index;
76 if (index >= this->data.Length() && index < Tmax_size) {
77 this->data.Resize(index + 1);
97 template <
typename Titem,
typename Tindex>
108 next(next), value(value) {}
137 template <
typename Titem,
typename Tindex, Titem Tinval
id, Tindex Tgrowth_step, Tindex Tmax_size>
156 inline SmallStack(
const Titem &value = Tinvalid) : Item(value, Tmax_size) {}
164 while (this->next != Tmax_size) this->Pop();
180 if (
this == &other)
return *
this;
181 while (this->next != Tmax_size) this->Pop();
182 this->next = other.
next;
183 this->value = other.
value;
195 inline void Push(
const Titem &item)
197 if (this->value != Tinvalid) {
199 Tindex new_item = _pool.Create();
200 if (new_item != Tmax_size) {
202 pushed.
value = this->value;
203 pushed.
next = this->next;
205 this->next = new_item;
217 Titem ret = this->value;
218 if (this->next == Tmax_size) {
219 this->value = Tinvalid;
223 this->value = popped.
value;
224 if (popped.branch_count == 0) {
225 _pool.Destroy(this->next);
227 --popped.branch_count;
229 if (popped.next != Tmax_size) {
230 ++(_pool.Get(popped.next).branch_count);
237 this->next = popped.next;
248 return this->value == Tinvalid && this->next == Tmax_size;
258 if (item == Tinvalid || item == this->value)
return true;
259 if (this->next != Tmax_size) {
264 static_cast<const Item *
>(&_pool.Get(in_list->next)));
265 if (in_list->value == item)
return true;
266 }
while (in_list->next != Tmax_size);
272 static SmallStackPool _pool;
279 if (this->next != Tmax_size) {
281 ++(_pool.
Get(this->next).branch_count);
Simple mutex locker to keep a mutex locked until the locker goes out of scope.
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...
Simple vector template class.
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.
A simplified pool which stores values instead of pointers and doesn't redefine operator new/delete...
static ThreadMutex * New()
Create a new mutex.
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.
Tindex branch_count
Number of branches in the tree structure this item is parent of.
ThreadMutex * GetMutex()
Get the mutex.
SmallStack(const Titem &value=Tinvalid)
Constructor for a stack with one or two items in it.