UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
indexabletable.h
Go to the documentation of this file.
1 #ifndef UGDK_STRUCTURE_INDEXABLETABLE_H_
2 #define UGDK_STRUCTURE_INDEXABLETABLE_H_
3 
5 
6 #include <string>
7 #include <vector>
8 #include <unordered_map>
9 #include <memory>
10 
11 namespace ugdk {
12 namespace structure {
13 
14 template<class T>
16  public:
17  static const int MAX_NUM_INDEX = 1024;
18 
19  IndexableTable(int expected_size = 16) : index_generator_(0, MAX_NUM_INDEX - 1, -1) {
20  indices_.reserve(expected_size);
21  }
23 
24  size_t size() const {
25  return data_.size();
26  }
27 
28 #ifndef SWIG
29  void Add(const std::string& name, std::unique_ptr<T> element) {
30  data_[name] = std::move(element);
31  }
32 #endif
33 
35 
36  bool Remove(const std::string& name) {
37  auto it = data_.find(name);
38  if (it == data_.end()) {
39  return false;
40  }
41  removeIndices(it->second.get());
42  data_.erase(it);
43  return true;
44  }
45 
47  T* Search(const std::string& name) const {
48  auto it = data_.find(name);
49  if (it == data_.end()) {
50  return nullptr;
51  }
52  else {
53  return it->second.get();
54  }
55  }
56 
58  T* Get(int index) const { return indices_[index]; }
59 
60  // Optimizes access to the animations identified by the given name.
61  // The caller should be conscious of the returned indexes for later use
62  // of these animations through the Get() method.
63  // Returns the generated index or -1 if the animation was not found.
65  int MakeIndex(const std::string& name) {
66  T* val = Search(name);
67  if (!val) {
68  return index_generator_.error_value();
69  }
70  int id = index_generator_.GenerateID();
71  if(id != index_generator_.error_value()) {
72  if (static_cast<decltype(indices_.size())>(id) >= indices_.size()) {
73  indices_.resize(id + 1);
74  }
75  indices_[id] = val;
76  }
77  return id;
78  }
79 
83  bool ReleaseIndex(int id) {
84  return index_generator_.ReleaseID(id) == id;
85  }
86 
87  private:
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;
92  index_generator_.ReleaseID(id);
93  }
94  }
95  }
96 
97  util::IDGenerator index_generator_;
98  std::unordered_map<std::string, std::unique_ptr<T>> data_;
99  std::vector<T*> indices_;
100 };
101 
102 } // namespace structure
103 } // namespace ugdk
104 
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