1 //===--- ImmutableSet.h - Immutable (functional) set interface --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the ImutAVLTree and ImmutableSet classes.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ADT_IMMUTABLESET_H
14 #define LLVM_ADT_IMMUTABLESET_H
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/IntrusiveRefCntPtr.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Support/ErrorHandling.h"
32 //===----------------------------------------------------------------------===//
33 // Immutable AVL-Tree Definition.
34 //===----------------------------------------------------------------------===//
36 template <typename ImutInfo> class ImutAVLFactory;
37 template <typename ImutInfo> class ImutIntervalAVLFactory;
38 template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
39 template <typename ImutInfo> class ImutAVLTreeGenericIterator;
41 template <typename ImutInfo >
44 using key_type_ref = typename ImutInfo::key_type_ref;
45 using value_type = typename ImutInfo::value_type;
46 using value_type_ref = typename ImutInfo::value_type_ref;
47 using Factory = ImutAVLFactory<ImutInfo>;
48 using iterator = ImutAVLTreeInOrderIterator<ImutInfo>;
50 friend class ImutAVLFactory<ImutInfo>;
51 friend class ImutIntervalAVLFactory<ImutInfo>;
52 friend class ImutAVLTreeGenericIterator<ImutInfo>;
54 //===----------------------------------------------------===//
56 //===----------------------------------------------------===//
58 /// Return a pointer to the left subtree. This value
59 /// is NULL if there is no left subtree.
60 ImutAVLTree *getLeft() const { return left; }
62 /// Return a pointer to the right subtree. This value is
63 /// NULL if there is no right subtree.
64 ImutAVLTree *getRight() const { return right; }
66 /// getHeight - Returns the height of the tree. A tree with no subtrees
67 /// has a height of 1.
68 unsigned getHeight() const { return height; }
70 /// getValue - Returns the data value associated with the tree node.
71 const value_type& getValue() const { return value; }
73 /// find - Finds the subtree associated with the specified key value.
74 /// This method returns NULL if no matching subtree is found.
75 ImutAVLTree* find(key_type_ref K) {
76 ImutAVLTree *T = this;
78 key_type_ref CurrentKey = ImutInfo::KeyOfValue(T->getValue());
79 if (ImutInfo::isEqual(K,CurrentKey))
81 else if (ImutInfo::isLess(K,CurrentKey))
89 /// getMaxElement - Find the subtree associated with the highest ranged
91 ImutAVLTree* getMaxElement() {
92 ImutAVLTree *T = this;
93 ImutAVLTree *Right = T->getRight();
94 while (Right) { T = Right; Right = T->getRight(); }
98 /// size - Returns the number of nodes in the tree, which includes
99 /// both leaves and non-leaf nodes.
100 unsigned size() const {
102 if (const ImutAVLTree* L = getLeft())
104 if (const ImutAVLTree* R = getRight())
109 /// begin - Returns an iterator that iterates over the nodes of the tree
110 /// in an inorder traversal. The returned iterator thus refers to the
111 /// the tree node with the minimum data element.
112 iterator begin() const { return iterator(this); }
114 /// end - Returns an iterator for the tree that denotes the end of an
115 /// inorder traversal.
116 iterator end() const { return iterator(); }
118 bool isElementEqual(value_type_ref V) const {
120 if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(getValue()),
121 ImutInfo::KeyOfValue(V)))
124 // Also compare the data values.
125 if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(getValue()),
126 ImutInfo::DataOfValue(V)))
132 bool isElementEqual(const ImutAVLTree* RHS) const {
133 return isElementEqual(RHS->getValue());
136 /// isEqual - Compares two trees for structural equality and returns true
137 /// if they are equal. This worst case performance of this operation is
138 // linear in the sizes of the trees.
139 bool isEqual(const ImutAVLTree& RHS) const {
143 iterator LItr = begin(), LEnd = end();
144 iterator RItr = RHS.begin(), REnd = RHS.end();
146 while (LItr != LEnd && RItr != REnd) {
147 if (&*LItr == &*RItr) {
153 if (!LItr->isElementEqual(&*RItr))
160 return LItr == LEnd && RItr == REnd;
163 /// isNotEqual - Compares two trees for structural inequality. Performance
164 /// is the same is isEqual.
165 bool isNotEqual(const ImutAVLTree& RHS) const { return !isEqual(RHS); }
167 /// contains - Returns true if this tree contains a subtree (node) that
168 /// has an data element that matches the specified key. Complexity
169 /// is logarithmic in the size of the tree.
170 bool contains(key_type_ref K) { return (bool) find(K); }
172 /// foreach - A member template the accepts invokes operator() on a functor
173 /// object (specified by Callback) for every node/subtree in the tree.
174 /// Nodes are visited using an inorder traversal.
175 template <typename Callback>
176 void foreach(Callback& C) {
177 if (ImutAVLTree* L = getLeft())
182 if (ImutAVLTree* R = getRight())
186 /// validateTree - A utility method that checks that the balancing and
187 /// ordering invariants of the tree are satisfied. It is a recursive
188 /// method that returns the height of the tree, which is then consumed
189 /// by the enclosing validateTree call. External callers should ignore the
190 /// return value. An invalid tree will cause an assertion to fire in
192 unsigned validateTree() const {
193 unsigned HL = getLeft() ? getLeft()->validateTree() : 0;
194 unsigned HR = getRight() ? getRight()->validateTree() : 0;
198 assert(getHeight() == ( HL > HR ? HL : HR ) + 1
199 && "Height calculation wrong");
201 assert((HL > HR ? HL-HR : HR-HL) <= 2
202 && "Balancing invariant violated");
204 assert((!getLeft() ||
205 ImutInfo::isLess(ImutInfo::KeyOfValue(getLeft()->getValue()),
206 ImutInfo::KeyOfValue(getValue()))) &&
207 "Value in left child is not less that current value");
209 assert((!getRight() ||
210 ImutInfo::isLess(ImutInfo::KeyOfValue(getValue()),
211 ImutInfo::KeyOfValue(getRight()->getValue()))) &&
212 "Current value is not less that value of right child");
217 //===----------------------------------------------------===//
219 //===----------------------------------------------------===//
225 ImutAVLTree *prev = nullptr;
226 ImutAVLTree *next = nullptr;
228 unsigned height : 28;
230 bool IsDigestCached : 1;
231 bool IsCanonicalized : 1;
235 uint32_t refCount = 0;
237 //===----------------------------------------------------===//
238 // Internal methods (node manipulation; used by Factory).
239 //===----------------------------------------------------===//
242 /// ImutAVLTree - Internal constructor that is only called by
244 ImutAVLTree(Factory *f, ImutAVLTree* l, ImutAVLTree* r, value_type_ref v,
246 : factory(f), left(l), right(r), height(height), IsMutable(true),
247 IsDigestCached(false), IsCanonicalized(false), value(v)
249 if (left) left->retain();
250 if (right) right->retain();
253 /// isMutable - Returns true if the left and right subtree references
254 /// (as well as height) can be changed. If this method returns false,
255 /// the tree is truly immutable. Trees returned from an ImutAVLFactory
256 /// object should always have this method return true. Further, if this
257 /// method returns false for an instance of ImutAVLTree, all subtrees
258 /// will also have this method return false. The converse is not true.
259 bool isMutable() const { return IsMutable; }
261 /// hasCachedDigest - Returns true if the digest for this tree is cached.
262 /// This can only be true if the tree is immutable.
263 bool hasCachedDigest() const { return IsDigestCached; }
265 //===----------------------------------------------------===//
266 // Mutating operations. A tree root can be manipulated as
267 // long as its reference has not "escaped" from internal
268 // methods of a factory object (see below). When a tree
269 // pointer is externally viewable by client code, the
270 // internal "mutable bit" is cleared to mark the tree
271 // immutable. Note that a tree that still has its mutable
272 // bit set may have children (subtrees) that are themselves
274 //===----------------------------------------------------===//
276 /// markImmutable - Clears the mutable flag for a tree. After this happens,
277 /// it is an error to call setLeft(), setRight(), and setHeight().
278 void markImmutable() {
279 assert(isMutable() && "Mutable flag already removed.");
283 /// markedCachedDigest - Clears the NoCachedDigest flag for a tree.
284 void markedCachedDigest() {
285 assert(!hasCachedDigest() && "NoCachedDigest flag already removed.");
286 IsDigestCached = true;
289 /// setHeight - Changes the height of the tree. Used internally by
291 void setHeight(unsigned h) {
292 assert(isMutable() && "Only a mutable tree can have its height changed.");
296 static uint32_t computeDigest(ImutAVLTree *L, ImutAVLTree *R,
301 digest += L->computeDigest();
303 // Compute digest of stored data.
305 ImutInfo::Profile(ID,V);
306 digest += ID.ComputeHash();
309 digest += R->computeDigest();
314 uint32_t computeDigest() {
315 // Check the lowest bit to determine if digest has actually been
317 if (hasCachedDigest())
320 uint32_t X = computeDigest(getLeft(), getRight(), getValue());
322 markedCachedDigest();
326 //===----------------------------------------------------===//
327 // Reference count operations.
328 //===----------------------------------------------------===//
331 void retain() { ++refCount; }
334 assert(refCount > 0);
344 if (IsCanonicalized) {
351 factory->Cache[factory->maskCacheIndex(computeDigest())] = next;
354 // We need to clear the mutability bit in case we are
355 // destroying the node as part of a sweep in ImutAVLFactory::recoverNodes().
357 factory->freeNodes.push_back(this);
361 template <typename ImutInfo>
362 struct IntrusiveRefCntPtrInfo<ImutAVLTree<ImutInfo>> {
363 static void retain(ImutAVLTree<ImutInfo> *Tree) { Tree->retain(); }
364 static void release(ImutAVLTree<ImutInfo> *Tree) { Tree->release(); }
367 //===----------------------------------------------------------------------===//
368 // Immutable AVL-Tree Factory class.
369 //===----------------------------------------------------------------------===//
371 template <typename ImutInfo >
372 class ImutAVLFactory {
373 friend class ImutAVLTree<ImutInfo>;
375 using TreeTy = ImutAVLTree<ImutInfo>;
376 using value_type_ref = typename TreeTy::value_type_ref;
377 using key_type_ref = typename TreeTy::key_type_ref;
378 using CacheTy = DenseMap<unsigned, TreeTy*>;
382 std::vector<TreeTy*> createdNodes;
383 std::vector<TreeTy*> freeNodes;
385 bool ownsAllocator() const {
386 return (Allocator & 0x1) == 0;
389 BumpPtrAllocator& getAllocator() const {
390 return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
393 //===--------------------------------------------------===//
395 //===--------------------------------------------------===//
399 : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
401 ImutAVLFactory(BumpPtrAllocator& Alloc)
402 : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
405 if (ownsAllocator()) delete &getAllocator();
408 TreeTy* add(TreeTy* T, value_type_ref V) {
409 T = add_internal(V,T);
415 TreeTy* remove(TreeTy* T, key_type_ref V) {
416 T = remove_internal(V,T);
422 TreeTy* getEmptyTree() const { return nullptr; }
425 //===--------------------------------------------------===//
426 // A bunch of quick helper functions used for reasoning
427 // about the properties of trees and their children.
428 // These have succinct names so that the balancing code
429 // is as terse (and readable) as possible.
430 //===--------------------------------------------------===//
432 bool isEmpty(TreeTy* T) const { return !T; }
433 unsigned getHeight(TreeTy* T) const { return T ? T->getHeight() : 0; }
434 TreeTy* getLeft(TreeTy* T) const { return T->getLeft(); }
435 TreeTy* getRight(TreeTy* T) const { return T->getRight(); }
436 value_type_ref getValue(TreeTy* T) const { return T->value; }
438 // Make sure the index is not the Tombstone or Entry key of the DenseMap.
439 static unsigned maskCacheIndex(unsigned I) { return (I & ~0x02); }
441 unsigned incrementHeight(TreeTy* L, TreeTy* R) const {
442 unsigned hl = getHeight(L);
443 unsigned hr = getHeight(R);
444 return (hl > hr ? hl : hr) + 1;
447 static bool compareTreeWithSection(TreeTy* T,
448 typename TreeTy::iterator& TI,
449 typename TreeTy::iterator& TE) {
450 typename TreeTy::iterator I = T->begin(), E = T->end();
451 for ( ; I!=E ; ++I, ++TI) {
452 if (TI == TE || !I->isElementEqual(&*TI))
458 //===--------------------------------------------------===//
459 // "createNode" is used to generate new tree roots that link
460 // to other trees. The function may also simply move links
461 // in an existing root if that root is still marked mutable.
462 // This is necessary because otherwise our balancing code
463 // would leak memory as it would create nodes that are
464 // then discarded later before the finished tree is
465 // returned to the caller.
466 //===--------------------------------------------------===//
468 TreeTy* createNode(TreeTy* L, value_type_ref V, TreeTy* R) {
469 BumpPtrAllocator& A = getAllocator();
471 if (!freeNodes.empty()) {
472 T = freeNodes.back();
473 freeNodes.pop_back();
477 T = (TreeTy*) A.Allocate<TreeTy>();
479 new (T) TreeTy(this, L, R, V, incrementHeight(L,R));
480 createdNodes.push_back(T);
484 TreeTy* createNode(TreeTy* newLeft, TreeTy* oldTree, TreeTy* newRight) {
485 return createNode(newLeft, getValue(oldTree), newRight);
488 void recoverNodes() {
489 for (unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
490 TreeTy *N = createdNodes[i];
491 if (N->isMutable() && N->refCount == 0)
494 createdNodes.clear();
497 /// balanceTree - Used by add_internal and remove_internal to
498 /// balance a newly created tree.
499 TreeTy* balanceTree(TreeTy* L, value_type_ref V, TreeTy* R) {
500 unsigned hl = getHeight(L);
501 unsigned hr = getHeight(R);
504 assert(!isEmpty(L) && "Left tree cannot be empty to have a height >= 2");
506 TreeTy *LL = getLeft(L);
507 TreeTy *LR = getRight(L);
509 if (getHeight(LL) >= getHeight(LR))
510 return createNode(LL, L, createNode(LR,V,R));
512 assert(!isEmpty(LR) && "LR cannot be empty because it has a height >= 1");
514 TreeTy *LRL = getLeft(LR);
515 TreeTy *LRR = getRight(LR);
517 return createNode(createNode(LL,L,LRL), LR, createNode(LRR,V,R));
521 assert(!isEmpty(R) && "Right tree cannot be empty to have a height >= 2");
523 TreeTy *RL = getLeft(R);
524 TreeTy *RR = getRight(R);
526 if (getHeight(RR) >= getHeight(RL))
527 return createNode(createNode(L,V,RL), R, RR);
529 assert(!isEmpty(RL) && "RL cannot be empty because it has a height >= 1");
531 TreeTy *RLL = getLeft(RL);
532 TreeTy *RLR = getRight(RL);
534 return createNode(createNode(L,V,RLL), RL, createNode(RLR,R,RR));
537 return createNode(L,V,R);
540 /// add_internal - Creates a new tree that includes the specified
541 /// data and the data from the original tree. If the original tree
542 /// already contained the data item, the original tree is returned.
543 TreeTy* add_internal(value_type_ref V, TreeTy* T) {
545 return createNode(T, V, T);
546 assert(!T->isMutable());
548 key_type_ref K = ImutInfo::KeyOfValue(V);
549 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
551 if (ImutInfo::isEqual(K,KCurrent))
552 return createNode(getLeft(T), V, getRight(T));
553 else if (ImutInfo::isLess(K,KCurrent))
554 return balanceTree(add_internal(V, getLeft(T)), getValue(T), getRight(T));
556 return balanceTree(getLeft(T), getValue(T), add_internal(V, getRight(T)));
559 /// remove_internal - Creates a new tree that includes all the data
560 /// from the original tree except the specified data. If the
561 /// specified data did not exist in the original tree, the original
562 /// tree is returned.
563 TreeTy* remove_internal(key_type_ref K, TreeTy* T) {
567 assert(!T->isMutable());
569 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
571 if (ImutInfo::isEqual(K,KCurrent)) {
572 return combineTrees(getLeft(T), getRight(T));
573 } else if (ImutInfo::isLess(K,KCurrent)) {
574 return balanceTree(remove_internal(K, getLeft(T)),
575 getValue(T), getRight(T));
577 return balanceTree(getLeft(T), getValue(T),
578 remove_internal(K, getRight(T)));
582 TreeTy* combineTrees(TreeTy* L, TreeTy* R) {
588 TreeTy* newRight = removeMinBinding(R,OldNode);
589 return balanceTree(L, getValue(OldNode), newRight);
592 TreeTy* removeMinBinding(TreeTy* T, TreeTy*& Noderemoved) {
594 if (isEmpty(getLeft(T))) {
598 return balanceTree(removeMinBinding(getLeft(T), Noderemoved),
599 getValue(T), getRight(T));
602 /// markImmutable - Clears the mutable bits of a root and all of its
604 void markImmutable(TreeTy* T) {
605 if (!T || !T->isMutable())
608 markImmutable(getLeft(T));
609 markImmutable(getRight(T));
613 TreeTy *getCanonicalTree(TreeTy *TNew) {
617 if (TNew->IsCanonicalized)
620 // Search the hashtable for another tree with the same digest, and
621 // if find a collision compare those trees by their contents.
622 unsigned digest = TNew->computeDigest();
623 TreeTy *&entry = Cache[maskCacheIndex(digest)];
627 for (TreeTy *T = entry ; T != nullptr; T = T->next) {
628 // Compare the Contents('T') with Contents('TNew')
629 typename TreeTy::iterator TI = T->begin(), TE = T->end();
630 if (!compareTreeWithSection(TNew, TI, TE))
633 continue; // T has more contents than TNew.
634 // Trees did match! Return 'T'.
635 if (TNew->refCount == 0)
645 TNew->IsCanonicalized = true;
650 //===----------------------------------------------------------------------===//
651 // Immutable AVL-Tree Iterators.
652 //===----------------------------------------------------------------------===//
654 template <typename ImutInfo>
655 class ImutAVLTreeGenericIterator
656 : public std::iterator<std::bidirectional_iterator_tag,
657 ImutAVLTree<ImutInfo>> {
658 SmallVector<uintptr_t,20> stack;
661 enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
664 using TreeTy = ImutAVLTree<ImutInfo>;
666 ImutAVLTreeGenericIterator() = default;
667 ImutAVLTreeGenericIterator(const TreeTy *Root) {
668 if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
671 TreeTy &operator*() const {
672 assert(!stack.empty());
673 return *reinterpret_cast<TreeTy *>(stack.back() & ~Flags);
675 TreeTy *operator->() const { return &*this; }
677 uintptr_t getVisitState() const {
678 assert(!stack.empty());
679 return stack.back() & Flags;
682 bool atEnd() const { return stack.empty(); }
684 bool atBeginning() const {
685 return stack.size() == 1 && getVisitState() == VisitedNone;
688 void skipToParent() {
689 assert(!stack.empty());
693 switch (getVisitState()) {
695 stack.back() |= VisitedLeft;
698 stack.back() |= VisitedRight;
701 llvm_unreachable("Unreachable.");
705 bool operator==(const ImutAVLTreeGenericIterator &x) const {
706 return stack == x.stack;
709 bool operator!=(const ImutAVLTreeGenericIterator &x) const {
710 return !(*this == x);
713 ImutAVLTreeGenericIterator &operator++() {
714 assert(!stack.empty());
715 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
717 switch (getVisitState()) {
719 if (TreeTy* L = Current->getLeft())
720 stack.push_back(reinterpret_cast<uintptr_t>(L));
722 stack.back() |= VisitedLeft;
725 if (TreeTy* R = Current->getRight())
726 stack.push_back(reinterpret_cast<uintptr_t>(R));
728 stack.back() |= VisitedRight;
734 llvm_unreachable("Unreachable.");
739 ImutAVLTreeGenericIterator &operator--() {
740 assert(!stack.empty());
741 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
743 switch (getVisitState()) {
748 stack.back() &= ~Flags; // Set state to "VisitedNone."
749 if (TreeTy* L = Current->getLeft())
750 stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
753 stack.back() &= ~Flags;
754 stack.back() |= VisitedLeft;
755 if (TreeTy* R = Current->getRight())
756 stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
759 llvm_unreachable("Unreachable.");
765 template <typename ImutInfo>
766 class ImutAVLTreeInOrderIterator
767 : public std::iterator<std::bidirectional_iterator_tag,
768 ImutAVLTree<ImutInfo>> {
769 using InternalIteratorTy = ImutAVLTreeGenericIterator<ImutInfo>;
771 InternalIteratorTy InternalItr;
774 using TreeTy = ImutAVLTree<ImutInfo>;
776 ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
778 ++*this; // Advance to first element.
781 ImutAVLTreeInOrderIterator() : InternalItr() {}
783 bool operator==(const ImutAVLTreeInOrderIterator &x) const {
784 return InternalItr == x.InternalItr;
787 bool operator!=(const ImutAVLTreeInOrderIterator &x) const {
788 return !(*this == x);
791 TreeTy &operator*() const { return *InternalItr; }
792 TreeTy *operator->() const { return &*InternalItr; }
794 ImutAVLTreeInOrderIterator &operator++() {
796 while (!InternalItr.atEnd() &&
797 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
802 ImutAVLTreeInOrderIterator &operator--() {
804 while (!InternalItr.atBeginning() &&
805 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
811 InternalItr.skipToParent();
813 while (!InternalItr.atEnd() &&
814 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
819 /// Generic iterator that wraps a T::TreeTy::iterator and exposes
820 /// iterator::getValue() on dereference.
821 template <typename T>
822 struct ImutAVLValueIterator
823 : iterator_adaptor_base<
824 ImutAVLValueIterator<T>, typename T::TreeTy::iterator,
825 typename std::iterator_traits<
826 typename T::TreeTy::iterator>::iterator_category,
827 const typename T::value_type> {
828 ImutAVLValueIterator() = default;
829 explicit ImutAVLValueIterator(typename T::TreeTy *Tree)
830 : ImutAVLValueIterator::iterator_adaptor_base(Tree) {}
832 typename ImutAVLValueIterator::reference operator*() const {
833 return this->I->getValue();
837 //===----------------------------------------------------------------------===//
838 // Trait classes for Profile information.
839 //===----------------------------------------------------------------------===//
841 /// Generic profile template. The default behavior is to invoke the
842 /// profile method of an object. Specializations for primitive integers
843 /// and generic handling of pointers is done below.
844 template <typename T>
845 struct ImutProfileInfo {
846 using value_type = const T;
847 using value_type_ref = const T&;
849 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
850 FoldingSetTrait<T>::Profile(X,ID);
854 /// Profile traits for integers.
855 template <typename T>
856 struct ImutProfileInteger {
857 using value_type = const T;
858 using value_type_ref = const T&;
860 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
865 #define PROFILE_INTEGER_INFO(X)\
866 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
868 PROFILE_INTEGER_INFO(char)
869 PROFILE_INTEGER_INFO(unsigned char)
870 PROFILE_INTEGER_INFO(short)
871 PROFILE_INTEGER_INFO(unsigned short)
872 PROFILE_INTEGER_INFO(unsigned)
873 PROFILE_INTEGER_INFO(signed)
874 PROFILE_INTEGER_INFO(long)
875 PROFILE_INTEGER_INFO(unsigned long)
876 PROFILE_INTEGER_INFO(long long)
877 PROFILE_INTEGER_INFO(unsigned long long)
879 #undef PROFILE_INTEGER_INFO
881 /// Profile traits for booleans.
883 struct ImutProfileInfo<bool> {
884 using value_type = const bool;
885 using value_type_ref = const bool&;
887 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
892 /// Generic profile trait for pointer types. We treat pointers as
893 /// references to unique objects.
894 template <typename T>
895 struct ImutProfileInfo<T*> {
896 using value_type = const T*;
897 using value_type_ref = value_type;
899 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
904 //===----------------------------------------------------------------------===//
905 // Trait classes that contain element comparison operators and type
906 // definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap. These
907 // inherit from the profile traits (ImutProfileInfo) to include operations
908 // for element profiling.
909 //===----------------------------------------------------------------------===//
911 /// ImutContainerInfo - Generic definition of comparison operations for
912 /// elements of immutable containers that defaults to using
913 /// std::equal_to<> and std::less<> to perform comparison of elements.
914 template <typename T>
915 struct ImutContainerInfo : public ImutProfileInfo<T> {
916 using value_type = typename ImutProfileInfo<T>::value_type;
917 using value_type_ref = typename ImutProfileInfo<T>::value_type_ref;
918 using key_type = value_type;
919 using key_type_ref = value_type_ref;
920 using data_type = bool;
921 using data_type_ref = bool;
923 static key_type_ref KeyOfValue(value_type_ref D) { return D; }
924 static data_type_ref DataOfValue(value_type_ref) { return true; }
926 static bool isEqual(key_type_ref LHS, key_type_ref RHS) {
927 return std::equal_to<key_type>()(LHS,RHS);
930 static bool isLess(key_type_ref LHS, key_type_ref RHS) {
931 return std::less<key_type>()(LHS,RHS);
934 static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
937 /// ImutContainerInfo - Specialization for pointer values to treat pointers
938 /// as references to unique objects. Pointers are thus compared by
940 template <typename T>
941 struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
942 using value_type = typename ImutProfileInfo<T*>::value_type;
943 using value_type_ref = typename ImutProfileInfo<T*>::value_type_ref;
944 using key_type = value_type;
945 using key_type_ref = value_type_ref;
946 using data_type = bool;
947 using data_type_ref = bool;
949 static key_type_ref KeyOfValue(value_type_ref D) { return D; }
950 static data_type_ref DataOfValue(value_type_ref) { return true; }
952 static bool isEqual(key_type_ref LHS, key_type_ref RHS) { return LHS == RHS; }
954 static bool isLess(key_type_ref LHS, key_type_ref RHS) { return LHS < RHS; }
956 static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
959 //===----------------------------------------------------------------------===//
961 //===----------------------------------------------------------------------===//
963 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT>>
966 using value_type = typename ValInfo::value_type;
967 using value_type_ref = typename ValInfo::value_type_ref;
968 using TreeTy = ImutAVLTree<ValInfo>;
971 IntrusiveRefCntPtr<TreeTy> Root;
974 /// Constructs a set from a pointer to a tree root. In general one
975 /// should use a Factory object to create sets instead of directly
976 /// invoking the constructor, but there are cases where make this
977 /// constructor public is useful.
978 explicit ImmutableSet(TreeTy *R) : Root(R) {}
981 typename TreeTy::Factory F;
982 const bool Canonicalize;
985 Factory(bool canonicalize = true)
986 : Canonicalize(canonicalize) {}
988 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
989 : F(Alloc), Canonicalize(canonicalize) {}
991 Factory(const Factory& RHS) = delete;
992 void operator=(const Factory& RHS) = delete;
994 /// getEmptySet - Returns an immutable set that contains no elements.
995 ImmutableSet getEmptySet() {
996 return ImmutableSet(F.getEmptyTree());
999 /// add - Creates a new immutable set that contains all of the values
1000 /// of the original set with the addition of the specified value. If
1001 /// the original set already included the value, then the original set is
1002 /// returned and no memory is allocated. The time and space complexity
1003 /// of this operation is logarithmic in the size of the original set.
1004 /// The memory allocated to represent the set is released when the
1005 /// factory object that created the set is destroyed.
1006 LLVM_NODISCARD ImmutableSet add(ImmutableSet Old, value_type_ref V) {
1007 TreeTy *NewT = F.add(Old.Root.get(), V);
1008 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1011 /// remove - Creates a new immutable set that contains all of the values
1012 /// of the original set with the exception of the specified value. If
1013 /// the original set did not contain the value, the original set is
1014 /// returned and no memory is allocated. The time and space complexity
1015 /// of this operation is logarithmic in the size of the original set.
1016 /// The memory allocated to represent the set is released when the
1017 /// factory object that created the set is destroyed.
1018 LLVM_NODISCARD ImmutableSet remove(ImmutableSet Old, value_type_ref V) {
1019 TreeTy *NewT = F.remove(Old.Root.get(), V);
1020 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1023 BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
1025 typename TreeTy::Factory *getTreeFactory() const {
1026 return const_cast<typename TreeTy::Factory *>(&F);
1030 friend class Factory;
1032 /// Returns true if the set contains the specified value.
1033 bool contains(value_type_ref V) const {
1034 return Root ? Root->contains(V) : false;
1037 bool operator==(const ImmutableSet &RHS) const {
1038 return Root && RHS.Root ? Root->isEqual(*RHS.Root.get()) : Root == RHS.Root;
1041 bool operator!=(const ImmutableSet &RHS) const {
1042 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root.get())
1047 if (Root) { Root->retain(); }
1051 TreeTy *getRootWithoutRetain() const { return Root.get(); }
1053 /// isEmpty - Return true if the set contains no elements.
1054 bool isEmpty() const { return !Root; }
1056 /// isSingleton - Return true if the set contains exactly one element.
1057 /// This method runs in constant time.
1058 bool isSingleton() const { return getHeight() == 1; }
1060 template <typename Callback>
1061 void foreach(Callback& C) { if (Root) Root->foreach(C); }
1063 template <typename Callback>
1064 void foreach() { if (Root) { Callback C; Root->foreach(C); } }
1066 //===--------------------------------------------------===//
1068 //===--------------------------------------------------===//
1070 using iterator = ImutAVLValueIterator<ImmutableSet>;
1072 iterator begin() const { return iterator(Root.get()); }
1073 iterator end() const { return iterator(); }
1075 //===--------------------------------------------------===//
1077 //===--------------------------------------------------===//
1079 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1081 static void Profile(FoldingSetNodeID &ID, const ImmutableSet &S) {
1082 ID.AddPointer(S.Root.get());
1085 void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
1087 //===--------------------------------------------------===//
1089 //===--------------------------------------------------===//
1091 void validateTree() const { if (Root) Root->validateTree(); }
1094 // NOTE: This may some day replace the current ImmutableSet.
1095 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT>>
1096 class ImmutableSetRef {
1098 using value_type = typename ValInfo::value_type;
1099 using value_type_ref = typename ValInfo::value_type_ref;
1100 using TreeTy = ImutAVLTree<ValInfo>;
1101 using FactoryTy = typename TreeTy::Factory;
1104 IntrusiveRefCntPtr<TreeTy> Root;
1108 /// Constructs a set from a pointer to a tree root. In general one
1109 /// should use a Factory object to create sets instead of directly
1110 /// invoking the constructor, but there are cases where make this
1111 /// constructor public is useful.
1112 ImmutableSetRef(TreeTy *R, FactoryTy *F) : Root(R), Factory(F) {}
1114 static ImmutableSetRef getEmptySet(FactoryTy *F) {
1115 return ImmutableSetRef(0, F);
1118 ImmutableSetRef add(value_type_ref V) {
1119 return ImmutableSetRef(Factory->add(Root.get(), V), Factory);
1122 ImmutableSetRef remove(value_type_ref V) {
1123 return ImmutableSetRef(Factory->remove(Root.get(), V), Factory);
1126 /// Returns true if the set contains the specified value.
1127 bool contains(value_type_ref V) const {
1128 return Root ? Root->contains(V) : false;
1131 ImmutableSet<ValT> asImmutableSet(bool canonicalize = true) const {
1132 return ImmutableSet<ValT>(
1133 canonicalize ? Factory->getCanonicalTree(Root.get()) : Root.get());
1136 TreeTy *getRootWithoutRetain() const { return Root.get(); }
1138 bool operator==(const ImmutableSetRef &RHS) const {
1139 return Root && RHS.Root ? Root->isEqual(*RHS.Root.get()) : Root == RHS.Root;
1142 bool operator!=(const ImmutableSetRef &RHS) const {
1143 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root.get())
1147 /// isEmpty - Return true if the set contains no elements.
1148 bool isEmpty() const { return !Root; }
1150 /// isSingleton - Return true if the set contains exactly one element.
1151 /// This method runs in constant time.
1152 bool isSingleton() const { return getHeight() == 1; }
1154 //===--------------------------------------------------===//
1156 //===--------------------------------------------------===//
1158 using iterator = ImutAVLValueIterator<ImmutableSetRef>;
1160 iterator begin() const { return iterator(Root.get()); }
1161 iterator end() const { return iterator(); }
1163 //===--------------------------------------------------===//
1165 //===--------------------------------------------------===//
1167 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1169 static void Profile(FoldingSetNodeID &ID, const ImmutableSetRef &S) {
1170 ID.AddPointer(S.Root.get());
1173 void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
1175 //===--------------------------------------------------===//
1177 //===--------------------------------------------------===//
1179 void validateTree() const { if (Root) Root->validateTree(); }
1182 } // end namespace llvm
1184 #endif // LLVM_ADT_IMMUTABLESET_H