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
80namespace original {
81
106 template <typename K_TYPE,
107 typename V_TYPE,
108 typename HASH = hash<K_TYPE>,
109 typename ALLOC = allocator<couple<const K_TYPE, V_TYPE>>>
111 : public hashTable<K_TYPE, V_TYPE, ALLOC, HASH>,
112 public map<K_TYPE, V_TYPE, ALLOC>,
113 public iterable<couple<const K_TYPE, V_TYPE>>,
114 public printable{
115
121
127 public:
128
142 class Iterator final : public hashTable<K_TYPE, V_TYPE, ALLOC, HASH>::Iterator,
143 public baseIterator<couple<const K_TYPE, V_TYPE>> {
144
152 explicit Iterator(vector<hashNode*, rebind_alloc_pointer>* buckets = nullptr,
153 u_integer bucket = 0, hashNode* node = nullptr);
154
161 bool equalPtr(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
162 public:
163 friend class hashMap;
164
169 Iterator(const Iterator& other);
170
177
182 Iterator* clone() const override;
183
188 [[nodiscard]] std::string className() const override;
189
195 void operator+=(integer steps) const override;
196
201 void operator-=(integer steps) const override;
202
208
213 [[nodiscard]] bool hasNext() const override;
214
219 [[nodiscard]] bool hasPrev() const override;
220
226 bool atPrev(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
227
233 bool atNext(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
234
238 void next() const override;
239
244 void prev() const override;
245
250 Iterator* getPrev() const override;
251
257
262 couple<const K_TYPE, V_TYPE> get() const override;
263
268 void set(const couple<const K_TYPE, V_TYPE> &data) override;
269
274 [[nodiscard]] bool isValid() const override;
275
276 ~Iterator() override = default;
277 };
278
284 explicit hashMap(HASH hash = HASH{}, ALLOC alloc = ALLOC{});
285
292 hashMap(const hashMap& other);
293
301 hashMap& operator=(const hashMap& other);
302
309 hashMap(hashMap&& other) noexcept;
310
319 hashMap& operator=(hashMap&& other) noexcept;
320
345 void swap(hashMap& other) noexcept;
346
351 [[nodiscard]] u_integer size() const override;
352
358 bool contains(const couple<const K_TYPE, V_TYPE> &e) const override;
359
366 bool add(const K_TYPE &k, const V_TYPE &v) override;
367
373 bool remove(const K_TYPE &k) override;
374
380 [[nodiscard]] bool containsKey(const K_TYPE &k) const override;
381
388 V_TYPE get(const K_TYPE &k) const override;
389
396 bool update(const K_TYPE &key, const V_TYPE &value) override;
397
404 const V_TYPE & operator[](const K_TYPE &k) const override;
405
412 V_TYPE & operator[](const K_TYPE &k) override;
413
418 Iterator* begins() const override;
419
424 Iterator* ends() const override;
425
430 [[nodiscard]] std::string className() const override;
431
437 [[nodiscard]] std::string toString(bool enter) const override;
438
439 ~hashMap() override;
440 };
441
468 template <typename K_TYPE,
469 typename V_TYPE,
470 typename Compare = increaseComparator<K_TYPE>,
471 typename ALLOC = allocator<couple<const K_TYPE, V_TYPE>>>
472 class treeMap final : public RBTree<K_TYPE, V_TYPE, ALLOC, Compare>,
473 public map<K_TYPE, V_TYPE, ALLOC>,
474 public iterable<couple<const K_TYPE, V_TYPE>>,
475 public printable {
476
482
488 public:
489
504 public baseIterator<couple<const K_TYPE, V_TYPE>> {
511 explicit Iterator(RBTreeType* tree, RBNode* cur);
512
519 bool equalPtr(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
520 public:
521 friend class treeMap;
522
527 Iterator(const Iterator& other);
528
535
540 Iterator* clone() const override;
541
546 [[nodiscard]] std::string className() const override;
547
552 void operator+=(integer steps) const override;
553
558 void operator-=(integer steps) const override;
559
564
569 [[nodiscard]] bool hasNext() const override;
570
575 [[nodiscard]] bool hasPrev() const override;
576
582 bool atPrev(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
583
589 bool atNext(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
590
594 void next() const override;
595
599 void prev() const override;
600
605 Iterator* getPrev() const override;
606
612
617 couple<const K_TYPE, V_TYPE> get() const override;
618
623 void set(const couple<const K_TYPE, V_TYPE> &data) override;
624
629 [[nodiscard]] bool isValid() const override;
630
631 ~Iterator() override = default;
632 };
633
634 friend class Iterator;
635
641 explicit treeMap(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
642
649 treeMap(const treeMap& other);
650
658 treeMap& operator=(const treeMap& other);
659
666 treeMap(treeMap&& other) noexcept;
667
676 treeMap& operator=(treeMap&& other) noexcept;
677
703 void swap(treeMap& other) noexcept;
704
709 [[nodiscard]] u_integer size() const override;
710
716 bool contains(const couple<const K_TYPE, V_TYPE> &e) const override;
717
724 bool add(const K_TYPE &k, const V_TYPE &v) override;
725
731 bool remove(const K_TYPE &k) override;
732
738 [[nodiscard]] bool containsKey(const K_TYPE &k) const override;
739
746 V_TYPE get(const K_TYPE &k) const override;
747
754 bool update(const K_TYPE &key, const V_TYPE &value) override;
755
762 const V_TYPE & operator[](const K_TYPE &k) const override;
763
770 V_TYPE & operator[](const K_TYPE &k) override;
771
776 Iterator* begins() const override;
777
782 Iterator* ends() const override;
783
788 [[nodiscard]] std::string className() const override;
789
795 [[nodiscard]] std::string toString(bool enter) const override;
796
801 ~treeMap() override;
802 };
803
829 template <typename K_TYPE,
830 typename V_TYPE,
833 class JMap final : public map<K_TYPE, V_TYPE, ALLOC>,
834 public skipList<const K_TYPE, V_TYPE, ALLOC, Compare>,
835 public iterable<couple<const K_TYPE, V_TYPE>>,
836 public printable {
837
839
845
846 public:
861 public baseIterator<couple<const K_TYPE, V_TYPE>> {
862
869 bool equalPtr(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
870
871 public:
872 friend class JMap;
873
879 explicit Iterator(skipListNode* cur);
880
885 Iterator(const Iterator& other);
886
893
898 Iterator* clone() const override;
899
904 [[nodiscard]] std::string className() const override;
905
910 void operator+=(integer steps) const override;
911
915 void operator-=(integer steps) const override;
916
924
929 [[nodiscard]] bool hasNext() const override;
930
934 [[nodiscard]] bool hasPrev() const override;
935
941 bool atPrev(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
942
948 bool atNext(const iterator<couple<const K_TYPE, V_TYPE>>* other) const override;
949
953 void next() const override;
954
958 void prev() const override;
959
963 Iterator* getPrev() const override;
964
970
975 couple<const K_TYPE, V_TYPE> get() const override;
976
980 void set(const couple<const K_TYPE, V_TYPE> &data) override;
981
986 [[nodiscard]] bool isValid() const override;
987
988 ~Iterator() override = default;
989 };
990
991 friend class Iterator;
992
998 explicit JMap(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
999
1006 JMap(const JMap& other);
1007
1015 JMap& operator=(const JMap& other);
1016
1023 JMap(JMap&& other) noexcept;
1024
1033 JMap& operator=(JMap&& other) noexcept;
1034
1060 void swap(JMap& other) noexcept;
1061
1066 [[nodiscard]] u_integer size() const override;
1067
1073 bool contains(const couple<const K_TYPE, V_TYPE> &e) const override;
1074
1081 bool add(const K_TYPE &k, const V_TYPE &v) override;
1082
1088 bool remove(const K_TYPE &k) override;
1089
1095 [[nodiscard]] bool containsKey(const K_TYPE &k) const override;
1096
1103 V_TYPE get(const K_TYPE &k) const override;
1104
1111 bool update(const K_TYPE &key, const V_TYPE &value) override;
1112
1119 const V_TYPE & operator[](const K_TYPE &k) const override;
1120
1127 V_TYPE & operator[](const K_TYPE &k) override;
1128
1133 Iterator* begins() const override;
1134
1139 Iterator* ends() const override;
1140
1145 [[nodiscard]] std::string className() const override;
1146
1152 [[nodiscard]] std::string toString(bool enter) const override;
1153
1158 ~JMap() override;
1159 };
1160}
1161
1162namespace std {
1191 template <typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1194
1222 template <typename K_TYPE, typename V_TYPE, typename COMPARE, typename ALLOC>
1225
1253 template <typename K_TYPE, typename V_TYPE, typename COMPARE, typename ALLOC>
1256}
1257
1258template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1260 vector<hashNode *, rebind_alloc_pointer> *buckets, u_integer bucket, hashNode *node)
1261 : hashTable<K_TYPE, V_TYPE, ALLOC, HASH>::Iterator(
1262 const_cast<vector<hashNode*, rebind_alloc_pointer>*>(buckets), bucket, node) {}
1263
1264template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1266 const iterator<couple<const K_TYPE, V_TYPE>>* other) const {
1267 auto other_it = dynamic_cast<const Iterator*>(other);
1268 return other_it &&
1269 this->p_buckets == other_it->p_buckets &&
1270 this->cur_bucket == other_it->cur_bucket &&
1271 this->p_node == other_it->p_node;
1272}
1273
1274template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1278
1279template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1288
1289template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1294
1295template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1297 return "hashMap::Iterator";
1298}
1299
1300template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1304
1305template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1309
1310template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1315
1316template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1320
1321template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1325
1326template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1329 auto other_it = dynamic_cast<const Iterator*>(other);
1330 if (!other_it) {
1331 return false;
1332 }
1333 auto next = ownerPtr(this->clone());
1334 if (!next->isValid()){
1335 return false;
1336 }
1337
1338 next->next();
1339 return next->equalPtr(other_it);
1340}
1341
1342template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1347
1348template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1352
1353template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1357
1358template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1363
1364template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1369
1370template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1375
1376template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1380
1381template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1385
1386template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1390
1391template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1395
1396template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1399 if (this == &other) {
1400 return *this;
1401 }
1402
1403 this->buckets = this->bucketsCopy(other.buckets);
1404 this->size_ = other.size_;
1405 this->hash_ = other.hash_;
1406 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1407 this->allocator = other.allocator;
1408 this->rebind_alloc = other.rebind_alloc;
1409 }
1410 return *this;
1411}
1412
1413template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1417
1418template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1421 if (this == &other) {
1422 return *this;
1423 }
1424
1425 this->buckets = std::move(other.buckets);
1426 this->size_ = other.size_;
1427 other.size_ = 0;
1428 this->hash_ = std::move(other.hash_);
1429 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1430 this->allocator = std::move(other.allocator);
1431 this->rebind_alloc = std::move(other.rebind_alloc);
1432 }
1433 return *this;
1434}
1435
1436template <typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1438{
1439 if (this == &other)
1440 return;
1441
1442 std::swap(this->size_, other.size_);
1443 std::swap(this->buckets, other.buckets);
1444 std::swap(this->hash_, other.hash_);
1445 if constexpr (ALLOC::propagate_on_container_swap::value) {
1446 std::swap(this->allocator, other.allocator);
1447 std::swap(this->rebind_alloc, other.rebind_alloc);
1448 }
1449}
1450
1451template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1454 return this->size_;
1455}
1456
1457template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1458bool
1460 return this->containsKey(e.first()) && this->get(e.first()) == e.second();
1461}
1462
1463template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1465 return this->insert(k, v);
1466}
1467
1468template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1470 return this->erase(k);
1471}
1472
1473template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1475 return this->find(k);
1476}
1477
1478template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1480 auto node = this->find(k);
1481 if (!node)
1482 throw noElementError();
1483 return node->getValue();
1484}
1485
1486template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1488 return this->modify(key, value);
1489}
1490
1491template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1493 auto node = this->find(k);
1494 if (!node)
1495 throw noElementError();
1496 return node->getValue();
1497}
1498
1499template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1501 auto node = this->find(k);
1502 if (!node) {
1503 this->insert(k, V_TYPE{});
1504 node = this->find(k);
1505 }
1506 return node->getValue();
1507}
1508
1509template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1512 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
1513 if (this->buckets[0]) {
1514 return new Iterator(p_buckets, 0, this->buckets[0]);
1515 }
1516 auto bucket = Iterator::findNextValidBucket(p_buckets, 0);
1517 return new Iterator(p_buckets, bucket, p_buckets->get(bucket));
1518}
1519
1520template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1523 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
1524 auto bucket = Iterator::findPrevValidBucket(p_buckets, this->buckets.size());
1525 auto node = this->buckets[bucket];
1526 while (node && node->getPNext()) {
1527 node = node->getPNext();
1528 }
1529 return new Iterator(p_buckets, bucket, node);
1530}
1531
1532template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1534 return "hashMap";
1535}
1536
1537template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1539 std::stringstream ss;
1540 ss << this->className();
1541 ss << "(";
1542 bool first = true;
1543 for (auto it = this->begin(); it != this->end(); it.next()){
1544 if (!first){
1545 ss << ", ";
1546 }
1547 ss << "{" << printable::formatString(it.get().template get<0>()) << ": "
1548 << printable::formatString(it.get().template get<1>()) << "}";
1549 first = false;
1550 }
1551 ss << ")";
1552 if (enter)
1553 ss << "\n";
1554 return ss.str();
1555}
1556
1557template<typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
1559
1560template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1562 : RBTreeType::Iterator(tree, cur) {}
1563
1564template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1565bool
1567 const iterator<couple<const K_TYPE, V_TYPE>>* other) const
1568{
1569 auto other_it = dynamic_cast<const Iterator*>(other);
1570 return other_it &&
1571 this->tree_ == other_it->tree_ &&
1572 this->cur_ == other_it->cur_;
1573}
1574
1575template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1580
1581template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1584{
1585 if (this == &other) {
1586 return *this;
1587 }
1588
1589 this->tree_ = other.tree_;
1590 this->cur_ = other.cur_;
1591 return *this;
1592}
1593
1594template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1600
1601template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1603{
1604 return "treeMap::Iterator";
1605}
1606
1607template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1609{
1610 RBTreeType::Iterator::operator+=(steps);
1611}
1612
1613template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1615{
1616 RBTreeType::Iterator::operator-=(steps);
1617}
1618
1619template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1626
1627template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1629{
1630 return RBTreeType::Iterator::hasNext();
1631}
1632
1633template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1635{
1636 return RBTreeType::Iterator::hasPrev();
1637}
1638
1639template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1642{
1643 auto other_it = dynamic_cast<const Iterator*>(other);
1644 if (!other_it) {
1645 return false;
1646 }
1647 auto next = ownerPtr(this->clone());
1648 if (!next->isValid()){
1649 return false;
1650 }
1651
1652 next->next();
1653 return next->equalPtr(other_it);
1654}
1655
1656template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1662
1663template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1665{
1666 RBTreeType::Iterator::next();
1667}
1668
1669template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1671{
1672 RBTreeType::Iterator::prev();
1673}
1674
1675template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1678{
1679 auto it = this->clone();
1680 it->prev();
1681 return it;
1682}
1683
1684template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1689
1690template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1695
1696template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1701
1702template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1704{
1705 return RBTreeType::Iterator::isValid();
1706}
1707
1708template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1712
1713template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1717
1718template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1721 if (this == &other){
1722 return *this;
1723 }
1724
1725 this->destroyTree();
1726 this->root_ = other.treeCopy();
1727 this->size_ = other.size_;
1728 this->compare_ = other.compare_;
1729 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1730 this->allocator = other.allocator;
1731 this->rebind_alloc = other.rebind_alloc;
1732 }
1733 return *this;
1734}
1735
1736template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1740
1741template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1744 if (this == &other){
1745 return *this;
1746 }
1747
1748 this->destroyTree();
1749 this->root_ = other.root_;
1750 other.root_ = nullptr;
1751 this->size_ = other.size_;
1752 other.size_ = 0;
1753 this->compare_ = std::move(other.compare_);
1754 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1755 this->allocator = std::move(other.allocator);
1756 this->rebind_alloc = std::move(other.rebind_alloc);
1757 }
1758 return *this;
1759}
1760
1761template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1763{
1764 if (this == &other)
1765 return;
1766
1767 std::swap(this->root_, other.root_);
1768 std::swap(this->size_, other.size_);
1769 std::swap(this->compare_, other.compare_);
1770 if constexpr (ALLOC::propagate_on_container_swap::value) {
1771 std::swap(this->allocator, other.allocator);
1772 std::swap(this->rebind_alloc, other.rebind_alloc);
1773 }
1774}
1775
1776template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1780
1781template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1782bool
1784 return this->containsKey(e.first()) && this->get(e.first()) == e.second();
1785}
1786
1787template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1789 return this->insert(k, v);
1790}
1791
1792template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1794 return this->erase(k);
1795}
1796
1797template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1799 return this->find(k);
1800}
1801
1802template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1804 auto node = this->find(k);
1805 if (!node)
1806 throw noElementError();
1807 return node->getValue();
1808}
1809
1810template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1812 return this->modify(key, value);
1813}
1814
1815template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1817 auto node = this->find(k);
1818 if (!node)
1819 throw noElementError();
1820 return node->getValue();
1821}
1822
1823template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1825 auto node = this->find(k);
1826 if (!node) {
1827 this->insert(k, V_TYPE{});
1828 node = this->find(k);
1829 }
1830 return node->getValue();
1831}
1832
1833template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1836{
1837 return new Iterator(const_cast<treeMap*>(this), this->getMinNode());
1838}
1839
1840template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1843{
1844 return new Iterator(const_cast<treeMap*>(this), this->getMaxNode());
1845}
1846
1847template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1849 return "treeMap";
1850}
1851
1852template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1854 std::stringstream ss;
1855 ss << this->className();
1856 ss << "(";
1857 bool first = true;
1858 for (auto it = this->begin(); it != this->end(); it.next()){
1859 if (!first){
1860 ss << ", ";
1861 }
1862 ss << "{" << printable::formatString(it.get().template get<0>()) << ": "
1863 << printable::formatString(it.get().template get<1>()) << "}";
1864 first = false;
1865 }
1866 ss << ")";
1867 if (enter)
1868 ss << "\n";
1869 return ss.str();
1870}
1871
1872template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1874
1875template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1878 auto other_it = dynamic_cast<const Iterator*>(other);
1879 return other_it && this->cur_ == other_it->cur_;
1880}
1881
1882template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1885
1886template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1889
1890template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1893 skipListType::Iterator::operator=(other);
1894 return *this;
1895}
1896
1897template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1902
1903template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1904std::string
1908
1909template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1911 skipListType::Iterator::operator+=(steps);
1912}
1913
1914template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1918
1919template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1923 auto other_it = dynamic_cast<const Iterator*>(&other);
1924 if (other_it == nullptr)
1925 return this > &other ?
1926 (std::numeric_limits<integer>::max)() :
1927 (std::numeric_limits<integer>::min)();
1928 return skipListType::Iterator::operator-(*other_it);
1929}
1930
1931template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1933 return skipListType::Iterator::hasNext();
1934}
1935
1936template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1940
1941template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1944 const auto other_it = dynamic_cast<const Iterator*>(other);
1945 if (!other_it){
1946 return false;
1947 }
1948 auto cloned_it = ownerPtr(other_it->clone());
1949 return this->equalPtr(cloned_it.get());
1950}
1951
1952template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1957
1958template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1960 skipListType::Iterator::next();
1961}
1962
1963template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1967
1968template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1973
1974template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1978
1979template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1983
1984template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1988
1989template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1991 return skipListType::Iterator::isValid();
1992}
1993
1994template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
1997
1998template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2002
2003template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2006 if (this == &other){
2007 return *this;
2008 }
2009
2010 this->listDestroy();
2011 this->head_ = other.listCopy();
2012 this->size_ = other.size_;
2013 this->compare_ = other.compare_;
2014 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
2015 this->allocator = other.allocator;
2016 this->rebind_alloc = other.rebind_alloc;
2017 }
2018 return *this;
2019}
2020
2021template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2025
2026template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2029 if (this == &other){
2030 return *this;
2031 }
2032
2033 this->listDestroy();
2034 this->head_ = other.head_;
2035 other.head_ = other.createNode();
2036 this->size_ = other.size_;
2037 other.size_ = 0;
2038 this->compare_ = std::move(other.compare_);
2039 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
2040 this->allocator = std::move(other.allocator);
2041 this->rebind_alloc = std::move(other.rebind_alloc);
2042 }
2043 return *this;
2044}
2045
2046template <typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2048{
2049 if (this == &other)
2050 return;
2051
2052 std::swap(this->size_, other.size_);
2053 std::swap(this->head_, other.head_);
2054 std::swap(this->compare_, other.compare_);
2055 if constexpr(ALLOC::propagate_on_container_swap::value) {
2056 std::swap(this->allocator, other.allocator);
2057 std::swap(this->rebind_alloc, other.rebind_alloc);
2058 }
2059}
2060
2061template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2065
2066template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2068 return this->containsKey(e.first()) && this->get(e.first()) == e.second();
2069}
2070
2071template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2073 return this->insert(k, v);
2074}
2075
2076template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2078 return this->erase(k);
2079}
2080
2081template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2083 return this->find(k);
2084}
2085
2086template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2088 auto node = this->find(k);
2089 if (!node)
2090 throw noElementError();
2091 return node->getValue();
2092}
2093
2094template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2096 return this->modify(key, value);
2097}
2098
2099template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2101 auto node = this->find(k);
2102 if (!node)
2103 throw noElementError();
2104 return node->getValue();
2105}
2106
2107template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2109 auto node = this->find(k);
2110 if (!node) {
2111 this->insert(k, V_TYPE{});
2112 node = this->find(k);
2113 }
2114 return node->getValue();
2115}
2116
2117template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2120 return new Iterator(this->head_->getPNext(1));
2121}
2122
2123template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2126 return new Iterator(this->findLastNode());
2127}
2128
2129template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2131 return "JMap";
2132}
2133
2134template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2136 std::stringstream ss;
2137 ss << this->className();
2138 ss << "(";
2139 bool first = true;
2140 for (auto it = this->begin(); it != this->end(); it.next()){
2141 if (!first){
2142 ss << ", ";
2143 }
2144 ss << "{" << printable::formatString(it.get().template get<0>()) << ": "
2145 << printable::formatString(it.get().template get<1>()) << "}";
2146 first = false;
2147 }
2148 ss << ")";
2149 if (enter)
2150 ss << "\n";
2151 return ss.str();
2152}
2153
2154template<typename K_TYPE, typename V_TYPE, typename Compare, typename ALLOC>
2156
2157template <typename K_TYPE, typename V_TYPE, typename HASH, typename ALLOC>
2163
2164template <typename K_TYPE, typename V_TYPE, typename COMPARE, typename ALLOC>
2170
2171template <typename K_TYPE, typename V_TYPE, typename COMPARE, typename ALLOC>
2177
2178#endif //MAPS_H
Red-Black Tree implementation header.
Memory allocation interface and implementations.
Forward iterator for JMap.
Definition maps.h:861
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition maps.h:1892
bool atNext(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is next to this.
Definition maps.h:1953
Iterator * getPrev() const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1970
bool hasPrev() const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1937
Iterator(skipListNode *cur)
Constructs iterator pointing to specific skip list node.
Definition maps.h:1883
std::string className() const override
Gets iterator class name.
Definition maps.h:1905
void prev() const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1964
void operator+=(integer steps) const override
Advances iterator by steps.
Definition maps.h:1910
void operator-=(integer steps) const override
Not supported (throws unSupportedMethodError)
Definition maps.h:1915
void next() const override
Moves to next element.
Definition maps.h:1959
bool isValid() const override
Checks if iterator is valid.
Definition maps.h:1990
void set(const couple< const K_TYPE, V_TYPE > &data) override
Not supported (throws unSupportedMethodError)
Definition maps.h:1985
couple< const K_TYPE, V_TYPE > & get() override
Gets current element (non-const)
Definition maps.h:1975
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition maps.h:1932
bool atPrev(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is previous to this.
Definition maps.h:1942
Iterator * clone() const override
Creates a copy of this iterator.
Definition maps.h:1899
Skip List based implementation of the map interface.
Definition maps.h:836
bool update(const K_TYPE &key, const V_TYPE &value) override
Updates value for existing key.
Definition maps.h:2095
std::string toString(bool enter) const override
Converts to string representation.
Definition maps.h:2135
Iterator * ends() const override
Gets end iterator.
Definition maps.h:2125
const V_TYPE & operator[](const K_TYPE &k) const override
Const element access.
Definition maps.h:2100
bool remove(const K_TYPE &k) override
Removes key-value pair.
Definition maps.h:2077
bool containsKey(const K_TYPE &k) const override
Checks if key exists.
Definition maps.h:2082
bool contains(const couple< const K_TYPE, V_TYPE > &e) const override
Checks if key-value pair exists.
Definition maps.h:2067
~JMap() override
Destructor.
void swap(JMap &other) noexcept
Swaps contents with another JMap.
Definition maps.h:2047
u_integer size() const override
Gets number of elements.
Definition maps.h:2062
std::string className() const override
Gets class name.
Definition maps.h:2130
bool add(const K_TYPE &k, const V_TYPE &v) override
Adds new key-value pair.
Definition maps.h:2072
Iterator * begins() const override
Gets begin iterator.
Definition maps.h:2119
V_TYPE get(const K_TYPE &k) const override
Gets value for key.
Definition maps.h:2087
JMap & operator=(const JMap &other)
Copy assignment operator.
Definition maps.h:2005
JMap(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty JMap.
Definition maps.h:1995
Bidirectional iterator for RBTree.
Definition RBTree.h:219
Internal node class for Red-Black Tree.
Definition RBTree.h:51
Red-Black Tree container implementation.
Definition RBTree.h:40
Default memory allocator using allocators utilities.
Definition allocator.h:153
void swap(autoPtr &other) noexcept
Swaps the reference counters between two autoPtr instances.
Definition autoPtr.h:703
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:629
A base class for basic iterators.
Definition iterator.h:296
Bidirectional iterator for hashMap.
Definition maps.h:143
void next() const override
Moves to next element.
Definition maps.h:1349
bool atNext(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is next to this.
Definition maps.h:1343
Iterator * getPrev() const override
Not supported.
Definition maps.h:1360
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition maps.h:1281
bool hasNext() const override
Checks if more elements exist.
Definition maps.h:1317
bool atPrev(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is previous to this.
Definition maps.h:1327
std::string className() const override
Gets iterator class name.
Definition maps.h:1296
void prev() const override
Not supported.
Definition maps.h:1354
void operator+=(integer steps) const override
Advances iterator by steps.
Definition maps.h:1301
void operator-=(integer steps) const override
Not supported.
Definition maps.h:1306
bool isValid() const override
Checks if iterator is valid.
Definition maps.h:1382
void set(const couple< const K_TYPE, V_TYPE > &data) override
Not supported.
Definition maps.h:1377
Iterator * clone() const override
Creates a copy of this iterator.
Definition maps.h:1291
couple< const K_TYPE, V_TYPE > & get() override
Gets current element (non-const)
Definition maps.h:1366
bool hasPrev() const override
Not supported.
Definition maps.h:1322
Hash table based implementation of the map interface.
Definition maps.h:114
void swap(hashMap &other) noexcept
Swaps contents with another hashMap.
Definition maps.h:1437
hashMap(HASH hash=HASH{}, ALLOC alloc=ALLOC{})
Constructs empty hashMap.
Definition maps.h:1387
u_integer size() const override
Gets number of elements.
Definition maps.h:1453
bool add(const K_TYPE &k, const V_TYPE &v) override
Adds new key-value pair.
Definition maps.h:1464
bool remove(const K_TYPE &k) override
Removes key-value pair.
Definition maps.h:1469
Iterator * ends() const override
Gets end iterator.
Definition maps.h:1522
bool contains(const couple< const K_TYPE, V_TYPE > &e) const override
Checks if key-value pair exists.
Definition maps.h:1459
bool containsKey(const K_TYPE &k) const override
Checks if key exists.
Definition maps.h:1474
std::string className() const override
Gets class name.
Definition maps.h:1533
const V_TYPE & operator[](const K_TYPE &k) const override
Const element access.
Definition maps.h:1492
bool update(const K_TYPE &key, const V_TYPE &value) override
Updates value for existing key.
Definition maps.h:1487
Iterator * begins() const override
Gets begin iterator.
Definition maps.h:1511
std::string toString(bool enter) const override
Converts to string representation.
Definition maps.h:1538
hashMap & operator=(const hashMap &other)
Copy assignment operator.
Definition maps.h:1398
V_TYPE get(const K_TYPE &k) const override
Gets value for key.
Definition maps.h:1479
void next() const
Advances to the next element.
Definition hashTable.h:633
couple< const K_TYPE, V_TYPE > & get()
Gets current key-value pair (non-const)
Definition hashTable.h:667
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition hashTable.h:614
void operator+=(integer steps) const
Advances iterator by steps positions.
Definition hashTable.h:655
bool isValid() const
Checks if iterator points to valid element.
Definition hashTable.h:686
bool hasNext() const
Checks if more elements are available.
Definition hashTable.h:624
Internal node type for hash table storage.
Definition hashTable.h:70
Hash table implementation with separate chaining.
Definition hashTable.h:54
ALLOC::template rebind_alloc< hashNode * > rebind_alloc_pointer
Rebound allocator type for pointer storage.
Definition hashTable.h:173
Generic hash function object supporting multiple types.
Definition hash.h:62
A base class for iterable containers that support multiple iteration patterns.
Definition iterable.h:70
Base iterator interface that supports common operations for iteration.
Definition iterator.h:37
friend iterator< T > * operator-(const iterator< T > &it, integer steps)
Subtracts a number of steps from the iterator's current position and returns a new iterator.
Abstract base class for key-value mapping containers.
Definition map.h:49
Exception for missing element requests.
Definition error.h:298
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Base class providing polymorphic string conversion capabilities.
Definition printable.h:39
static std::string formatString(const TYPE &t)
Universal value-to-string conversion with type-specific formatting.
Definition printable.h:339
Abstract base class for unique element containers.
Definition set.h:44
Forward iterator for skipList.
Definition skipList.h:164
Internal node class for Skip List.
Definition skipList.h:54
Skip List container implementation.
Definition skipList.h:45
Bidirectional iterator for treeMap.
Definition maps.h:504
couple< const K_TYPE, V_TYPE > & get() override
Gets current element (non-const)
Definition maps.h:1685
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition maps.h:1628
void next() const override
Moves to next element.
Definition maps.h:1664
bool atNext(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is next to this.
Definition maps.h:1657
bool atPrev(const iterator< couple< const K_TYPE, V_TYPE > > *other) const override
Checks if other is previous to this.
Definition maps.h:1640
void operator-=(integer steps) const override
Rewinds iterator by steps.
Definition maps.h:1614
Iterator * getPrev() const override
Gets previous iterator.
Definition maps.h:1677
std::string className() const override
Gets iterator class name.
Definition maps.h:1602
Iterator * clone() const override
Creates a copy of this iterator.
Definition maps.h:1596
bool isValid() const override
Checks if iterator is valid.
Definition maps.h:1703
void prev() const override
Moves to previous element.
Definition maps.h:1670
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition maps.h:1583
void operator+=(integer steps) const override
Advances iterator by steps.
Definition maps.h:1608
bool hasPrev() const override
Checks if more elements exist in backward direction.
Definition maps.h:1634
void set(const couple< const K_TYPE, V_TYPE > &data) override
Not supported.
Definition maps.h:1697
Red-Black Tree based implementation of the map interface.
Definition maps.h:475
Iterator * begins() const override
Gets begin iterator.
Definition maps.h:1835
treeMap & operator=(const treeMap &other)
Copy assignment operator.
Definition maps.h:1720
std::string className() const override
Gets class name.
Definition maps.h:1848
Iterator * ends() const override
Gets end iterator.
Definition maps.h:1842
bool containsKey(const K_TYPE &k) const override
Checks if key exists.
Definition maps.h:1798
V_TYPE get(const K_TYPE &k) const override
Gets value for key.
Definition maps.h:1803
~treeMap() override
Destructor.
std::string toString(bool enter) const override
Converts to string representation.
Definition maps.h:1853
bool contains(const couple< const K_TYPE, V_TYPE > &e) const override
Checks if key-value pair exists.
Definition maps.h:1783
bool remove(const K_TYPE &k) override
Removes key-value pair.
Definition maps.h:1793
void swap(treeMap &other) noexcept
Swaps contents with another treeMap.
Definition maps.h:1762
bool add(const K_TYPE &k, const V_TYPE &v) override
Adds new key-value pair.
Definition maps.h:1788
const V_TYPE & operator[](const K_TYPE &k) const override
Const element access.
Definition maps.h:1816
bool update(const K_TYPE &key, const V_TYPE &value) override
Updates value for existing key.
Definition maps.h:1811
treeMap(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty treeMap.
Definition maps.h:1709
u_integer size() const override
Gets number of elements.
Definition maps.h:1777
Exception for unimplemented method calls.
Definition error.h:271
Comparator base class and concrete comparator classes.
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:377
Generic pair container implementation.
Implementation of hashTable container.
Provides a generic hashing utility and interface for hashable types.
Abstract base class for map-like container implementations.
Main namespace for the project Original.
Definition algorithms.h:21
TYPE find(coroutine::generator< TYPE > gen, Callback &&c)
Finds the first element satisfying a predicate.
Definition generators.h:856
Standard namespace extensions for original::alternative.
Definition allocator.h:351
void swap(original::objPoolAllocator< TYPE > &lhs, original::objPoolAllocator< TYPE > &rhs) noexcept
Specialization of std::swap for objPoolAllocator.
Definition allocator.h:635
Exclusive-ownership smart pointer implementation.
Skip List container implementation.