Elaztek Developer Hub
Blamite Game Engine - Strings  00386.06.16.23.0646.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 
5 #ifdef STRINGS_EXPORTS
6 #define STRINGS_API __declspec(dllexport)
7 #else
8 #define STRINGS_API __declspec(dllimport)
9 #endif
10 
11 template<typename KeyType, typename ValueType> struct BlamMapPair
12 {
13  KeyType key;
14  ValueType value;
15 };
16 
17 template<typename KeyType, typename ValueType> class BlamMap
18 {
19 private:
20  std::vector<BlamMapPair<KeyType, ValueType>> internal_map = std::vector<BlamMapPair<KeyType, ValueType>>();
21 
22 public:
24  {
25 
26  }
27 
28  BlamMap(std::map<KeyType, ValueType> std_map)
29  {
30  internal_map = std_map;
31  }
32 
33  int Size()
34  {
35  return internal_map.size();
36  }
37 
38  ValueType At(KeyType key)
39  {
40  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
41  {
42  if (pair.key == key)
43  {
44  return pair.value;
45  }
46  }
47 
48  throw std::exception("Map did not contain any item with matching key. Use Contains() before calling to ensure safety.");
49  }
50 
52  {
53  if (index < Size())
54  {
55  return internal_map.at(index);
56  }
57 
58  throw std::exception("Attempted to fetch a pair with an index that was out of range. Check index against Size() first.");
59  }
60 
61  void Add(std::pair<KeyType, ValueType> pair)
62  {
64  {
65  new_pair.key = pair.first;
66  new_pair.value = pair.second;
67  }
68 
69  internal_map.push_back(new_pair);
70  }
71 
72  void Add(KeyType key, ValueType value)
73  {
75  {
76  new_pair.key = key;
77  new_pair.value = value;
78  }
79 
80  internal_map.push_back(new_pair);
81  }
82 
83  bool Contains(KeyType key)
84  {
85  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
86  {
87  if (pair.key == key)
88  {
89  return true;
90  }
91  }
92 
93  return false;
94  }
95 
96  std::vector<KeyType> KeySet()
97  {
98  std::vector<KeyType> key_list = std::vector<KeyType>();
99 
100  for (BlamMapPair<KeyType, ValueType> pair : internal_map)
101  {
102  key_list.push_back(pair.key);
103  }
104 
105  return key_list;
106  }
107 };
BlamMap::PairAt
BlamMapPair< KeyType, ValueType > PairAt(int index)
Definition: map.h:51
BlamMap::Add
void Add(std::pair< KeyType, ValueType > pair)
Definition: map.h:61
BlamMapPair
Definition: map.h:11
BlamMap::Size
int Size()
Definition: map.h:33
BlamMap
Definition: map.h:17
BlamMap::BlamMap
BlamMap()
Definition: map.h:23
BlamMap::At
ValueType At(KeyType key)
Definition: map.h:38
BlamMap::Contains
bool Contains(KeyType key)
Definition: map.h:83
BlamMap::BlamMap
BlamMap(std::map< KeyType, ValueType > std_map)
Definition: map.h:28
BlamMapPair::value
ValueType value
Definition: map.h:14
BlamMap::Add
void Add(KeyType key, ValueType value)
Definition: map.h:72
BlamMapPair::key
KeyType key
Definition: map.h:13
BlamMap::KeySet
std::vector< KeyType > KeySet()
Definition: map.h:96