5 #ifndef DYNAHASHMAPIMPL_H 6 #define DYNAHASHMAPIMPL_H 16 #define MAKE_MAPTYPE_INSTANCE(K, V, T) \ 17 template class DynaHashMap<K,V>; \ 18 typedef DynaHashMap<K,V> T##Map 35 hash = key->hashCode();
44 int oldCapacity = _capacity;
46 memset((
void*)newTable, 0, newCapacity *
sizeof(
MapEntry<K,V>*));
47 for (
int idx = 0; idx < oldCapacity; ++idx) {
49 if (obj ==
nullptr || obj == deletedObject)
51 int hash = _getHashCode(obj->
getKey());
52 int index = ( hash & 0x7FFFFFF ) % newCapacity;
54 while (newTable[index] !=
nullptr) {
55 index = ((index + offset) & 0x7FFFFFFF) % newCapacity;
56 offset = offset * 2 + 1;
60 newTable[index] = obj;
61 _table[idx] =
nullptr;
65 _capacity = newCapacity;
66 _freeCells = newCapacity - _count;
80 _key(key), _value(value), _ownsValue(ownsValue) {
84 _key = (entry._key)->
copy();
85 _ownsValue = entry._ownsValue;
86 if (entry._value !=
nullptr) {
88 _value = _ownsValue ? (V*)(entry._value)->copy() : entry._value;
91 _value = entry._value;
119 _count(0), _capacity(INITIAL_SIZE), _freeCells(INITIAL_SIZE), _modCount(0), _ownsMembers(true) {
141 for (
int i = 0, len = other.
_capacity; i < len; ++i) {
143 if (entry !=
nullptr) {
159 for (;
hasNext() && (table[_curIndex] ==
nullptr || table[_curIndex] == _map->deletedObject); ++_curIndex);
164 for (;
hasNext() && (table[_curIndex] ==
nullptr || table[_curIndex] == _map->deletedObject); ++_curIndex);
170 int hash = _getHashCode(key);
171 int index = ( hash & 0x7FFFFFF ) % _capacity;
175 while (entry !=
nullptr &&
176 (entry == deletedObject || !((_getHashCode(entry->
getKey()) == hash) && (*(entry->
getKey()) == *key)))) {
177 index = ((index + offset) & 0x7FFFFFFF) % _capacity;
178 entry = _table[index];
179 offset = offset * 2 + 1;
197 V* oldValue = _value;
207 if (v1 == v2 || (v1 !=
nullptr && *v1 == *v2))
221 int index = _getTableIndex(key);
222 return _table[index];
226 return getEntry(&key);
231 return entry !=
nullptr ? entry->
getValue() :
nullptr;
241 int hash = _getHashCode(key);
242 int index = ( hash & 0x7FFFFFF ) % _capacity;
247 while ( entry !=
nullptr &&
248 (entry == deletedObject || !((_getHashCode(entry->
getKey()) == hash) && (*(entry->
getKey()) == *key)))) {
249 if (entry == deletedObject)
252 index = ((index + offset) & 0x7FFFFFFF) % _capacity;
253 entry = _table[index];
254 offset = offset * 2 + 1;
259 V* oldValue =
nullptr;
260 if (entry ==
nullptr) {
261 if (deletedIdx != -1)
269 if (1 - (_freeCells / (
double)_capacity) > LOAD_FACTOR) {
271 if (1 - (_freeCells / (
double)_capacity) > LOAD_FACTOR) {
272 _reHash(_capacity * 2 + 1);
277 oldValue = _table[index]->getValue();
278 _table[index]->setValue(value);
285 return put(
new K(key), value);
289 int index = _getTableIndex(key);
291 if (_table[index] !=
nullptr && _table[index] != deletedObject) {
292 V* saveValue = _table[index]->getValue();
293 _table[index]->setValue(
nullptr);
294 delete _table[index];
295 _table[index] = deletedObject;
309 int index = _getTableIndex(key);
311 if (_table[index] !=
nullptr && _table[index] != deletedObject) {
312 delete _table[index];
313 _table[index] = deletedObject;
320 return deleteEntry(&key);
326 int index = _getTableIndex(ent->
getKey());
328 if (_table[index] !=
nullptr && _table[index] != deletedObject) {
330 _table[index] = deletedObject;
340 for (
int idx = 0; idx < _capacity; ++idx) {
342 if (entry !=
nullptr) {
347 _table[idx] =
nullptr;
351 _freeCells = _capacity;
374 (table[_curIndex] ==
nullptr || table[_curIndex] == _map->deletedObject); ++_curIndex);
381 (table[_curIndex] ==
nullptr || table[_curIndex] == _map->deletedObject); --_curIndex);
388 (table[_curIndex] ==
nullptr || table[_curIndex] == _map->deletedObject); ++_curIndex);
395 (table[_curIndex] ==
nullptr || table[_curIndex] == _map->deletedObject); --_curIndex);
412 #endif //DYNAHASHMAPIMPL_H int _getTableIndex(K *key)
Definition: DynaHashMapImpl.h:167
Definition: DynaHashMap.h:26
bool _ownsMembers
Definition: DynaHashMap.h:92
MapKeyIter< K, V > keys()
Definition: DynaHashMapImpl.h:363
bool hasNext()
Definition: DynaHashMap.h:196
GeneratorWrapper< std::tuple< Ts... > > table(std::initializer_list< std::tuple< typename std::decay< Ts >::type... >> tuples)
Definition: catch.hpp:4058
const MapKeyIter< K, V > & operator++()
Definition: DynaHashMapImpl.h:371
bool hasPrev()
Definition: DynaHashMap.h:238
K * getKey() const
Definition: DynaHashMap.h:37
bool hasNext()
Definition: DynaHashMap.h:237
DynaHashMap< K, V > * copy() override
Definition: DynaHashMapImpl.h:153
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4005
MapValueIter(const DynaHashMap< K, V > *map, int start)
Definition: DynaHashMapImpl.h:162
const MapKeyIter< K, V > & operator--()
Definition: DynaHashMapImpl.h:378
MapEntry< K, V > * getEntry(K *key)
Definition: DynaHashMapImpl.h:220
Definition: DynaHashMap.h:73
const MapValueIter< K, V > & operator++()
Definition: DynaHashMapImpl.h:385
MapValueIter< K, V > values()
Definition: DynaHashMapImpl.h:367
MapKeyIter(const DynaHashMap< K, V > *map, int start)
Definition: DynaHashMapImpl.h:157
const V * setValue(V *newValue)
Definition: DynaHashMapImpl.h:196
void clear()
Definition: DynaHashMapImpl.h:339
void deleteEntry(K *key)
Definition: DynaHashMapImpl.h:308
V * getValue() const
Definition: DynaHashMap.h:38
Definition: DynaHashMap.h:64
MapEntry< K, V > ** _table
Definition: DynaHashMap.h:87
static constexpr int INITIAL_SIZE
Definition: DynaHashMap.h:84
DynaMapIter< K, V > end()
Definition: DynaHashMapImpl.h:359
virtual ~DynaHashMap()
Definition: DynaHashMapImpl.h:128
bool operator==(const MapEntry< K, V > &other) const override
Definition: DynaHashMapImpl.h:201
V * get(K *key)
Definition: DynaHashMapImpl.h:229
void _init(int size)
Definition: DynaHashMapImpl.h:27
const MapValueIter< K, V > & operator--()
Definition: DynaHashMapImpl.h:392
V * put(K *key, V *value)
Definition: DynaHashMapImpl.h:238
virtual ~MapEntry()
Definition: DynaHashMapImpl.h:97
Definition: Exception.h:13
Definition: DynaHashMap.h:63
int _freeCells
Definition: DynaHashMap.h:90
MapEntry< K, V > * removeEntry(MapEntry< K, V > *entry)
Definition: DynaHashMapImpl.h:323
DynaHashMap()
Definition: DynaHashMapImpl.h:118
int _capacity
Definition: DynaHashMap.h:89
MapEntry< K, V > * copy() override
Definition: DynaHashMapImpl.h:108
void _reHash(int newCapacity)
Definition: DynaHashMapImpl.h:43
V * remove(K *key)
Definition: DynaHashMapImpl.h:288
int _getHashCode(const K *key) const
Definition: DynaHashMapImpl.h:32
MapEntry(K *key, V *value, bool ownsValue)
Definition: DynaHashMapImpl.h:79
int _count
Definition: DynaHashMap.h:88
Definition: DynaHashMap.h:65
int _modCount
Definition: DynaHashMap.h:91
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4282
DynaMapIter< K, V > begin()
Definition: DynaHashMapImpl.h:355