-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.hpp
700 lines (647 loc) · 12 KB
/
HashMap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#ifndef EX3_CPP_HASHMAP_HPP
#define EX3_CPP_HASHMAP_HPP
static const int INITCAPACITY = 16;
static const double DECREASE = 0.5;
static const double INCREASE = 2;
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <iostream>
using std::vector;
using std::pair;
/**
* A class representing a hashmap.
* @tparam KeyT
* @tparam ValueT
*/
template<class KeyT , class ValueT>
class HashMap
{
using bucket = std::vector<std::pair<KeyT , ValueT>>;
private:
/**
* The STL to contain the map in.
*/
bucket *_myMap = new bucket[INITCAPACITY];
/**
* The upper factor.
*/
double _topFactor = 0.75;
/**
* The lower factor.
*/
double _botFactor = 0.25;
/**
* The amount of items in the hashmap.
*/
int _size = 0;
/**
* The amount of items the map can hold.
*/
int _capacity = INITCAPACITY;
/**
* nested class iterator.
*/
class const_iterator
{
public:
/**
* A ctr.
* @param map the items of the hash map to go over.
* @param isEnd was the ctr called from begin or end. 1 for end 0 for begin.
* @param capacity the size of the pointer map.(length of list).
*/
const_iterator (bucket *map , int isEnd , int capacity) : _map (map) , _cap (capacity)
{
int i = 0;
while (i < _cap)
{
if (map[i].empty ())
{
++i;
}
else
{
current = map[i].begin ();
currentidx = i;
break;
}
}
i = capacity - 1;
while (i > -1)
{
if (map[i].empty ())
{
i--;
}
else
{
ending = map[i].end ();
if (isEnd)
{
current = ending;
}
break;
}
}
}
/**
* A default copy ctr.
* @param other
*/
const_iterator (const const_iterator &other) = default;
pair<KeyT , ValueT> &operator* () const
{
return *current;
}
/**
* operator overload.
* @return
*/
pair<KeyT , ValueT> *operator-> () const
{
return &(*current);
}
/**
* operator overload.
* @return
*/
const_iterator &operator++ ()
{
if (current == ending)
{
return *this;
}
++current;
if (current == ending)
{
return *this;
}
if (current == _map[currentidx].end ())
{
++currentidx;
current = _map[currentidx].begin ();
while (currentidx < _cap)
{
if (_map[currentidx].empty ())
{
++currentidx;
}
else
{
current = _map[currentidx].begin ();
break;
}
}
}
return *this;
}
/**
* operator overload.
* @return
*/
const const_iterator operator++ (int)
{
const_iterator tmp = *this;
++(*this);
return tmp;
}
/**
* operator overload.
* @return
*/
bool operator== (const const_iterator &rhs) const
{
return current == rhs.current && _map == rhs._map;
}
/**
* operator overload.
* @return
*/
bool operator!= (const const_iterator &rhs) const
{
return !(*this == rhs);
}
private:
/**
* The items of the hashmap to go over.
*/
bucket *_map;
/**
* The capacity of *map.
*/
int _cap;
/**
* The current iterator in the current vector we are at, and the ending iterator.
*/
typename bucket::iterator current , ending;
/**
* The current vector we are at in map.
*/
int currentidx;
};
/**
* returns the hashing for a given key.
* @param key
* @return
*/
int _hashfunc (KeyT key) const
{
int ind = std::hash<KeyT>{} (key);
return ind & (_capacity - 1);
}
/**
* A function to resize the array *map.
* @param factor
*/
void _resize (double factor)
{
auto oldmap = _myMap;
HashMap::const_iterator b = begin () , e = end ();
_myMap = new bucket[(int) (_capacity * factor)];
_capacity = static_cast<int>(_capacity * factor);
_size = 0;
for (auto c = b; c != e; ++c)
{
insert (c->first , c->second);
}
delete[] oldmap;
}
/**
* A function that returns a reference to the value given a key. throws an exception if not
* found.
* @param key
* @return
*/
ValueT& _getVal (KeyT &key)
{
pair<KeyT , ValueT> &p = _getPair (key);
return p.second;
}
/**
* A function that returns the value given a key. throws an exception if not
* found.
* @param key
* @return
*/
ValueT _getVal (KeyT &key)const
{
pair<KeyT , ValueT> p = _getPair (key);
return p.second;
}
/**
* A function that returns a referrence to the pair given a key. throws an exception if not
* found.
* @param key
* @return
*/
pair<KeyT , ValueT>& _getPair (KeyT key)
{
if (_myMap != nullptr)
{
int ind = _hashfunc (key);
for (int i = 0; i < (int)_myMap[ind].size (); ++i)
{
if (_myMap[ind][i].first == key)
{
return _myMap[ind][i];
}
}
}
throw std::invalid_argument ("Bad input");
// check this syntax.
}
/**
* A function that returns a the pair given a key. throws an exception if not
* found.
* @param key
* @return
*/
pair<KeyT , ValueT> _getPair (KeyT key)const
{
if (_myMap != nullptr)
{
int ind = _hashfunc (key);
for (int i = 0; i < (int)_myMap[ind].size (); ++i)
{
if (_myMap[ind][i].first == key)
{
return _myMap[ind][i];
}
}
}
throw std::invalid_argument ("bad input");
}
/**
* A function that inserts a key and a value , if flag is 1 can overwrite existing key.
* @param key
* @param val
* @param flag
* @return
*/
bool _insertOverWrite (KeyT key , ValueT val , int flag = 0)
{
if (containsKey (key))
{
if (flag)
{
auto &p = _getPair (key);
p.second = val;
return true;
}
return false;
}
++_size;
if (getLoadFactor () > _topFactor)
{
_resize (INCREASE);
++_size;
// because i assigned size = 0 ;
}
int ind = _hashfunc (key);
_myMap[ind].push_back (std::make_pair (key , val));
return true;
}
public:
// class const_iterator;
typedef HashMap<KeyT , ValueT>::const_iterator const_iterator;
/**
* A default ctr.
*/
HashMap () = default;
/**
* A ctr accepting lower and upper factor.
* @param bot
* @param top
*/
HashMap (double bot , double top)
{
if (top >= 1 || top <= 0 || bot >= 1 || bot <= 0 || bot >= top )
{
throw std::invalid_argument ("bad input");
}
else
{
_topFactor = top;
_botFactor = bot;
}
}
/**
* A ctr that accepts two vectors, and generates from them a hashmap.
* @param keyVec
* @param valVec
*/
HashMap (vector<KeyT> keyVec , vector<ValueT> valVec)
{
if ((int)valVec.size () != (int)keyVec.size ())
{
throw std::invalid_argument ("bad input");
}
for (int i = 0; i < (int)keyVec.size (); ++i)
{
_insertOverWrite (keyVec[i] , valVec[i] , 1);
}
}
/**
* A copy ctr.
* @param other
*/
HashMap (const HashMap &other) : _myMap (new bucket[other.capacity ()]) , _topFactor (other
._topFactor) , _botFactor (other._botFactor) , _capacity (other._capacity)
{
for (auto i = other.begin (); i != other.end (); ++i)
{
insert (i->first , i->second);
}
}
/**
* A move ctr.
* @param other
*/
HashMap (HashMap && other) : _myMap (other._myMap) , _topFactor (other._topFactor) ,
_botFactor (other._botFactor) , _capacity (other._capacity) ,
_size (other._size)
{
other._myMap = nullptr;
}
/**
* A destructor.
*/
~HashMap ()
{
delete[] _myMap;
}
/**
* A function that returns the amount of elements in the hashmap.
* @return
*/
int size () const
{
return _size;
}
/**
* A function that returns the amount of elements the map can hold.
* @return
*/
int capacity () const
{
return _capacity;
}
/**
* A function that returns the current load factor of the map.
* @return
*/
double getLoadFactor () const
{
return _size / (double) _capacity;
}
/**
* A function that returns 1 if the map is empty.
* @return
*/
bool empty ()
{
return _size == 0;
}
/**
* A function that inserts a key and a value.
* @param key
* @param val
* @return true on success else false.
*/
bool insert (KeyT key , ValueT val)
{
return _insertOverWrite (key , val);
}
/**
* A function that returns a ref to a value given a key, if not found throws an exception.
* @param key
* @return
*/
ValueT &at (KeyT key)
{
return _getVal (key);
}
/**
* A function that erases a pair given the pair's key.
* @param key
* @return
*/
bool erase (KeyT key)
{
try
{
auto p = _getPair (key);
int ind = _hashfunc (key);
for (int i = 0; i < (int)_myMap[ind].size (); ++i)
{
if (_myMap[ind][i] == p)
{
_myMap[ind].erase (_myMap[ind].begin () + i);
}
}
--_size;
if (getLoadFactor () < _botFactor)
{
_resize (DECREASE);
}
return true;
}
catch (std::invalid_argument &a)
{
return false;
}
}
/**
* A function that clears the map.
*/
void clear ()
{
delete[] _myMap;
_size = 0;
_myMap = new bucket[_capacity];
}
/**
* returns 1 if there exists a pair with that key.
* @param key
* @return
*/
bool containsKey (KeyT key)const
{
try
{
_getVal (key);
return true;
}
catch (std::invalid_argument &a)
{
return false;
}
}
/**
* overload ctr.
* @param other
* @return
*/
HashMap &operator= (const HashMap &other)
{
if (this == &other)
{
return *this;
}
_topFactor = other._topFactor;
_botFactor = other._botFactor;
delete[] _myMap;
_myMap = new bucket[other._capacity];
_capacity = other._capacity;
for (int i = 0; i < other._capacity; ++i)
{
for (auto it = other._myMap[i].begin (); it != other._myMap[i].end (); ++it)
{
insert (it->first , it->second);
}
}
return *this;
}
/**
* overload ctr.
* @param other
* @return
*/
HashMap& operator= ( HashMap && other)noexcept
{
_topFactor = other._topFactor;
_botFactor = other._botFactor;
delete[] _myMap;
_myMap = new bucket[other._capacity];
_capacity = other._capacity;
for (int i = 0; i < other._capacity; ++i)
{
for (auto it = other._myMap[i].begin (); it != other._myMap[i].end (); ++it)
{
insert (it->first , it->second);
}
}
other._myMap = nullptr;
return *this;
}
/**
* overload ctr.
* @param key
* @return
*/
ValueT &operator[] (KeyT key)
{
try
{
return _getVal (key);
}
catch (std::invalid_argument & a)
{
ValueT t;
// is alright because it exists in function
_insertOverWrite (key , t , 1);
return _getVal (key);
}
}
/**
* overload ctr.
* @param key
* @return
*/
ValueT operator[] (KeyT key) const
{
int ind = _hashfunc (key);
for (int i = 0; i < (int)_myMap[ind].size (); ++i)
{
if (_myMap[ind][i].first == key)
{
return _myMap[ind][i].second;
}
}
return ValueT ();
}
/**
* overload ctr.
* @param key
* @return
*/
bool operator== (const HashMap &other) const
{
int count = 0;
if (_size != other._size || _capacity != other._capacity || _topFactor != other._topFactor
|| _botFactor != other._botFactor)
{
return false;
}
for (auto i = other.begin (); i != other.end (); ++i)
{
try
{
if (!containsKey (i->first) || i->second != _getVal (i->first))
{
return false;
}
}
catch (std::invalid_argument &a)
{
return false;
}
}
return true;
}
/**
* overload ctr.
*/
bool operator!= (const HashMap &other)
{
return !(*this == other);
}
/**
* A function that returns the amount of items in the same bucket with a given key.
* @param key
* @return
*/
int bucketSize (KeyT key)
{
if (containsKey (key))
{
int ind = _hashfunc (key);
return (int) (_myMap[ind].size ());
}
throw std::invalid_argument ("bad input");
}
/**
* A begin func.
* @return
*/
const_iterator begin () const noexcept
{
return const_iterator (this->_myMap , 0 , this->capacity ());
}
/**
* A end func.
* @return
*/
const_iterator end () const noexcept
{
return const_iterator (this->_myMap , 1 , this->capacity ());
}
/**
* A cbegin func.
* @return
*/
const_iterator cbegin () const noexcept
{
return const_iterator (this->_myMap , 0 , this->capacity ());
}
/**
* A cend func.
* @return
*/
const_iterator cend () const noexcept
{
return const_iterator (this->_myMap , 1 , this->capacity ());
}
};
#endif //EX3_CPP_HASHMAP_HPP