Elaztek Developer Hub
Blamite Game Engine - Strings  00402.09.29.23.0627.blamite
A library containing general purpose utilities and classes for use in multiple projects.
map.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <map>
5 
6 #ifdef STRINGS_EXPORTS
7 #define STRINGS_API __declspec(dllexport)
8 #else
9 #define STRINGS_API __declspec(dllimport)
10 #endif
11 
17 template<typename KeyType, typename ValueType> struct BlamMapPair
18 {
19  KeyType key;
20  ValueType value;
21 };
22 
30 template<typename KeyType, typename ValueType> class BlamMap
31 {
32 private:
33  std::vector<BlamMapPair<KeyType, ValueType>> internal_map = std::vector<BlamMapPair<KeyType, ValueType>>();
34 
35 public:
40  {
41 
42  }
43 
49  BlamMap(std::map<KeyType, ValueType> std_map)
50  {
51  typename std::map<KeyType, ValueType>::iterator it;
52 
53  for (it = std_map.begin(); it != std_map.end(); it++)
54  {
55  Add(it->first, it->second);
56  }
57  }
58 
64  int Size()
65  {
66  return internal_map.size();
67  }
68 
79  ValueType At(KeyType key)
80  {
81  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
82  {
83  if (pair.key == key)
84  {
85  return pair.value;
86  }
87  }
88 
89  throw std::exception("Map did not contain any item with matching key. Use Contains() before calling to ensure safety.");
90  }
91 
103  {
104  if (index < Size())
105  {
106  return &internal_map.at(index);
107  }
108 
109  throw std::exception("Attempted to fetch a pair with an index that was out of range. Check index against Size() first.");
110  }
111 
117  void Add(std::pair<KeyType, ValueType> pair)
118  {
119  Add(pair.first, pair.second);
120  }
121 
131  void Add(KeyType key, ValueType value)
132  {
134  {
135  new_pair.key = key;
136  new_pair.value = value;
137  }
138 
139  for (int i = 0; i < internal_map.size(); i++)
140  {
141  BlamMapPair<KeyType, ValueType> pair = internal_map.at(i);
142 
143  if (pair.key == key)
144  {
145  pair.value = value;
146  internal_map[i] = pair;
147  return;
148  }
149  }
150 
151  internal_map.push_back(new_pair);
152  }
153 
161  bool Contains(KeyType key)
162  {
163  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
164  {
165  if (pair.key == key)
166  {
167  return true;
168  }
169  }
170 
171  return false;
172  }
173 
179  std::vector<KeyType> KeySet()
180  {
181  std::vector<KeyType> key_list = std::vector<KeyType>();
182 
183  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
184  {
185  key_list.push_back(pair.key);
186  }
187 
188  return key_list;
189  }
190 
196  std::vector<ValueType> Values()
197  {
198  std::vector<ValueType> value_list = std::vector<ValueType>();
199 
200  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
201  {
202  value_list.push_back(pair.value);
203  }
204 
205  return value_list;
206  }
207 
213  std::map<KeyType, ValueType> ToStdMap()
214  {
215  std::map<KeyType, ValueType> std_map = std::map<KeyType, ValueType>();
216 
217  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
218  {
219  std_map.insert(std::pair<KeyType, ValueType>(pair.key, pair.value));
220  }
221 
222  return std_map;
223  }
224 
238  bool GetFirstKey(ValueType value, KeyType* out_key)
239  {
240  if (!out_key)
241  {
242  return false;
243  }
244 
245  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
246  {
247  if (pair.value == value)
248  {
249  *out_key = pair.key;
250  return true;
251  }
252  }
253 
254  return false;
255  }
256 
264  bool Erase(KeyType key)
265  {
266  for (int i = 0; i < internal_map.size(); i++)
267  {
268  if (internal_map.at(i).key == key)
269  {
270  internal_map.erase(internal_map.begin() + i);
271  return true;
272  }
273  }
274 
275  return false;
276  }
277 
285  bool EraseAt(int index)
286  {
287  if (index >= 0 && index < internal_map.size())
288  {
289  internal_map.erase(internal_map.begin() + index);
290  return true;
291  }
292 
293  return false;
294  }
295 
303  int EraseValues(ValueType value)
304  {
305  int count = 0;
306 
307  for (int i = internal_map.size() - 1; i >= 0; i--)
308  {
309  if (internal_map.at(i).value == value)
310  {
311  internal_map.erase(internal_map.begin() + i);
312  count++;
313  }
314  }
315 
316  return count;
317  }
318 
322  void Clear()
323  {
324  internal_map.clear();
325  }
326 };
BlamMap::Add
void Add(std::pair< KeyType, ValueType > pair)
Adds a new key/value pair to the map.
Definition: map.h:117
BlamMap::PairAt
BlamMapPair< KeyType, ValueType > * PairAt(int index)
Retrieves the pair at the specified index.
Definition: map.h:102
BlamMapPair
Class representing a BlamMapPair.
Definition: map.h:17
BlamMap::Size
int Size()
Returns the number of pairs within the map.
Definition: map.h:64
BlamMap
Class representing a BlamMap.
Definition: map.h:30
BlamMap::BlamMap
BlamMap()
Constructs a new BlamMap.
Definition: map.h:39
BlamMap::At
ValueType At(KeyType key)
Retrieves the value associated with the specified key.
Definition: map.h:79
BlamMap::Erase
bool Erase(KeyType key)
Removes the item in the map with the specified key.
Definition: map.h:264
BlamMap::Contains
bool Contains(KeyType key)
Checks if the map has a pair with the given key.
Definition: map.h:161
BlamMap::ToStdMap
std::map< KeyType, ValueType > ToStdMap()
Converts the map to a std::map.
Definition: map.h:213
BlamMap::BlamMap
BlamMap(std::map< KeyType, ValueType > std_map)
Constructs a new BlamMap.
Definition: map.h:49
BlamMap::Values
std::vector< ValueType > Values()
Creates a list of all values within the map.
Definition: map.h:196
BlamMapPair::value
ValueType value
The value of the pair.
Definition: map.h:20
BlamMap::EraseValues
int EraseValues(ValueType value)
Removes all items in the map with the specified value.
Definition: map.h:303
BlamMap::Add
void Add(KeyType key, ValueType value)
Adds a new key/value pair to the map.
Definition: map.h:131
BlamMap::EraseAt
bool EraseAt(int index)
Removes the item in the map at the specified index.
Definition: map.h:285
BlamMapPair::key
KeyType key
The key of the pair.
Definition: map.h:19
BlamMap::KeySet
std::vector< KeyType > KeySet()
Creates a list of all keys within the map.
Definition: map.h:179
BlamMap::GetFirstKey
bool GetFirstKey(ValueType value, KeyType *out_key)
Attempts to locate a pair within the map with a specified value.
Definition: map.h:238
BlamMap::Clear
void Clear()
Removes all items within the map.
Definition: map.h:322