1 //===--- ImmutableSet.h - Immutable (functional) set interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ImutAVLTree and ImmutableSet classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_IMSET_H
15 #define LLVM_ADT_IMSET_H
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/ErrorHandling.h"
29 //===----------------------------------------------------------------------===//
30 // Immutable AVL-Tree Definition.
31 //===----------------------------------------------------------------------===//
33 template <typename ImutInfo> class ImutAVLFactory;
34 template <typename ImutInfo> class ImutIntervalAVLFactory;
35 template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
36 template <typename ImutInfo> class ImutAVLTreeGenericIterator;
38 template <typename ImutInfo >
41 typedef typename ImutInfo::key_type_ref key_type_ref;
42 typedef typename ImutInfo::value_type value_type;
43 typedef typename ImutInfo::value_type_ref value_type_ref;
45 typedef ImutAVLFactory<ImutInfo> Factory;
46 friend class ImutAVLFactory<ImutInfo>;
47 friend class ImutIntervalAVLFactory<ImutInfo>;
49 friend class ImutAVLTreeGenericIterator<ImutInfo>;
51 typedef ImutAVLTreeInOrderIterator<ImutInfo> iterator;
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");
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 //===----------------------------------------------------===//
228 unsigned height : 28;
229 unsigned IsMutable : 1;
230 unsigned IsDigestCached : 1;
231 unsigned IsCanonicalized : 1;
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), prev(0), next(0), height(height),
247 IsMutable(true), IsDigestCached(false), IsCanonicalized(0),
248 value(v), digest(0), refCount(0)
250 if (left) left->retain();
251 if (right) right->retain();
254 /// isMutable - Returns true if the left and right subtree references
255 /// (as well as height) can be changed. If this method returns false,
256 /// the tree is truly immutable. Trees returned from an ImutAVLFactory
257 /// object should always have this method return true. Further, if this
258 /// method returns false for an instance of ImutAVLTree, all subtrees
259 /// will also have this method return false. The converse is not true.
260 bool isMutable() const { return IsMutable; }
262 /// hasCachedDigest - Returns true if the digest for this tree is cached.
263 /// This can only be true if the tree is immutable.
264 bool hasCachedDigest() const { return IsDigestCached; }
266 //===----------------------------------------------------===//
267 // Mutating operations. A tree root can be manipulated as
268 // long as its reference has not "escaped" from internal
269 // methods of a factory object (see below). When a tree
270 // pointer is externally viewable by client code, the
271 // internal "mutable bit" is cleared to mark the tree
272 // immutable. Note that a tree that still has its mutable
273 // bit set may have children (subtrees) that are themselves
275 //===----------------------------------------------------===//
277 /// markImmutable - Clears the mutable flag for a tree. After this happens,
278 /// it is an error to call setLeft(), setRight(), and setHeight().
279 void markImmutable() {
280 assert(isMutable() && "Mutable flag already removed.");
284 /// markedCachedDigest - Clears the NoCachedDigest flag for a tree.
285 void markedCachedDigest() {
286 assert(!hasCachedDigest() && "NoCachedDigest flag already removed.");
287 IsDigestCached = true;
290 /// setHeight - Changes the height of the tree. Used internally by
292 void setHeight(unsigned h) {
293 assert(isMutable() && "Only a mutable tree can have its height changed.");
298 uint32_t computeDigest(ImutAVLTree* L, ImutAVLTree* R, value_type_ref V) {
302 digest += L->computeDigest();
304 // Compute digest of stored data.
306 ImutInfo::Profile(ID,V);
307 digest += ID.ComputeHash();
310 digest += R->computeDigest();
315 inline uint32_t computeDigest() {
316 // Check the lowest bit to determine if digest has actually been
318 if (hasCachedDigest())
321 uint32_t X = computeDigest(getLeft(), getRight(), getValue());
323 markedCachedDigest();
327 //===----------------------------------------------------===//
328 // Reference count operations.
329 //===----------------------------------------------------===//
332 void retain() { ++refCount; }
334 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>;
367 typedef ImutAVLTree<ImutInfo> TreeTy;
368 typedef typename TreeTy::value_type_ref value_type_ref;
369 typedef typename TreeTy::key_type_ref key_type_ref;
371 typedef DenseMap<unsigned, TreeTy*> CacheTy;
375 std::vector<TreeTy*> createdNodes;
376 std::vector<TreeTy*> freeNodes;
378 bool ownsAllocator() const {
379 return Allocator & 0x1 ? false : true;
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 NULL; }
419 //===--------------------------------------------------===//
420 // A bunch of quick helper functions used for reasoning
421 // about the properties of trees and their children.
422 // These have succinct names so that the balancing code
423 // is as terse (and readable) as possible.
424 //===--------------------------------------------------===//
426 bool isEmpty(TreeTy* T) const { return !T; }
427 unsigned getHeight(TreeTy* T) const { return T ? T->getHeight() : 0; }
428 TreeTy* getLeft(TreeTy* T) const { return T->getLeft(); }
429 TreeTy* getRight(TreeTy* T) const { return T->getRight(); }
430 value_type_ref getValue(TreeTy* T) const { return T->value; }
432 // Make sure the index is not the Tombstone or Entry key of the DenseMap.
433 static inline unsigned maskCacheIndex(unsigned I) {
437 unsigned incrementHeight(TreeTy* L, TreeTy* R) const {
438 unsigned hl = getHeight(L);
439 unsigned hr = getHeight(R);
440 return (hl > hr ? hl : hr) + 1;
443 static bool compareTreeWithSection(TreeTy* T,
444 typename TreeTy::iterator& TI,
445 typename TreeTy::iterator& TE) {
446 typename TreeTy::iterator I = T->begin(), E = T->end();
447 for ( ; I!=E ; ++I, ++TI) {
448 if (TI == TE || !I->isElementEqual(*TI))
454 //===--------------------------------------------------===//
455 // "createNode" is used to generate new tree roots that link
456 // to other trees. The functon may also simply move links
457 // in an existing root if that root is still marked mutable.
458 // This is necessary because otherwise our balancing code
459 // would leak memory as it would create nodes that are
460 // then discarded later before the finished tree is
461 // returned to the caller.
462 //===--------------------------------------------------===//
464 TreeTy* createNode(TreeTy* L, value_type_ref V, TreeTy* R) {
465 BumpPtrAllocator& A = getAllocator();
467 if (!freeNodes.empty()) {
468 T = freeNodes.back();
469 freeNodes.pop_back();
474 T = (TreeTy*) A.Allocate<TreeTy>();
476 new (T) TreeTy(this, L, R, V, incrementHeight(L,R));
477 createdNodes.push_back(T);
481 TreeTy* createNode(TreeTy* newLeft, TreeTy* oldTree, TreeTy* newRight) {
482 return createNode(newLeft, getValue(oldTree), newRight);
485 void recoverNodes() {
486 for (unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
487 TreeTy *N = createdNodes[i];
488 if (N->isMutable() && N->refCount == 0)
491 createdNodes.clear();
494 /// balanceTree - Used by add_internal and remove_internal to
495 /// balance a newly created tree.
496 TreeTy* balanceTree(TreeTy* L, value_type_ref V, TreeTy* R) {
497 unsigned hl = getHeight(L);
498 unsigned hr = getHeight(R);
501 assert(!isEmpty(L) && "Left tree cannot be empty to have a height >= 2");
503 TreeTy *LL = getLeft(L);
504 TreeTy *LR = getRight(L);
506 if (getHeight(LL) >= getHeight(LR))
507 return createNode(LL, L, createNode(LR,V,R));
509 assert(!isEmpty(LR) && "LR cannot be empty because it has a height >= 1");
511 TreeTy *LRL = getLeft(LR);
512 TreeTy *LRR = getRight(LR);
514 return createNode(createNode(LL,L,LRL), LR, createNode(LRR,V,R));
516 else if (hr > hl + 2) {
517 assert(!isEmpty(R) && "Right tree cannot be empty to have a height >= 2");
519 TreeTy *RL = getLeft(R);
520 TreeTy *RR = getRight(R);
522 if (getHeight(RR) >= getHeight(RL))
523 return createNode(createNode(L,V,RL), R, RR);
525 assert(!isEmpty(RL) && "RL cannot be empty because it has a height >= 1");
527 TreeTy *RLL = getLeft(RL);
528 TreeTy *RLR = getRight(RL);
530 return createNode(createNode(L,V,RLL), RL, createNode(RLR,R,RR));
533 return createNode(L,V,R);
536 /// add_internal - Creates a new tree that includes the specified
537 /// data and the data from the original tree. If the original tree
538 /// already contained the data item, the original tree is returned.
539 TreeTy* add_internal(value_type_ref V, TreeTy* T) {
541 return createNode(T, V, T);
542 assert(!T->isMutable());
544 key_type_ref K = ImutInfo::KeyOfValue(V);
545 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
547 if (ImutInfo::isEqual(K,KCurrent))
548 return createNode(getLeft(T), V, getRight(T));
549 else if (ImutInfo::isLess(K,KCurrent))
550 return balanceTree(add_internal(V, getLeft(T)), getValue(T), getRight(T));
552 return balanceTree(getLeft(T), getValue(T), add_internal(V, getRight(T)));
555 /// remove_internal - Creates a new tree that includes all the data
556 /// from the original tree except the specified data. If the
557 /// specified data did not exist in the original tree, the original
558 /// tree is returned.
559 TreeTy* remove_internal(key_type_ref K, TreeTy* T) {
563 assert(!T->isMutable());
565 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
567 if (ImutInfo::isEqual(K,KCurrent)) {
568 return combineTrees(getLeft(T), getRight(T));
569 } else if (ImutInfo::isLess(K,KCurrent)) {
570 return balanceTree(remove_internal(K, getLeft(T)),
571 getValue(T), getRight(T));
573 return balanceTree(getLeft(T), getValue(T),
574 remove_internal(K, getRight(T)));
578 TreeTy* combineTrees(TreeTy* L, TreeTy* R) {
584 TreeTy* newRight = removeMinBinding(R,OldNode);
585 return balanceTree(L, getValue(OldNode), newRight);
588 TreeTy* removeMinBinding(TreeTy* T, TreeTy*& Noderemoved) {
590 if (isEmpty(getLeft(T))) {
594 return balanceTree(removeMinBinding(getLeft(T), Noderemoved),
595 getValue(T), getRight(T));
598 /// markImmutable - Clears the mutable bits of a root and all of its
600 void markImmutable(TreeTy* T) {
601 if (!T || !T->isMutable())
604 markImmutable(getLeft(T));
605 markImmutable(getRight(T));
609 TreeTy *getCanonicalTree(TreeTy *TNew) {
613 if (TNew->IsCanonicalized)
616 // Search the hashtable for another tree with the same digest, and
617 // if find a collision compare those trees by their contents.
618 unsigned digest = TNew->computeDigest();
619 TreeTy *&entry = Cache[maskCacheIndex(digest)];
623 for (TreeTy *T = entry ; T != 0; T = T->next) {
624 // Compare the Contents('T') with Contents('TNew')
625 typename TreeTy::iterator TI = T->begin(), TE = T->end();
626 if (!compareTreeWithSection(TNew, TI, TE))
629 continue; // T has more contents than TNew.
630 // Trees did match! Return 'T'.
631 if (TNew->refCount == 0)
641 TNew->IsCanonicalized = true;
646 //===----------------------------------------------------------------------===//
647 // Immutable AVL-Tree Iterators.
648 //===----------------------------------------------------------------------===//
650 template <typename ImutInfo>
651 class ImutAVLTreeGenericIterator {
652 SmallVector<uintptr_t,20> stack;
654 enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
657 typedef ImutAVLTree<ImutInfo> TreeTy;
658 typedef ImutAVLTreeGenericIterator<ImutInfo> _Self;
660 inline ImutAVLTreeGenericIterator() {}
661 inline ImutAVLTreeGenericIterator(const TreeTy* Root) {
662 if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
665 TreeTy* operator*() const {
666 assert(!stack.empty());
667 return reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
670 uintptr_t getVisitState() {
671 assert(!stack.empty());
672 return stack.back() & Flags;
676 bool atEnd() const { return stack.empty(); }
678 bool atBeginning() const {
679 return stack.size() == 1 && getVisitState() == VisitedNone;
682 void skipToParent() {
683 assert(!stack.empty());
687 switch (getVisitState()) {
689 stack.back() |= VisitedLeft;
692 stack.back() |= VisitedRight;
695 llvm_unreachable("Unreachable.");
699 inline bool operator==(const _Self& x) const {
700 if (stack.size() != x.stack.size())
702 for (unsigned i = 0 ; i < stack.size(); i++)
703 if (stack[i] != x.stack[i])
708 inline bool operator!=(const _Self& x) const { return !operator==(x); }
710 _Self& operator++() {
711 assert(!stack.empty());
712 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
714 switch (getVisitState()) {
716 if (TreeTy* L = Current->getLeft())
717 stack.push_back(reinterpret_cast<uintptr_t>(L));
719 stack.back() |= VisitedLeft;
722 if (TreeTy* R = Current->getRight())
723 stack.push_back(reinterpret_cast<uintptr_t>(R));
725 stack.back() |= VisitedRight;
731 llvm_unreachable("Unreachable.");
736 _Self& operator--() {
737 assert(!stack.empty());
738 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
740 switch (getVisitState()) {
745 stack.back() &= ~Flags; // Set state to "VisitedNone."
746 if (TreeTy* L = Current->getLeft())
747 stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
750 stack.back() &= ~Flags;
751 stack.back() |= VisitedLeft;
752 if (TreeTy* R = Current->getRight())
753 stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
756 llvm_unreachable("Unreachable.");
762 template <typename ImutInfo>
763 class ImutAVLTreeInOrderIterator {
764 typedef ImutAVLTreeGenericIterator<ImutInfo> InternalIteratorTy;
765 InternalIteratorTy InternalItr;
768 typedef ImutAVLTree<ImutInfo> TreeTy;
769 typedef ImutAVLTreeInOrderIterator<ImutInfo> _Self;
771 ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
772 if (Root) operator++(); // Advance to first element.
775 ImutAVLTreeInOrderIterator() : InternalItr() {}
777 inline bool operator==(const _Self& x) const {
778 return InternalItr == x.InternalItr;
781 inline bool operator!=(const _Self& x) const { return !operator==(x); }
783 inline TreeTy* operator*() const { return *InternalItr; }
784 inline TreeTy* operator->() const { return *InternalItr; }
786 inline _Self& operator++() {
788 while (!InternalItr.atEnd() &&
789 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
794 inline _Self& operator--() {
796 while (!InternalItr.atBeginning() &&
797 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
802 inline void skipSubTree() {
803 InternalItr.skipToParent();
805 while (!InternalItr.atEnd() &&
806 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
811 //===----------------------------------------------------------------------===//
812 // Trait classes for Profile information.
813 //===----------------------------------------------------------------------===//
815 /// Generic profile template. The default behavior is to invoke the
816 /// profile method of an object. Specializations for primitive integers
817 /// and generic handling of pointers is done below.
818 template <typename T>
819 struct ImutProfileInfo {
820 typedef const T value_type;
821 typedef const T& value_type_ref;
823 static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
824 FoldingSetTrait<T>::Profile(X,ID);
828 /// Profile traits for integers.
829 template <typename T>
830 struct ImutProfileInteger {
831 typedef const T value_type;
832 typedef const T& value_type_ref;
834 static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
839 #define PROFILE_INTEGER_INFO(X)\
840 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
842 PROFILE_INTEGER_INFO(char)
843 PROFILE_INTEGER_INFO(unsigned char)
844 PROFILE_INTEGER_INFO(short)
845 PROFILE_INTEGER_INFO(unsigned short)
846 PROFILE_INTEGER_INFO(unsigned)
847 PROFILE_INTEGER_INFO(signed)
848 PROFILE_INTEGER_INFO(long)
849 PROFILE_INTEGER_INFO(unsigned long)
850 PROFILE_INTEGER_INFO(long long)
851 PROFILE_INTEGER_INFO(unsigned long long)
853 #undef PROFILE_INTEGER_INFO
855 /// Generic profile trait for pointer types. We treat pointers as
856 /// references to unique objects.
857 template <typename T>
858 struct ImutProfileInfo<T*> {
859 typedef const T* value_type;
860 typedef value_type value_type_ref;
862 static inline void Profile(FoldingSetNodeID &ID, value_type_ref X) {
867 //===----------------------------------------------------------------------===//
868 // Trait classes that contain element comparison operators and type
869 // definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap. These
870 // inherit from the profile traits (ImutProfileInfo) to include operations
871 // for element profiling.
872 //===----------------------------------------------------------------------===//
875 /// ImutContainerInfo - Generic definition of comparison operations for
876 /// elements of immutable containers that defaults to using
877 /// std::equal_to<> and std::less<> to perform comparison of elements.
878 template <typename T>
879 struct ImutContainerInfo : public ImutProfileInfo<T> {
880 typedef typename ImutProfileInfo<T>::value_type value_type;
881 typedef typename ImutProfileInfo<T>::value_type_ref value_type_ref;
882 typedef value_type key_type;
883 typedef value_type_ref key_type_ref;
884 typedef bool data_type;
885 typedef bool data_type_ref;
887 static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
888 static inline data_type_ref DataOfValue(value_type_ref) { return true; }
890 static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
891 return std::equal_to<key_type>()(LHS,RHS);
894 static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
895 return std::less<key_type>()(LHS,RHS);
898 static inline bool isDataEqual(data_type_ref,data_type_ref) { return true; }
901 /// ImutContainerInfo - Specialization for pointer values to treat pointers
902 /// as references to unique objects. Pointers are thus compared by
904 template <typename T>
905 struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
906 typedef typename ImutProfileInfo<T*>::value_type value_type;
907 typedef typename ImutProfileInfo<T*>::value_type_ref value_type_ref;
908 typedef value_type key_type;
909 typedef value_type_ref key_type_ref;
910 typedef bool data_type;
911 typedef bool data_type_ref;
913 static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
914 static inline data_type_ref DataOfValue(value_type_ref) { return true; }
916 static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
920 static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
924 static inline bool isDataEqual(data_type_ref,data_type_ref) { return true; }
927 //===----------------------------------------------------------------------===//
929 //===----------------------------------------------------------------------===//
931 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT> >
934 typedef typename ValInfo::value_type value_type;
935 typedef typename ValInfo::value_type_ref value_type_ref;
936 typedef ImutAVLTree<ValInfo> TreeTy;
942 /// Constructs a set from a pointer to a tree root. In general one
943 /// should use a Factory object to create sets instead of directly
944 /// invoking the constructor, but there are cases where make this
945 /// constructor public is useful.
946 explicit ImmutableSet(TreeTy* R) : Root(R) {
947 if (Root) { Root->retain(); }
949 ImmutableSet(const ImmutableSet &X) : Root(X.Root) {
950 if (Root) { Root->retain(); }
952 ImmutableSet &operator=(const ImmutableSet &X) {
953 if (Root != X.Root) {
954 if (X.Root) { X.Root->retain(); }
955 if (Root) { Root->release(); }
961 if (Root) { Root->release(); }
965 typename TreeTy::Factory F;
966 const bool Canonicalize;
969 Factory(bool canonicalize = true)
970 : Canonicalize(canonicalize) {}
972 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
973 : F(Alloc), Canonicalize(canonicalize) {}
975 /// getEmptySet - Returns an immutable set that contains no elements.
976 ImmutableSet getEmptySet() {
977 return ImmutableSet(F.getEmptyTree());
980 /// add - Creates a new immutable set that contains all of the values
981 /// of the original set with the addition of the specified value. If
982 /// the original set already included the value, then the original set is
983 /// returned and no memory is allocated. The time and space complexity
984 /// of this operation is logarithmic in the size of the original set.
985 /// The memory allocated to represent the set is released when the
986 /// factory object that created the set is destroyed.
987 ImmutableSet add(ImmutableSet Old, value_type_ref V) {
988 TreeTy *NewT = F.add(Old.Root, V);
989 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
992 /// remove - Creates a new immutable set that contains all of the values
993 /// of the original set with the exception of the specified value. If
994 /// the original set did not contain the value, the original set is
995 /// returned and no memory is allocated. The time and space complexity
996 /// of this operation is logarithmic in the size of the original set.
997 /// The memory allocated to represent the set is released when the
998 /// factory object that created the set is destroyed.
999 ImmutableSet remove(ImmutableSet Old, value_type_ref V) {
1000 TreeTy *NewT = F.remove(Old.Root, V);
1001 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1004 BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
1006 typename TreeTy::Factory *getTreeFactory() const {
1007 return const_cast<typename TreeTy::Factory *>(&F);
1011 Factory(const Factory& RHS); // DO NOT IMPLEMENT
1012 void operator=(const Factory& RHS); // DO NOT IMPLEMENT
1015 friend class Factory;
1017 /// Returns true if the set contains the specified value.
1018 bool contains(value_type_ref V) const {
1019 return Root ? Root->contains(V) : false;
1022 bool operator==(const ImmutableSet &RHS) const {
1023 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
1026 bool operator!=(const ImmutableSet &RHS) const {
1027 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
1031 if (Root) { Root->retain(); }
1035 TreeTy *getRootWithoutRetain() const {
1039 /// isEmpty - Return true if the set contains no elements.
1040 bool isEmpty() const { return !Root; }
1042 /// isSingleton - Return true if the set contains exactly one element.
1043 /// This method runs in constant time.
1044 bool isSingleton() const { return getHeight() == 1; }
1046 template <typename Callback>
1047 void foreach(Callback& C) { if (Root) Root->foreach(C); }
1049 template <typename Callback>
1050 void foreach() { if (Root) { Callback C; Root->foreach(C); } }
1052 //===--------------------------------------------------===//
1054 //===--------------------------------------------------===//
1057 typename TreeTy::iterator itr;
1058 iterator(TreeTy* t) : itr(t) {}
1059 friend class ImmutableSet<ValT,ValInfo>;
1062 inline value_type_ref operator*() const { return itr->getValue(); }
1063 inline iterator& operator++() { ++itr; return *this; }
1064 inline iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
1065 inline iterator& operator--() { --itr; return *this; }
1066 inline iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
1067 inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
1068 inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
1069 inline value_type *operator->() const { return &(operator*()); }
1072 iterator begin() const { return iterator(Root); }
1073 iterator end() const { return iterator(); }
1075 //===--------------------------------------------------===//
1077 //===--------------------------------------------------===//
1079 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1081 static inline void Profile(FoldingSetNodeID& ID, const ImmutableSet& S) {
1082 ID.AddPointer(S.Root);
1085 inline void Profile(FoldingSetNodeID& ID) const {
1086 return Profile(ID,*this);
1089 //===--------------------------------------------------===//
1091 //===--------------------------------------------------===//
1093 void validateTree() const { if (Root) Root->validateTree(); }
1096 // NOTE: This may some day replace the current ImmutableSet.
1097 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT> >
1098 class ImmutableSetRef {
1100 typedef typename ValInfo::value_type value_type;
1101 typedef typename ValInfo::value_type_ref value_type_ref;
1102 typedef ImutAVLTree<ValInfo> TreeTy;
1103 typedef typename TreeTy::Factory FactoryTy;
1110 /// Constructs a set from a pointer to a tree root. In general one
1111 /// should use a Factory object to create sets instead of directly
1112 /// invoking the constructor, but there are cases where make this
1113 /// constructor public is useful.
1114 explicit ImmutableSetRef(TreeTy* R, FactoryTy *F)
1117 if (Root) { Root->retain(); }
1119 ImmutableSetRef(const ImmutableSetRef &X)
1121 Factory(X.Factory) {
1122 if (Root) { Root->retain(); }
1124 ImmutableSetRef &operator=(const ImmutableSetRef &X) {
1125 if (Root != X.Root) {
1126 if (X.Root) { X.Root->retain(); }
1127 if (Root) { Root->release(); }
1129 Factory = X.Factory;
1133 ~ImmutableSetRef() {
1134 if (Root) { Root->release(); }
1137 static inline ImmutableSetRef getEmptySet(FactoryTy *F) {
1138 return ImmutableSetRef(0, F);
1141 ImmutableSetRef add(value_type_ref V) {
1142 return ImmutableSetRef(Factory->add(Root, V), Factory);
1145 ImmutableSetRef remove(value_type_ref V) {
1146 return ImmutableSetRef(Factory->remove(Root, V), Factory);
1149 /// Returns true if the set contains the specified value.
1150 bool contains(value_type_ref V) const {
1151 return Root ? Root->contains(V) : false;
1154 ImmutableSet<ValT> asImmutableSet(bool canonicalize = true) const {
1155 return ImmutableSet<ValT>(canonicalize ?
1156 Factory->getCanonicalTree(Root) : Root);
1159 TreeTy *getRootWithoutRetain() const {
1163 bool operator==(const ImmutableSetRef &RHS) const {
1164 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
1167 bool operator!=(const ImmutableSetRef &RHS) const {
1168 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
1171 /// isEmpty - Return true if the set contains no elements.
1172 bool isEmpty() const { return !Root; }
1174 /// isSingleton - Return true if the set contains exactly one element.
1175 /// This method runs in constant time.
1176 bool isSingleton() const { return getHeight() == 1; }
1178 //===--------------------------------------------------===//
1180 //===--------------------------------------------------===//
1183 typename TreeTy::iterator itr;
1184 iterator(TreeTy* t) : itr(t) {}
1185 friend class ImmutableSetRef<ValT,ValInfo>;
1188 inline value_type_ref operator*() const { return itr->getValue(); }
1189 inline iterator& operator++() { ++itr; return *this; }
1190 inline iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
1191 inline iterator& operator--() { --itr; return *this; }
1192 inline iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
1193 inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
1194 inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
1195 inline value_type *operator->() const { return &(operator*()); }
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 inline void Profile(FoldingSetNodeID& ID, const ImmutableSetRef& S) {
1208 ID.AddPointer(S.Root);
1211 inline void Profile(FoldingSetNodeID& ID) const {
1212 return Profile(ID,*this);
1215 //===--------------------------------------------------===//
1217 //===--------------------------------------------------===//
1219 void validateTree() const { if (Root) Root->validateTree(); }
1222 } // end namespace llvm