Elaztek Developer Hub
Blamite Game Engine - Strings  00411.04.21.24.0017.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:
39  BlamMap() {}
40 
46  BlamMap(std::map<KeyType, ValueType> std_map)
47  {
48  typename std::map<KeyType, ValueType>::iterator it;
49 
50  for (it = std_map.begin(); it != std_map.end(); it++)
51  {
52  Add(it->first, it->second);
53  }
54  }
55 
61  int Size()
62  {
63  return internal_map.size();
64  }
65 
76  ValueType At(KeyType key)
77  {
78  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
79  {
80  if (pair.key == key)
81  {
82  return pair.value;
83  }
84  }
85 
86  throw std::exception("Map did not contain any item with matching key. Use Contains() before calling to ensure safety.");
87  }
88 
100  {
101  if (index < Size())
102  {
103  return &internal_map.at(index);
104  }
105 
106  throw std::exception("Attempted to fetch a pair with an index that was out of range. Check index against Size() first.");
107  }
108 
114  void Add(std::pair<KeyType, ValueType> pair)
115  {
116  Add(pair.first, pair.second);
117  }
118 
128  void Add(KeyType key, ValueType value)
129  {
131  {
132  new_pair.key = key;
133  new_pair.value = value;
134  }
135 
136  for (int i = 0; i < internal_map.size(); i++)
137  {
138  BlamMapPair<KeyType, ValueType> pair = internal_map.at(i);
139 
140  if (pair.key == key)
141  {
142  pair.value = value;
143  internal_map[i] = pair;
144  return;
145  }
146  }
147 
148  internal_map.push_back(new_pair);
149  }
150 
158  bool Contains(KeyType key)
159  {
160  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
161  {
162  if (pair.key == key)
163  {
164  return true;
165  }
166  }
167 
168  return false;
169  }
170 
176  std::vector<KeyType> KeySet()
177  {
178  std::vector<KeyType> key_list = std::vector<KeyType>();
179 
180  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
181  {
182  key_list.push_back(pair.key);
183  }
184 
185  return key_list;
186  }
187 
193  std::vector<ValueType> Values()
194  {
195  std::vector<ValueType> value_list = std::vector<ValueType>();
196 
197  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
198  {
199  value_list.push_back(pair.value);
200  }
201 
202  return value_list;
203  }
204 
210  std::map<KeyType, ValueType> ToStdMap()
211  {
212  std::map<KeyType, ValueType> std_map = std::map<KeyType, ValueType>();
213 
214  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
215  {
216  std_map.insert(std::pair<KeyType, ValueType>(pair.key, pair.value));
217  }
218 
219  return std_map;
220  }
221 
235  bool GetFirstKey(ValueType value, KeyType* out_key)
236  {
237  if (!out_key)
238  {
239  return false;
240  }
241 
242  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
243  {
244  if (pair.value == value)
245  {
246  *out_key = pair.key;
247  return true;
248  }
249  }
250 
251  return false;
252  }
253 
261  bool Erase(KeyType key)
262  {
263  for (int i = 0; i < internal_map.size(); i++)
264  {
265  if (internal_map.at(i).key == key)
266  {
267  internal_map.erase(internal_map.begin() + i);
268  return true;
269  }
270  }
271 
272  return false;
273  }
274 
282  bool EraseAt(int index)
283  {
284  if (index >= 0 && index < internal_map.size())
285  {
286  internal_map.erase(internal_map.begin() + index);
287  return true;
288  }
289 
290  return false;
291  }
292 
300  int EraseValues(ValueType value)
301  {
302  int count = 0;
303 
304  for (int i = internal_map.size() - 1; i >= 0; i--)
305  {
306  if (internal_map.at(i).value == value)
307  {
308  internal_map.erase(internal_map.begin() + i);
309  count++;
310  }
311  }
312 
313  return count;
314  }
315 
319  void Clear()
320  {
321  internal_map.clear();
322  }
323 };
BlamMap::Add
void Add(std::pair< KeyType, ValueType > pair)
Adds a new key/value pair to the map.
Definition: map.h:114
BlamMap::PairAt
BlamMapPair< KeyType, ValueType > * PairAt(int index)
Retrieves the pair at the specified index.
Definition: map.h:99
BlamMapPair
Class representing a BlamMapPair.
Definition: map.h:17
BlamMap::Size
int Size()
Returns the number of pairs within the map.
Definition: map.h:61
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:76
BlamMap::Erase
bool Erase(KeyType key)
Removes the item in the map with the specified key.
Definition: map.h:261
BlamMap::Contains
bool Contains(KeyType key)
Checks if the map has a pair with the given key.
Definition: map.h:158
BlamMap::ToStdMap
std::map< KeyType, ValueType > ToStdMap()
Converts the map to a std::map.
Definition: map.h:210
BlamMap::BlamMap
BlamMap(std::map< KeyType, ValueType > std_map)
Constructs a new BlamMap.
Definition: map.h:46
BlamMap::Values
std::vector< ValueType > Values()
Creates a list of all values within the map.
Definition: map.h:193
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:300
BlamMap::Add
void Add(KeyType key, ValueType value)
Adds a new key/value pair to the map.
Definition: map.h:128
BlamMap::EraseAt
bool EraseAt(int index)
Removes the item in the map at the specified index.
Definition: map.h:282
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:176
BlamMap::GetFirstKey
bool GetFirstKey(ValueType value, KeyType *out_key)
Attempts to locate a pair within the map with a specified value.
Definition: map.h:235
BlamMap::Clear
void Clear()
Removes all items within the map.
Definition: map.h:319