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/SmallVector.h"
19 #include "llvm/ADT/iterator.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/ErrorHandling.h"
31 //===----------------------------------------------------------------------===//
32 // Immutable AVL-Tree Definition.
33 //===----------------------------------------------------------------------===//
35 template <typename ImutInfo> class ImutAVLFactory;
36 template <typename ImutInfo> class ImutIntervalAVLFactory;
37 template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
38 template <typename ImutInfo> class ImutAVLTreeGenericIterator;
40 template <typename ImutInfo >
43 using key_type_ref = typename ImutInfo::key_type_ref;
44 using value_type = typename ImutInfo::value_type;
45 using value_type_ref = typename ImutInfo::value_type_ref;
46 using Factory = ImutAVLFactory<ImutInfo>;
47 using iterator = ImutAVLTreeInOrderIterator<ImutInfo>;
49 friend class ImutAVLFactory<ImutInfo>;
50 friend class ImutIntervalAVLFactory<ImutInfo>;
51 friend class ImutAVLTreeGenericIterator<ImutInfo>;
53 //===----------------------------------------------------===//
55 //===----------------------------------------------------===//
57 /// Return a pointer to the left subtree. This value
58 /// is NULL if there is no left subtree.
59 ImutAVLTree *getLeft() const { return left; }
61 /// Return a pointer to the right subtree. This value is
62 /// NULL if there is no right subtree.
63 ImutAVLTree *getRight() const { return right; }
65 /// getHeight - Returns the height of the tree. A tree with no subtrees
66 /// has a height of 1.
67 unsigned getHeight() const { return height; }
69 /// getValue - Returns the data value associated with the tree node.
70 const value_type& getValue() const { return value; }
72 /// find - Finds the subtree associated with the specified key value.
73 /// This method returns NULL if no matching subtree is found.
74 ImutAVLTree* find(key_type_ref K) {
75 ImutAVLTree *T = this;
77 key_type_ref CurrentKey = ImutInfo::KeyOfValue(T->getValue());
78 if (ImutInfo::isEqual(K,CurrentKey))
80 else if (ImutInfo::isLess(K,CurrentKey))
88 /// getMaxElement - Find the subtree associated with the highest ranged
90 ImutAVLTree* getMaxElement() {
91 ImutAVLTree *T = this;
92 ImutAVLTree *Right = T->getRight();
93 while (Right) { T = Right; Right = T->getRight(); }
97 /// size - Returns the number of nodes in the tree, which includes
98 /// both leaves and non-leaf nodes.
99 unsigned size() const {
101 if (const ImutAVLTree* L = getLeft())
103 if (const ImutAVLTree* R = getRight())
108 /// begin - Returns an iterator that iterates over the nodes of the tree
109 /// in an inorder traversal. The returned iterator thus refers to the
110 /// the tree node with the minimum data element.
111 iterator begin() const { return iterator(this); }
113 /// end - Returns an iterator for the tree that denotes the end of an
114 /// inorder traversal.
115 iterator end() const { return iterator(); }
117 bool isElementEqual(value_type_ref V) const {
119 if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(getValue()),
120 ImutInfo::KeyOfValue(V)))
123 // Also compare the data values.
124 if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(getValue()),
125 ImutInfo::DataOfValue(V)))
131 bool isElementEqual(const ImutAVLTree* RHS) const {
132 return isElementEqual(RHS->getValue());
135 /// isEqual - Compares two trees for structural equality and returns true
136 /// if they are equal. This worst case performance of this operation is
137 // linear in the sizes of the trees.
138 bool isEqual(const ImutAVLTree& RHS) const {
142 iterator LItr = begin(), LEnd = end();
143 iterator RItr = RHS.begin(), REnd = RHS.end();
145 while (LItr != LEnd && RItr != REnd) {
146 if (&*LItr == &*RItr) {
152 if (!LItr->isElementEqual(&*RItr))
159 return LItr == LEnd && RItr == REnd;
162 /// isNotEqual - Compares two trees for structural inequality. Performance
163 /// is the same is isEqual.
164 bool isNotEqual(const ImutAVLTree& RHS) const { return !isEqual(RHS); }
166 /// contains - Returns true if this tree contains a subtree (node) that
167 /// has an data element that matches the specified key. Complexity
168 /// is logarithmic in the size of the tree.
169 bool contains(key_type_ref K) { return (bool) find(K); }
171 /// foreach - A member template the accepts invokes operator() on a functor
172 /// object (specifed by Callback) for every node/subtree in the tree.
173 /// Nodes are visited using an inorder traversal.
174 template <typename Callback>
175 void foreach(Callback& C) {
176 if (ImutAVLTree* L = getLeft())
181 if (ImutAVLTree* R = getRight())
185 /// validateTree - A utility method that checks that the balancing and
186 /// ordering invariants of the tree are satisifed. It is a recursive
187 /// method that returns the height of the tree, which is then consumed
188 /// by the enclosing validateTree call. External callers should ignore the
189 /// return value. An invalid tree will cause an assertion to fire in
191 unsigned validateTree() const {
192 unsigned HL = getLeft() ? getLeft()->validateTree() : 0;
193 unsigned HR = getRight() ? getRight()->validateTree() : 0;
197 assert(getHeight() == ( HL > HR ? HL : HR ) + 1
198 && "Height calculation wrong");
200 assert((HL > HR ? HL-HR : HR-HL) <= 2
201 && "Balancing invariant violated");
203 assert((!getLeft() ||
204 ImutInfo::isLess(ImutInfo::KeyOfValue(getLeft()->getValue()),
205 ImutInfo::KeyOfValue(getValue()))) &&
206 "Value in left child is not less that current value");
208 assert((!getRight() ||
209 ImutInfo::isLess(ImutInfo::KeyOfValue(getValue()),
210 ImutInfo::KeyOfValue(getRight()->getValue()))) &&
211 "Current value is not less that value of right child");
216 //===----------------------------------------------------===//
218 //===----------------------------------------------------===//
224 ImutAVLTree *prev = nullptr;
225 ImutAVLTree *next = nullptr;
227 unsigned height : 28;
229 bool IsDigestCached : 1;
230 bool IsCanonicalized : 1;
234 uint32_t refCount = 0;
236 //===----------------------------------------------------===//
237 // Internal methods (node manipulation; used by Factory).
238 //===----------------------------------------------------===//
241 /// ImutAVLTree - Internal constructor that is only called by
243 ImutAVLTree(Factory *f, ImutAVLTree* l, ImutAVLTree* r, value_type_ref v,
245 : factory(f), left(l), right(r), height(height), IsMutable(true),
246 IsDigestCached(false), IsCanonicalized(false), value(v)
248 if (left) left->retain();
249 if (right) right->retain();
252 /// isMutable - Returns true if the left and right subtree references
253 /// (as well as height) can be changed. If this method returns false,
254 /// the tree is truly immutable. Trees returned from an ImutAVLFactory
255 /// object should always have this method return true. Further, if this
256 /// method returns false for an instance of ImutAVLTree, all subtrees
257 /// will also have this method return false. The converse is not true.
258 bool isMutable() const { return IsMutable; }
260 /// hasCachedDigest - Returns true if the digest for this tree is cached.
261 /// This can only be true if the tree is immutable.
262 bool hasCachedDigest() const { return IsDigestCached; }
264 //===----------------------------------------------------===//
265 // Mutating operations. A tree root can be manipulated as
266 // long as its reference has not "escaped" from internal
267 // methods of a factory object (see below). When a tree
268 // pointer is externally viewable by client code, the
269 // internal "mutable bit" is cleared to mark the tree
270 // immutable. Note that a tree that still has its mutable
271 // bit set may have children (subtrees) that are themselves
273 //===----------------------------------------------------===//
275 /// markImmutable - Clears the mutable flag for a tree. After this happens,
276 /// it is an error to call setLeft(), setRight(), and setHeight().
277 void markImmutable() {
278 assert(isMutable() && "Mutable flag already removed.");
282 /// markedCachedDigest - Clears the NoCachedDigest flag for a tree.
283 void markedCachedDigest() {
284 assert(!hasCachedDigest() && "NoCachedDigest flag already removed.");
285 IsDigestCached = true;
288 /// setHeight - Changes the height of the tree. Used internally by
290 void setHeight(unsigned h) {
291 assert(isMutable() && "Only a mutable tree can have its height changed.");
295 static uint32_t computeDigest(ImutAVLTree *L, ImutAVLTree *R,
300 digest += L->computeDigest();
302 // Compute digest of stored data.
304 ImutInfo::Profile(ID,V);
305 digest += ID.ComputeHash();
308 digest += R->computeDigest();
313 uint32_t computeDigest() {
314 // Check the lowest bit to determine if digest has actually been
316 if (hasCachedDigest())
319 uint32_t X = computeDigest(getLeft(), getRight(), getValue());
321 markedCachedDigest();
325 //===----------------------------------------------------===//
326 // Reference count operations.
327 //===----------------------------------------------------===//
330 void retain() { ++refCount; }
333 assert(refCount > 0);
343 if (IsCanonicalized) {
350 factory->Cache[factory->maskCacheIndex(computeDigest())] = next;
353 // We need to clear the mutability bit in case we are
354 // destroying the node as part of a sweep in ImutAVLFactory::recoverNodes().
356 factory->freeNodes.push_back(this);
360 //===----------------------------------------------------------------------===//
361 // Immutable AVL-Tree Factory class.
362 //===----------------------------------------------------------------------===//
364 template <typename ImutInfo >
365 class ImutAVLFactory {
366 friend class ImutAVLTree<ImutInfo>;
368 using TreeTy = ImutAVLTree<ImutInfo>;
369 using value_type_ref = typename TreeTy::value_type_ref;
370 using key_type_ref = typename TreeTy::key_type_ref;
371 using CacheTy = DenseMap<unsigned, TreeTy*>;
375 std::vector<TreeTy*> createdNodes;
376 std::vector<TreeTy*> freeNodes;
378 bool ownsAllocator() const {
379 return (Allocator & 0x1) == 0;
382 BumpPtrAllocator& getAllocator() const {
383 return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
386 //===--------------------------------------------------===//
388 //===--------------------------------------------------===//
392 : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
394 ImutAVLFactory(BumpPtrAllocator& Alloc)
395 : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
398 if (ownsAllocator()) delete &getAllocator();
401 TreeTy* add(TreeTy* T, value_type_ref V) {
402 T = add_internal(V,T);
408 TreeTy* remove(TreeTy* T, key_type_ref V) {
409 T = remove_internal(V,T);
415 TreeTy* getEmptyTree() const { return nullptr; }
418 //===--------------------------------------------------===//
419 // A bunch of quick helper functions used for reasoning
420 // about the properties of trees and their children.
421 // These have succinct names so that the balancing code
422 // is as terse (and readable) as possible.
423 //===--------------------------------------------------===//
425 bool isEmpty(TreeTy* T) const { return !T; }
426 unsigned getHeight(TreeTy* T) const { return T ? T->getHeight() : 0; }
427 TreeTy* getLeft(TreeTy* T) const { return T->getLeft(); }
428 TreeTy* getRight(TreeTy* T) const { return T->getRight(); }
429 value_type_ref getValue(TreeTy* T) const { return T->value; }
431 // Make sure the index is not the Tombstone or Entry key of the DenseMap.
432 static unsigned maskCacheIndex(unsigned I) { return (I & ~0x02); }
434 unsigned incrementHeight(TreeTy* L, TreeTy* R) const {
435 unsigned hl = getHeight(L);
436 unsigned hr = getHeight(R);
437 return (hl > hr ? hl : hr) + 1;
440 static bool compareTreeWithSection(TreeTy* T,
441 typename TreeTy::iterator& TI,
442 typename TreeTy::iterator& TE) {
443 typename TreeTy::iterator I = T->begin(), E = T->end();
444 for ( ; I!=E ; ++I, ++TI) {
445 if (TI == TE || !I->isElementEqual(&*TI))
451 //===--------------------------------------------------===//
452 // "createNode" is used to generate new tree roots that link
453 // to other trees. The functon may also simply move links
454 // in an existing root if that root is still marked mutable.
455 // This is necessary because otherwise our balancing code
456 // would leak memory as it would create nodes that are
457 // then discarded later before the finished tree is
458 // returned to the caller.
459 //===--------------------------------------------------===//
461 TreeTy* createNode(TreeTy* L, value_type_ref V, TreeTy* R) {
462 BumpPtrAllocator& A = getAllocator();
464 if (!freeNodes.empty()) {
465 T = freeNodes.back();
466 freeNodes.pop_back();
470 T = (TreeTy*) A.Allocate<TreeTy>();
472 new (T) TreeTy(this, L, R, V, incrementHeight(L,R));
473 createdNodes.push_back(T);
477 TreeTy* createNode(TreeTy* newLeft, TreeTy* oldTree, TreeTy* newRight) {
478 return createNode(newLeft, getValue(oldTree), newRight);
481 void recoverNodes() {
482 for (unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
483 TreeTy *N = createdNodes[i];
484 if (N->isMutable() && N->refCount == 0)
487 createdNodes.clear();
490 /// balanceTree - Used by add_internal and remove_internal to
491 /// balance a newly created tree.
492 TreeTy* balanceTree(TreeTy* L, value_type_ref V, TreeTy* R) {
493 unsigned hl = getHeight(L);
494 unsigned hr = getHeight(R);
497 assert(!isEmpty(L) && "Left tree cannot be empty to have a height >= 2");
499 TreeTy *LL = getLeft(L);
500 TreeTy *LR = getRight(L);
502 if (getHeight(LL) >= getHeight(LR))
503 return createNode(LL, L, createNode(LR,V,R));
505 assert(!isEmpty(LR) && "LR cannot be empty because it has a height >= 1");
507 TreeTy *LRL = getLeft(LR);
508 TreeTy *LRR = getRight(LR);
510 return createNode(createNode(LL,L,LRL), LR, createNode(LRR,V,R));
514 assert(!isEmpty(R) && "Right tree cannot be empty to have a height >= 2");
516 TreeTy *RL = getLeft(R);
517 TreeTy *RR = getRight(R);
519 if (getHeight(RR) >= getHeight(RL))
520 return createNode(createNode(L,V,RL), R, RR);
522 assert(!isEmpty(RL) && "RL cannot be empty because it has a height >= 1");
524 TreeTy *RLL = getLeft(RL);
525 TreeTy *RLR = getRight(RL);
527 return createNode(createNode(L,V,RLL), RL, createNode(RLR,R,RR));
530 return createNode(L,V,R);
533 /// add_internal - Creates a new tree that includes the specified
534 /// data and the data from the original tree. If the original tree
535 /// already contained the data item, the original tree is returned.
536 TreeTy* add_internal(value_type_ref V, TreeTy* T) {
538 return createNode(T, V, T);
539 assert(!T->isMutable());
541 key_type_ref K = ImutInfo::KeyOfValue(V);
542 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
544 if (ImutInfo::isEqual(K,KCurrent))
545 return createNode(getLeft(T), V, getRight(T));
546 else if (ImutInfo::isLess(K,KCurrent))
547 return balanceTree(add_internal(V, getLeft(T)), getValue(T), getRight(T));
549 return balanceTree(getLeft(T), getValue(T), add_internal(V, getRight(T)));
552 /// remove_internal - Creates a new tree that includes all the data
553 /// from the original tree except the specified data. If the
554 /// specified data did not exist in the original tree, the original
555 /// tree is returned.
556 TreeTy* remove_internal(key_type_ref K, TreeTy* T) {
560 assert(!T->isMutable());
562 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
564 if (ImutInfo::isEqual(K,KCurrent)) {
565 return combineTrees(getLeft(T), getRight(T));
566 } else if (ImutInfo::isLess(K,KCurrent)) {
567 return balanceTree(remove_internal(K, getLeft(T)),
568 getValue(T), getRight(T));
570 return balanceTree(getLeft(T), getValue(T),
571 remove_internal(K, getRight(T)));
575 TreeTy* combineTrees(TreeTy* L, TreeTy* R) {
581 TreeTy* newRight = removeMinBinding(R,OldNode);
582 return balanceTree(L, getValue(OldNode), newRight);
585 TreeTy* removeMinBinding(TreeTy* T, TreeTy*& Noderemoved) {
587 if (isEmpty(getLeft(T))) {
591 return balanceTree(removeMinBinding(getLeft(T), Noderemoved),
592 getValue(T), getRight(T));
595 /// markImmutable - Clears the mutable bits of a root and all of its
597 void markImmutable(TreeTy* T) {
598 if (!T || !T->isMutable())
601 markImmutable(getLeft(T));
602 markImmutable(getRight(T));
606 TreeTy *getCanonicalTree(TreeTy *TNew) {
610 if (TNew->IsCanonicalized)
613 // Search the hashtable for another tree with the same digest, and
614 // if find a collision compare those trees by their contents.
615 unsigned digest = TNew->computeDigest();
616 TreeTy *&entry = Cache[maskCacheIndex(digest)];
620 for (TreeTy *T = entry ; T != nullptr; T = T->next) {
621 // Compare the Contents('T') with Contents('TNew')
622 typename TreeTy::iterator TI = T->begin(), TE = T->end();
623 if (!compareTreeWithSection(TNew, TI, TE))
626 continue; // T has more contents than TNew.
627 // Trees did match! Return 'T'.
628 if (TNew->refCount == 0)
638 TNew->IsCanonicalized = true;
643 //===----------------------------------------------------------------------===//
644 // Immutable AVL-Tree Iterators.
645 //===----------------------------------------------------------------------===//
647 template <typename ImutInfo>
648 class ImutAVLTreeGenericIterator
649 : public std::iterator<std::bidirectional_iterator_tag,
650 ImutAVLTree<ImutInfo>> {
651 SmallVector<uintptr_t,20> stack;
654 enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
657 using TreeTy = ImutAVLTree<ImutInfo>;
659 ImutAVLTreeGenericIterator() = default;
660 ImutAVLTreeGenericIterator(const TreeTy *Root) {
661 if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
664 TreeTy &operator*() const {
665 assert(!stack.empty());
666 return *reinterpret_cast<TreeTy *>(stack.back() & ~Flags);
668 TreeTy *operator->() const { return &*this; }
670 uintptr_t getVisitState() const {
671 assert(!stack.empty());
672 return stack.back() & Flags;
675 bool atEnd() const { return stack.empty(); }
677 bool atBeginning() const {
678 return stack.size() == 1 && getVisitState() == VisitedNone;
681 void skipToParent() {
682 assert(!stack.empty());
686 switch (getVisitState()) {
688 stack.back() |= VisitedLeft;
691 stack.back() |= VisitedRight;
694 llvm_unreachable("Unreachable.");
698 bool operator==(const ImutAVLTreeGenericIterator &x) const {
699 return stack == x.stack;
702 bool operator!=(const ImutAVLTreeGenericIterator &x) const {
703 return !(*this == x);
706 ImutAVLTreeGenericIterator &operator++() {
707 assert(!stack.empty());
708 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
710 switch (getVisitState()) {
712 if (TreeTy* L = Current->getLeft())
713 stack.push_back(reinterpret_cast<uintptr_t>(L));
715 stack.back() |= VisitedLeft;
718 if (TreeTy* R = Current->getRight())
719 stack.push_back(reinterpret_cast<uintptr_t>(R));
721 stack.back() |= VisitedRight;
727 llvm_unreachable("Unreachable.");
732 ImutAVLTreeGenericIterator &operator--() {
733 assert(!stack.empty());
734 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
736 switch (getVisitState()) {
741 stack.back() &= ~Flags; // Set state to "VisitedNone."
742 if (TreeTy* L = Current->getLeft())
743 stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
746 stack.back() &= ~Flags;
747 stack.back() |= VisitedLeft;
748 if (TreeTy* R = Current->getRight())
749 stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
752 llvm_unreachable("Unreachable.");
758 template <typename ImutInfo>
759 class ImutAVLTreeInOrderIterator
760 : public std::iterator<std::bidirectional_iterator_tag,
761 ImutAVLTree<ImutInfo>> {
762 using InternalIteratorTy = ImutAVLTreeGenericIterator<ImutInfo>;
764 InternalIteratorTy InternalItr;
767 using TreeTy = ImutAVLTree<ImutInfo>;
769 ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
771 ++*this; // Advance to first element.
774 ImutAVLTreeInOrderIterator() : InternalItr() {}
776 bool operator==(const ImutAVLTreeInOrderIterator &x) const {
777 return InternalItr == x.InternalItr;
780 bool operator!=(const ImutAVLTreeInOrderIterator &x) const {
781 return !(*this == x);
784 TreeTy &operator*() const { return *InternalItr; }
785 TreeTy *operator->() const { return &*InternalItr; }
787 ImutAVLTreeInOrderIterator &operator++() {
789 while (!InternalItr.atEnd() &&
790 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
795 ImutAVLTreeInOrderIterator &operator--() {
797 while (!InternalItr.atBeginning() &&
798 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
804 InternalItr.skipToParent();
806 while (!InternalItr.atEnd() &&
807 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
812 /// Generic iterator that wraps a T::TreeTy::iterator and exposes
813 /// iterator::getValue() on dereference.
814 template <typename T>
815 struct ImutAVLValueIterator
816 : iterator_adaptor_base<
817 ImutAVLValueIterator<T>, typename T::TreeTy::iterator,
818 typename std::iterator_traits<
819 typename T::TreeTy::iterator>::iterator_category,
820 const typename T::value_type> {
821 ImutAVLValueIterator() = default;
822 explicit ImutAVLValueIterator(typename T::TreeTy *Tree)
823 : ImutAVLValueIterator::iterator_adaptor_base(Tree) {}
825 typename ImutAVLValueIterator::reference operator*() const {
826 return this->I->getValue();
830 //===----------------------------------------------------------------------===//
831 // Trait classes for Profile information.
832 //===----------------------------------------------------------------------===//
834 /// Generic profile template. The default behavior is to invoke the
835 /// profile method of an object. Specializations for primitive integers
836 /// and generic handling of pointers is done below.
837 template <typename T>
838 struct ImutProfileInfo {
839 using value_type = const T;
840 using value_type_ref = const T&;
842 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
843 FoldingSetTrait<T>::Profile(X,ID);
847 /// Profile traits for integers.
848 template <typename T>
849 struct ImutProfileInteger {
850 using value_type = const T;
851 using value_type_ref = const T&;
853 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
858 #define PROFILE_INTEGER_INFO(X)\
859 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
861 PROFILE_INTEGER_INFO(char)
862 PROFILE_INTEGER_INFO(unsigned char)
863 PROFILE_INTEGER_INFO(short)
864 PROFILE_INTEGER_INFO(unsigned short)
865 PROFILE_INTEGER_INFO(unsigned)
866 PROFILE_INTEGER_INFO(signed)
867 PROFILE_INTEGER_INFO(long)
868 PROFILE_INTEGER_INFO(unsigned long)
869 PROFILE_INTEGER_INFO(long long)
870 PROFILE_INTEGER_INFO(unsigned long long)
872 #undef PROFILE_INTEGER_INFO
874 /// Profile traits for booleans.
876 struct ImutProfileInfo<bool> {
877 using value_type = const bool;
878 using value_type_ref = const bool&;
880 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
885 /// Generic profile trait for pointer types. We treat pointers as
886 /// references to unique objects.
887 template <typename T>
888 struct ImutProfileInfo<T*> {
889 using value_type = const T*;
890 using value_type_ref = value_type;
892 static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
897 //===----------------------------------------------------------------------===//
898 // Trait classes that contain element comparison operators and type
899 // definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap. These
900 // inherit from the profile traits (ImutProfileInfo) to include operations
901 // for element profiling.
902 //===----------------------------------------------------------------------===//
904 /// ImutContainerInfo - Generic definition of comparison operations for
905 /// elements of immutable containers that defaults to using
906 /// std::equal_to<> and std::less<> to perform comparison of elements.
907 template <typename T>
908 struct ImutContainerInfo : public ImutProfileInfo<T> {
909 using value_type = typename ImutProfileInfo<T>::value_type;
910 using value_type_ref = typename ImutProfileInfo<T>::value_type_ref;
911 using key_type = value_type;
912 using key_type_ref = value_type_ref;
913 using data_type = bool;
914 using data_type_ref = bool;
916 static key_type_ref KeyOfValue(value_type_ref D) { return D; }
917 static data_type_ref DataOfValue(value_type_ref) { return true; }
919 static bool isEqual(key_type_ref LHS, key_type_ref RHS) {
920 return std::equal_to<key_type>()(LHS,RHS);
923 static bool isLess(key_type_ref LHS, key_type_ref RHS) {
924 return std::less<key_type>()(LHS,RHS);
927 static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
930 /// ImutContainerInfo - Specialization for pointer values to treat pointers
931 /// as references to unique objects. Pointers are thus compared by
933 template <typename T>
934 struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
935 using value_type = typename ImutProfileInfo<T*>::value_type;
936 using value_type_ref = typename ImutProfileInfo<T*>::value_type_ref;
937 using key_type = value_type;
938 using key_type_ref = value_type_ref;
939 using data_type = bool;
940 using data_type_ref = bool;
942 static key_type_ref KeyOfValue(value_type_ref D) { return D; }
943 static data_type_ref DataOfValue(value_type_ref) { return true; }
945 static bool isEqual(key_type_ref LHS, key_type_ref RHS) { return LHS == RHS; }
947 static bool isLess(key_type_ref LHS, key_type_ref RHS) { return LHS < RHS; }
949 static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
952 //===----------------------------------------------------------------------===//
954 //===----------------------------------------------------------------------===//
956 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT>>
959 using value_type = typename ValInfo::value_type;
960 using value_type_ref = typename ValInfo::value_type_ref;
961 using TreeTy = ImutAVLTree<ValInfo>;
967 /// Constructs a set from a pointer to a tree root. In general one
968 /// should use a Factory object to create sets instead of directly
969 /// invoking the constructor, but there are cases where make this
970 /// constructor public is useful.
971 explicit ImmutableSet(TreeTy* R) : Root(R) {
972 if (Root) { Root->retain(); }
975 ImmutableSet(const ImmutableSet &X) : Root(X.Root) {
976 if (Root) { Root->retain(); }
980 if (Root) { Root->release(); }
983 ImmutableSet &operator=(const ImmutableSet &X) {
984 if (Root != X.Root) {
985 if (X.Root) { X.Root->retain(); }
986 if (Root) { Root->release(); }
993 typename TreeTy::Factory F;
994 const bool Canonicalize;
997 Factory(bool canonicalize = true)
998 : Canonicalize(canonicalize) {}
1000 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
1001 : F(Alloc), Canonicalize(canonicalize) {}
1003 Factory(const Factory& RHS) = delete;
1004 void operator=(const Factory& RHS) = delete;
1006 /// getEmptySet - Returns an immutable set that contains no elements.
1007 ImmutableSet getEmptySet() {
1008 return ImmutableSet(F.getEmptyTree());
1011 /// add - Creates a new immutable set that contains all of the values
1012 /// of the original set with the addition of the specified value. If
1013 /// the original set already included the value, then 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 add(ImmutableSet Old, value_type_ref V) {
1019 TreeTy *NewT = F.add(Old.Root, V);
1020 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1023 /// remove - Creates a new immutable set that contains all of the values
1024 /// of the original set with the exception of the specified value. If
1025 /// the original set did not contain the value, the original set is
1026 /// returned and no memory is allocated. The time and space complexity
1027 /// of this operation is logarithmic in the size of the original set.
1028 /// The memory allocated to represent the set is released when the
1029 /// factory object that created the set is destroyed.
1030 LLVM_NODISCARD ImmutableSet remove(ImmutableSet Old, value_type_ref V) {
1031 TreeTy *NewT = F.remove(Old.Root, V);
1032 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1035 BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
1037 typename TreeTy::Factory *getTreeFactory() const {
1038 return const_cast<typename TreeTy::Factory *>(&F);
1042 friend class Factory;
1044 /// Returns true if the set contains the specified value.
1045 bool contains(value_type_ref V) const {
1046 return Root ? Root->contains(V) : false;
1049 bool operator==(const ImmutableSet &RHS) const {
1050 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
1053 bool operator!=(const ImmutableSet &RHS) const {
1054 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
1058 if (Root) { Root->retain(); }
1062 TreeTy *getRootWithoutRetain() const {
1066 /// isEmpty - Return true if the set contains no elements.
1067 bool isEmpty() const { return !Root; }
1069 /// isSingleton - Return true if the set contains exactly one element.
1070 /// This method runs in constant time.
1071 bool isSingleton() const { return getHeight() == 1; }
1073 template <typename Callback>
1074 void foreach(Callback& C) { if (Root) Root->foreach(C); }
1076 template <typename Callback>
1077 void foreach() { if (Root) { Callback C; Root->foreach(C); } }
1079 //===--------------------------------------------------===//
1081 //===--------------------------------------------------===//
1083 using iterator = ImutAVLValueIterator<ImmutableSet>;
1085 iterator begin() const { return iterator(Root); }
1086 iterator end() const { return iterator(); }
1088 //===--------------------------------------------------===//
1090 //===--------------------------------------------------===//
1092 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1094 static void Profile(FoldingSetNodeID &ID, const ImmutableSet &S) {
1095 ID.AddPointer(S.Root);
1098 void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
1100 //===--------------------------------------------------===//
1102 //===--------------------------------------------------===//
1104 void validateTree() const { if (Root) Root->validateTree(); }
1107 // NOTE: This may some day replace the current ImmutableSet.
1108 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT>>
1109 class ImmutableSetRef {
1111 using value_type = typename ValInfo::value_type;
1112 using value_type_ref = typename ValInfo::value_type_ref;
1113 using TreeTy = ImutAVLTree<ValInfo>;
1114 using FactoryTy = typename TreeTy::Factory;
1121 /// Constructs a set from a pointer to a tree root. In general one
1122 /// should use a Factory object to create sets instead of directly
1123 /// invoking the constructor, but there are cases where make this
1124 /// constructor public is useful.
1125 explicit ImmutableSetRef(TreeTy* R, FactoryTy *F)
1128 if (Root) { Root->retain(); }
1131 ImmutableSetRef(const ImmutableSetRef &X)
1133 Factory(X.Factory) {
1134 if (Root) { Root->retain(); }
1137 ~ImmutableSetRef() {
1138 if (Root) { Root->release(); }
1141 ImmutableSetRef &operator=(const ImmutableSetRef &X) {
1142 if (Root != X.Root) {
1143 if (X.Root) { X.Root->retain(); }
1144 if (Root) { Root->release(); }
1146 Factory = X.Factory;
1151 static ImmutableSetRef getEmptySet(FactoryTy *F) {
1152 return ImmutableSetRef(0, F);
1155 ImmutableSetRef add(value_type_ref V) {
1156 return ImmutableSetRef(Factory->add(Root, V), Factory);
1159 ImmutableSetRef remove(value_type_ref V) {
1160 return ImmutableSetRef(Factory->remove(Root, V), Factory);
1163 /// Returns true if the set contains the specified value.
1164 bool contains(value_type_ref V) const {
1165 return Root ? Root->contains(V) : false;
1168 ImmutableSet<ValT> asImmutableSet(bool canonicalize = true) const {
1169 return ImmutableSet<ValT>(canonicalize ?
1170 Factory->getCanonicalTree(Root) : Root);
1173 TreeTy *getRootWithoutRetain() const {
1177 bool operator==(const ImmutableSetRef &RHS) const {
1178 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
1181 bool operator!=(const ImmutableSetRef &RHS) const {
1182 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
1185 /// isEmpty - Return true if the set contains no elements.
1186 bool isEmpty() const { return !Root; }
1188 /// isSingleton - Return true if the set contains exactly one element.
1189 /// This method runs in constant time.
1190 bool isSingleton() const { return getHeight() == 1; }
1192 //===--------------------------------------------------===//
1194 //===--------------------------------------------------===//
1196 using iterator = ImutAVLValueIterator<ImmutableSetRef>;
1198 iterator begin() const { return iterator(Root); }
1199 iterator end() const { return iterator(); }
1201 //===--------------------------------------------------===//
1203 //===--------------------------------------------------===//
1205 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1207 static void Profile(FoldingSetNodeID &ID, const ImmutableSetRef &S) {
1208 ID.AddPointer(S.Root);
1211 void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
1213 //===--------------------------------------------------===//
1215 //===--------------------------------------------------===//
1217 void validateTree() const { if (Root) Root->validateTree(); }
1220 } // end namespace llvm
1222 #endif // LLVM_ADT_IMMUTABLESET_H