1 #ifndef UGDK_STRUCTURE_INDEXABLETABLE_H_
2 #define UGDK_STRUCTURE_INDEXABLETABLE_H_
8 #include <unordered_map>
19 IndexableTable(
int expected_size = 16) : index_generator_(0, MAX_NUM_INDEX - 1, -1) {
20 indices_.reserve(expected_size);
29 void Add(
const std::string& name, std::unique_ptr<T> element) {
30 data_[name] = std::move(element);
36 bool Remove(
const std::string& name) {
37 auto it = data_.find(name);
38 if (it == data_.end()) {
41 removeIndices(it->second.get());
47 T*
Search(
const std::string& name)
const {
48 auto it = data_.find(name);
49 if (it == data_.end()) {
53 return it->second.get();
58 T*
Get(
int index)
const {
return indices_[index]; }
72 if (
static_cast<decltype(indices_.
size())
>(
id) >= indices_.size()) {
73 indices_.resize(
id + 1);
84 return index_generator_.
ReleaseID(
id) == id;
88 void removeIndices(T* val) {
89 for (
int id = 0; id < static_cast<int>(indices_.size()); ++id) {
90 if (indices_[
id] == val) {
91 indices_[id] =
nullptr;
97 util::IDGenerator index_generator_;
98 std::unordered_map<std::string, std::unique_ptr<T>> data_;
99 std::vector<T*> indices_;
105 #endif // UGDK_STRUCTURE_INDEXABLETABLE_H_
int GenerateID()
Generate a new id.
Definition: indexabletable.h:15
bool Remove(const std::string &name)
Removes the element with the given name.
Definition: indexabletable.h:36
Definition: animation.h:11
bool ReleaseIndex(int id)
Definition: indexabletable.h:83
int MakeIndex(const std::string &name)
Generates an ID for future instant access.
Definition: indexabletable.h:65
T * Get(int index) const
Instant access to an element, using a pre-generated ID.
Definition: indexabletable.h:58
IndexableTable(int expected_size=16)
Definition: indexabletable.h:19
static const int MAX_NUM_INDEX
Definition: indexabletable.h:17
T * Search(const std::string &name) const
Searches for the element with the given name.
Definition: indexabletable.h:47
int error_value() const
The error value for this generator.
Definition: idgenerator.h:24
size_t size() const
Definition: indexabletable.h:24
~IndexableTable()
Definition: indexabletable.h:22
int ReleaseID(int id)
Release an id.
void Add(const std::string &name, std::unique_ptr< T > element)
Definition: indexabletable.h:29