Dynalib Utils
DynaHashMap.h
Go to the documentation of this file.
1 //
2 // Created by Ken Kopelson on 20/10/17.
3 //
4 
5 #ifndef DYNAHASHMAP_H
6 #define DYNAHASHMAP_H
7 
8 #include <typeinfo>
9 #include <iostream>
10 #include "TypeDefs.h"
11 #include "Exception.h"
12 #include "ICopyable.h"
13 #include "IHashable.h"
14 
15 #define INVALID_INDEX -1
16 
17 #define MAKE_MAPTYPE_DEF(K, V, T) \
18  typedef DynaHashMap<K,V> T##Map
19 
26 template <typename K, typename V> class MapEntry : IComparable<MapEntry<K,V>>, ICopyable<MapEntry<K,V>> {
27  K* _key = nullptr;
28  V* _value = nullptr;
29  bool _ownsValue;
30 
31 public:
32  MapEntry(K* key, V* value, bool ownsValue);
33  MapEntry(const MapEntry<K,V>& entry);
34  virtual ~MapEntry<K,V>();
35  MapEntry<K,V>* copy() override;
36 
37  K* getKey() const { return _key; }
38  V* getValue() const { return _value; }
39  bool isOwnsValue() { return _ownsValue; }
40  void setOwnsValue(bool ownsValue) { _ownsValue = ownsValue; }
41 
42  const V* setValue(V* newValue);
43  bool operator== (const MapEntry<K,V>& other) const override;
44 };
45 
50 // template <typename K> class MapObject : IHashable<K> {
51 // public:
52 // MapObject<K>() = default;
53 // virtual ~MapObject() = default;
54 
55 // int hashCode() const override {
56 // return 0;
57 // }
58 // bool operator== (const K& other) const override {
59 // return *this == other;
60 // }
61 // };
62 
63 template <typename K, typename V> class DynaMapIter;
64 template <typename K, typename V> class MapKeyIter;
65 template <typename K, typename V> class MapValueIter;
66 
73 template <typename K, typename V> class DynaHashMap : public ICopyable<DynaHashMap<K,V>> {
74 protected:
75 
76  friend class MapEntry<K,V>;
77  friend class DynaMapIter<K,V>;
78  friend class MapKeyIter<K,V>;
79  friend class MapValueIter<K,V>;
80 
83 
84  constexpr static int INITIAL_SIZE = 3;
85  constexpr static double LOAD_FACTOR = 0.75;
86 
87  MapEntry<K,V>** _table = nullptr;
88  int _count;
89  int _capacity;
90  int _freeCells; // _capacity - _bufEnd + deleteCells
91  int _modCount;
93 
94  void _init(int size);
95  int _getHashCode(const K* key) const;
96  void _reHash(int newCapacity);
97  int _getTableIndex(K* key);
98 
99 public:
100  // static MapObject<K> nullObjectInstance;
102  static K* nullObject;
103 
104  explicit DynaHashMap();
105  explicit DynaHashMap(int size);
106  virtual ~DynaHashMap();
107 
108  DynaHashMap(const DynaHashMap<K,V>& other);
109  DynaHashMap<K,V>* copy() override;
110 
111  //=========================================================================================
112  // Getters/Setters
113  //=========================================================================================
114  int count() const { return _count; }
115  int capacity() const { return _capacity; }
116  int freeCells() const { return _freeCells; }
117  bool isEmpty() const { return _count == 0; }
118  bool containsKey(K* key) { return getEntry(key) != nullptr; }
119  bool containsKey(K key) { return getEntry(key) != nullptr; }
120  bool isOwnsMembers() const { return _ownsMembers; }
121  void setOwnsMembers(bool ownsMembers) {
122  _ownsMembers = ownsMembers;
123  }
124 
125  //=========================================================================================
126  // Public Methods
127  //=========================================================================================
128  MapEntry<K,V>* getEntry(K* key);
129  MapEntry<K,V>* getEntry(K key);
130  V* get(K* key);
131  V* get(K key);
132  V* put(K* key, V* value);
133  V* put(K key, V* value);
134  V* remove(K* key);
135  V* remove(K key);
136  void deleteEntry(K* key);
137  void deleteEntry(K key);
138  MapEntry<K,V>* removeEntry(MapEntry<K,V>* entry);
139  void clear();
140  V* operator[](K key) { return get(key); }
141  V* operator[](K* key) { return get(key); }
142 
145 
146  MapKeyIter<K,V> keys();
148 };
149 
150 template <typename K, typename V> class DynaMapIter {
151  int _curIndex = 0;
152  int _lastReturned = INVALID_INDEX;
153  int _expectedModCount;
154  const DynaHashMap<K,V>* _map = nullptr;
155 
156 public:
157  DynaMapIter (const DynaHashMap<K,V>* map, int start)
158  : _curIndex(start), _expectedModCount(0), _map(map) {}
159 
160  int getIndex() { return _curIndex; }
161  bool hasNext() { return _curIndex < _map->_capacity; }
162  bool hasPrev() { return _curIndex >= 0; }
163 
164  bool operator== (const DynaMapIter<K,V>& other) const {
165  return _curIndex == other._curIndex;
166  }
167 
168  bool operator!= (const DynaMapIter<K,V>& other) const {
169  return _curIndex != other._curIndex;
170  }
171 
172  MapEntry<K,V>* operator* () const {
173  return _map->_table[_curIndex];
174  }
175 
176  const DynaMapIter<K,V>& operator++ () {
177  ++_curIndex;
178  return *this;
179  }
180 
181  const DynaMapIter<K,V>& operator-- () {
182  --_curIndex;
183  return *this;
184  }
185 };
186 
187 template <typename K, typename V> class MapKeyIter {
188  int _curIndex = 0;
189  const DynaHashMap<K,V>* _map = nullptr;
190 
191 public:
192  MapKeyIter (const DynaHashMap<K,V>* map, int start);
193  explicit MapKeyIter (const DynaHashMap<K,V>* map) : MapKeyIter(map, 0) {};
194 
195  int getIndex() { return _curIndex; }
196  bool hasNext() { return _curIndex < _map->_capacity; }
197  bool hasPrev() { return _curIndex >= 0; }
198 
199  bool operator== (const MapKeyIter<K,V>& other) const {
200  return _curIndex == other._curIndex;
201  }
202 
203  bool operator!= (const MapKeyIter<K,V>& other) const {
204  return _curIndex != other._curIndex;
205  }
206 
207  K* operator* () const {
208  auto* entry = _map->_table[_curIndex];
209  if (entry != nullptr) {
210  K* key = entry->getKey();
211  return key == _map->nullObject ? nullptr : key;
212  }
213  return nullptr;
214  }
215 
216  const MapKeyIter<K,V>& operator++ ();
217  const MapKeyIter<K,V>& operator-- ();
218 
220  return MapKeyIter<K,V>( _map, _curIndex );
221  }
222 
224  return MapKeyIter<K,V>( _map, _map->capacity() );
225  }
226 };
227 
228 template <typename K, typename V> class MapValueIter {
229  int _curIndex = 0;
230  const DynaHashMap<K,V>* _map = nullptr;
231 
232 public:
233  MapValueIter (const DynaHashMap<K,V>* map, int start);
234  explicit MapValueIter (const DynaHashMap<K,V>* map) : MapValueIter(map, 0) {};
235 
236  int getIndex() { return _curIndex; }
237  bool hasNext() { return _curIndex < _map->_capacity; }
238  bool hasPrev() { return _curIndex >= 0; }
239 
240  bool operator== (const MapValueIter<K,V>& other) const {
241  return _curIndex == other._curIndex;
242  }
243 
244  bool operator!= (const MapValueIter<K,V>& other) const {
245  return _curIndex != other._curIndex;
246  }
247 
248  V* operator* () const {
249  auto* entry = _map->_table[_curIndex];
250  return entry != nullptr ? entry->getValue() : nullptr;
251  }
252 
253  const MapValueIter<K,V>& operator++ ();
254  const MapValueIter<K,V>& operator-- ();
255 
257  return MapValueIter<K,V>( _map, _curIndex );
258  }
259 
261  return MapValueIter<K,V>( _map, _map->capacity() );
262  }
263 };
264 
265 #endif //DYNAHASHMAP_H
bool isOwnsValue()
Definition: DynaHashMap.h:39
int freeCells() const
Definition: DynaHashMap.h:116
GeneratorWrapper< T > values(std::initializer_list< T > values)
Definition: catch.hpp:4009
Definition: DynaHashMap.h:26
bool _ownsMembers
Definition: DynaHashMap.h:92
bool hasNext()
Definition: DynaHashMap.h:196
V * operator[](K *key)
Definition: DynaHashMap.h:141
bool hasPrev()
Definition: DynaHashMap.h:238
static K * nullObject
Definition: DynaHashMap.h:102
K * getKey() const
Definition: DynaHashMap.h:37
bool hasNext()
Definition: DynaHashMap.h:237
int getIndex()
Definition: DynaHashMap.h:160
MapKeyIter< K, V > end()
Definition: DynaHashMap.h:223
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4005
bool hasPrev()
Definition: DynaHashMap.h:162
int count() const
Definition: DynaHashMap.h:114
Definition: DynaHashMap.h:73
int capacity() const
Definition: DynaHashMap.h:115
void setOwnsValue(bool ownsValue)
Definition: DynaHashMap.h:40
static MapEntry< K, V > deletedObjectInstance
Definition: DynaHashMap.h:81
const V * setValue(V *newValue)
Definition: DynaHashMapImpl.h:196
int getIndex()
Definition: DynaHashMap.h:195
DynaMapIter(const DynaHashMap< K, V > *map, int start)
Definition: DynaHashMap.h:157
MapKeyIter< K, V > begin()
Definition: DynaHashMap.h:219
V * getValue() const
Definition: DynaHashMap.h:38
Definition: DynaHashMap.h:64
MapKeyIter(const DynaHashMap< K, V > *map)
Definition: DynaHashMap.h:193
MapEntry< K, V > ** _table
Definition: DynaHashMap.h:87
static MapEntry< K, V > * deletedObject
Definition: DynaHashMap.h:82
CONSTCD11 bool operator!=(const day &x, const day &y) NOEXCEPT
Definition: date.h:1282
bool operator==(const MapEntry< K, V > &other) const override
Definition: DynaHashMapImpl.h:201
bool isOwnsMembers() const
Definition: DynaHashMap.h:120
bool isEmpty() const
Definition: DynaHashMap.h:117
MapValueIter< K, V > begin()
Definition: DynaHashMap.h:256
MapValueIter(const DynaHashMap< K, V > *map)
Definition: DynaHashMap.h:234
int getIndex()
Definition: DynaHashMap.h:236
bool containsKey(K key)
Definition: DynaHashMap.h:119
bool hasPrev()
Definition: DynaHashMap.h:197
Definition: DynaHashMap.h:63
int _freeCells
Definition: DynaHashMap.h:90
int _capacity
Definition: DynaHashMap.h:89
bool hasNext()
Definition: DynaHashMap.h:161
MapEntry< K, V > * copy() override
Definition: DynaHashMapImpl.h:108
Definition: IComparable.h:8
bool containsKey(K *key)
Definition: DynaHashMap.h:118
MapValueIter< K, V > end()
Definition: DynaHashMap.h:260
static K nullObjectInstance
Definition: DynaHashMap.h:101
#define INVALID_INDEX
Definition: DynaHashMap.h:15
void setOwnsMembers(bool ownsMembers)
Definition: DynaHashMap.h:121
MapEntry(K *key, V *value, bool ownsValue)
Definition: DynaHashMapImpl.h:79
int _count
Definition: DynaHashMap.h:88
Definition: ICopyable.h:8
Definition: DynaHashMap.h:65
int _modCount
Definition: DynaHashMap.h:91
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4282
V * operator[](K key)
Definition: DynaHashMap.h:140