Skip to content

Commit

Permalink
utils: simplify slot manager
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Feb 5, 2024
1 parent 8d62faa commit 0d15c3a
Showing 1 changed file with 9 additions and 32 deletions.
41 changes: 9 additions & 32 deletions code/framework/src/utils/slot_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Framework::Utils {
std::vector<bool> _occupied;
std::vector<T> _slots;
std::vector<size_t> _freeList;
bool _recycle = false;

public:
SlotManager() {
Expand All @@ -25,8 +26,12 @@ namespace Framework::Utils {
_freeList.reserve(64);
}

void RecycleSlots(bool recycle) {
_recycle = recycle;
}

size_t AllocateSlot(const T &val) {
if (_freeList.empty()) {
if (_freeList.empty() || !_recycle) {
_occupied.push_back(true);
_slots.push_back(val);
return _slots.size() - 1;
Expand All @@ -43,7 +48,9 @@ namespace Framework::Utils {
void DeallocateSlot(size_t id) {
if (id < _occupied.size()) {
_occupied[id] = false;
_freeList.push_back(id);

if (_recycle)
_freeList.push_back(id);
}
}

Expand All @@ -70,34 +77,4 @@ namespace Framework::Utils {
return nullptr;
}
};

template <typename T>
class IncrementalSlotManager {
private:
std::vector<T> _slots;

public:
IncrementalSlotManager() {
_slots.reserve(64);
}

size_t AllocateSlot(const T &val) {
_slots.push_back(val);
return _slots.size() - 1;
}

T *GetSlot(size_t id) {
if (id < _slots.size()) {
return &_slots[id];
}
return nullptr;
}

const T *GetSlot(size_t id) const {
if (id < _slots.size()) {
return &_slots[id];
}
return nullptr;
}
};
} // namespace Framework::Utils

0 comments on commit 0d15c3a

Please sign in to comment.