ORIGINAL
Loading...
Searching...
No Matches
maps.h
Go to the documentation of this file.
1#ifndef MAPS_H
2#define MAPS_H
3
4#include "allocator.h"
5#include "couple.h"
6#include "hash.h"
7#include "hashTable.h"
8#include "map.h"
9#include "ownerPtr.h"
10#include "comparator.h"
11#include "RBTree.h"
12#include "skipList.h"
13
14
54
55namespace original {
56
81 template <typename K_TYPE,
82 typename V_TYPE,
83 typename HASH = hash<K_TYPE>,
85 class hashMap final
86 : public hashTable<K_TYPE, V_TYPE, ALLOC, HASH>,
87 public map<K_TYPE, V_TYPE, ALLOC>,
88 public iterable<couple<const K_TYPE, V_TYPE>>,
89 public printable{
90
96
101 using rebind_alloc_pointer = typename hashTable<K_TYPE, V_TYPE, ALLOC, HASH>::rebind_alloc_pointer;
102 public:
103
117 class Iterator final : public hashTable<K_TYPE, V_TYPE, ALLOC, HASH>::Iterator,
118 public baseIterator<couple<const K_TYPE, V_TYPE>> {
119
127 explicit Iterator(vector<hashNode*, rebind_alloc_pointer>* buckets = nullptr,
128 u_integer bucket = 0, hashNode* node = nullptr);
129
136 bool equalPtr(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
137 public:
138 friend class hashMap;
139
144 Iterator(const Iterator& other);
145
151 Iterator& operator=(const Iterator& other);
152
157 Iterator* clone() const override;
158
163 [[nodiscard]] std::string className() const override;
164
170 void operator+=(integer steps) const override;
171
176 void operator-=(integer steps) const override;
177
182 integer operator-(const iterator<couple<const K_TYPE, V_TYPE>> &other) const override;
183
188 [[nodiscard]] bool hasNext() const override;
189
194 [[nodiscard]] bool hasPrev() const override;
195
201 bool atPrev(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
202
208 bool atNext(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
209
213 void next() const override;
214
219 void prev() const override;
220
225 Iterator* getPrev() const override;
226
232
237 couple<const K_TYPE, V_TYPE> get() const override;
238
243 void set(const couple<const K_TYPE, V_TYPE> &data) override;
244
249 [[nodiscard]] bool isValid() const override;
250
251 ~Iterator() override = default;
252 };
253
259 explicit hashMap(HASH hash = HASH{}, ALLOC alloc = ALLOC{});
260
267 hashMap(const hashMap& other);
268
276 hashMap& operator=(const hashMap& other);
277
284 hashMap(hashMap&& other) noexcept;
285
294 hashMap& operator=(hashMap&& other) noexcept;
295
300 [[nodiscard]] u_integer size() const override;
301
307 bool contains(const couple<const K_TYPE, V_TYPE> &e) const override;
308
315 bool add(const K_TYPE &k, const V_TYPE &v) override;
316
322 bool remove(const K_TYPE &k) override;
323
329 [[nodiscard]] bool containsKey(const K_TYPE &k) const override;
330
337 V_TYPE get(const K_TYPE &k) const override;
338
345 bool update(const K_TYPE &key, const V_TYPE &value) override;
346
353 const V_TYPE & operator[](const K_TYPE &k) const override;
354
361 V_TYPE & operator[](const K_TYPE &k) override;
362
367 Iterator* begins() const override;
368
373 Iterator* ends() const override;
374
379 [[nodiscard]] std::string className() const override;
380
386 [[nodiscard]] std::string toString(bool enter) const override;
387
388 ~hashMap() override;
389 };
390
417 template <typename K_TYPE,
418 typename V_TYPE,
421 class treeMap final : public RBTree<K_TYPE, V_TYPE, ALLOC, Compare>,
422 public map<K_TYPE, V_TYPE, ALLOC>,
423 public iterable<couple<const K_TYPE, V_TYPE>>,
424 public printable {
425
431
436 using RBNode = typename RBTreeType::RBNode;
437 public:
438
452 class Iterator final : public RBTreeType::Iterator,
453 public baseIterator<couple<const K_TYPE, V_TYPE>> {
460 explicit Iterator(RBTreeType* tree, RBNode* cur);
461
468 bool equalPtr(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
469 public:
470 friend class treeMap;
471
476 Iterator(const Iterator& other);
477
483 Iterator& operator=(const Iterator& other);
484
489 Iterator* clone() const override;
490
495 [[nodiscard]] std::string className() const override;
496
501 void operator+=(integer steps) const override;
502
507 void operator-=(integer steps) const override;
508
512 integer operator-(const iterator<couple<const K_TYPE, V_TYPE>> &other) const override;
513
518 [[nodiscard]] bool hasNext() const override;
519
524 [[nodiscard]] bool hasPrev() const override;
525
531 bool atPrev(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
532
538 bool atNext(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
539
543 void next() const override;
544
548 void prev() const override;
549
554 Iterator* getPrev() const override;
555
561
566 couple<const K_TYPE, V_TYPE> get() const override;
567
572 void set(const couple<const K_TYPE, V_TYPE> &data) override;
573
578 [[nodiscard]] bool isValid() const override;
579
580 ~Iterator() override = default;
581 };
582
583 friend class Iterator;
584
590 explicit treeMap(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
591
598 treeMap(const treeMap& other);
599
607 treeMap& operator=(const treeMap& other);
608
615 treeMap(treeMap&& other) noexcept;
616
625 treeMap& operator=(treeMap&& other) noexcept;
626
631 [[nodiscard]] u_integer size() const override;
632
638 bool contains(const couple<const K_TYPE, V_TYPE> &e) const override;
639
646 bool add(const K_TYPE &k, const V_TYPE &v) override;
647
653 bool remove(const K_TYPE &k) override;
654
660 [[nodiscard]] bool containsKey(const K_TYPE &k) const override;
661
668 V_TYPE get(const K_TYPE &k) const override;
669
676 bool update(const K_TYPE &key, const V_TYPE &value) override;
677
684 const V_TYPE & operator[](const K_TYPE &k) const override;
685
692 V_TYPE & operator[](const K_TYPE &k) override;
693
698 Iterator* begins() const override;
699
704 Iterator* ends() const override;
705
710 [[nodiscard]] std::string className() const override;
711
717 [[nodiscard]] std::string toString(bool enter) const override;
718
723 ~treeMap() override;
724 };
725
751 template <typename K_TYPE,
752 typename V_TYPE,
755 class JMap final : public skipList<const K_TYPE, V_TYPE, ALLOC, Compare>,
756 public map<K_TYPE, V_TYPE, ALLOC>,
757 public iterable<couple<const K_TYPE, V_TYPE>>,
758 public printable {
759
761
766 using skipListNode = typename skipListType::skipListNode;
767
768 public:
782 class Iterator final : public skipListType::Iterator,
783 public baseIterator<couple<const K_TYPE, V_TYPE>> {
784
791 bool equalPtr(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
792
793 public:
794 friend class JMap;
795
801 explicit Iterator(skipListNode* cur);
802
807 Iterator(const Iterator& other);
808
814 Iterator& operator=(const Iterator& other);
815
820 Iterator* clone() const override;
821
826 [[nodiscard]] std::string className() const override;
827
832 void operator+=(integer steps) const override;
833
837 void operator-=(integer steps) const override;
838
845 integer operator-(const iterator<couple<const K_TYPE, V_TYPE>> &other) const override;
846
851 [[nodiscard]] bool hasNext() const override;
852
856 [[nodiscard]] bool hasPrev() const override;
857
863 bool atPrev(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
864
870 bool atNext(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
871
875 void next() const override;
876
880 void prev() const override;
881
885 Iterator* getPrev() const override;
886
892
897 couple<const K_TYPE, V_TYPE> get() const override;
898
902 void set(const couple<const K_TYPE, V_TYPE> &data) override;
903
908 [[nodiscard]] bool isValid() const override;
909
910 ~Iterator() override = default;
911 };
912
913 friend class Iterator;
914
920 explicit JMap(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
921
928 JMap(const JMap& other);
929
937 JMap& operator=(const JMap& other);
938
945 JMap(JMap&& other) noexcept;
946
955 JMap& operator=(JMap&& other) noexcept;
956
961 [[nodiscard]] u_integer size() const override;
962
968 bool contains(const couple<const K_TYPE, V_TYPE> &e) const override;
969
976 bool add(const K_TYPE &k, const V_TYPE &v) override;
977
983 bool remove(const K_TYPE &k) override;
984
990 [[nodiscard]] bool containsKey(const K_TYPE &k) const override;
991
998 V_TYPE get(const K_TYPE &k) const override;
999
1006 bool update(const K_TYPE &key, const V_TYPE &value) override;
1007
1014 const V_TYPE & operator[](const K_TYPE &k) const override;
1015
1022 V_TYPE & operator[](const K_TYPE &k) override;
1023
1028 Iterator* begins() const override;
1029
1034 Iterator* ends() const override;
1035
1040 [[nodiscard]] std::string className() const override;
1041
1047 [[nodiscard]] std::string toString(bool enter) const override;
1048
1053 ~JMap() override;
1054 };
1055}
1056
1057template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1058original::hashMap<K_TYPE, V_TYPE, HASH, ALLOC>::Iterator::Iterator(
1059 vector<hashNode *, rebind_alloc_pointer> *buckets, u_integer bucket, hashNode *node)
1060 : hashTable<K_TYPE, V_TYPE, ALLOC, HASH>::Iterator(
1061 const_cast<vector<hashNode*, rebind_alloc_pointer>*>(buckets), bucket, node) {}
1062
1063template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1064bool original::hashMap<K_TYPE, V_TYPE, HASH, ALLOC>::Iterator::equalPtr(
1065 const iterator<couple<const K_TYPE, V_TYPE>>* other) const {
1066 auto other_it = dynamic_cast<const Iterator*>(other);
1067 return other_it &&
1068 this->p_buckets == other_it->p_buckets &&
1069 this->cur_bucket == other_it->cur_bucket &&
1070 this->p_node == other_it->p_node;
1071}
1072
1073template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1074original::hashMap<K_TYPE, V_TYPE, HASH, ALLOC>::Iterator::Iterator(const Iterator &other) : Iterator() {
1075 this->operator=(other);
1076}
1077
1078template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1081 if (this == &other)
1082 return *this;
1083
1085 return *this;
1086}
1087
1088template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1091 return new Iterator(*this);
1092}
1093
1094template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1096 return "hashMap::Iterator";
1097}
1098
1099template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1103
1104template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1108
1109template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1114
1115template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1119
1120template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1124
1125template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1127 const iterator<couple<const K_TYPE, V_TYPE>> *other) const {
1128 auto other_it = dynamic_cast<const Iterator*>(other);
1129 if (!other_it) {
1130 return false;
1131 }
1132 auto next = ownerPtr(this->clone());
1133 if (!next->isValid()){
1134 return false;
1135 }
1136
1137 next->next();
1138 return next->equalPtr(other_it);
1139}
1140
1141template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1143 const iterator<couple<const K_TYPE, V_TYPE>> *other) const {
1144 return other->atPrev(*this);
1145}
1146
1147template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1151
1152template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1156
1157template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1162
1163template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1168
1169template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1174
1175template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1179
1180template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1184
1185template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1187 : hashTable<K_TYPE, V_TYPE, ALLOC, HASH>(std::move(hash)),
1188 map<K_TYPE, V_TYPE, ALLOC>(std::move(alloc)) {}
1189
1190template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1194
1195template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1198 if (this == &other) {
1199 return *this;
1200 }
1201
1202 this->buckets = this->bucketsCopy(other.buckets);
1203 this->size_ = other.size_;
1204 this->hash_ = other.hash_;
1205 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1206 this->allocator = other.allocator;
1207 this->rebind_alloc = other.rebind_alloc;
1208 }
1209 return *this;
1210}
1211
1212template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1214 this->operator=(std::move(other));
1215}
1216
1217template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1220 if (this == &other) {
1221 return *this;
1222 }
1223
1224 this->buckets = std::move(other.buckets);
1225 this->size_ = other.size_;
1226 other.size_ = 0;
1227 this->hash_ = std::move(other.hash_);
1228 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1229 this->allocator = std::move(other.allocator);
1230 this->rebind_alloc = std::move(other.rebind_alloc);
1231 }
1232 return *this;
1233}
1234
1235template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1238 return this->size_;
1239}
1240
1241template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1242bool
1246
1247template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1248bool original::hashMap<K_TYPE, V_TYPE, HASH, ALLOC>::add(const K_TYPE &k, const V_TYPE &v) {
1249 return this->insert(k, v);
1250}
1251
1252template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1254 return this->erase(k);
1255}
1256
1257template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1259 return this->find(k);
1260}
1261
1262template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1264 auto node = this->find(k);
1265 if (!node)
1266 throw noElementError();
1267 return node->getValue();
1268}
1269
1270template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1271bool original::hashMap<K_TYPE, V_TYPE, HASH, ALLOC>::update(const K_TYPE &key, const V_TYPE &value) {
1272 return this->modify(key, value);
1273}
1274
1275template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1277 auto node = this->find(k);
1278 if (!node)
1279 throw noElementError();
1280 return node->getValue();
1281}
1282
1283template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1285 auto node = this->find(k);
1286 if (!node) {
1287 this->insert(k, V_TYPE{});
1288 node = this->find(k);
1289 }
1290 return node->getValue();
1291}
1292
1293template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1296 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
1297 if (this->buckets[0]) {
1298 return new Iterator(p_buckets, 0, this->buckets[0]);
1299 }
1300 auto bucket = Iterator::findNextValidBucket(p_buckets, 0);
1301 return new Iterator(p_buckets, bucket, p_buckets->get(bucket));
1302}
1303
1304template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1307 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
1308 auto bucket = Iterator::findPrevValidBucket(p_buckets, this->buckets.size());
1309 auto node = this->buckets[bucket];
1310 while (node && node->getPNext()) {
1311 node = node->getPNext();
1312 }
1313 return new Iterator(p_buckets, bucket, node);
1314}
1315
1316template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1318 return "hashMap";
1319}
1320
1321template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1323 std::stringstream ss;
1324 ss << this->className();
1325 ss << "(";
1326 bool first = true;
1327 for (auto it = this->begin(); it != this->end(); it.next()){
1328 if (!first){
1329 ss << ", ";
1330 }
1331 ss << "{" << printable::formatString(it.get().template get<0>()) << ": "
1332 << printable::formatString(it.get().template get<1>()) << "}";
1333 first = false;
1334 }
1335 ss << ")";
1336 if (enter)
1337 ss << "\n";
1338 return ss.str();
1339}
1340
1341template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1342original::hashMap<K_TYPE, V_TYPE, HASH, ALLOC>::~hashMap() = default;
1343
1344template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1345original::treeMap<K_TYPE, V_TYPE, Compare, ALLOC>::Iterator::Iterator(RBTreeType* tree, RBNode* cur)
1346 : RBTreeType::Iterator(tree, cur) {}
1347
1348template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1349bool
1350original::treeMap<K_TYPE, V_TYPE, Compare, ALLOC>::Iterator::equalPtr(
1351 const iterator<couple<const K_TYPE, V_TYPE>>* other) const
1352{
1353 auto other_it = dynamic_cast<const Iterator*>(other);
1354 return other_it &&
1355 this->tree_ == other_it->tree_ &&
1356 this->cur_ == other_it->cur_;
1357}
1358
1359template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1360original::treeMap<K_TYPE, V_TYPE, Compare, ALLOC>::Iterator::Iterator(const Iterator& other) : Iterator(nullptr, nullptr)
1361{
1362 this->operator=(other);
1363}
1364
1365template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1368{
1369 if (this == &other) {
1370 return *this;
1371 }
1372
1373 this->tree_ = other.tree_;
1374 this->cur_ = other.cur_;
1375 return *this;
1376}
1377
1378template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1381{
1382 return new Iterator(*this);
1383}
1384
1385template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1387{
1388 return "treeMap::Iterator";
1389}
1390
1391template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1393{
1394 RBTreeType::Iterator::operator+=(steps);
1395}
1396
1397template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1399{
1400 RBTreeType::Iterator::operator-=(steps);
1401}
1402
1403template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1410
1411template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1413{
1414 return RBTreeType::Iterator::hasNext();
1415}
1416
1417template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1419{
1420 return RBTreeType::Iterator::hasPrev();
1421}
1422
1423template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1425 const iterator<couple<const K_TYPE, V_TYPE>>* other) const
1426{
1427 auto other_it = dynamic_cast<const Iterator*>(other);
1428 if (!other_it) {
1429 return false;
1430 }
1431 auto next = ownerPtr(this->clone());
1432 if (!next->isValid()){
1433 return false;
1434 }
1435
1436 next->next();
1437 return next->equalPtr(other_it);
1438}
1439
1440template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1442 const iterator<couple<const K_TYPE, V_TYPE>>* other) const
1443{
1444 return other->atNext(*this);
1445}
1446
1447template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1449{
1450 RBTreeType::Iterator::next();
1451}
1452
1453template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1455{
1456 RBTreeType::Iterator::prev();
1457}
1458
1459template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1462{
1463 auto it = this->clone();
1464 it->prev();
1465 return it;
1466}
1467
1468template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1473
1474template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1479
1480template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1485
1486template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1488{
1489 return RBTreeType::Iterator::isValid();
1490}
1491
1492template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1494 : RBTreeType(std::move(comp)),
1495 map<K_TYPE, V_TYPE, ALLOC>(std::move(alloc)) {}
1496
1497template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1501
1502template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1505 if (this == &other){
1506 return *this;
1507 }
1508
1509 this->destroyTree();
1510 this->root_ = other.treeCopy();
1511 this->size_ = other.size_;
1512 this->compare_ = other.compare_;
1513 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1514 this->allocator = other.allocator;
1515 this->rebind_alloc = other.rebind_alloc;
1516 }
1517 return *this;
1518}
1519
1520template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1522 this->operator=(std::move(other));
1523}
1524
1525template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1528 if (this == &other){
1529 return *this;
1530 }
1531
1532 this->destroyTree();
1533 this->root_ = other.root_;
1534 other.root_ = nullptr;
1535 this->size_ = other.size_;
1536 other.size_ = 0;
1537 this->compare_ = std::move(other.compare_);
1538 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1539 this->allocator = std::move(other.allocator);
1540 this->rebind_alloc = std::move(other.rebind_alloc);
1541 }
1542 return *this;
1543}
1544
1545template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1549
1550template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1551bool
1555
1556template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1557bool original::treeMap<K_TYPE, V_TYPE, Compare, ALLOC>::add(const K_TYPE &k, const V_TYPE &v) {
1558 return this->insert(k, v);
1559}
1560
1561template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1563 return this->erase(k);
1564}
1565
1566template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1568 return this->find(k);
1569}
1570
1571template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1573 auto node = this->find(k);
1574 if (!node)
1575 throw noElementError();
1576 return node->getValue();
1577}
1578
1579template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1580bool original::treeMap<K_TYPE, V_TYPE, Compare, ALLOC>::update(const K_TYPE &key, const V_TYPE &value) {
1581 return this->modify(key, value);
1582}
1583
1584template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1586 auto node = this->find(k);
1587 if (!node)
1588 throw noElementError();
1589 return node->getValue();
1590}
1591
1592template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1594 auto node = this->find(k);
1595 if (!node) {
1596 this->insert(k, V_TYPE{});
1597 node = this->find(k);
1598 }
1599 return node->getValue();
1600}
1601
1602template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1605{
1606 return new Iterator(const_cast<treeMap*>(this), this->getMinNode());
1607}
1608
1609template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1612{
1613 return new Iterator(const_cast<treeMap*>(this), this->getMaxNode());
1614}
1615
1616template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1618 return "treeMap";
1619}
1620
1621template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1623 std::stringstream ss;
1624 ss << this->className();
1625 ss << "(";
1626 bool first = true;
1627 for (auto it = this->begin(); it != this->end(); it.next()){
1628 if (!first){
1629 ss << ", ";
1630 }
1631 ss << "{" << printable::formatString(it.get().template get<0>()) << ": "
1632 << printable::formatString(it.get().template get<1>()) << "}";
1633 first = false;
1634 }
1635 ss << ")";
1636 if (enter)
1637 ss << "\n";
1638 return ss.str();
1639}
1640
1641template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1643
1644template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1645bool original::JMap<K_TYPE, V_TYPE, Compare, ALLOC>::Iterator::equalPtr(
1646 const iterator<couple<const K_TYPE, V_TYPE>> *other) const {
1647 auto other_it = dynamic_cast<const Iterator*>(other);
1648 return other_it && this->cur_ == other_it->cur_;
1649}
1650
1651template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1654
1655template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1658
1659template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1662 skipListType::Iterator::operator=(other);
1663}
1664
1665template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1670
1671template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1672std::string
1676
1677template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1679 skipListType::Iterator::operator+=(steps);
1680}
1681
1682template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1686
1687template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1690 const iterator<couple<const K_TYPE, V_TYPE>> &other) const {
1691 auto other_it = dynamic_cast<const Iterator*>(&other);
1692 if (other_it == nullptr)
1693 return this > &other ?
1694 std::numeric_limits<integer>::max() :
1695 std::numeric_limits<integer>::min();
1696 return skipListType::Iterator::operator-(*other_it);
1697}
1698
1699template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1701 return skipListType::Iterator::hasNext();
1702}
1703
1704template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1708
1709template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1711 const iterator<couple<const K_TYPE, V_TYPE>>* other) const {
1712 const auto other_it = dynamic_cast<const Iterator*>(other);
1713 if (!other_it){
1714 return false;
1715 }
1716 auto cloned_it = ownerPtr(other_it->clone());
1717 return this->equalPtr(cloned_it.get());
1718}
1719
1720template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1725
1726template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1728 skipListType::Iterator::next();
1729}
1730
1731template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1735
1736template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1741
1742template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1746
1747template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1751
1752template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1756
1757template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1759 return skipListType::Iterator::isValid();
1760}
1761
1762template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1764 : skipListType(std::move(comp)) ,
1765 map<K_TYPE, V_TYPE, ALLOC>(std::move(alloc)) {}
1766
1767template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1771
1772template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1775 if (this == &other){
1776 return *this;
1777 }
1778
1779 this->listDestroy();
1780 this->head_ = other.listCopy();
1781 this->size_ = other.size_;
1782 this->compare_ = other.compare_;
1783 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1784 this->allocator = other.allocator;
1785 this->rebind_alloc = other.rebind_alloc;
1786 }
1787 return *this;
1788}
1789
1790template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1792 this->operator=(std::move(other));
1793}
1794
1795template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1798 if (this == &other){
1799 return *this;
1800 }
1801
1802 this->listDestroy();
1803 this->head_ = other.head_;
1804 other.head_ = other.createNode();
1805 this->size_ = other.size_;
1806 other.size_ = 0;
1807 this->compare_ = std::move(other.compare_);
1808 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1809 this->allocator = std::move(other.allocator);
1810 this->rebind_alloc = std::move(other.rebind_alloc);
1811 }
1812 return *this;
1813}
1814
1815template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1819
1820template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1822 return this->containsKey(e.first()) && this->get(e.first()) == e.second();
1823}
1824
1825template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1826bool original::JMap<K_TYPE, V_TYPE, Compare, ALLOC>::add(const K_TYPE &k, const V_TYPE &v) {
1827 return this->insert(k, v);
1828}
1829
1830template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1832 return this->erase(k);
1833}
1834
1835template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1837 return this->find(k);
1838}
1839
1840template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1842 auto node = this->find(k);
1843 if (!node)
1844 throw noElementError();
1845 return node->getValue();
1846}
1847
1848template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1849bool original::JMap<K_TYPE, V_TYPE, Compare, ALLOC>::update(const K_TYPE &key, const V_TYPE &value) {
1850 return this->modify(key, value);
1851}
1852
1853template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1855 auto node = this->find(k);
1856 if (!node)
1857 throw noElementError();
1858 return node->getValue();
1859}
1860
1861template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1863 auto node = this->find(k);
1864 if (!node) {
1865 this->insert(k, V_TYPE{});
1866 node = this->find(k);
1867 }
1868 return node->getValue();
1869}
1870
1871template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1874 return new Iterator(this->head_->getPNext(1));
1875}
1876
1877template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1880 return new Iterator(this->findLastNode());
1881}
1882
1883template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1885 return "JMap";
1886}
1887
1888template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1890 std::stringstream ss;
1891 ss << this->className();
1892 ss << "(";
1893 bool first = true;
1894 for (auto it = this->begin(); it != this->end(); it.next()){
1895 if (!first){
1896 ss << ", ";
1897 }
1898 ss << "{" << printable::formatString(it.get().template get<0>()) << ": "
1899 << printable::formatString(it.get().template get<1>()) << "}";
1900 first = false;
1901 }
1902 ss << ")";
1903 if (enter)
1904 ss << "\n";
1905 return ss.str();
1906}
1907
1908template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1910
1911#endif //MAPS_H
Red-Black Tree implementation header.
Memory allocation interface and implementations.
Forward iterator for JMap.
Definition maps.h:783
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition maps.h:1661
bool atNext(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is next to this.
Definition maps.h:1721
Iterator * getPrev() const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1738
bool hasPrev() const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1705
Iterator(skipListNode *cur)
Constructs iterator pointing to specific skip list node.
Definition maps.h:1652
std::string className() const override
Gets iterator class name.
Definition maps.h:1673
integer operator-(const iterator< couple< const K_TYPE, V_TYPE > > &other) const override
Calculates distance between iterators.
Definition maps.h:1689
void prev() const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1732
void operator+=(integer steps) const override
Advances iterator by steps.
Definition maps.h:1678
void operator-=(integer steps) const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1683
void next() const override
Moves to next element.
Definition maps.h:1727
bool isValid() const override
Checks if iterator is valid.
Definition maps.h:1758
void set(const couple< const K_TYPE, V_TYPE > &data) override
Not supported (throws unSupportedMethodError)
Definition maps.h:1753
couple< const K_TYPE, V_TYPE > & get() override
Gets current element (non-const)
Definition maps.h:1743
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition maps.h:1700
bool atPrev(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is previous to this.
Definition maps.h:1710
Iterator * clone() const override
Creates a copy of this iterator.
Definition maps.h:1667
Skip List based implementation of the map interface.
Definition maps.h:758
bool update(const K_TYPE &key, const V_TYPE &value) override
Updates value for existing key.
Definition maps.h:1849
std::string toString(bool enter) const override
Converts to string representation.
Definition maps.h:1889
Iterator * ends() const override
Gets end iterator.
Definition maps.h:1879
const V_TYPE & operator[](const K_TYPE &k) const override
Const element access.
Definition maps.h:1854
bool remove(const K_TYPE &k) override
Removes key-value pair.
Definition maps.h:1831
bool containsKey(const K_TYPE &k) const override
Checks if key exists.
Definition maps.h:1836
bool contains(const couple< const K_TYPE, V_TYPE > &e) const override
Checks if key-value pair exists.
Definition maps.h:1821
~JMap() override
Destructor.
u_integer size() const override
Gets number of elements.
Definition maps.h:1816
std::string className() const override
Gets class name.
Definition maps.h:1884
bool add(const K_TYPE &k, const V_TYPE &v) override
Adds new key-value pair.
Definition maps.h:1826
Iterator * begins() const override
Gets begin iterator.
Definition maps.h:1873
V_TYPE get(const K_TYPE &k) const override
Gets value for key.
Definition maps.h:1841
JMap & operator=(const JMap &other)
Copy assignment operator.
Definition maps.h:1774
JMap(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty JMap.
Definition maps.h:1763
Red-Black Tree container implementation.
Definition RBTree.h:40
Default memory allocator using allocators utilities.
Definition allocator.h:154
A base class for basic iterators.
Definition iterator.h:296
Container for two heterogeneous elements.
Definition couple.h:37
F_TYPE & first()
Access first element.
Definition couple.h:323
S_TYPE & second()
Access second element.
Definition couple.h:329
Forward declaration of hash class template.
Definition hash.h:80
Bidirectional iterator for hashMap.
Definition maps.h:118
void next() const override
Moves to next element.
Definition maps.h:1148
bool atNext(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is next to this.
Definition maps.h:1142
Iterator * getPrev() const override
Not supported.
Definition maps.h:1159
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition maps.h:1080
bool hasNext() const override
Checks if more elements exist.
Definition maps.h:1116
bool atPrev(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is previous to this.
Definition maps.h:1126
std::string className() const override
Gets iterator class name.
Definition maps.h:1095
void prev() const override
Not supported.
Definition maps.h:1153
void operator+=(integer steps) const override
Advances iterator by steps.
Definition maps.h:1100
void operator-=(integer steps) const override
Not supported.
Definition maps.h:1105
bool isValid() const override
Checks if iterator is valid.
Definition maps.h:1181
integer operator-(const iterator< couple< const K_TYPE, V_TYPE > > &other) const override
Not supported.
Definition maps.h:1110
void set(const couple< const K_TYPE, V_TYPE > &data) override
Not supported.
Definition maps.h:1176
Iterator * clone() const override
Creates a copy of this iterator.
Definition maps.h:1090
couple< const K_TYPE, V_TYPE > & get() override
Gets current element (non-const)
Definition maps.h:1165
bool hasPrev() const override
Not supported.
Definition maps.h:1121
Hash table based implementation of the map interface.
Definition maps.h:89
hashMap(HASH hash=HASH{}, ALLOC alloc=ALLOC{})
Constructs empty hashMap.
Definition maps.h:1186
u_integer size() const override
Gets number of elements.
Definition maps.h:1237
bool add(const K_TYPE &k, const V_TYPE &v) override
Adds new key-value pair.
Definition maps.h:1248
bool remove(const K_TYPE &k) override
Removes key-value pair.
Definition maps.h:1253
Iterator * ends() const override
Gets end iterator.
Definition maps.h:1306
bool contains(const couple< const K_TYPE, V_TYPE > &e) const override
Checks if key-value pair exists.
Definition maps.h:1243
bool containsKey(const K_TYPE &k) const override
Checks if key exists.
Definition maps.h:1258
std::string className() const override
Gets class name.
Definition maps.h:1317
const V_TYPE & operator[](const K_TYPE &k) const override
Const element access.
Definition maps.h:1276
bool update(const K_TYPE &key, const V_TYPE &value) override
Updates value for existing key.
Definition maps.h:1271
Iterator * begins() const override
Gets begin iterator.
Definition maps.h:1295
std::string toString(bool enter) const override
Converts to string representation.
Definition maps.h:1322
hashMap & operator=(const hashMap &other)
Copy assignment operator.
Definition maps.h:1197
V_TYPE get(const K_TYPE &k) const override
Gets value for key.
Definition maps.h:1263
void next() const
Advances to the next element.
Definition hashTable.h:634
couple< const K_TYPE, V_TYPE > & get()
Gets current key-value pair (non-const)
Definition hashTable.h:668
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition hashTable.h:615
static u_integer findPrevValidBucket(vector< hashNode *, rebind_alloc_pointer > *buckets, u_integer bucket)
Finds the previous non-empty bucket.
Definition hashTable.h:590
void operator+=(integer steps) const
Advances iterator by steps positions.
Definition hashTable.h:656
bool isValid() const
Checks if iterator points to valid element.
Definition hashTable.h:687
bool hasNext() const
Checks if more elements are available.
Definition hashTable.h:625
static u_integer findNextValidBucket(vector< hashNode *, rebind_alloc_pointer > *buckets, u_integer bucket)
Finds the next non-empty bucket.
Definition hashTable.h:579
Internal node type for hash table storage.
Definition hashTable.h:71
Hash table implementation with separate chaining.
Definition hashTable.h:55
typename ALLOC::template rebind_alloc< hashNode * > rebind_alloc_pointer
Rebound allocator type for pointer storage.
Definition hashTable.h:174
Comparator for increasing comparison (less than).
Definition comparator.h:63
A base class for iterable containers that support multiple iteration patterns.
Definition iterable.h:59
iterAdaptor first()
Definition iterable.h:561
iterAdaptor end()
Definition iterable.h:542
iterAdaptor begin()
Definition iterable.h:537
Base iterator interface that supports common operations for iteration.
Definition iterator.h:37
Abstract base class for key-value mapping containers.
Definition map.h:49
Exception for missing element requests.
Definition error.h:136
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:30
Base class providing polymorphic string conversion capabilities.
Definition printable.h:29
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Skip List container implementation.
Definition skipList.h:45
Bidirectional iterator for treeMap.
Definition maps.h:453
couple< const K_TYPE, V_TYPE > & get() override
Gets current element (non-const)
Definition maps.h:1469
integer operator-(const iterator< couple< const K_TYPE, V_TYPE > > &other) const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1405
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition maps.h:1412
void next() const override
Moves to next element.
Definition maps.h:1448
bool atNext(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is next to this.
Definition maps.h:1441
bool atPrev(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is previous to this.
Definition maps.h:1424
void operator-=(integer steps) const override
Rewinds iterator by steps.
Definition maps.h:1398
Iterator * getPrev() const override
Gets previous iterator.
Definition maps.h:1461
std::string className() const override
Gets iterator class name.
Definition maps.h:1386
Iterator * clone() const override
Creates a copy of this iterator.
Definition maps.h:1380
bool isValid() const override
Checks if iterator is valid.
Definition maps.h:1487
void prev() const override
Moves to previous element.
Definition maps.h:1454
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition maps.h:1367
void operator+=(integer steps) const override
Advances iterator by steps.
Definition maps.h:1392
bool hasPrev() const override
Checks if more elements exist in backward direction.
Definition maps.h:1418
void set(const couple< const K_TYPE, V_TYPE > &data) override
Not supported.
Definition maps.h:1481
Red-Black Tree based implementation of the map interface.
Definition maps.h:424
Iterator * begins() const override
Gets begin iterator.
Definition maps.h:1604
treeMap & operator=(const treeMap &other)
Copy assignment operator.
Definition maps.h:1504
std::string className() const override
Gets class name.
Definition maps.h:1617
Iterator * ends() const override
Gets end iterator.
Definition maps.h:1611
bool containsKey(const K_TYPE &k) const override
Checks if key exists.
Definition maps.h:1567
V_TYPE get(const K_TYPE &k) const override
Gets value for key.
Definition maps.h:1572
~treeMap() override
Destructor.
std::string toString(bool enter) const override
Converts to string representation.
Definition maps.h:1622
bool contains(const couple< const K_TYPE, V_TYPE > &e) const override
Checks if key-value pair exists.
Definition maps.h:1552
bool remove(const K_TYPE &k) override
Removes key-value pair.
Definition maps.h:1562
bool add(const K_TYPE &k, const V_TYPE &v) override
Adds new key-value pair.
Definition maps.h:1557
const V_TYPE & operator[](const K_TYPE &k) const override
Const element access.
Definition maps.h:1585
bool update(const K_TYPE &key, const V_TYPE &value) override
Updates value for existing key.
Definition maps.h:1580
treeMap(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty treeMap.
Definition maps.h:1493
u_integer size() const override
Gets number of elements.
Definition maps.h:1546
Exception for unimplemented method calls.
Definition error.h:123
Dynamic array container with amortized constant time operations.
Definition vector.h:43
Comparator base class and concrete comparator classes.
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:92
Generic pair container implementation.
Provides a generic hashing utility and a base interface for hashable types.
Implementation of hashTable container.
Abstract base class for map-like container implementations.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:43
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:35
Exclusive-ownership smart pointer implementation.
Skip List container implementation.