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/FoldingSet.h"
19 #include "llvm/Support/DataTypes.h"
25 //===----------------------------------------------------------------------===//
26 // Immutable AVL-Tree Definition.
27 //===----------------------------------------------------------------------===//
29 template <typename ImutInfo> class ImutAVLFactory;
30 template <typename ImutInfo> class ImutIntervalAVLFactory;
31 template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
32 template <typename ImutInfo> class ImutAVLTreeGenericIterator;
34 template <typename ImutInfo >
35 class ImutAVLTree : public FoldingSetNode {
37 typedef typename ImutInfo::key_type_ref key_type_ref;
38 typedef typename ImutInfo::value_type value_type;
39 typedef typename ImutInfo::value_type_ref value_type_ref;
41 typedef ImutAVLFactory<ImutInfo> Factory;
42 friend class ImutAVLFactory<ImutInfo>;
43 friend class ImutIntervalAVLFactory<ImutInfo>;
45 friend class ImutAVLTreeGenericIterator<ImutInfo>;
46 friend class FoldingSet<ImutAVLTree>;
48 typedef ImutAVLTreeInOrderIterator<ImutInfo> iterator;
50 //===----------------------------------------------------===//
52 //===----------------------------------------------------===//
54 /// getLeft - Returns a pointer to the left subtree. This value
55 /// is NULL if there is no left subtree.
56 ImutAVLTree *getLeft() const { return left; }
58 /// getRight - Returns a pointer to the right subtree. This value is
59 /// NULL if there is no right subtree.
60 ImutAVLTree *getRight() const { return right; }
62 /// getHeight - Returns the height of the tree. A tree with no subtrees
63 /// has a height of 1.
64 unsigned getHeight() const { return height; }
66 /// getValue - Returns the data value associated with the tree node.
67 const value_type& getValue() const { return value; }
69 /// find - Finds the subtree associated with the specified key value.
70 /// This method returns NULL if no matching subtree is found.
71 ImutAVLTree* find(key_type_ref K) {
72 ImutAVLTree *T = this;
74 key_type_ref CurrentKey = ImutInfo::KeyOfValue(T->getValue());
75 if (ImutInfo::isEqual(K,CurrentKey))
77 else if (ImutInfo::isLess(K,CurrentKey))
85 /// getMaxElement - Find the subtree associated with the highest ranged
87 ImutAVLTree* getMaxElement() {
88 ImutAVLTree *T = this;
89 ImutAVLTree *Right = T->getRight();
90 while (Right) { T = right; right = T->getRight(); }
94 /// size - Returns the number of nodes in the tree, which includes
95 /// both leaves and non-leaf nodes.
96 unsigned size() const {
98 if (const ImutAVLTree* L = getLeft())
100 if (const ImutAVLTree* R = getRight())
105 /// begin - Returns an iterator that iterates over the nodes of the tree
106 /// in an inorder traversal. The returned iterator thus refers to the
107 /// the tree node with the minimum data element.
108 iterator begin() const { return iterator(this); }
110 /// end - Returns an iterator for the tree that denotes the end of an
111 /// inorder traversal.
112 iterator end() const { return iterator(); }
114 bool isElementEqual(value_type_ref V) const {
116 if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(getValue()),
117 ImutInfo::KeyOfValue(V)))
120 // Also compare the data values.
121 if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(getValue()),
122 ImutInfo::DataOfValue(V)))
128 bool isElementEqual(const ImutAVLTree* RHS) const {
129 return isElementEqual(RHS->getValue());
132 /// isEqual - Compares two trees for structural equality and returns true
133 /// if they are equal. This worst case performance of this operation is
134 // linear in the sizes of the trees.
135 bool isEqual(const ImutAVLTree& RHS) const {
139 iterator LItr = begin(), LEnd = end();
140 iterator RItr = RHS.begin(), REnd = RHS.end();
142 while (LItr != LEnd && RItr != REnd) {
143 if (*LItr == *RItr) {
149 if (!LItr->isElementEqual(*RItr))
156 return LItr == LEnd && RItr == REnd;
159 /// isNotEqual - Compares two trees for structural inequality. Performance
160 /// is the same is isEqual.
161 bool isNotEqual(const ImutAVLTree& RHS) const { return !isEqual(RHS); }
163 /// contains - Returns true if this tree contains a subtree (node) that
164 /// has an data element that matches the specified key. Complexity
165 /// is logarithmic in the size of the tree.
166 bool contains(key_type_ref K) { return (bool) find(K); }
168 /// foreach - A member template the accepts invokes operator() on a functor
169 /// object (specifed by Callback) for every node/subtree in the tree.
170 /// Nodes are visited using an inorder traversal.
171 template <typename Callback>
172 void foreach(Callback& C) {
173 if (ImutAVLTree* L = getLeft())
178 if (ImutAVLTree* R = getRight())
182 /// validateTree - A utility method that checks that the balancing and
183 /// ordering invariants of the tree are satisifed. It is a recursive
184 /// method that returns the height of the tree, which is then consumed
185 /// by the enclosing validateTree call. External callers should ignore the
186 /// return value. An invalid tree will cause an assertion to fire in
188 unsigned validateTree() const {
189 unsigned HL = getLeft() ? getLeft()->validateTree() : 0;
190 unsigned HR = getRight() ? getRight()->validateTree() : 0;
194 assert(getHeight() == ( HL > HR ? HL : HR ) + 1
195 && "Height calculation wrong");
197 assert((HL > HR ? HL-HR : HR-HL) <= 2
198 && "Balancing invariant violated");
200 assert((!getLeft() ||
201 ImutInfo::isLess(ImutInfo::KeyOfValue(getLeft()->getValue()),
202 ImutInfo::KeyOfValue(getValue()))) &&
203 "Value in left child is not less that current value");
206 assert(!(getRight() ||
207 ImutInfo::isLess(ImutInfo::KeyOfValue(getValue()),
208 ImutInfo::KeyOfValue(getRight()->getValue()))) &&
209 "Current value is not less that value of right child");
214 /// Profile - Profiling for ImutAVLTree.
215 void Profile(llvm::FoldingSetNodeID& ID) {
216 ID.AddInteger(computeDigest());
219 //===----------------------------------------------------===//
221 //===----------------------------------------------------===//
226 unsigned height : 28;
227 unsigned IsMutable : 1;
228 unsigned IsDigestCached : 1;
232 //===----------------------------------------------------===//
233 // Internal methods (node manipulation; used by Factory).
234 //===----------------------------------------------------===//
237 /// ImutAVLTree - Internal constructor that is only called by
239 ImutAVLTree(ImutAVLTree* l, ImutAVLTree* r, value_type_ref v,
241 : left(l), right(r), height(height), IsMutable(true),
242 IsDigestCached(false), value(v), digest(0) {}
244 /// isMutable - Returns true if the left and right subtree references
245 /// (as well as height) can be changed. If this method returns false,
246 /// the tree is truly immutable. Trees returned from an ImutAVLFactory
247 /// object should always have this method return true. Further, if this
248 /// method returns false for an instance of ImutAVLTree, all subtrees
249 /// will also have this method return false. The converse is not true.
250 bool isMutable() const { return IsMutable; }
252 /// hasCachedDigest - Returns true if the digest for this tree is cached.
253 /// This can only be true if the tree is immutable.
254 bool hasCachedDigest() const { return IsDigestCached; }
256 //===----------------------------------------------------===//
257 // Mutating operations. A tree root can be manipulated as
258 // long as its reference has not "escaped" from internal
259 // methods of a factory object (see below). When a tree
260 // pointer is externally viewable by client code, the
261 // internal "mutable bit" is cleared to mark the tree
262 // immutable. Note that a tree that still has its mutable
263 // bit set may have children (subtrees) that are themselves
265 //===----------------------------------------------------===//
267 /// markImmutable - Clears the mutable flag for a tree. After this happens,
268 /// it is an error to call setLeft(), setRight(), and setHeight().
269 void markImmutable() {
270 assert(isMutable() && "Mutable flag already removed.");
274 /// markedCachedDigest - Clears the NoCachedDigest flag for a tree.
275 void markedCachedDigest() {
276 assert(!hasCachedDigest() && "NoCachedDigest flag already removed.");
277 IsDigestCached = true;
280 /// setLeft - Changes the reference of the left subtree. Used internally
281 /// by ImutAVLFactory.
282 void setLeft(ImutAVLTree* NewLeft) {
283 assert(isMutable() &&
284 "Only a mutable tree can have its left subtree changed.");
286 IsDigestCached = false;
289 /// setRight - Changes the reference of the right subtree. Used internally
290 /// by ImutAVLFactory.
291 void setRight(ImutAVLTree* newRight) {
292 assert(isMutable() &&
293 "Only a mutable tree can have its right subtree changed.");
296 IsDigestCached = false;
299 /// setHeight - Changes the height of the tree. Used internally by
301 void setHeight(unsigned h) {
302 assert(isMutable() && "Only a mutable tree can have its height changed.");
307 uint32_t computeDigest(ImutAVLTree* L, ImutAVLTree* R, value_type_ref V) {
311 digest += L->computeDigest();
313 // Compute digest of stored data.
315 ImutInfo::Profile(ID,V);
316 digest += ID.ComputeHash();
319 digest += R->computeDigest();
324 inline uint32_t computeDigest() {
325 // Check the lowest bit to determine if digest has actually been
327 if (hasCachedDigest())
330 uint32_t X = computeDigest(getLeft(), getRight(), getValue());
332 markedCachedDigest();
337 //===----------------------------------------------------------------------===//
338 // Immutable AVL-Tree Factory class.
339 //===----------------------------------------------------------------------===//
341 template <typename ImutInfo >
342 class ImutAVLFactory {
343 typedef ImutAVLTree<ImutInfo> TreeTy;
344 typedef typename TreeTy::value_type_ref value_type_ref;
345 typedef typename TreeTy::key_type_ref key_type_ref;
347 typedef FoldingSet<TreeTy> CacheTy;
352 bool ownsAllocator() const {
353 return Allocator & 0x1 ? false : true;
356 BumpPtrAllocator& getAllocator() const {
357 return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
360 //===--------------------------------------------------===//
362 //===--------------------------------------------------===//
366 : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
368 ImutAVLFactory(BumpPtrAllocator& Alloc)
369 : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
372 if (ownsAllocator()) delete &getAllocator();
375 TreeTy* add(TreeTy* T, value_type_ref V) {
376 T = add_internal(V,T);
381 TreeTy* remove(TreeTy* T, key_type_ref V) {
382 T = remove_internal(V,T);
387 TreeTy* getEmptyTree() const { return NULL; }
391 //===--------------------------------------------------===//
392 // A bunch of quick helper functions used for reasoning
393 // about the properties of trees and their children.
394 // These have succinct names so that the balancing code
395 // is as terse (and readable) as possible.
396 //===--------------------------------------------------===//
398 bool isEmpty(TreeTy* T) const { return !T; }
399 unsigned getHeight(TreeTy* T) const { return T ? T->getHeight() : 0; }
400 TreeTy* getLeft(TreeTy* T) const { return T->getLeft(); }
401 TreeTy* getRight(TreeTy* T) const { return T->getRight(); }
402 value_type_ref getValue(TreeTy* T) const { return T->value; }
404 unsigned incrementHeight(TreeTy* L, TreeTy* R) const {
405 unsigned hl = getHeight(L);
406 unsigned hr = getHeight(R);
407 return (hl > hr ? hl : hr) + 1;
410 static bool compareTreeWithSection(TreeTy* T,
411 typename TreeTy::iterator& TI,
412 typename TreeTy::iterator& TE) {
413 typename TreeTy::iterator I = T->begin(), E = T->end();
414 for ( ; I!=E ; ++I, ++TI) {
415 if (TI == TE || !I->isElementEqual(*TI))
421 //===--------------------------------------------------===//
422 // "createNode" is used to generate new tree roots that link
423 // to other trees. The functon may also simply move links
424 // in an existing root if that root is still marked mutable.
425 // This is necessary because otherwise our balancing code
426 // would leak memory as it would create nodes that are
427 // then discarded later before the finished tree is
428 // returned to the caller.
429 //===--------------------------------------------------===//
431 TreeTy* createNode(TreeTy* L, value_type_ref V, TreeTy* R) {
432 BumpPtrAllocator& A = getAllocator();
433 TreeTy* T = (TreeTy*) A.Allocate<TreeTy>();
434 new (T) TreeTy(L, R, V, incrementHeight(L,R));
438 TreeTy* createNode(TreeTy* newLeft, TreeTy* oldTree, TreeTy* newRight) {
439 assert(!isEmpty(oldTree));
440 if (oldTree->isMutable()) {
441 oldTree->setLeft(newLeft);
442 oldTree->setRight(newRight);
443 oldTree->setHeight(incrementHeight(newLeft, newRight));
447 return createNode(newLeft, getValue(oldTree), newRight);
450 /// balanceTree - Used by add_internal and remove_internal to
451 /// balance a newly created tree.
452 TreeTy* balanceTree(TreeTy* L, value_type_ref V, TreeTy* R) {
453 unsigned hl = getHeight(L);
454 unsigned hr = getHeight(R);
457 assert(!isEmpty(L) && "Left tree cannot be empty to have a height >= 2");
459 TreeTy *LL = getLeft(L);
460 TreeTy *LR = getRight(L);
462 if (getHeight(LL) >= getHeight(LR))
463 return createNode(LL, L, createNode(LR,V,R));
465 assert(!isEmpty(LR) && "LR cannot be empty because it has a height >= 1");
467 TreeTy *LRL = getLeft(LR);
468 TreeTy *LRR = getRight(LR);
470 return createNode(createNode(LL,L,LRL), LR, createNode(LRR,V,R));
472 else if (hr > hl + 2) {
473 assert(!isEmpty(R) && "Right tree cannot be empty to have a height >= 2");
475 TreeTy *RL = getLeft(R);
476 TreeTy *RR = getRight(R);
478 if (getHeight(RR) >= getHeight(RL))
479 return createNode(createNode(L,V,RL), R, RR);
481 assert(!isEmpty(RL) && "RL cannot be empty because it has a height >= 1");
483 TreeTy *RLL = getLeft(RL);
484 TreeTy *RLR = getRight(RL);
486 return createNode(createNode(L,V,RLL), RL, createNode(RLR,R,RR));
489 return createNode(L,V,R);
492 /// add_internal - Creates a new tree that includes the specified
493 /// data and the data from the original tree. If the original tree
494 /// already contained the data item, the original tree is returned.
495 TreeTy* add_internal(value_type_ref V, TreeTy* T) {
497 return createNode(T, V, T);
498 assert(!T->isMutable());
500 key_type_ref K = ImutInfo::KeyOfValue(V);
501 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
503 if (ImutInfo::isEqual(K,KCurrent))
504 return createNode(getLeft(T), V, getRight(T));
505 else if (ImutInfo::isLess(K,KCurrent))
506 return balanceTree(add_internal(V, getLeft(T)), getValue(T), getRight(T));
508 return balanceTree(getLeft(T), getValue(T), add_internal(V, getRight(T)));
511 /// remove_internal - Creates a new tree that includes all the data
512 /// from the original tree except the specified data. If the
513 /// specified data did not exist in the original tree, the original
514 /// tree is returned.
515 TreeTy* remove_internal(key_type_ref K, TreeTy* T) {
519 assert(!T->isMutable());
521 key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
523 if (ImutInfo::isEqual(K,KCurrent)) {
524 return combineTrees(getLeft(T), getRight(T));
525 } else if (ImutInfo::isLess(K,KCurrent)) {
526 return balanceTree(remove_internal(K, getLeft(T)),
527 getValue(T), getRight(T));
529 return balanceTree(getLeft(T), getValue(T),
530 remove_internal(K, getRight(T)));
534 TreeTy* combineTrees(TreeTy* L, TreeTy* R) {
540 TreeTy* newRight = removeMinBinding(R,OldNode);
541 return balanceTree(L, getValue(OldNode), newRight);
544 TreeTy* removeMinBinding(TreeTy* T, TreeTy*& Noderemoved) {
546 if (isEmpty(getLeft(T))) {
550 return balanceTree(removeMinBinding(getLeft(T), Noderemoved),
551 getValue(T), getRight(T));
554 /// markImmutable - Clears the mutable bits of a root and all of its
556 void markImmutable(TreeTy* T) {
557 if (!T || !T->isMutable())
560 markImmutable(getLeft(T));
561 markImmutable(getRight(T));
565 TreeTy *getCanonicalTree(TreeTy *TNew) {
569 // Search the FoldingSet bucket for a Tree with the same digest.
571 unsigned digest = TNew->computeDigest();
572 ID.AddInteger(digest);
573 unsigned hash = ID.ComputeHash();
575 typename CacheTy::bucket_iterator I = Cache.bucket_begin(hash);
576 typename CacheTy::bucket_iterator E = Cache.bucket_end(hash);
578 for (; I != E; ++I) {
581 if (T->computeDigest() != digest)
584 // We found a collision. Perform a comparison of Contents('T')
585 // with Contents('TNew')
586 typename TreeTy::iterator TI = T->begin(), TE = T->end();
588 if (!compareTreeWithSection(TNew, TI, TE))
592 continue; // T has more contents than TNew.
594 // Trees did match! Return 'T'.
598 // 'TNew' is the only tree of its kind. Return it.
599 Cache.InsertNode(TNew, (void*) &*Cache.bucket_end(hash));
605 //===----------------------------------------------------------------------===//
606 // Immutable AVL-Tree Iterators.
607 //===----------------------------------------------------------------------===//
609 template <typename ImutInfo>
610 class ImutAVLTreeGenericIterator {
611 SmallVector<uintptr_t,20> stack;
613 enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
616 typedef ImutAVLTree<ImutInfo> TreeTy;
617 typedef ImutAVLTreeGenericIterator<ImutInfo> _Self;
619 inline ImutAVLTreeGenericIterator() {}
620 inline ImutAVLTreeGenericIterator(const TreeTy* Root) {
621 if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
624 TreeTy* operator*() const {
625 assert(!stack.empty());
626 return reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
629 uintptr_t getVisitState() {
630 assert(!stack.empty());
631 return stack.back() & Flags;
635 bool atEnd() const { return stack.empty(); }
637 bool atBeginning() const {
638 return stack.size() == 1 && getVisitState() == VisitedNone;
641 void skipToParent() {
642 assert(!stack.empty());
646 switch (getVisitState()) {
648 stack.back() |= VisitedLeft;
651 stack.back() |= VisitedRight;
654 assert(false && "Unreachable.");
658 inline bool operator==(const _Self& x) const {
659 if (stack.size() != x.stack.size())
661 for (unsigned i = 0 ; i < stack.size(); i++)
662 if (stack[i] != x.stack[i])
667 inline bool operator!=(const _Self& x) const { return !operator==(x); }
669 _Self& operator++() {
670 assert(!stack.empty());
671 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
673 switch (getVisitState()) {
675 if (TreeTy* L = Current->getLeft())
676 stack.push_back(reinterpret_cast<uintptr_t>(L));
678 stack.back() |= VisitedLeft;
681 if (TreeTy* R = Current->getRight())
682 stack.push_back(reinterpret_cast<uintptr_t>(R));
684 stack.back() |= VisitedRight;
690 assert(false && "Unreachable.");
695 _Self& operator--() {
696 assert(!stack.empty());
697 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
699 switch (getVisitState()) {
704 stack.back() &= ~Flags; // Set state to "VisitedNone."
705 if (TreeTy* L = Current->getLeft())
706 stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
709 stack.back() &= ~Flags;
710 stack.back() |= VisitedLeft;
711 if (TreeTy* R = Current->getRight())
712 stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
715 assert(false && "Unreachable.");
721 template <typename ImutInfo>
722 class ImutAVLTreeInOrderIterator {
723 typedef ImutAVLTreeGenericIterator<ImutInfo> InternalIteratorTy;
724 InternalIteratorTy InternalItr;
727 typedef ImutAVLTree<ImutInfo> TreeTy;
728 typedef ImutAVLTreeInOrderIterator<ImutInfo> _Self;
730 ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
731 if (Root) operator++(); // Advance to first element.
734 ImutAVLTreeInOrderIterator() : InternalItr() {}
736 inline bool operator==(const _Self& x) const {
737 return InternalItr == x.InternalItr;
740 inline bool operator!=(const _Self& x) const { return !operator==(x); }
742 inline TreeTy* operator*() const { return *InternalItr; }
743 inline TreeTy* operator->() const { return *InternalItr; }
745 inline _Self& operator++() {
747 while (!InternalItr.atEnd() &&
748 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
753 inline _Self& operator--() {
755 while (!InternalItr.atBeginning() &&
756 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
761 inline void skipSubTree() {
762 InternalItr.skipToParent();
764 while (!InternalItr.atEnd() &&
765 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
770 //===----------------------------------------------------------------------===//
771 // Trait classes for Profile information.
772 //===----------------------------------------------------------------------===//
774 /// Generic profile template. The default behavior is to invoke the
775 /// profile method of an object. Specializations for primitive integers
776 /// and generic handling of pointers is done below.
777 template <typename T>
778 struct ImutProfileInfo {
779 typedef const T value_type;
780 typedef const T& value_type_ref;
782 static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
783 FoldingSetTrait<T>::Profile(X,ID);
787 /// Profile traits for integers.
788 template <typename T>
789 struct ImutProfileInteger {
790 typedef const T value_type;
791 typedef const T& value_type_ref;
793 static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
798 #define PROFILE_INTEGER_INFO(X)\
799 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
801 PROFILE_INTEGER_INFO(char)
802 PROFILE_INTEGER_INFO(unsigned char)
803 PROFILE_INTEGER_INFO(short)
804 PROFILE_INTEGER_INFO(unsigned short)
805 PROFILE_INTEGER_INFO(unsigned)
806 PROFILE_INTEGER_INFO(signed)
807 PROFILE_INTEGER_INFO(long)
808 PROFILE_INTEGER_INFO(unsigned long)
809 PROFILE_INTEGER_INFO(long long)
810 PROFILE_INTEGER_INFO(unsigned long long)
812 #undef PROFILE_INTEGER_INFO
814 /// Generic profile trait for pointer types. We treat pointers as
815 /// references to unique objects.
816 template <typename T>
817 struct ImutProfileInfo<T*> {
818 typedef const T* value_type;
819 typedef value_type value_type_ref;
821 static inline void Profile(FoldingSetNodeID &ID, value_type_ref X) {
826 //===----------------------------------------------------------------------===//
827 // Trait classes that contain element comparison operators and type
828 // definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap. These
829 // inherit from the profile traits (ImutProfileInfo) to include operations
830 // for element profiling.
831 //===----------------------------------------------------------------------===//
834 /// ImutContainerInfo - Generic definition of comparison operations for
835 /// elements of immutable containers that defaults to using
836 /// std::equal_to<> and std::less<> to perform comparison of elements.
837 template <typename T>
838 struct ImutContainerInfo : public ImutProfileInfo<T> {
839 typedef typename ImutProfileInfo<T>::value_type value_type;
840 typedef typename ImutProfileInfo<T>::value_type_ref value_type_ref;
841 typedef value_type key_type;
842 typedef value_type_ref key_type_ref;
843 typedef bool data_type;
844 typedef bool data_type_ref;
846 static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
847 static inline data_type_ref DataOfValue(value_type_ref) { return true; }
849 static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
850 return std::equal_to<key_type>()(LHS,RHS);
853 static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
854 return std::less<key_type>()(LHS,RHS);
857 static inline bool isDataEqual(data_type_ref,data_type_ref) { return true; }
860 /// ImutContainerInfo - Specialization for pointer values to treat pointers
861 /// as references to unique objects. Pointers are thus compared by
863 template <typename T>
864 struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
865 typedef typename ImutProfileInfo<T*>::value_type value_type;
866 typedef typename ImutProfileInfo<T*>::value_type_ref value_type_ref;
867 typedef value_type key_type;
868 typedef value_type_ref key_type_ref;
869 typedef bool data_type;
870 typedef bool data_type_ref;
872 static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
873 static inline data_type_ref DataOfValue(value_type_ref) { return true; }
875 static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
879 static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
883 static inline bool isDataEqual(data_type_ref,data_type_ref) { return true; }
886 //===----------------------------------------------------------------------===//
888 //===----------------------------------------------------------------------===//
890 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT> >
893 typedef typename ValInfo::value_type value_type;
894 typedef typename ValInfo::value_type_ref value_type_ref;
895 typedef ImutAVLTree<ValInfo> TreeTy;
901 /// Constructs a set from a pointer to a tree root. In general one
902 /// should use a Factory object to create sets instead of directly
903 /// invoking the constructor, but there are cases where make this
904 /// constructor public is useful.
905 explicit ImmutableSet(TreeTy* R) : Root(R) {}
908 typename TreeTy::Factory F;
909 const bool Canonicalize;
912 Factory(bool canonicalize = true)
913 : Canonicalize(canonicalize) {}
915 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
916 : F(Alloc), Canonicalize(canonicalize) {}
918 /// getEmptySet - Returns an immutable set that contains no elements.
919 ImmutableSet getEmptySet() {
920 return ImmutableSet(F.getEmptyTree());
923 /// add - Creates a new immutable set that contains all of the values
924 /// of the original set with the addition of the specified value. If
925 /// the original set already included the value, then the original set is
926 /// returned and no memory is allocated. The time and space complexity
927 /// of this operation is logarithmic in the size of the original set.
928 /// The memory allocated to represent the set is released when the
929 /// factory object that created the set is destroyed.
930 ImmutableSet add(ImmutableSet Old, value_type_ref V) {
931 TreeTy *NewT = F.add(Old.Root, V);
932 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
935 /// remove - Creates a new immutable set that contains all of the values
936 /// of the original set with the exception of the specified value. If
937 /// the original set did not contain the value, the original set is
938 /// returned and no memory is allocated. The time and space complexity
939 /// of this operation is logarithmic in the size of the original set.
940 /// The memory allocated to represent the set is released when the
941 /// factory object that created the set is destroyed.
942 ImmutableSet remove(ImmutableSet Old, value_type_ref V) {
943 TreeTy *NewT = F.remove(Old.Root, V);
944 return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
947 BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
950 Factory(const Factory& RHS); // DO NOT IMPLEMENT
951 void operator=(const Factory& RHS); // DO NOT IMPLEMENT
954 friend class Factory;
956 /// contains - Returns true if the set contains the specified value.
957 bool contains(value_type_ref V) const {
958 return Root ? Root->contains(V) : false;
961 bool operator==(ImmutableSet RHS) const {
962 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
965 bool operator!=(ImmutableSet RHS) const {
966 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
973 /// isEmpty - Return true if the set contains no elements.
974 bool isEmpty() const { return !Root; }
976 /// isSingleton - Return true if the set contains exactly one element.
977 /// This method runs in constant time.
978 bool isSingleton() const { return getHeight() == 1; }
980 template <typename Callback>
981 void foreach(Callback& C) { if (Root) Root->foreach(C); }
983 template <typename Callback>
984 void foreach() { if (Root) { Callback C; Root->foreach(C); } }
986 //===--------------------------------------------------===//
988 //===--------------------------------------------------===//
991 typename TreeTy::iterator itr;
992 iterator(TreeTy* t) : itr(t) {}
993 friend class ImmutableSet<ValT,ValInfo>;
996 inline value_type_ref operator*() const { return itr->getValue(); }
997 inline iterator& operator++() { ++itr; return *this; }
998 inline iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
999 inline iterator& operator--() { --itr; return *this; }
1000 inline iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
1001 inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
1002 inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
1003 inline value_type *operator->() const { return &(operator*()); }
1006 iterator begin() const { return iterator(Root); }
1007 iterator end() const { return iterator(); }
1009 //===--------------------------------------------------===//
1011 //===--------------------------------------------------===//
1013 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1015 static inline void Profile(FoldingSetNodeID& ID, const ImmutableSet& S) {
1016 ID.AddPointer(S.Root);
1019 inline void Profile(FoldingSetNodeID& ID) const {
1020 return Profile(ID,*this);
1023 //===--------------------------------------------------===//
1025 //===--------------------------------------------------===//
1027 void validateTree() const { if (Root) Root->validateTree(); }
1030 } // end namespace llvm