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 = std::max(this->first_unused, this->first_free);
60 this->data[index].valid =
false;
61 this->first_free = std::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>
135 template <
typename Titem,
typename Tindex, Titem Tinval
id, Tindex Tgrowth_step, Tindex Tmax_size>
162 while (this->
next != Tmax_size) this->
Pop();
178 if (
this == &other)
return *
this;
179 while (this->
next != Tmax_size) this->
Pop();
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) {
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());
227 if (popped.
next != Tmax_size) {
228 ++(SmallStack::GetPool().
Get(popped.
next).branch_count);
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);