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>
164 while (this->
next != Tmax_size) this->
Pop();
180 if (
this == &other)
return *
this;
181 while (this->
next != Tmax_size) this->
Pop();
195 inline void Push(
const Titem &item)
197 if (this->
value != Tinvalid) {
199 Tindex new_item = _pool.
Create();
200 if (new_item != Tmax_size) {
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) {
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);