10 #ifndef SMALLSTACK_TYPE_HPP
11 #define SMALLSTACK_TYPE_HPP
20 template<
typename Titem,
typename Tindex, Tindex Tgrowth_step, Tindex Tmax_size>
23 inline SimplePool() : first_unused(0), first_free(0) {}
30 inline std::mutex &
GetMutex() {
return this->mutex; }
36 inline Titem &
Get(Tindex index) {
return this->data[index]; }
44 Tindex index = this->FindFirstFree();
45 if (index < Tmax_size) {
46 this->data[index].valid =
true;
47 this->first_free = index + 1;
48 this->first_unused = std::max(this->first_unused, this->first_free);
59 this->data[index].valid =
false;
60 this->first_free = std::min(this->first_free, index);
65 inline Tindex FindFirstFree()
67 Tindex index = this->first_free;
68 for (; index < this->first_unused; index++) {
69 if (!this->data[index].
valid)
return index;
72 if (index >= this->data.size() && index < Tmax_size) {
73 this->data.resize(index + 1);
86 std::vector<SimplePoolPoolItem> data;
93 template <
typename Titem,
typename Tindex>
134 template <
typename Titem,
typename Tindex, Titem Tinval
id, Tindex Tgrowth_step, Tindex Tmax_size>
161 while (this->
next != Tmax_size) this->
Pop();
177 if (
this == &other)
return *
this;
178 while (this->
next != Tmax_size) this->
Pop();
192 inline void Push(
const Titem &item)
194 if (this->
value != Tinvalid) {
195 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().GetMutex());
196 Tindex new_item = SmallStack::GetPool().
Create();
197 if (new_item != Tmax_size) {
202 this->
next = new_item;
214 Titem ret = this->
value;
215 if (this->
next == Tmax_size) {
216 this->
value = Tinvalid;
218 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().GetMutex());
226 if (popped.
next != Tmax_size) {
227 ++(SmallStack::GetPool().
Get(popped.
next).branch_count);
245 return this->
value == Tinvalid && this->
next == Tmax_size;
255 if (item == Tinvalid || item == this->
value)
return true;
256 if (this->
next != Tmax_size) {
257 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().GetMutex());
261 static_cast<const Item *
>(&SmallStack::GetPool().
Get(in_list->
next)));
262 if (in_list->
value == item)
return true;
263 }
while (in_list->
next != Tmax_size);
269 static SmallStackPool &GetPool()
271 static SmallStackPool pool;
280 if (this->
next != Tmax_size) {
281 std::lock_guard<std::mutex>
lock(SmallStack::GetPool().GetMutex());
282 ++(SmallStack::GetPool().
Get(this->
next).branch_count);