5#include "DSCEngine/debug/assert.hpp"
6#include "DSCEngine/types/hash.hpp"
17 template<
typename K,
typename V,
int (*H)(const K&) = default_hash<K, 128>,
int S = 128>
class HashMap
20 struct Entry { K key; V value;};
54 inline int size()
const {
return _size;}
68 struct MapEntry {
const int& key;
int& value; };
79 if(h>=S)
return *
this;
81 if(i>=hmap->container[h].size()) { i=0; h++; }
84 bool operator != (
const iterator& other)
86 if(hmap != other.hmap)
return false;
87 if(h>=S && other.h>=S)
return true;
88 return h==other.h && i==other.i;
93 nds_assert(i<hmap->container[h].size());
94 return {hmap->container[h][i].key, hmap->container[h][i].value};
99 iterator end() {
return iterator(
this, S, 0);}
103template<
typename K,
typename V,
int (*H)(const K&),
int S>
107 container[h].reset();
111template<
typename K,
typename V,
int (*H)(const K&),
int S>
115 nds_assert(0<=h && h<S);
117 for(
int i=0;i<container[h].size();i++)
118 if(container[h][i].key==key)
123template<
typename K,
typename V,
int (*H)(const K&),
int S>
127 nds_assert(0<=h && h<S);
129 for(
int i=0;i<container[h].size();i++)
130 if(container[h][i].key==key)
131 return container[h][i].value;
133 container[h].push_back({key, V()});
135 return container[h][container[h].size()-1].value;
139template<
typename K,
typename V,
int (*H)(const K&),
int S>
143 nds_assert(0<=h && h<S);
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");
151template<
typename K,
typename V,
int (*H)(const K&),
int S>
155 nds_assert(0<=h && h<S);
157 for(
int i=0;i<container[h].size();i++)
158 if(container[h][i].key==key)
160 container[h].remove_at(i);
166template<
typename K,
typename V,
int (*H)(const K&),
int S>
174template<
typename K,
typename V,
int (*H)(const K&),
int S>
178 container[i] = DSC::_move_(other.container[i]);
183template<
typename K,
typename V,
int (*H)(const K&),
int S>
192template<
typename K,
typename V,
int (*H)(const K&),
int S>
196 container[i] = DSC::_move_(other.container[i]);
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.