ORIGINAL
Loading...
Searching...
No Matches
sets.h
Go to the documentation of this file.
1#ifndef SETS_H
2#define SETS_H
3
4#include "allocator.h"
5#include "couple.h"
6#include "hash.h"
7#include "hashTable.h"
8#include "set.h"
9#include "ownerPtr.h"
10#include "comparator.h"
11#include "RBTree.h"
12#include "skipList.h"
13
14
48
49namespace original {
73 template <typename TYPE,
74 typename HASH = hash<TYPE>,
76 class hashSet final : public hashTable<TYPE, const bool, ALLOC, HASH>,
77 public set<TYPE, ALLOC>,
78 public iterable<const TYPE>,
79 public printable{
80
86
91 using rebind_alloc_pointer = typename hashTable<TYPE, const bool, ALLOC, HASH>::rebind_alloc_pointer;
92
93 public:
107 class Iterator final : public hashTable<TYPE, const bool, ALLOC, HASH>::Iterator,
108 public baseIterator<const TYPE> {
109
117 explicit Iterator(vector<hashNode*, rebind_alloc_pointer>* buckets = nullptr,
118 u_integer bucket = 0, hashNode* node = nullptr);
119
126 bool equalPtr(const iterator<const TYPE>* other) const override;
127
128 public:
129 friend class hashSet;
130
135 Iterator(const Iterator& other);
136
142 Iterator& operator=(const Iterator& other);
143
148 Iterator* clone() const override;
149
154 [[nodiscard]] std::string className() const override;
155
161 void operator+=(integer steps) const override;
162
166 void operator-=(integer steps) const override;
167
171 integer operator-(const iterator<const TYPE>& other) const override;
172
177 [[nodiscard]] bool hasNext() const override;
178
182 [[nodiscard]] bool hasPrev() const override;
183
189 bool atPrev(const iterator<const TYPE>* other) const override;
190
196 bool atNext(const iterator<const TYPE>* other) const override;
197
201 void next() const override;
202
206 void prev() const override;
207
211 Iterator* getPrev() const override;
212
217 const TYPE& get() override;
218
223 const TYPE get() const override;
224
228 void set(const TYPE& data) override;
229
234 [[nodiscard]] bool isValid() const override;
235
236 ~Iterator() override = default;
237 };
238
244 explicit hashSet(HASH hash = HASH{}, ALLOC alloc = ALLOC{});
245
252 hashSet(const hashSet& other);
253
261 hashSet& operator=(const hashSet& other);
262
269 hashSet(hashSet&& other) noexcept;
270
279 hashSet& operator=(hashSet&& other) noexcept;
280
285 [[nodiscard]] u_integer size() const override;
286
292 bool contains(const TYPE &e) const override;
293
299 bool add(const TYPE &e) override;
300
306 bool remove(const TYPE &e) override;
307
312 Iterator* begins() const override;
313
318 Iterator* ends() const override;
319
324 [[nodiscard]] std::string className() const override;
325
331 [[nodiscard]] std::string toString(bool enter) const override;
332
333 ~hashSet() override;
334 };
335
361 template <typename TYPE,
364 class treeSet final : public RBTree<TYPE, const bool, ALLOC, Compare>,
365 public set<TYPE, ALLOC>,
366 public iterable<const TYPE>,
367 public printable {
369
374 using RBNode = typename RBTreeType::RBNode;
375 public:
389 class Iterator final : public RBTreeType::Iterator,
390 public baseIterator<const TYPE>
391 {
398 explicit Iterator(RBTreeType* tree, RBNode* cur);
399
406 bool equalPtr(const iterator<const TYPE>* other) const override;
407 public:
408 friend class treeSet;
409
414 Iterator(const Iterator& other);
415
421 Iterator& operator=(const Iterator& other);
422
427 Iterator* clone() const override;
428
433 [[nodiscard]] std::string className() const override;
434
439 void operator+=(integer steps) const override;
440
445 void operator-=(integer steps) const override;
446
450 integer operator-(const iterator<const TYPE> &other) const override;
451
456 [[nodiscard]] bool hasNext() const override;
457
462 [[nodiscard]] bool hasPrev() const override;
463
469 bool atPrev(const iterator<const TYPE>* other) const override;
470
476 bool atNext(const iterator<const TYPE>* other) const override;
477
481 void next() const override;
482
486 void prev() const override;
487
492 Iterator* getPrev() const override;
493
498 const TYPE& get() override;
499
504 const TYPE get() const override;
505
510 void set(const TYPE& data) override;
511
516 [[nodiscard]] bool isValid() const override;
517
518 ~Iterator() override = default;
519 };
520
521 friend class Iterator;
522
528 explicit treeSet(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
529
536 treeSet(const treeSet& other);
537
545 treeSet& operator=(const treeSet& other);
546
553 treeSet(treeSet&& other) noexcept;
554
563 treeSet& operator=(treeSet&& other) noexcept;
564
569 [[nodiscard]] u_integer size() const override;
570
576 bool contains(const TYPE &e) const override;
577
583 bool add(const TYPE &e) override;
584
590 bool remove(const TYPE &e) override;
591
596 Iterator* begins() const override;
597
602 Iterator* ends() const override;
603
608 [[nodiscard]] std::string className() const override;
609
615 [[nodiscard]] std::string toString(bool enter) const override;
616
621 ~treeSet() override;
622 };
623
624
649 template <typename TYPE,
652 class JSet final : public skipList<const TYPE, const bool, ALLOC, Compare>,
653 public set<TYPE, ALLOC>,
654 public iterable<const TYPE>,
655 public printable {
657
662 using skipListNode = typename skipListType::skipListNode;
663
664 public:
678 class Iterator final : public skipListType::Iterator,
679 public baseIterator<const TYPE> {
680
687 bool equalPtr(const iterator<const TYPE>* other) const override;
688
689 public:
690 friend class JSet;
691
697 explicit Iterator(skipListNode* cur);
698
703 Iterator(const Iterator& other);
704
710 Iterator& operator=(const Iterator& other);
711
716 Iterator* clone() const override;
717
722 [[nodiscard]] std::string className() const override;
723
728 void operator+=(integer steps) const override;
729
733 void operator-=(integer steps) const override;
734
741 integer operator-(const iterator<const TYPE>& other) const override;
742
747 [[nodiscard]] bool hasNext() const override;
748
752 [[nodiscard]] bool hasPrev() const override;
753
759 bool atPrev(const iterator<const TYPE>* other) const override;
760
766 bool atNext(const iterator<const TYPE>* other) const override;
767
771 void next() const override;
772
776 void prev() const override;
777
781 Iterator* getPrev() const override;
782
787 const TYPE& get() override;
788
793 const TYPE get() const override;
794
798 void set(const TYPE& data) override;
799
804 [[nodiscard]] bool isValid() const override;
805
806 ~Iterator() override = default;
807 };
808
809 friend class Iterator;
810
816 explicit JSet(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
817
824 JSet(const JSet& other);
825
833 JSet& operator=(const JSet& other);
834
841 JSet(JSet&& other) noexcept;
842
851 JSet& operator=(JSet&& other) noexcept;
852
857 [[nodiscard]] u_integer size() const override;
858
864 bool contains(const TYPE &e) const override;
865
871 bool add(const TYPE &e) override;
872
879 bool remove(const TYPE &e) override;
880
886 Iterator* begins() const override;
887
893 Iterator* ends() const override;
894
899 [[nodiscard]] std::string className() const override;
900
907 [[nodiscard]] std::string toString(bool enter) const override;
908
914 ~JSet() override;
915 };
916}
917
918template<typename TYPE, typename HASH, typename ALLOC>
919original::hashSet<TYPE, HASH, ALLOC>::Iterator::Iterator(vector<hashNode*, rebind_alloc_pointer> *buckets,
920u_integer bucket, hashNode *node) : hashTable<TYPE, const bool, ALLOC, HASH>::Iterator(
921 const_cast<vector<hashNode*, rebind_alloc_pointer>*>(buckets), bucket, node) {}
922
923template<typename TYPE, typename HASH, typename ALLOC>
924bool original::hashSet<TYPE, HASH, ALLOC>::Iterator::equalPtr(const iterator<const TYPE> *other) const {
925 auto other_it = dynamic_cast<const Iterator*>(other);
926 return other_it &&
927 this->p_buckets == other_it->p_buckets &&
928 this->cur_bucket == other_it->cur_bucket &&
929 this->p_node == other_it->p_node;
930}
931
932template<typename TYPE, typename HASH, typename ALLOC>
933original::hashSet<TYPE, HASH, ALLOC>::Iterator::Iterator(const Iterator &other) : Iterator() {
934 this->operator=(other);
935}
936
937template<typename TYPE, typename HASH, typename ALLOC>
940 if (this == &other) {
941 return *this;
942 }
943
945 return *this;
946}
947
948template<typename TYPE, typename HASH, typename ALLOC>
951 return new Iterator(*this);
952}
953
954template<typename TYPE, typename HASH, typename ALLOC>
956 return "hashSet::Iterator";
957}
958
959template<typename TYPE, typename HASH, typename ALLOC>
963
964template<typename TYPE, typename HASH, typename ALLOC>
968
969template<typename TYPE, typename HASH, typename ALLOC>
974
975template<typename TYPE, typename HASH, typename ALLOC>
979
980template<typename TYPE, typename HASH, typename ALLOC>
984
985template<typename TYPE, typename HASH, typename ALLOC>
987 auto other_it = dynamic_cast<const Iterator*>(other);
988 if (!other_it) {
989 return false;
990 }
991 auto next = ownerPtr(this->clone());
992 if (!next->isValid()){
993 return false;
994 }
995
996 next->next();
997 return next->equalPtr(other_it);
998}
999
1000template<typename TYPE, typename HASH, typename ALLOC>
1002 return other->atPrev(*this);
1003}
1004
1005template<typename TYPE, typename HASH, typename ALLOC>
1009
1010template<typename TYPE, typename HASH, typename ALLOC>
1014
1015template<typename TYPE, typename HASH, typename ALLOC>
1020
1021template<typename TYPE, typename HASH, typename ALLOC>
1025
1026template<typename TYPE, typename HASH, typename ALLOC>
1030
1031template<typename TYPE, typename HASH, typename ALLOC>
1035
1036template<typename TYPE, typename HASH, typename ALLOC>
1040
1041template<typename TYPE, typename HASH, typename ALLOC>
1043 : hashTable<TYPE, const bool, ALLOC, HASH>(std::move(hash)),
1044 set<TYPE, ALLOC>(std::move(alloc)) {}
1045
1046template<typename TYPE, typename HASH, typename ALLOC>
1048 this->operator=(other);
1049}
1050
1051template<typename TYPE, typename HASH, typename ALLOC>
1054 if (this == &other) {
1055 return *this;
1056 }
1057
1058 this->buckets = this->bucketsCopy(other.buckets);
1059 this->size_ = other.size_;
1060 this->hash_ = other.hash_;
1061 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1062 this->allocator = other.allocator;
1063 this->rebind_alloc = other.rebind_alloc;
1064 }
1065 return *this;
1066}
1067
1068template<typename TYPE, typename HASH, typename ALLOC>
1070 this->operator=(std::move(other));
1071}
1072
1073template<typename TYPE, typename HASH, typename ALLOC>
1076 if (this == &other) {
1077 return *this;
1078 }
1079
1080 this->buckets = std::move(other.buckets);
1081 this->size_ = other.size_;
1082 other.size_ = 0;
1083 this->hash_ = std::move(other.hash_);
1084 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1085 this->allocator = std::move(other.allocator);
1086 this->rebind_alloc = std::move(other.rebind_alloc);
1087 }
1088 return *this;
1089}
1090
1091template<typename TYPE, typename HASH, typename ALLOC>
1095
1096template<typename TYPE, typename HASH, typename ALLOC>
1098 return this->find(e);
1099}
1100
1101template<typename TYPE, typename HASH, typename ALLOC>
1103 return this->insert(e, true);
1104}
1105
1106template<typename TYPE, typename HASH, typename ALLOC>
1108 return this->erase(e);
1109}
1110
1111template<typename TYPE, typename HASH, typename ALLOC>
1114 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
1115 if (this->buckets[0]) {
1116 return new Iterator(p_buckets, 0, this->buckets[0]);
1117 }
1118 auto bucket = Iterator::findNextValidBucket(p_buckets, 0);
1119 return new Iterator(p_buckets, bucket, p_buckets->get(bucket));
1120}
1121
1122template<typename TYPE, typename HASH, typename ALLOC>
1125 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
1126 auto bucket = Iterator::findPrevValidBucket(p_buckets, this->buckets.size());
1127 auto node = this->buckets[bucket];
1128 while (node && node->getPNext()) {
1129 node = node->getPNext();
1130 }
1131 return new Iterator(p_buckets, bucket, node);
1132}
1133
1134template<typename TYPE, typename HASH, typename ALLOC>
1136 return "hashSet";
1137}
1138
1139template<typename TYPE, typename HASH, typename ALLOC>
1140std::string original::hashSet<TYPE, HASH, ALLOC>::toString(const bool enter) const {
1141 std::stringstream ss;
1142 ss << this->className();
1143 ss << "(";
1144 bool first = true;
1145 for (auto it = this->begin(); it != this->end(); it.next()){
1146 if (!first){
1147 ss << ", ";
1148 }
1149 ss << printable::formatString(it.get());
1150 first = false;
1151 }
1152 ss << ")";
1153 if (enter)
1154 ss << "\n";
1155 return ss.str();
1156}
1157
1158template<typename TYPE, typename HASH, typename ALLOC>
1159original::hashSet<TYPE, HASH, ALLOC>::~hashSet() = default;
1160
1161template <typename TYPE, typename Compare, typename ALLOC>
1162original::treeSet<TYPE, Compare, ALLOC>::Iterator::Iterator(RBTreeType* tree, RBNode* cur)
1163 : RBTreeType::Iterator(tree, cur) {}
1164
1165template <typename TYPE, typename Compare, typename ALLOC>
1166bool original::treeSet<TYPE, Compare, ALLOC>::Iterator::equalPtr(const iterator<const TYPE>* other) const
1167{
1168 auto other_it = dynamic_cast<const Iterator*>(other);
1169 return other_it &&
1170 this->tree_ == other_it->tree_ &&
1171 this->cur_ == other_it->cur_;
1172}
1173
1174template <typename TYPE, typename Compare, typename ALLOC>
1175original::treeSet<TYPE, Compare, ALLOC>::Iterator::Iterator(const Iterator& other)
1176 : Iterator(nullptr, nullptr)
1177{
1178 this->operator=(other);
1179}
1180
1181template <typename TYPE, typename Compare, typename ALLOC>
1184{
1185 if (this == &other) {
1186 return *this;
1187 }
1188
1189 this->tree_ = other.tree_;
1190 this->cur_ = other.cur_;
1191 return *this;
1192}
1193
1194template <typename TYPE, typename Compare, typename ALLOC>
1197{
1198 return new Iterator(*this);
1199}
1200
1201template <typename TYPE, typename Compare, typename ALLOC>
1203{
1204 return "treeSet::Iterator";
1205}
1206
1207template <typename TYPE, typename Compare, typename ALLOC>
1209{
1210 RBTreeType::Iterator::operator+=(steps);
1211}
1212
1213template <typename TYPE, typename Compare, typename ALLOC>
1215{
1216 RBTreeType::Iterator::operator-=(steps);
1217}
1218
1219template <typename TYPE, typename Compare, typename ALLOC>
1225
1226template <typename TYPE, typename Compare, typename ALLOC>
1228{
1229 return RBTreeType::Iterator::hasNext();
1230}
1231
1232template <typename TYPE, typename Compare, typename ALLOC>
1234{
1235 return RBTreeType::Iterator::hasPrev();
1236}
1237
1238template <typename TYPE, typename Compare, typename ALLOC>
1240{
1241 auto other_it = dynamic_cast<const Iterator*>(other);
1242 if (!other_it) {
1243 return false;
1244 }
1245 auto next = ownerPtr(this->clone());
1246 if (!next->isValid()){
1247 return false;
1248 }
1249
1250 next->next();
1251 return next->equalPtr(other_it);
1252}
1253
1254template <typename TYPE, typename Compare, typename ALLOC>
1256{
1257 return other->atNext(*this);
1258}
1259
1260template <typename TYPE, typename Compare, typename ALLOC>
1262{
1263 RBTreeType::Iterator::next();
1264}
1265
1266template <typename TYPE, typename Compare, typename ALLOC>
1268{
1269 RBTreeType::Iterator::prev();
1270}
1271
1272template <typename TYPE, typename Compare, typename ALLOC>
1275{
1276 auto it = this->clone();
1277 it->prev();
1278 return it;
1279}
1280
1281template <typename TYPE, typename Compare, typename ALLOC>
1283{
1284 return RBTreeType::Iterator::get().template get<0>();
1285}
1286
1287template <typename TYPE, typename Compare, typename ALLOC>
1289{
1290 return RBTreeType::Iterator::get().template get<0>();
1291}
1292
1293template <typename TYPE, typename Compare, typename ALLOC>
1298
1299template <typename TYPE, typename Compare, typename ALLOC>
1301{
1302 return RBTreeType::Iterator::isValid();
1303}
1304
1305template<typename TYPE, typename Compare, typename ALLOC>
1307 : RBTreeType(std::move(comp)),
1308 set<TYPE, ALLOC>(std::move(alloc)) {}
1309
1310template<typename TYPE, typename Compare, typename ALLOC>
1314
1315template<typename TYPE, typename Compare, typename ALLOC>
1318 if (this == &other){
1319 return *this;
1320 }
1321
1322 this->destroyTree();
1323 this->root_ = other.treeCopy();
1324 this->size_ = other.size_;
1325 this->compare_ = other.compare_;
1326 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1327 this->allocator = other.allocator;
1328 this->rebind_alloc = other.rebind_alloc;
1329 }
1330 return *this;
1331}
1332
1333template<typename TYPE, typename Compare, typename ALLOC>
1335 this->operator=(std::move(other));
1336}
1337
1338template<typename TYPE, typename Compare, typename ALLOC>
1341 if (this == &other){
1342 return *this;
1343 }
1344
1345 this->destroyTree();
1346 this->root_ = other.root_;
1347 other.root_ = nullptr;
1348 this->size_ = other.size_;
1349 other.size_ = 0;
1350 this->compare_ = std::move(other.compare_);
1351 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1352 this->allocator = std::move(other.allocator);
1353 this->rebind_alloc = std::move(other.rebind_alloc);
1354 }
1355 return *this;
1356}
1357
1358template<typename TYPE, typename Compare, typename ALLOC>
1362
1363template<typename TYPE, typename Compare, typename ALLOC>
1365 return this->find(e);
1366}
1367
1368template<typename TYPE, typename Compare, typename ALLOC>
1370 return this->insert(e, true);
1371}
1372
1373template<typename TYPE, typename Compare, typename ALLOC>
1375 return this->erase(e);
1376}
1377
1378template <typename TYPE, typename Compare, typename ALLOC>
1381{
1382 return new Iterator(const_cast<treeSet*>(this), this->getMinNode());
1383}
1384
1385template <typename TYPE, typename Compare, typename ALLOC>
1388{
1389 return new Iterator(const_cast<treeSet*>(this), this->getMaxNode());
1390}
1391
1392template<typename TYPE, typename Compare, typename ALLOC>
1394 return "treeSet";
1395}
1396
1397template<typename TYPE, typename Compare, typename ALLOC>
1398std::string original::treeSet<TYPE, Compare, ALLOC>::toString(const bool enter) const {
1399 std::stringstream ss;
1400 ss << this->className();
1401 ss << "(";
1402 bool first = true;
1403 for (auto it = this->begin(); it != this->end(); it.next()){
1404 if (!first){
1405 ss << ", ";
1406 }
1407 ss << printable::formatString(it.get());
1408 first = false;
1409 }
1410 ss << ")";
1411 if (enter)
1412 ss << "\n";
1413 return ss.str();
1414}
1415
1416template<typename TYPE, typename Compare, typename ALLOC>
1418
1419template<typename TYPE, typename Compare, typename ALLOC>
1420bool original::JSet<TYPE, Compare, ALLOC>::Iterator::equalPtr(
1421 const iterator<const TYPE>* other) const {
1422 auto other_it = dynamic_cast<const Iterator*>(other);
1423 return other_it && this->cur_ == other_it->cur_;
1424}
1425
1426template<typename TYPE, typename Compare, typename ALLOC>
1428 : skipListType::Iterator(cur) {}
1429
1430template<typename TYPE, typename Compare, typename ALLOC>
1433
1434template<typename TYPE, typename Compare, typename ALLOC>
1437 skipListType::Iterator::operator=(other);
1438 return *this;
1439}
1440
1441template<typename TYPE, typename Compare, typename ALLOC>
1446
1447template<typename TYPE, typename Compare, typename ALLOC>
1448std::string
1450 return "JSet::Iterator";
1451}
1452
1453template<typename TYPE, typename Compare, typename ALLOC>
1455 skipListType::Iterator::operator+=(steps);
1456}
1457
1458template<typename TYPE, typename Compare, typename ALLOC>
1462
1463template<typename TYPE, typename Compare, typename ALLOC>
1466 const iterator<const TYPE>& other) const {
1467 auto other_it = dynamic_cast<const Iterator*>(&other);
1468 if (other_it == nullptr)
1469 return this > &other ?
1470 std::numeric_limits<integer>::max() :
1471 std::numeric_limits<integer>::min();
1472 return skipListType::Iterator::operator-(*other_it);
1473}
1474
1475template<typename TYPE, typename Compare, typename ALLOC>
1477 return skipListType::Iterator::hasNext();
1478}
1479
1480template<typename TYPE, typename Compare, typename ALLOC>
1484
1485template<typename TYPE, typename Compare, typename ALLOC>
1487 const iterator<const TYPE>* other) const {
1488 const auto other_it = dynamic_cast<const Iterator*>(other);
1489 if (!other_it){
1490 return false;
1491 }
1492 auto cloned_it = ownerPtr(other_it->clone());
1493 return this->equalPtr(cloned_it.get());
1494}
1495
1496template<typename TYPE, typename Compare, typename ALLOC>
1498 const original::iterator<const TYPE>* other) const {
1499 return other->atPrev(*this);
1500}
1501
1502template<typename TYPE, typename Compare, typename ALLOC>
1504 skipListType::Iterator::next();
1505}
1506
1507template<typename TYPE, typename Compare, typename ALLOC>
1511
1512template<typename TYPE, typename Compare, typename ALLOC>
1517
1518template<typename TYPE, typename Compare, typename ALLOC>
1520 return skipListType::Iterator::get().template get<0>();
1521}
1522
1523template<typename TYPE, typename Compare, typename ALLOC>
1525 return skipListType::Iterator::get().template get<0>();
1526}
1527
1528template<typename TYPE, typename Compare, typename ALLOC>
1532
1533template<typename TYPE, typename Compare, typename ALLOC>
1535 return skipListType::Iterator::isValid();
1536}
1537
1538template<typename TYPE, typename Compare, typename ALLOC>
1540 : skipListType(std::move(comp)),
1541 set<TYPE, ALLOC>(std::move(alloc)) {}
1542
1543template<typename TYPE, typename Compare, typename ALLOC>
1545 this->operator=(other);
1546}
1547
1548template<typename TYPE, typename Compare, typename ALLOC>
1551 if (this == &other){
1552 return *this;
1553 }
1554
1555 this->listDestroy();
1556 this->head_ = other.listCopy();
1557 this->size_ = other.size_;
1558 this->compare_ = other.compare_;
1559 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1560 this->allocator = other.allocator;
1561 this->rebind_alloc = other.rebind_alloc;
1562 }
1563 return *this;
1564}
1565
1566template<typename TYPE, typename Compare, typename ALLOC>
1568 this->operator=(std::move(other));
1569}
1570
1571template<typename TYPE, typename Compare, typename ALLOC>
1574 if (this == &other){
1575 return *this;
1576 }
1577
1578 this->listDestroy();
1579 this->head_ = other.head_;
1580 other.head_ = other.createNode();
1581 this->size_ = other.size_;
1582 other.size_ = 0;
1583 this->compare_ = std::move(other.compare_);
1584 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1585 this->allocator = std::move(other.allocator);
1586 this->rebind_alloc = std::move(other.rebind_alloc);
1587 }
1588 return *this;
1589}
1590
1591template<typename TYPE, typename Compare, typename ALLOC>
1595
1596template<typename TYPE, typename Compare, typename ALLOC>
1598 return this->find(e);
1599}
1600
1601template<typename TYPE, typename Compare, typename ALLOC>
1603 return this->insert(e, true);
1604}
1605
1606template<typename TYPE, typename Compare, typename ALLOC>
1608 return this->erase(e);
1609}
1610
1611template<typename TYPE, typename Compare, typename ALLOC>
1614 return new Iterator(this->head_->getPNext(1));
1615}
1616
1617template<typename TYPE, typename Compare, typename ALLOC>
1620 return new Iterator(this->findLastNode());
1621}
1622
1623template<typename TYPE, typename Compare, typename ALLOC>
1625 return "JSet";
1626}
1627
1628template<typename TYPE, typename Compare, typename ALLOC>
1630 std::stringstream ss;
1631 ss << this->className();
1632 ss << "(";
1633 bool first = true;
1634 for (auto it = this->begin(); it != this->end(); it.next()){
1635 if (!first){
1636 ss << ", ";
1637 }
1638 ss << printable::formatString(it.get());
1639 first = false;
1640 }
1641 ss << ")";
1642 if (enter)
1643 ss << "\n";
1644 return ss.str();
1645}
1646
1647template<typename TYPE, typename Compare, typename ALLOC>
1649
1650#endif //SETS_H
Red-Black Tree implementation header.
Memory allocation interface and implementations.
Forward iterator for JSet.
Definition sets.h:679
void prev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1508
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition sets.h:1476
bool atNext(const iterator< const TYPE > *other) const override
Checks if other is next to this.
Definition sets.h:1497
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition sets.h:1436
Iterator(skipListNode *cur)
Constructs iterator pointing to specific skip list node.
Definition sets.h:1427
Iterator * getPrev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1514
void next() const override
Moves to next element.
Definition sets.h:1503
void operator-=(integer steps) const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1459
std::string className() const override
Gets iterator class name.
Definition sets.h:1449
void operator+=(integer steps) const override
Advances iterator by steps.
Definition sets.h:1454
bool hasPrev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1481
const TYPE & get() override
Gets current element (non-const)
Definition sets.h:1519
integer operator-(const iterator< const TYPE > &other) const override
Calculates distance between iterators.
Definition sets.h:1465
bool atPrev(const iterator< const TYPE > *other) const override
Checks if other is previous to this.
Definition sets.h:1486
void set(const TYPE &data) override
Not supported (throws unSupportedMethodError)
Definition sets.h:1529
Iterator * clone() const override
Creates a copy of this iterator.
Definition sets.h:1443
bool isValid() const override
Checks if iterator is valid.
Definition sets.h:1534
Skip List based implementation of the set interface.
Definition sets.h:655
JSet(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty JSet.
Definition sets.h:1539
std::string className() const override
Gets class name.
Definition sets.h:1624
~JSet() override
Destructor.
JSet & operator=(const JSet &other)
Copy assignment operator.
Definition sets.h:1550
Iterator * begins() const override
Gets begin iterator.
Definition sets.h:1613
std::string toString(bool enter) const override
Converts to string representation.
Definition sets.h:1629
bool add(const TYPE &e) override
Adds new element.
Definition sets.h:1602
bool remove(const TYPE &e) override
Removes element.
Definition sets.h:1607
u_integer size() const override
Gets number of elements.
Definition sets.h:1592
bool contains(const TYPE &e) const override
Checks if element exists.
Definition sets.h:1597
Iterator * ends() const override
Gets end iterator.
Definition sets.h:1619
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
Forward declaration of hash class template.
Definition hash.h:80
Forward iterator for hashSet.
Definition sets.h:108
bool atNext(const iterator< const TYPE > *other) const override
Checks if other is next to this.
Definition sets.h:1001
bool hasNext() const override
Checks if more elements exist.
Definition sets.h:976
void operator-=(integer steps) const override
Not supported (throws unSupportedMethodError)
Definition sets.h:965
void next() const override
Moves to next element.
Definition sets.h:1006
bool atPrev(const iterator< const TYPE > *other) const override
Checks if other is previous to this.
Definition sets.h:986
Iterator * clone() const override
Creates a copy of this iterator.
Definition sets.h:950
bool hasPrev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:981
void set(const TYPE &data) override
Not supported (throws unSupportedMethodError)
Definition sets.h:1032
Iterator * getPrev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1017
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition sets.h:939
bool isValid() const override
Checks if iterator is valid.
Definition sets.h:1037
const TYPE & get() override
Gets current element (non-const)
Definition sets.h:1022
integer operator-(const iterator< const TYPE > &other) const override
Not supported (throws unSupportedMethodError)
Definition sets.h:971
std::string className() const override
Gets iterator class name.
Definition sets.h:955
void prev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1011
void operator+=(integer steps) const override
Advances iterator by steps.
Definition sets.h:960
Hash table based implementation of the set interface.
Definition sets.h:79
std::string className() const override
Gets class name.
Definition sets.h:1135
u_integer size() const override
Gets number of elements.
Definition sets.h:1092
hashSet(HASH hash=HASH{}, ALLOC alloc=ALLOC{})
Constructs empty hashSet.
Definition sets.h:1042
bool add(const TYPE &e) override
Adds new element.
Definition sets.h:1102
std::string toString(bool enter) const override
Converts to string representation.
Definition sets.h:1140
bool remove(const TYPE &e) override
Removes element.
Definition sets.h:1107
bool contains(const TYPE &e) const override
Checks if element exists.
Definition sets.h:1097
hashSet & operator=(const hashSet &other)
Copy assignment operator.
Definition sets.h:1053
Iterator * ends() const override
Gets end iterator.
Definition sets.h:1124
Iterator * begins() const override
Gets begin iterator.
Definition sets.h:1113
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
virtual bool atNext(const iterator *other) const =0
Checks if this iterator is positioned at the next element.
virtual bool atPrev(const iterator *other) const =0
Checks if this iterator is positioned at the previous element.
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.
Abstract base class for unique element containers.
Definition set.h:44
Skip List container implementation.
Definition skipList.h:45
Bidirectional iterator for treeSet.
Definition sets.h:391
bool isValid() const override
Checks if iterator is valid.
Definition sets.h:1300
void next() const override
Moves to next element.
Definition sets.h:1261
bool atPrev(const iterator< const TYPE > *other) const override
Checks if other is previous to this.
Definition sets.h:1239
void operator-=(integer steps) const override
Rewinds iterator by steps.
Definition sets.h:1214
const TYPE & get() override
Gets current element (non-const)
Definition sets.h:1282
void set(const TYPE &data) override
Not supported.
Definition sets.h:1294
std::string className() const override
Gets iterator class name.
Definition sets.h:1202
bool atNext(const iterator< const TYPE > *other) const override
Checks if other is next to this.
Definition sets.h:1255
void operator+=(integer steps) const override
Advances iterator by steps.
Definition sets.h:1208
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition sets.h:1183
void prev() const override
Moves to previous element.
Definition sets.h:1267
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition sets.h:1227
integer operator-(const iterator< const TYPE > &other) const override
Not supported (throws unSupportedMethodError)
Definition sets.h:1221
bool hasPrev() const override
Checks if more elements exist in backward direction.
Definition sets.h:1233
Iterator * getPrev() const override
Gets previous iterator.
Definition sets.h:1274
Iterator * clone() const override
Creates a copy of this iterator.
Definition sets.h:1196
Red-Black Tree based implementation of the set interface.
Definition sets.h:367
bool contains(const TYPE &e) const override
Checks if element exists.
Definition sets.h:1364
bool remove(const TYPE &e) override
Removes element.
Definition sets.h:1374
treeSet(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty treeSet.
Definition sets.h:1306
treeSet & operator=(const treeSet &other)
Copy assignment operator.
Definition sets.h:1317
Iterator * ends() const override
Gets end iterator.
Definition sets.h:1387
~treeSet() override
Destructor.
std::string className() const override
Gets class name.
Definition sets.h:1393
Iterator * begins() const override
Gets begin iterator.
Definition sets.h:1380
bool add(const TYPE &e) override
Adds new element.
Definition sets.h:1369
u_integer size() const override
Gets number of elements.
Definition sets.h:1359
std::string toString(bool enter) const override
Converts to string representation.
Definition sets.h:1398
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.
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.
Abstract base class for set container implementations.
Skip List container implementation.