R-Type
R-Type project
Dictionary.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** gameEngine2
4 ** File description:
5 ** Dictionary
6 */
7 
8 #ifndef DICTIONARY_HPP_
9 #define DICTIONARY_HPP_
10 
11 #include "Profiler/KapProfiler.hpp"
12 #include <map>
13 
14 namespace KapEngine {
15  template <typename TKey, typename TValue>
16  class Dictionary {
17  private:
18  std::map<TKey, TValue> map;
19 
20  public:
21  Dictionary() = default;
22 
23  ~Dictionary() {
24  PROFILER_FUNC_START();
25  map.clear();
26  PROFILER_FUNC_END();
27  };
28 
29  TValue& operator[](TKey key) {
30  PROFILER_FUNC_START();
31  PROFILER_FUNC_END();
32  return map[key];
33  }
34 
35  bool tryGetValue(TKey key, TValue& value) {
36  PROFILER_FUNC_START();
37  auto it = map.find(key);
38  if (it == map.end()) {
39  PROFILER_FUNC_END();
40  return false;
41  }
42  value = it->second;
43  PROFILER_FUNC_END();
44  return true;
45  }
46 
47  bool containsKey(TKey key) {
48  PROFILER_FUNC_START();
49  auto it = map.find(key);
50  PROFILER_FUNC_END();
51  return it != map.end();
52  }
53 
54  bool remove(TKey key) {
55  PROFILER_FUNC_START();
56  auto it = map.find(key);
57  if (it == map.end()) {
58  PROFILER_FUNC_END();
59  return false;
60  }
61  map.erase(it);
62  PROFILER_FUNC_END();
63  return true;
64  }
65 
66  void clear() {
67  PROFILER_FUNC_START();
68  map.clear();
69  PROFILER_FUNC_END();
70  }
71 
72  int size() const {
73  PROFILER_FUNC_START();
74  PROFILER_FUNC_END();
75  return map.size();
76  }
77 
78  class Iterator {
79  private:
80  typename std::map<TKey, TValue>::iterator it;
81 
82  public:
83  Iterator(typename std::map<TKey, TValue>::iterator _it) : it(_it) {
84  PROFILER_FUNC_START();
85  PROFILER_FUNC_END();
86  }
87 
88  Iterator& operator++() {
89  PROFILER_FUNC_START();
90  ++it;
91  PROFILER_FUNC_END();
92  return *this;
93  }
94 
95  bool operator!=(const Iterator& other) const {
96  PROFILER_FUNC_START();
97  PROFILER_FUNC_END();
98  return it != other.it;
99  }
100 
101  std::pair<TKey, TValue> operator*() const {
102  PROFILER_FUNC_START();
103  PROFILER_FUNC_END();
104  return *it;
105  }
106  };
107 
108  Iterator begin() {
109  PROFILER_FUNC_START();
110  PROFILER_FUNC_END();
111  return Iterator(map.begin());
112  }
113 
114  Iterator end() {
115  PROFILER_FUNC_START();
116  PROFILER_FUNC_END();
117  return Iterator(map.end());
118  }
119 
121  private:
122  typename std::map<TKey, TValue>::const_iterator it;
123 
124  public:
125  ConstIterator(typename std::map<TKey, TValue>::const_iterator _it) : it(_it) {
126  PROFILER_FUNC_START();
127  PROFILER_FUNC_END();
128  }
129 
130  ConstIterator& operator++() {
131  PROFILER_FUNC_START();
132  ++it;
133  PROFILER_FUNC_END();
134  return *this;
135  }
136 
137  bool operator!=(const ConstIterator& other) const {
138  PROFILER_FUNC_START();
139  PROFILER_FUNC_END();
140  return it != other.it;
141  }
142 
143  std::pair<TKey, TValue> operator*() const {
144  PROFILER_FUNC_START();
145  PROFILER_FUNC_END();
146  return *it;
147  }
148  };
149 
150  ConstIterator begin() const {
151  PROFILER_FUNC_START();
152  PROFILER_FUNC_END();
153  return ConstIterator(map.begin());
154  }
155 
156  ConstIterator end() const {
157  PROFILER_FUNC_START();
158  PROFILER_FUNC_END();
159  return ConstIterator(map.end());
160  }
161  };
162 }
163 
164 #endif
KapEngine::Dictionary::Iterator
Definition: Dictionary.hpp:78
KapEngine::Dictionary
Definition: Dictionary.hpp:16
KapEngine
main namespace
Definition: Component.hpp:17
KapEngine::Dictionary::ConstIterator
Definition: Dictionary.hpp:120