DSC Engine
Loading...
Searching...
No Matches
hash_map.hpp
1#pragma once
2
5#include "DSCEngine/debug/assert.hpp"
6#include "DSCEngine/types/hash.hpp"
7
8namespace DSC
9{
17 template<typename K, typename V, int (*H)(const K&) = default_hash<K, 128>, int S = 128> class HashMap
18 {
19 public:
20 struct Entry { K key; V value;};
21 private:
23 int _size = 0;
24 public:
29 bool contains_key(const K& key) const;
30
37 V& operator[] (const K& key);
38
45 const V& operator[] (const K& key) const;
46
52 void remove_key(const K& key);
53
54 inline int size() const {return _size;}
55
56 void clear();
57
58 HashMap() = default;
59 HashMap(const HashMap<K,V,H,S>& other);
60
62
63
64 HashMap<K,V,H,S>& operator = (const HashMap<K,V,H,S>& other);
65 HashMap<K,V,H,S>& operator = (HashMap<K,V,H,S>&& other);
66
67 // make map iterable
68 struct MapEntry { const int& key; int& value; };
69
71 {
72 private:
73 HashMap<K,V,H,S>* hmap;
74 int h, i;
75 public:
76 iterator(HashMap<K,V,H,S>* hmap, int h=0, int i=0) : hmap(hmap), h(h), i(i) {}
77 iterator operator ++()
78 {
79 if(h>=S) return *this;
80 i++;
81 if(i>=hmap->container[h].size()) { i=0; h++; }
82 return *this;
83 }
84 bool operator != (const iterator& other)
85 {
86 if(hmap != other.hmap) return false;
87 if(h>=S && other.h>=S) return true;
88 return h==other.h && i==other.i;
89 }
90 MapEntry operator*()
91 {
92 nds_assert(h<S);
93 nds_assert(i<hmap->container[h].size());
94 return {hmap->container[h][i].key, hmap->container[h][i].value};
95 }
96 };
97
98 iterator begin() { return iterator(this, 0, 0);}
99 iterator end() { return iterator(this, S, 0);}
100 };
101}
102
103template<typename K, typename V, int (*H)(const K&), int S>
105{
106 for(int h=0;h<S;h++)
107 container[h].reset();
108 _size=0;
109}
110
111template<typename K, typename V, int (*H)(const K&), int S>
113{
114 int h = H(key);
115 nds_assert(0<=h && h<S);
116
117 for(int i=0;i<container[h].size();i++)
118 if(container[h][i].key==key)
119 return true;
120 return false;
121}
122
123template<typename K, typename V, int (*H)(const K&), int S>
125{
126 int h = H(key);
127 nds_assert(0<=h && h<S);
128
129 for(int i=0;i<container[h].size();i++)
130 if(container[h][i].key==key)
131 return container[h][i].value;
132
133 container[h].push_back({key, V()});
134 _size++;
135 return container[h][container[h].size()-1].value;
136
137}
138
139template<typename K, typename V, int (*H)(const K&), int S>
140const V& DSC::HashMap<K,V,H,S>::operator[] (const K& key) const
141{
142 int h = H(key);
143 nds_assert(0<=h && h<S);
144
145 for(int i=0;i<container[h].size();i++)
146 if(container[h][i].key==key)
147 return container[h][i].value;
148 __assert__(false, "Key not found");
149}
150
151template<typename K, typename V, int (*H)(const K&), int S>
153{
154 int h = H(key);
155 nds_assert(0<=h && h<S);
156
157 for(int i=0;i<container[h].size();i++)
158 if(container[h][i].key==key)
159 {
160 container[h].remove_at(i);
161 _size--;
162 return;
163 }
164}
165
166template<typename K, typename V, int (*H)(const K&), int S>
168{
169 for(int i=0;i<S;i++)
170 container[i] = DSC::Vector<Entry>(other.container[i]);
171 _size = other._size;
172}
173
174template<typename K, typename V, int (*H)(const K&), int S>
176{
177 for(int i=0;i<S;i++)
178 container[i] = DSC::_move_(other.container[i]);
179 _size = other._size;
180 other._size = 0;
181}
182
183template<typename K, typename V, int (*H)(const K&), int S>
185{
186 for(int i=0;i<S;i++)
187 container[i] = DSC::Vector<Entry>(other.container[i]);
188 _size = other._size;
189 return *this;
190}
191
192template<typename K, typename V, int (*H)(const K&), int S>
194{
195 for(int i=0;i<S;i++)
196 container[i] = DSC::_move_(other.container[i]);
197 _size = other._size;
198 other._size = 0;
199 return *this;
200}
Definition: hash_map.hpp:71
Generic hash map.
Definition: hash_map.hpp:18
V & operator[](const K &key)
get left-value reference to the value held by a key
Definition: hash_map.hpp:124
void remove_key(const K &key)
removes key from the hash map
Definition: hash_map.hpp:152
bool contains_key(const K &key) const
checks if a key exists in the hash map
Definition: hash_map.hpp:112
Generic dynamic vector.
Definition: vector.hpp:23
Definition: hash_map.hpp:20
Definition: hash_map.hpp:68
type template definitions
Generic dynamic vector definition.