-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.hpp
779 lines (676 loc) · 21.2 KB
/
Map.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#ifndef MAP_H
#define MAP_H
#include <vector>
#include <iostream>
#include <cstdlib>
#include <climits>
#include <stdexcept>
namespace cs440
{
template<typename Key_T, typename Mapped_T>
class Map
{
private:
class SkipList;
SkipList *list;
typedef std::pair<const Key_T, Mapped_T> ValueType;
class SentinelNode
{
public:
int level;
std::vector<SentinelNode *> forward_ptrs;
SentinelNode *prev;
SentinelNode(int level)
{
this->level = level;
for(int i=0; i <= level; i++)
{
forward_ptrs.emplace_back(nullptr);
}
prev = nullptr;
}
virtual ~SentinelNode() = default;
};
class SkipListNode: public SentinelNode
{
public:
// Key_T key;
// Mapped_T value;
ValueType data;
SkipListNode (ValueType tup, int level): SentinelNode(level), data(tup)
{}
};
class SkipList
{
private:
double probability;
int maxLevel;
int curr_level;
public:
SentinelNode *head;
SentinelNode *tail;
size_t used_size;
SkipList()
{
probability = 0.5;
maxLevel = 16;
used_size = 0;
curr_level = 0;
head = new SentinelNode(maxLevel);
tail = new SentinelNode(maxLevel);
tail->prev = head;
for(size_t i = 0; i<=head->forward_ptrs.size(); ++i)
{
head->forward_ptrs[i] = tail;
}
}
~SkipList()
{
used_size = 0;
SentinelNode *temp = head;
while(temp!=nullptr)
{
SentinelNode *next = temp->forward_ptrs[0];
delete temp;
temp = next;
}
}
int randomLevel()
{
int l = 0;
while (((double)std::rand() / RAND_MAX) < probability && l < maxLevel)
{
l++;
}
return l;
}
SentinelNode *search(Key_T k)
{
SentinelNode *temp = head;
int currMax = this->curr_level;
for(int i = currMax; i>=0; i--)
{
while(temp->forward_ptrs[i]!=tail && temp->forward_ptrs[i] != nullptr && static_cast<SkipListNode *>(temp->forward_ptrs[i])->data.first < k)
{
temp = temp->forward_ptrs[i];
}
}
temp = temp->forward_ptrs[0];
if(temp!= head && temp!=tail && temp!=nullptr)
if(static_cast<SkipListNode *>(temp)->data.first == k)
return temp;
return nullptr;
}
std::pair<SentinelNode*, bool> insert(ValueType tup)
{
SentinelNode *temp = nullptr;
// temp = search(tup.first);
std::vector<SentinelNode *> update(head->forward_ptrs);
int currMax = this->curr_level;
temp = head;
for(int i = currMax; i>=0; i--)
{
while(temp->forward_ptrs[i] != nullptr && temp->forward_ptrs[i]!=tail && static_cast<SkipListNode *>(temp->forward_ptrs[i])->data.first < tup.first)
{
temp = temp->forward_ptrs[i];
}
update[i] = temp;
}
temp = temp->forward_ptrs[0];
if(temp!= head && temp!=tail && temp!=nullptr)
if(static_cast<SkipListNode *>(temp)->data.first == tup.first)
{
std::pair<SentinelNode*, bool>res{temp,false};
return res;
}
SentinelNode *prev = update[0];
int new_level = 0;
new_level = randomLevel();
if(new_level > maxLevel)
new_level = maxLevel;
if(new_level > curr_level)
{
for(int i=curr_level + 1; i <= new_level; ++i)
{
update[i] = head;
}
curr_level = new_level;
}
temp = new SkipListNode(tup, new_level);
for(int i=0; i<=new_level; i++)
{
if(update[i] == tail)
{
temp->forward_ptrs[i] = tail;
}
else
{
temp->forward_ptrs[i] = update[i]->forward_ptrs[i];
update[i]->forward_ptrs[i] = temp;
}
}
temp->prev = prev;
if(temp->forward_ptrs[0] == tail)
tail->prev = temp;
else
temp->forward_ptrs[0]->prev = temp;
used_size++;
std::pair<SentinelNode*, bool>res{temp, true};
return res;
}
void erase(Key_T k)
{
std::vector<SentinelNode *> update(head->forward_ptrs);
SentinelNode *temp=head;
int curr_max = curr_level;
for(int i = curr_max; i >= 0; i--)
{
while(temp->forward_ptrs[i]!=tail && temp->forward_ptrs[i] != nullptr && static_cast<SkipListNode *>(temp->forward_ptrs[i])->data.first < k)
{
temp = temp->forward_ptrs[i];
}
update[i] = temp;
}
temp = temp->forward_ptrs[0];
if(temp!= tail && static_cast<SkipListNode *>(temp)->data.first == k)
{
for(int i = 0; i <= curr_level; ++i)
{
if(update[i]->forward_ptrs[i] != temp)
break;
update[i]->forward_ptrs[i] = temp->forward_ptrs[i];
}
temp->forward_ptrs[0]->prev = temp->prev;
delete temp;
while(curr_level > 0 && update[curr_level] == tail)
{
curr_level--;
}
used_size--;
}
else
{
throw std::out_of_range("Key not found");
}
}
};
public:
class Iterator;
class ConstIterator;
class ReverseIterator;
Map()
{
list = new SkipList;
}
Map(const Map &map_obj)
{
list = new SkipList;
for(auto it = map_obj.begin(); it!=map_obj.end(); it++)
{
list->insert(*it);
}
}
Map &operator=(const Map &map_obj)
{
delete(list);
list = new SkipList;
for(auto it = map_obj.begin(); it!=map_obj.end(); it++)
{
list->insert(*it);
}
return *this;
}
Map (std::initializer_list<ValueType> init_ls)
{
list = new SkipList;
for(auto it = init_ls.begin(); it!=init_ls.end(); it++)
{
list->insert(*it);
}
}
~Map()
{
delete list;
}
size_t size() const
{
return list->used_size;
}
bool empty() const
{
if(size() == 0)
return true;
return false;
}
Iterator begin()
{
Iterator it(list->head->forward_ptrs[0]);
return it;
}
Iterator end()
{
Iterator it(list->tail);
return it;
}
ConstIterator begin() const
{
ConstIterator it(list->head->forward_ptrs[0]);
return it;
}
ConstIterator end() const
{
ConstIterator it(list->tail);
return it;
}
ReverseIterator rbegin()
{
ReverseIterator it(list->tail->prev);
return it;
}
ReverseIterator rend()
{
ReverseIterator it(list->head);
return it;
}
std::pair<Iterator, bool> insert(const ValueType& kv)
{
std::pair<SentinelNode *, bool>res;
res = list->insert(kv);
Iterator it(res.first);
bool search_flag = res.second;
std::pair<Iterator, bool>response{it, search_flag};
return response;
}
template<typename IT_T>
void insert(IT_T range_beg, IT_T range_end)
{
for(auto i = range_beg; i!=range_end; i++)
{
insert(i);
}
}
Mapped_T& at(const Key_T& k)
{
SentinelNode *node = list->search(k);
if(node)
{
return static_cast<SkipListNode *>(node)->data.second;
}
else
{
throw std::out_of_range("Key not found");
}
}
const Mapped_T& at(const Key_T& k) const
{
SentinelNode *node = list->search(k);
if(node)
{
return static_cast<SkipListNode *>(node)->data.second;
}
else
{
throw std::out_of_range("Key not found");
}
}
Iterator find(const Key_T& k)
{
SentinelNode *node = list->search(k);
if(node)
{
Iterator it(node);
return it;
}
else
{
return end();
}
}
ConstIterator find(const Key_T& k) const
{
SentinelNode *node = list->search(k);
if(node)
{
ConstIterator it(node);
return it;
}
else
{
return end();
}
}
void erase(Iterator pos)
{
if(pos.curr_node)
{
Key_T k = (*pos).first;
list->erase(k);
}
}
void erase(const Key_T& k)
{
list->erase(k);
}
void clear()
{
delete list;
list = new SkipList;
}
// Added this function to test the behaviour of the previous node ptr in every node.
/*
void print()
{
SkipListNode *temp = list->head->forward_ptrs[0];
int node_num = 1;
std::cout<<"temp: "<<temp<<" tail: "<<list->tail<<std::endl;
while(temp!=list->tail)
{
std::cout<<"Node: "<<node_num<<std::endl;
std::cout<<"Curr Addr:"<<temp<<std::endl;
std::cout<<"Prev Addr:"<<temp->prev<<std::endl;
std::cout<<"Key:"<<temp->key<<" Value:"<<temp->value<<std::endl;
std::cout<<"**********************************"<<std::endl;
temp = temp->forward_ptrs[0];
node_num++;
}
}
*/
Mapped_T& operator[](Key_T k)
{
SentinelNode *res = list->search(k);
if(res == nullptr)
{
list->insert(std::make_pair(k, Mapped_T()));
res = list->search(k);
}
return static_cast<SkipListNode *>(res)->data.second;
}
friend bool operator==(const Map& m1, const Map& m2)
{
if(m1.size() != m2.size())
return false;
else
{
auto it1 = m1.begin();
auto it2 = m2.begin();
auto it1_end = m1.end();
auto it2_end = m2.end();
int true_count = m1.size();
while(((*it1).first == (*it2).first) && ((*it1).second == (*it2).second))
{
++it1;
++it2;
--true_count;
if(it1 == it1_end || it2 == it2_end)
break;
}
if(true_count !=0)
return false;
}
return true;
}
friend bool operator!=(const Map& m1, const Map& m2)
{
if(m1.size() != m2.size())
return true;
else
{
auto it1 = m1.begin();
auto it2 = m2.begin();
auto it1_end = m1.end();
auto it2_end = m2.end();
int false_count = m1.size();
while(((*it1).first != (*it2).first) || ((*it1).second != (*it2).second))
{
++it1;
++it2;
--false_count;
if(it1 == it1_end || it2 == it2_end)
break;
}
// std::cout<<"False Count: "<<false_count<<std::endl;
if(false_count != 0)
return false;
}
return true;
}
friend bool operator<(const Map& m1, const Map& m2)
{
auto it1 = m1.begin();
auto it2 = m2.begin();
auto it1_end = m1.end();
auto it2_end = m2.end();
size_t elem_equal = 0;
while (it1 != it1_end && it2 != it2_end)
{
if((*it1) > (*it2))
{
return false;
}
else if ((*it1) == (*it2))
{
++elem_equal;
}
++it1;
++it2;
}
size_t s1 = m1.size();
size_t s2 = m2.size();
if(elem_equal == s1)
{
if(s1 < s2)
return true;
return false;
}
return true;
}
class Iterator
{
public:
SentinelNode *curr_node;
Iterator() = delete;
Iterator(SentinelNode *node)
{
curr_node = node;
}
Iterator(const Iterator &it)
{
curr_node = it.curr_node;
}
Iterator& operator=(const Iterator &obj)
{
curr_node = obj.curr_node;
return *this;
}
Iterator& operator++()
{
curr_node = curr_node->forward_ptrs[0];
return *this;
}
Iterator operator++(int)
{
Iterator &it = *this;
curr_node = curr_node->forward_ptrs[0];
return it;
}
Iterator& operator--()
{
curr_node = curr_node->prev;
return *this;
}
Iterator operator--(int)
{
Iterator &it = *this;
curr_node = curr_node->prev;
return it;
}
ValueType& operator*() const
{
return static_cast<SkipListNode *>(curr_node)->data;
}
ValueType* operator->() const
{
return &(static_cast<SkipListNode *>(curr_node)->data);
}
friend bool operator==(const Iterator &it1, const Iterator &it2)
{
if (it1.curr_node == it2.curr_node)
return true;
return false;
}
friend bool operator==(const Iterator &it1, const ConstIterator &it2)
{
if (it1.curr_node == it2.curr_node)
return true;
return false;
}
friend bool operator==(const ConstIterator &it1, const Iterator &it2)
{
if (it1.curr_node == it2.curr_node)
return true;
return false;
}
friend bool operator!=(const Iterator &it1, const Iterator &it2)
{
if (it1.curr_node != it2.curr_node)
return true;
return false;
}
friend bool operator!=(const Iterator &it1, const ConstIterator &it2)
{
if (it1.curr_node != it2.curr_node)
return true;
return false;
}
friend bool operator!=(const ConstIterator &it1, const Iterator &it2)
{
if (it1.curr_node != it2.curr_node)
return true;
return false;
}
};
class ConstIterator
{
public:
SentinelNode *curr_node;
ConstIterator() = delete;
ConstIterator(SentinelNode *node)
{
curr_node = node;
}
ConstIterator(const Iterator &it)
{
curr_node = it.curr_node;
}
ConstIterator& operator++()
{
curr_node = curr_node->forward_ptrs[0];
return *this;
}
ConstIterator operator++(int)
{
ConstIterator &it = *this;
curr_node = curr_node->forward_ptrs[0];
return it;
}
ConstIterator& operator--()
{
curr_node = curr_node->prev;
return *this;
}
ConstIterator operator--(int)
{
ConstIterator &it = *this;
curr_node = curr_node->prev;
return it;
}
ConstIterator& operator=(const ConstIterator &obj)
{
curr_node = obj.curr_node;
return *this;
}
const ValueType& operator*() const
{
return static_cast<SkipListNode *>(curr_node)->data;
}
const ValueType* operator->() const
{
return &(static_cast<SkipListNode *>(curr_node)->data);
}
friend bool operator==(const ConstIterator &it1, const ConstIterator &it2)
{
if (it1.curr_node == it2.curr_node)
return true;
return false;
}
friend bool operator!=(const ConstIterator &it1, const ConstIterator &it2)
{
if (it1.curr_node != it2.curr_node)
return true;
return false;
}
};
class ReverseIterator
{
public:
SentinelNode *curr_node;
ReverseIterator() = delete;
ReverseIterator(SentinelNode *node)
{
curr_node = node;
}
ReverseIterator(const Iterator &it)
{
curr_node = it.curr_node;
}
ReverseIterator& operator++()
{
curr_node = curr_node->prev;
return *this;
}
ReverseIterator operator++(int)
{
ReverseIterator &it = *this;
curr_node = curr_node->prev;
return it;
}
ReverseIterator& operator--()
{
curr_node = curr_node->forward_ptrs[0];
return *this;
}
ReverseIterator operator--(int)
{
ReverseIterator &it = *this;
curr_node = curr_node->forward_ptrs[0];
return it;
}
ReverseIterator& operator=(const ReverseIterator &obj)
{
curr_node = obj.curr_node;
return *this;
}
ValueType& operator*() const
{
return static_cast<SkipListNode *>(curr_node)->data;
}
ValueType* operator->() const
{
return &(static_cast<SkipListNode *>(curr_node)->data);
}
friend bool operator==(const ReverseIterator &it1, const ReverseIterator &it2)
{
if (it1.curr_node == it2.curr_node)
return true;
return false;
}
friend bool operator!=(const ReverseIterator &it1, const ReverseIterator &it2)
{
if (it1.curr_node != it2.curr_node)
return true;
return false;
}
};
};
}
#endif