Skip to content

Commit

Permalink
utils: slot manager
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Feb 5, 2024
1 parent cdf37dc commit 30630f2
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions code/framework/src/utils/slot_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* MafiaHub OSS license
* Copyright (c) 2021-2023, MafiaHub. All rights reserved.
*
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework.
* See LICENSE file in the source repository for information regarding licensing.
*/

#pragma once

#include <vector>

namespace Framework::Utils {
template <typename T>
class SlotManager {
private:
std::vector<bool> _occupied;
std::vector<T> _slots;
std::vector<size_t> _freeList;

public:
SlotManager() {
_occupied.reserve(64);
_slots.reserve(64);
_freeList.reserve(64);
}

size_t AllocateSlot(const T &val) {
if (_freeList.empty()) {
_occupied.push_back(true);
_slots.push_back(val);
return _slots.size() - 1;
}
else {
size_t id = _freeList.back();
_freeList.pop_back();
_slots[id] = val;
_occupied[id] = true;
return id;
}
}

void DeallocateSlot(size_t id) {
if (id < _occupied.size()) {
_occupied[id] = false;
_freeList.push_back(id);
}
}

void DeallocateSlot(const T &val) {
for (size_t i = 0; i < _slots.size(); i++) {
if (_slots[i] == val) {
DeallocateSlot(i);
return;
}
}
}

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

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

0 comments on commit 30630f2

Please sign in to comment.