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
13
31namespace original {
55 template <typename TYPE,
56 typename HASH = hash<TYPE>,
57 typename ALLOC = allocator<couple<const TYPE, const bool>>>
58 class hashSet final : public hashTable<TYPE, const bool, ALLOC, HASH>,
59 public set<TYPE, ALLOC>,
60 public iterable<const TYPE>,
61 public printable{
62
68
73 using rebind_alloc_pointer = typename hashTable<TYPE, const bool, ALLOC, HASH>::rebind_alloc_pointer;
74
75 public:
89 class Iterator final : public hashTable<TYPE, const bool, ALLOC, HASH>::Iterator,
90 public baseIterator<const TYPE> {
91
99 explicit Iterator(vector<hashNode*, rebind_alloc_pointer>* buckets = nullptr,
100 u_integer bucket = 0, hashNode* node = nullptr);
101
108 bool equalPtr(const iterator<const TYPE>* other) const override;
109
110 public:
111 friend class hashSet;
112
117 Iterator(const Iterator& other);
118
124 Iterator& operator=(const Iterator& other);
125
130 Iterator* clone() const override;
131
136 [[nodiscard]] std::string className() const override;
137
143 void operator+=(integer steps) const override;
144
148 void operator-=(integer steps) const override;
149
153 integer operator-(const iterator<const TYPE>& other) const override;
154
159 [[nodiscard]] bool hasNext() const override;
160
164 [[nodiscard]] bool hasPrev() const override;
165
171 bool atPrev(const iterator<const TYPE>* other) const override;
172
178 bool atNext(const iterator<const TYPE>* other) const override;
179
183 void next() const override;
184
188 void prev() const override;
189
193 Iterator* getPrev() const override;
194
199 const TYPE& get() override;
200
205 const TYPE get() const override;
206
210 void set(const TYPE& data) override;
211
216 [[nodiscard]] bool isValid() const override;
217
218 ~Iterator() override = default;
219 };
220
226 explicit hashSet(HASH hash = HASH{}, ALLOC alloc = ALLOC{});
227
234 hashSet(const hashSet& other);
235
243 hashSet& operator=(const hashSet& other);
244
251 hashSet(hashSet&& other) noexcept;
252
261 hashSet& operator=(hashSet&& other) noexcept;
262
267 [[nodiscard]] u_integer size() const override;
268
274 bool contains(const TYPE &e) const override;
275
281 bool add(const TYPE &e) override;
282
288 bool remove(const TYPE &e) override;
289
294 Iterator* begins() const override;
295
300 Iterator* ends() const override;
301
306 [[nodiscard]] std::string className() const override;
307
313 [[nodiscard]] std::string toString(bool enter) const override;
314
315 ~hashSet() override;
316 };
317
343 template <typename TYPE,
344 typename Compare = increaseComparator<TYPE>,
345 typename ALLOC = allocator<couple<const TYPE, const bool>>>
346 class treeSet final : public RBTree<TYPE, const bool, ALLOC, Compare>,
347 public set<TYPE, ALLOC>,
348 public iterable<const TYPE>,
349 public printable {
351
356 using RBNode = typename RBTreeType::RBNode;
357 public:
371 class Iterator final : public RBTreeType::Iterator,
372 public baseIterator<const TYPE>
373 {
380 explicit Iterator(RBTreeType* tree, RBNode* cur);
381
388 bool equalPtr(const iterator<const TYPE>* other) const override;
389 public:
390 friend class treeSet;
391
396 Iterator(const Iterator& other);
397
403 Iterator& operator=(const Iterator& other);
404
409 Iterator* clone() const override;
410
415 [[nodiscard]] std::string className() const override;
416
421 void operator+=(integer steps) const override;
422
427 void operator-=(integer steps) const override;
428
432 integer operator-(const iterator<const TYPE> &other) const override;
433
438 [[nodiscard]] bool hasNext() const override;
439
444 [[nodiscard]] bool hasPrev() const override;
445
451 bool atPrev(const iterator<const TYPE>* other) const override;
452
458 bool atNext(const iterator<const TYPE>* other) const override;
459
463 void next() const override;
464
468 void prev() const override;
469
474 Iterator* getPrev() const override;
475
480 const TYPE& get() override;
481
486 const TYPE get() const override;
487
492 void set(const TYPE& data) override;
493
498 [[nodiscard]] bool isValid() const override;
499
500 ~Iterator() override = default;
501 };
502
503 friend class Iterator;
504
510 explicit treeSet(Compare comp = Compare{}, ALLOC alloc = ALLOC{});
511
518 treeSet(const treeSet& other);
519
527 treeSet& operator=(const treeSet& other);
528
535 treeSet(treeSet&& other) noexcept;
536
545 treeSet& operator=(treeSet&& other) noexcept;
546
551 [[nodiscard]] u_integer size() const override;
552
558 bool contains(const TYPE &e) const override;
559
565 bool add(const TYPE &e) override;
566
572 bool remove(const TYPE &e) override;
573
578 Iterator* begins() const override;
579
584 Iterator* ends() const override;
585
590 [[nodiscard]] std::string className() const override;
591
597 [[nodiscard]] std::string toString(bool enter) const override;
598
603 ~treeSet() override;
604 };
605}
606
607template<typename TYPE, typename HASH, typename ALLOC>
608original::hashSet<TYPE, HASH, ALLOC>::Iterator::Iterator(vector<hashNode*, rebind_alloc_pointer> *buckets,
609u_integer bucket, hashNode *node) : hashTable<TYPE, const bool, ALLOC, HASH>::Iterator(
610 const_cast<vector<hashNode*, rebind_alloc_pointer>*>(buckets), bucket, node) {}
611
612template<typename TYPE, typename HASH, typename ALLOC>
613bool original::hashSet<TYPE, HASH, ALLOC>::Iterator::equalPtr(const iterator<const TYPE> *other) const {
614 auto other_it = dynamic_cast<const Iterator*>(other);
615 return other_it &&
616 this->p_buckets == other_it->p_buckets &&
617 this->cur_bucket == other_it->cur_bucket &&
618 this->p_node == other_it->p_node;
619}
620
621template<typename TYPE, typename HASH, typename ALLOC>
625
626template<typename TYPE, typename HASH, typename ALLOC>
629 if (this == &other) {
630 return *this;
631 }
632
634 return *this;
635}
636
637template<typename TYPE, typename HASH, typename ALLOC>
642
643template<typename TYPE, typename HASH, typename ALLOC>
645 return "hashSet::Iterator";
646}
647
648template<typename TYPE, typename HASH, typename ALLOC>
652
653template<typename TYPE, typename HASH, typename ALLOC>
657
658template<typename TYPE, typename HASH, typename ALLOC>
663
664template<typename TYPE, typename HASH, typename ALLOC>
668
669template<typename TYPE, typename HASH, typename ALLOC>
673
674template<typename TYPE, typename HASH, typename ALLOC>
676 auto other_it = dynamic_cast<const Iterator*>(other);
677 if (!other_it) {
678 return false;
679 }
680 auto next = ownerPtr(this->clone());
681 if (!next->isValid()){
682 return false;
683 }
684
685 next->next();
686 return next->equalPtr(other_it);
687}
688
689template<typename TYPE, typename HASH, typename ALLOC>
691 return other->atPrev(*this);
692}
693
694template<typename TYPE, typename HASH, typename ALLOC>
698
699template<typename TYPE, typename HASH, typename ALLOC>
703
704template<typename TYPE, typename HASH, typename ALLOC>
709
710template<typename TYPE, typename HASH, typename ALLOC>
714
715template<typename TYPE, typename HASH, typename ALLOC>
719
720template<typename TYPE, typename HASH, typename ALLOC>
724
725template<typename TYPE, typename HASH, typename ALLOC>
729
730template<typename TYPE, typename HASH, typename ALLOC>
732 : hashTable<TYPE, const bool, ALLOC, HASH>(std::move(hash)),
733 set<TYPE, ALLOC>(std::move(alloc)) {}
734
735template<typename TYPE, typename HASH, typename ALLOC>
739
740template<typename TYPE, typename HASH, typename ALLOC>
743 if (this == &other) {
744 return *this;
745 }
746
747 this->buckets = this->bucketsCopy(other.buckets);
748 this->size_ = other.size_;
749 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
750 this->allocator = other.allocator;
751 this->rebind_alloc = other.rebind_alloc;
752 }
753 return *this;
754}
755
756template<typename TYPE, typename HASH, typename ALLOC>
758 this->operator=(std::move(other));
759}
760
761template<typename TYPE, typename HASH, typename ALLOC>
764 if (this == &other) {
765 return *this;
766 }
767
768 this->buckets = std::move(other.buckets);
769 this->size_ = other.size_;
770 other.size_ = 0;
771 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
772 this->allocator = std::move(other.allocator);
773 this->rebind_alloc = std::move(other.rebind_alloc);
774 }
775 return *this;
776}
777
778template<typename TYPE, typename HASH, typename ALLOC>
782
783template<typename TYPE, typename HASH, typename ALLOC>
785 return this->find(e);
786}
787
788template<typename TYPE, typename HASH, typename ALLOC>
790 return this->insert(e, true);
791}
792
793template<typename TYPE, typename HASH, typename ALLOC>
795 return this->erase(e);
796}
797
798template<typename TYPE, typename HASH, typename ALLOC>
801 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
802 if (this->buckets[0]) {
803 return new Iterator(p_buckets, 0, this->buckets[0]);
804 }
805 auto bucket = Iterator::findNextValidBucket(p_buckets, 0);
806 return new Iterator(p_buckets, bucket, p_buckets->get(bucket));
807}
808
809template<typename TYPE, typename HASH, typename ALLOC>
812 auto p_buckets = const_cast<vector<hashNode*, rebind_alloc_pointer>*>(&this->buckets);
813 auto bucket = Iterator::findPrevValidBucket(p_buckets, this->buckets.size());
814 auto node = this->buckets[bucket];
815 while (node && node->getPNext()) {
816 node = node->getPNext();
817 }
818 return new Iterator(p_buckets, bucket, node);
819}
820
821template<typename TYPE, typename HASH, typename ALLOC>
823 return "hashSet";
824}
825
826template<typename TYPE, typename HASH, typename ALLOC>
827std::string original::hashSet<TYPE, HASH, ALLOC>::toString(const bool enter) const {
828 std::stringstream ss;
829 ss << this->className();
830 ss << "(";
831 bool first = true;
832 for (auto it = this->begin(); it != this->end(); it.next()){
833 if (!first){
834 ss << ", ";
835 }
836 ss << printable::formatString(it.get());
837 first = false;
838 }
839 ss << ")";
840 if (enter)
841 ss << "\n";
842 return ss.str();
843}
844
845template<typename TYPE, typename HASH, typename ALLOC>
847
848template <typename TYPE, typename Compare, typename ALLOC>
850 : RBTreeType::Iterator(tree, cur) {}
851
852template <typename TYPE, typename Compare, typename ALLOC>
853bool original::treeSet<TYPE, Compare, ALLOC>::Iterator::equalPtr(const iterator<const TYPE>* other) const
854{
855 auto other_it = dynamic_cast<const Iterator*>(other);
856 return other_it &&
857 this->tree_ == other_it->tree_ &&
858 this->cur_ == other_it->cur_;
859}
860
861template <typename TYPE, typename Compare, typename ALLOC>
863 : Iterator(nullptr, nullptr)
864{
865 this->operator=(other);
866}
867
868template <typename TYPE, typename Compare, typename ALLOC>
871{
872 if (this == &other) {
873 return *this;
874 }
875
876 this->tree_ = other.tree_;
877 this->cur_ = other.cur_;
878 return *this;
879}
880
881template <typename TYPE, typename Compare, typename ALLOC>
887
888template <typename TYPE, typename Compare, typename ALLOC>
890{
891 return "treeSet::Iterator";
892}
893
894template <typename TYPE, typename Compare, typename ALLOC>
896{
897 RBTreeType::Iterator::operator+=(steps);
898}
899
900template <typename TYPE, typename Compare, typename ALLOC>
902{
903 RBTreeType::Iterator::operator-=(steps);
904}
905
906template <typename TYPE, typename Compare, typename ALLOC>
912
913template <typename TYPE, typename Compare, typename ALLOC>
915{
916 return RBTreeType::Iterator::hasNext();
917}
918
919template <typename TYPE, typename Compare, typename ALLOC>
921{
922 return RBTreeType::Iterator::hasPrev();
923}
924
925template <typename TYPE, typename Compare, typename ALLOC>
927{
928 auto other_it = dynamic_cast<const Iterator*>(other);
929 if (!other_it) {
930 return false;
931 }
932 auto next = ownerPtr(this->clone());
933 if (!next->isValid()){
934 return false;
935 }
936
937 next->next();
938 return next->equalPtr(other_it);
939}
940
941template <typename TYPE, typename Compare, typename ALLOC>
943{
944 return other->atNext(*this);
945}
946
947template <typename TYPE, typename Compare, typename ALLOC>
949{
950 RBTreeType::Iterator::next();
951}
952
953template <typename TYPE, typename Compare, typename ALLOC>
955{
956 RBTreeType::Iterator::prev();
957}
958
959template <typename TYPE, typename Compare, typename ALLOC>
962{
963 auto it = this->clone();
964 it->prev();
965 return it;
966}
967
968template <typename TYPE, typename Compare, typename ALLOC>
970{
971 return RBTreeType::Iterator::get().template get<0>();
972}
973
974template <typename TYPE, typename Compare, typename ALLOC>
976{
977 return RBTreeType::Iterator::get().template get<0>();
978}
979
980template <typename TYPE, typename Compare, typename ALLOC>
985
986template <typename TYPE, typename Compare, typename ALLOC>
988{
989 return RBTreeType::Iterator::isValid();
990}
991
992template<typename TYPE, typename Compare, typename ALLOC>
994 : RBTreeType(std::move(comp)),
995 set<TYPE, ALLOC>(std::move(alloc)) {}
996
997template<typename TYPE, typename Compare, typename ALLOC>
1001
1002template<typename TYPE, typename Compare, typename ALLOC>
1005 if (this == &other){
1006 return *this;
1007 }
1008
1009 this->destroyTree();
1010 this->root_ = other.treeCopy();
1011 this->size_ = other.size_;
1012 if constexpr(ALLOC::propagate_on_container_copy_assignment::value) {
1013 this->allocator = other.allocator;
1014 this->rebind_alloc = other.rebind_alloc;
1015 }
1016 return *this;
1017}
1018
1019template<typename TYPE, typename Compare, typename ALLOC>
1021 this->operator=(std::move(other));
1022}
1023
1024template<typename TYPE, typename Compare, typename ALLOC>
1027 if (this == &other){
1028 return *this;
1029 }
1030
1031 this->root_ = other.root_;
1032 other.root_ = nullptr;
1033 this->size_ = other.size_;
1034 other.size_ = 0;
1035 if constexpr(ALLOC::propagate_on_container_move_assignment::value) {
1036 this->allocator = std::move(other.allocator);
1037 this->rebind_alloc = std::move(other.rebind_alloc);
1038 }
1039 return *this;
1040}
1041
1042template<typename TYPE, typename Compare, typename ALLOC>
1046
1047template<typename TYPE, typename Compare, typename ALLOC>
1049 return this->find(e);
1050}
1051
1052template<typename TYPE, typename Compare, typename ALLOC>
1054 return this->insert(e, true);
1055}
1056
1057template<typename TYPE, typename Compare, typename ALLOC>
1059 return this->erase(e);
1060}
1061
1062template <typename TYPE, typename Compare, typename ALLOC>
1065{
1066 return new Iterator(const_cast<treeSet*>(this), this->getMinNode());
1067}
1068
1069template <typename TYPE, typename Compare, typename ALLOC>
1072{
1073 return new Iterator(const_cast<treeSet*>(this), this->getMaxNode());
1074}
1075
1076template<typename TYPE, typename Compare, typename ALLOC>
1078 return "treeSet";
1079}
1080
1081template<typename TYPE, typename Compare, typename ALLOC>
1082std::string original::treeSet<TYPE, Compare, ALLOC>::toString(const bool enter) const {
1083 std::stringstream ss;
1084 ss << this->className();
1085 ss << "(";
1086 bool first = true;
1087 for (auto it = this->begin(); it != this->end(); it.next()){
1088 if (!first){
1089 ss << ", ";
1090 }
1091 ss << printable::formatString(it.get());
1092 first = false;
1093 }
1094 ss << ")";
1095 if (enter)
1096 ss << "\n";
1097 return ss.str();
1098}
1099
1100template<typename TYPE, typename Compare, typename ALLOC>
1102
1103#endif //SETS_H
Red-Black Tree implementation header.
Memory allocation interface and implementations.
Bidirectional iterator for RBTree.
Definition RBTree.h:219
RBNode * cur_
Current node.
Definition RBTree.h:222
RBTree * tree_
Owning tree.
Definition RBTree.h:221
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:154
A base class for basic iterators.
Definition iterator.h:296
Forward iterator for hashSet.
Definition sets.h:90
bool atNext(const iterator< const TYPE > *other) const override
Checks if other is next to this.
Definition sets.h:690
bool hasNext() const override
Checks if more elements exist.
Definition sets.h:665
void operator-=(integer steps) const override
Not supported (throws unSupportedMethodError)
Definition sets.h:654
void next() const override
Moves to next element.
Definition sets.h:695
bool atPrev(const iterator< const TYPE > *other) const override
Checks if other is previous to this.
Definition sets.h:675
Iterator * clone() const override
Creates a copy of this iterator.
Definition sets.h:639
bool hasPrev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:670
void set(const TYPE &data) override
Not supported (throws unSupportedMethodError)
Definition sets.h:721
Iterator * getPrev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:706
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition sets.h:628
bool isValid() const override
Checks if iterator is valid.
Definition sets.h:726
const TYPE & get() override
Gets current element (non-const)
Definition sets.h:711
std::string className() const override
Gets iterator class name.
Definition sets.h:644
void prev() const override
Not supported (throws unSupportedMethodError)
Definition sets.h:700
void operator+=(integer steps) const override
Advances iterator by steps.
Definition sets.h:649
Hash table based implementation of the set interface.
Definition sets.h:61
std::string className() const override
Gets class name.
Definition sets.h:822
u_integer size() const override
Gets number of elements.
Definition sets.h:779
hashSet(HASH hash=HASH{}, ALLOC alloc=ALLOC{})
Constructs empty hashSet.
Definition sets.h:731
bool add(const TYPE &e) override
Adds new element.
Definition sets.h:789
std::string toString(bool enter) const override
Converts to string representation.
Definition sets.h:827
bool remove(const TYPE &e) override
Removes element.
Definition sets.h:794
bool contains(const TYPE &e) const override
Checks if element exists.
Definition sets.h:784
hashSet & operator=(const hashSet &other)
Copy assignment operator.
Definition sets.h:742
Iterator * ends() const override
Gets end iterator.
Definition sets.h:811
Iterator * begins() const override
Gets begin iterator.
Definition sets.h:800
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
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
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
Forward declaration of hash class template.
Definition hash.h:80
A base class for iterable containers that support multiple iteration patterns.
Definition iterable.h:59
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.
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.
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
Bidirectional iterator for treeSet.
Definition sets.h:373
bool isValid() const override
Checks if iterator is valid.
Definition sets.h:987
void next() const override
Moves to next element.
Definition sets.h:948
bool atPrev(const iterator< const TYPE > *other) const override
Checks if other is previous to this.
Definition sets.h:926
void operator-=(integer steps) const override
Rewinds iterator by steps.
Definition sets.h:901
const TYPE & get() override
Gets current element (non-const)
Definition sets.h:969
void set(const TYPE &data) override
Not supported.
Definition sets.h:981
std::string className() const override
Gets iterator class name.
Definition sets.h:889
bool atNext(const iterator< const TYPE > *other) const override
Checks if other is next to this.
Definition sets.h:942
void operator+=(integer steps) const override
Advances iterator by steps.
Definition sets.h:895
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition sets.h:870
void prev() const override
Moves to previous element.
Definition sets.h:954
bool hasNext() const override
Checks if more elements exist in forward direction.
Definition sets.h:914
bool hasPrev() const override
Checks if more elements exist in backward direction.
Definition sets.h:920
Iterator * getPrev() const override
Gets previous iterator.
Definition sets.h:961
Iterator * clone() const override
Creates a copy of this iterator.
Definition sets.h:883
Red-Black Tree based implementation of the set interface.
Definition sets.h:349
bool contains(const TYPE &e) const override
Checks if element exists.
Definition sets.h:1048
bool remove(const TYPE &e) override
Removes element.
Definition sets.h:1058
treeSet(Compare comp=Compare{}, ALLOC alloc=ALLOC{})
Constructs empty treeSet.
Definition sets.h:993
treeSet & operator=(const treeSet &other)
Copy assignment operator.
Definition sets.h:1004
Iterator * ends() const override
Gets end iterator.
Definition sets.h:1071
~treeSet() override
Destructor.
std::string className() const override
Gets class name.
Definition sets.h:1077
Iterator * begins() const override
Gets begin iterator.
Definition sets.h:1064
bool add(const TYPE &e) override
Adds new element.
Definition sets.h:1053
u_integer size() const override
Gets number of elements.
Definition sets.h:1043
std::string toString(bool enter) const override
Converts to string representation.
Definition sets.h:1082
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.
Implementation of hashTable container.
Provides a generic hashing utility and a base interface for hashable types.
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.