1 //===--- ImmutableSet.h - Immutable (functional) set interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Ted Kremenek and is distributed under
6 // the University of Illinois Open Source 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"
23 //===----------------------------------------------------------------------===//
24 // Immutable AVL-Tree Definition.
25 //===----------------------------------------------------------------------===//
27 template <typename ImutInfo> class ImutAVLFactory;
29 template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
31 template <typename ImutInfo >
32 class ImutAVLTree : public FoldingSetNode {
34 typedef typename ImutInfo::key_type_ref key_type_ref;
35 typedef typename ImutInfo::value_type value_type;
36 typedef typename ImutInfo::value_type_ref value_type_ref;
38 typedef ImutAVLFactory<ImutInfo> Factory;
39 friend class ImutAVLFactory<ImutInfo>;
41 typedef ImutAVLTreeInOrderIterator<ImutInfo> iterator;
43 //===----------------------------------------------------===//
45 //===----------------------------------------------------===//
47 /// getLeft - Returns a pointer to the left subtree. This value
48 /// is NULL if there is no left subtree.
49 ImutAVLTree* getLeft() const {
50 assert (!isMutable() && "Node is incorrectly marked mutable.");
52 return reinterpret_cast<ImutAVLTree*>(Left);
55 /// getRight - Returns a pointer to the right subtree. This value is
56 /// NULL if there is no right subtree.
57 ImutAVLTree* getRight() const { return Right; }
60 /// getHeight - Returns the height of the tree. A tree with no subtrees
61 /// has a height of 1.
62 unsigned getHeight() const { return Height; }
64 /// getValue - Returns the data value associated with the tree node.
65 const value_type& getValue() const { return Value; }
67 /// find - Finds the subtree associated with the specified key value.
68 /// This method returns NULL if no matching subtree is found.
69 ImutAVLTree* find(key_type_ref K) {
70 ImutAVLTree *T = this;
73 key_type_ref CurrentKey = ImutInfo::KeyOfValue(T->getValue());
75 if (ImutInfo::isEqual(K,CurrentKey))
77 else if (ImutInfo::isLess(K,CurrentKey))
86 /// size - Returns the number of nodes in the tree, which includes
87 /// both leaves and non-leaf nodes.
88 unsigned size() const {
91 if (const ImutAVLTree* L = getLeft()) n += L->size();
92 if (const ImutAVLTree* R = getRight()) n += R->size();
97 /// begin - Returns an iterator that iterates over the nodes of the tree
98 /// in an inorder traversal. The returned iterator thus refers to the
99 /// the tree node with the minimum data element.
100 iterator begin() const { return iterator(this); }
102 /// end - Returns an iterator for the tree that denotes the end of an
103 /// inorder traversal.
104 iterator end() const { return iterator(); }
106 /// isEqual - Compares two trees for structural equality and returns true
107 /// if they are equal. This worst case performance of this operation is
108 // linear in the sizes of the trees.
109 bool isEqual(const ImutAVLTree& RHS) const {
113 iterator LItr = begin(), LEnd = end();
114 iterator RItr = RHS.begin(), REnd = RHS.end();
116 while (LItr != LEnd && RItr != REnd) {
117 if (*LItr == *RItr) {
123 // FIXME: need to compare data values, not key values, but our
124 // traits don't support this yet.
125 if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(LItr->getValue()),
126 ImutInfo::KeyOfValue(RItr->getValue())))
133 return LItr == LEnd && RItr == REnd;
136 /// isNotEqual - Compares two trees for structural inequality. Performance
137 /// is the same is isEqual.
138 bool isNotEqual(const ImutAVLTree& RHS) const { return !isEqual(RHS); }
140 /// contains - Returns true if this tree contains a subtree (node) that
141 /// has an data element that matches the specified key. Complexity
142 /// is logarithmic in the size of the tree.
143 bool contains(const key_type_ref K) { return (bool) find(K); }
145 /// foreach - A member template the accepts invokes operator() on a functor
146 /// object (specifed by Callback) for every node/subtree in the tree.
147 /// Nodes are visited using an inorder traversal.
148 template <typename Callback>
149 void foreach(Callback& C) {
150 if (ImutAVLTree* L = getLeft()) L->foreach(C);
154 if (ImutAVLTree* R = getRight()) R->foreach(C);
157 /// verify - A utility method that checks that the balancing and
158 /// ordering invariants of the tree are satisifed. It is a recursive
159 /// method that returns the height of the tree, which is then consumed
160 /// by the enclosing verify call. External callers should ignore the
161 /// return value. An invalid tree will cause an assertion to fire in
163 unsigned verify() const {
164 unsigned HL = getLeft() ? getLeft()->verify() : 0;
165 unsigned HR = getRight() ? getRight()->verify() : 0;
167 assert (getHeight() == ( HL > HR ? HL : HR ) + 1
168 && "Height calculation wrong.");
170 assert ((HL > HR ? HL-HR : HR-HL) <= 2
171 && "Balancing invariant violated.");
175 || ImutInfo::isLess(ImutInfo::KeyOfValue(getLeft()->getValue()),
176 ImutInfo::KeyOfValue(getValue()))
177 && "Value in left child is not less that current value.");
181 || ImutInfo::isLess(ImutInfo::KeyOfValue(getValue()),
182 ImutInfo::KeyOfValue(getRight()->getValue()))
183 && "Current value is not less that value of right child.");
188 //===----------------------------------------------------===//
190 //===----------------------------------------------------===//
198 //===----------------------------------------------------===//
199 // Profiling or FoldingSet.
200 //===----------------------------------------------------===//
204 /// Profile - Generates a FoldingSet profile for a tree node before it is
205 /// created. This is used by the ImutAVLFactory when creating
208 void Profile(FoldingSetNodeID& ID, ImutAVLTree* L, ImutAVLTree* R,
212 ImutInfo::Profile(ID,V);
217 /// Profile - Generates a FoldingSet profile for an existing tree node.
218 void Profile(FoldingSetNodeID& ID) {
219 Profile(ID,getSafeLeft(),getRight(),getValue());
222 //===----------------------------------------------------===//
223 // Internal methods (node manipulation; used by Factory).
224 //===----------------------------------------------------===//
228 enum { Mutable = 0x1 };
230 /// ImutAVLTree - Internal constructor that is only called by
232 ImutAVLTree(ImutAVLTree* l, ImutAVLTree* r, value_type_ref v, unsigned height)
233 : Left(reinterpret_cast<uintptr_t>(l) | Mutable),
234 Right(r), Height(height), Value(v) {}
237 /// isMutable - Returns true if the left and right subtree references
238 /// (as well as height) can be changed. If this method returns false,
239 /// the tree is truly immutable. Trees returned from an ImutAVLFactory
240 /// object should always have this method return true. Further, if this
241 /// method returns false for an instance of ImutAVLTree, all subtrees
242 /// will also have this method return false. The converse is not true.
243 bool isMutable() const { return Left & Mutable; }
245 /// getSafeLeft - Returns the pointer to the left tree by always masking
246 /// out the mutable bit. This is used internally by ImutAVLFactory,
247 /// as no trees returned to the client should have the mutable flag set.
248 ImutAVLTree* getSafeLeft() const {
249 return reinterpret_cast<ImutAVLTree*>(Left & ~Mutable);
252 //===----------------------------------------------------===//
253 // Mutating operations. A tree root can be manipulated as
254 // long as its reference has not "escaped" from internal
255 // methods of a factory object (see below). When a tree
256 // pointer is externally viewable by client code, the
257 // internal "mutable bit" is cleared to mark the tree
258 // immutable. Note that a tree that still has its mutable
259 // bit set may have children (subtrees) that are themselves
261 //===----------------------------------------------------===//
264 /// MarkImmutable - Clears the mutable flag for a tree. After this happens,
265 /// it is an error to call setLeft(), setRight(), and setHeight(). It
266 /// is also then safe to call getLeft() instead of getSafeLeft().
268 assert (isMutable() && "Mutable flag already removed.");
272 /// setLeft - Changes the reference of the left subtree. Used internally
273 /// by ImutAVLFactory.
274 void setLeft(ImutAVLTree* NewLeft) {
275 assert (isMutable() &&
276 "Only a mutable tree can have its left subtree changed.");
278 Left = reinterpret_cast<uintptr_t>(NewLeft) | Mutable;
281 /// setRight - Changes the reference of the right subtree. Used internally
282 /// by ImutAVLFactory.
283 void setRight(ImutAVLTree* NewRight) {
284 assert (isMutable() &&
285 "Only a mutable tree can have its right subtree changed.");
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 //===----------------------------------------------------------------------===//
299 // Immutable AVL-Tree Factory class.
300 //===----------------------------------------------------------------------===//
302 template <typename ImutInfo >
303 class ImutAVLFactory {
304 typedef ImutAVLTree<ImutInfo> TreeTy;
305 typedef typename TreeTy::value_type_ref value_type_ref;
306 typedef typename TreeTy::key_type_ref key_type_ref;
308 typedef FoldingSet<TreeTy> CacheTy;
311 BumpPtrAllocator Allocator;
313 //===--------------------------------------------------===//
315 //===--------------------------------------------------===//
320 TreeTy* Add(TreeTy* T, value_type_ref V) {
321 T = Add_internal(V,T);
326 TreeTy* Remove(TreeTy* T, key_type_ref V) {
327 T = Remove_internal(V,T);
332 TreeTy* GetEmptyTree() const { return NULL; }
334 BumpPtrAllocator& getAllocator() { return Allocator; }
336 //===--------------------------------------------------===//
337 // A bunch of quick helper functions used for reasoning
338 // about the properties of trees and their children.
339 // These have succinct names so that the balancing code
340 // is as terse (and readable) as possible.
341 //===--------------------------------------------------===//
344 bool isEmpty(TreeTy* T) const { return !T; }
345 unsigned Height(TreeTy* T) const { return T ? T->getHeight() : 0; }
346 TreeTy* Left(TreeTy* T) const { return T->getSafeLeft(); }
347 TreeTy* Right(TreeTy* T) const { return T->getRight(); }
348 value_type_ref Value(TreeTy* T) const { return T->Value; }
350 unsigned IncrementHeight(TreeTy* L, TreeTy* R) const {
351 unsigned hl = Height(L);
352 unsigned hr = Height(R);
353 return ( hl > hr ? hl : hr ) + 1;
356 //===--------------------------------------------------===//
357 // "CreateNode" is used to generate new tree roots that link
358 // to other trees. The functon may also simply move links
359 // in an existing root if that root is still marked mutable.
360 // This is necessary because otherwise our balancing code
361 // would leak memory as it would create nodes that are
362 // then discarded later before the finished tree is
363 // returned to the caller.
364 //===--------------------------------------------------===//
366 TreeTy* CreateNode(TreeTy* L, value_type_ref V, TreeTy* R) {
368 TreeTy::Profile(ID,L,R,V);
371 if (TreeTy* T = Cache.FindNodeOrInsertPos(ID,InsertPos))
374 assert (InsertPos != NULL);
376 // FIXME: more intelligent calculation of alignment.
377 TreeTy* T = (TreeTy*) Allocator.Allocate(sizeof(*T),16);
379 new (T) TreeTy(L,R,V,IncrementHeight(L,R));
381 Cache.InsertNode(T,InsertPos);
385 TreeTy* CreateNode(TreeTy* L, TreeTy* OldTree, TreeTy* R) {
386 assert (!isEmpty(OldTree));
388 if (OldTree->isMutable()) {
390 OldTree->setRight(R);
391 OldTree->setHeight(IncrementHeight(L,R));
394 else return CreateNode(L, Value(OldTree), R);
397 /// Balance - Used by Add_internal and Remove_internal to
398 /// balance a newly created tree.
399 TreeTy* Balance(TreeTy* L, value_type_ref V, TreeTy* R) {
401 unsigned hl = Height(L);
402 unsigned hr = Height(R);
405 assert (!isEmpty(L) &&
406 "Left tree cannot be empty to have a height >= 2.");
408 TreeTy* LL = Left(L);
409 TreeTy* LR = Right(L);
411 if (Height(LL) >= Height(LR))
412 return CreateNode(LL, L, CreateNode(LR,V,R));
414 assert (!isEmpty(LR) &&
415 "LR cannot be empty because it has a height >= 1.");
417 TreeTy* LRL = Left(LR);
418 TreeTy* LRR = Right(LR);
420 return CreateNode(CreateNode(LL,L,LRL), LR, CreateNode(LRR,V,R));
422 else if (hr > hl + 2) {
423 assert (!isEmpty(R) &&
424 "Right tree cannot be empty to have a height >= 2.");
426 TreeTy* RL = Left(R);
427 TreeTy* RR = Right(R);
429 if (Height(RR) >= Height(RL))
430 return CreateNode(CreateNode(L,V,RL), R, RR);
432 assert (!isEmpty(RL) &&
433 "RL cannot be empty because it has a height >= 1.");
435 TreeTy* RLL = Left(RL);
436 TreeTy* RLR = Right(RL);
438 return CreateNode(CreateNode(L,V,RLL), RL, CreateNode(RLR,R,RR));
441 return CreateNode(L,V,R);
444 /// Add_internal - Creates a new tree that includes the specified
445 /// data and the data from the original tree. If the original tree
446 /// already contained the data item, the original tree is returned.
447 TreeTy* Add_internal(value_type_ref V, TreeTy* T) {
449 return CreateNode(T, V, T);
451 assert (!T->isMutable());
453 key_type_ref K = ImutInfo::KeyOfValue(V);
454 key_type_ref KCurrent = ImutInfo::KeyOfValue(Value(T));
456 if (ImutInfo::isEqual(K,KCurrent))
457 return CreateNode(Left(T), V, Right(T));
458 else if (ImutInfo::isLess(K,KCurrent))
459 return Balance(Add_internal(V,Left(T)), Value(T), Right(T));
461 return Balance(Left(T), Value(T), Add_internal(V,Right(T)));
464 /// Remove_interal - Creates a new tree that includes all the data
465 /// from the original tree except the specified data. If the
466 /// specified data did not exist in the original tree, the original
467 /// tree is returned.
468 TreeTy* Remove_internal(key_type_ref K, TreeTy* T) {
472 assert (!T->isMutable());
474 key_type_ref KCurrent = ImutInfo::KeyOfValue(Value(T));
476 if (ImutInfo::isEqual(K,KCurrent))
477 return CombineLeftRightTrees(Left(T),Right(T));
478 else if (ImutInfo::isLess(K,KCurrent))
479 return Balance(Remove_internal(K,Left(T)), Value(T), Right(T));
481 return Balance(Left(T), Value(T), Remove_internal(K,Right(T)));
484 TreeTy* CombineLeftRightTrees(TreeTy* L, TreeTy* R) {
485 if (isEmpty(L)) return R;
486 if (isEmpty(R)) return L;
489 TreeTy* NewRight = RemoveMinBinding(R,OldNode);
490 return Balance(L,Value(OldNode),NewRight);
493 TreeTy* RemoveMinBinding(TreeTy* T, TreeTy*& NodeRemoved) {
494 assert (!isEmpty(T));
496 if (isEmpty(Left(T))) {
501 return Balance(RemoveMinBinding(Left(T),NodeRemoved),Value(T),Right(T));
504 /// MarkImmutable - Clears the mutable bits of a root and all of its
506 void MarkImmutable(TreeTy* T) {
507 if (!T || !T->isMutable())
511 MarkImmutable(Left(T));
512 MarkImmutable(Right(T));
517 //===----------------------------------------------------------------------===//
518 // Immutable AVL-Tree Iterators.
519 //===----------------------------------------------------------------------===//
521 template <typename ImutInfo>
522 class ImutAVLTreeGenericIterator {
523 SmallVector<uintptr_t,20> stack;
525 enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
528 typedef ImutAVLTree<ImutInfo> TreeTy;
529 typedef ImutAVLTreeGenericIterator<ImutInfo> _Self;
531 inline ImutAVLTreeGenericIterator() {}
532 inline ImutAVLTreeGenericIterator(const TreeTy* Root) {
533 if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
536 TreeTy* operator*() const {
537 assert (!stack.empty());
538 return reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
541 uintptr_t getVisitState() {
542 assert (!stack.empty());
543 return stack.back() & Flags;
547 bool AtEnd() const { return stack.empty(); }
549 bool AtBeginning() const {
550 return stack.size() == 1 && getVisitState() == VisitedNone;
553 void SkipToParent() {
554 assert (!stack.empty());
560 switch (getVisitState()) {
562 stack.back() |= VisitedLeft;
565 stack.back() |= VisitedRight;
568 assert (false && "Unreachable.");
572 inline bool operator==(const _Self& x) const {
573 if (stack.size() != x.stack.size())
576 for (unsigned i = 0 ; i < stack.size(); i++)
577 if (stack[i] != x.stack[i])
583 inline bool operator!=(const _Self& x) const { return !operator==(x); }
585 _Self& operator++() {
586 assert (!stack.empty());
588 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
591 switch (getVisitState()) {
593 if (TreeTy* L = Current->getLeft())
594 stack.push_back(reinterpret_cast<uintptr_t>(L));
596 stack.back() |= VisitedLeft;
601 if (TreeTy* R = Current->getRight())
602 stack.push_back(reinterpret_cast<uintptr_t>(R));
604 stack.back() |= VisitedRight;
613 assert (false && "Unreachable.");
619 _Self& operator--() {
620 assert (!stack.empty());
622 TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
625 switch (getVisitState()) {
631 stack.back() &= ~Flags; // Set state to "VisitedNone."
633 if (TreeTy* L = Current->getLeft())
634 stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
639 stack.back() &= ~Flags;
640 stack.back() |= VisitedLeft;
642 if (TreeTy* R = Current->getRight())
643 stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
648 assert (false && "Unreachable.");
655 template <typename ImutInfo>
656 class ImutAVLTreeInOrderIterator {
657 typedef ImutAVLTreeGenericIterator<ImutInfo> InternalIteratorTy;
658 InternalIteratorTy InternalItr;
661 typedef ImutAVLTree<ImutInfo> TreeTy;
662 typedef ImutAVLTreeInOrderIterator<ImutInfo> _Self;
664 ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
665 if (Root) operator++(); // Advance to first element.
668 ImutAVLTreeInOrderIterator() : InternalItr() {}
670 inline bool operator==(const _Self& x) const {
671 return InternalItr == x.InternalItr;
674 inline bool operator!=(const _Self& x) const { return !operator==(x); }
676 inline TreeTy* operator*() const { return *InternalItr; }
677 inline TreeTy* operator->() const { return *InternalItr; }
679 inline _Self& operator++() {
681 while (!InternalItr.AtEnd() &&
682 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
687 inline _Self& operator--() {
689 while (!InternalItr.AtBeginning() &&
690 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
695 inline void SkipSubTree() {
696 InternalItr.SkipToParent();
698 while (!InternalItr.AtEnd() &&
699 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
704 //===----------------------------------------------------------------------===//
705 // Trait classes for Profile information.
706 //===----------------------------------------------------------------------===//
708 /// Generic profile template. The default behavior is to invoke the
709 /// profile method of an object. Specializations for primitive integers
710 /// and generic handling of pointers is done below.
711 template <typename T>
712 struct ImutProfileInfo {
713 typedef const T value_type;
714 typedef const T& value_type_ref;
716 static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
721 /// Profile traits for integers.
722 template <typename T>
723 struct ImutProfileInteger {
724 typedef const T value_type;
725 typedef const T& value_type_ref;
727 static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
732 #define PROFILE_INTEGER_INFO(X)\
733 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
735 PROFILE_INTEGER_INFO(char)
736 PROFILE_INTEGER_INFO(unsigned char)
737 PROFILE_INTEGER_INFO(short)
738 PROFILE_INTEGER_INFO(unsigned short)
739 PROFILE_INTEGER_INFO(unsigned)
740 PROFILE_INTEGER_INFO(signed)
741 PROFILE_INTEGER_INFO(long)
742 PROFILE_INTEGER_INFO(unsigned long)
743 PROFILE_INTEGER_INFO(long long)
744 PROFILE_INTEGER_INFO(unsigned long long)
746 #undef PROFILE_INTEGER_INFO
748 /// Generic profile trait for pointer types. We treat pointers as
749 /// references to unique objects.
750 template <typename T>
751 struct ImutProfileInfo<T*> {
752 typedef const T* value_type;
753 typedef value_type value_type_ref;
755 static inline void Profile(FoldingSetNodeID &ID, value_type_ref X) {
760 //===----------------------------------------------------------------------===//
761 // Trait classes that contain element comparison operators and type
762 // definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap. These
763 // inherit from the profile traits (ImutProfileInfo) to include operations
764 // for element profiling.
765 //===----------------------------------------------------------------------===//
768 /// ImutContainerInfo - Generic definition of comparison operations for
769 /// elements of immutable containers that defaults to using
770 /// std::equal_to<> and std::less<> to perform comparison of elements.
771 template <typename T>
772 struct ImutContainerInfo : public ImutProfileInfo<T> {
773 typedef typename ImutProfileInfo<T>::value_type value_type;
774 typedef typename ImutProfileInfo<T>::value_type_ref value_type_ref;
775 typedef value_type key_type;
776 typedef value_type_ref key_type_ref;
778 static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
780 static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
781 return std::equal_to<key_type>()(LHS,RHS);
784 static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
785 return std::less<key_type>()(LHS,RHS);
789 /// ImutContainerInfo - Specialization for pointer values to treat pointers
790 /// as references to unique objects. Pointers are thus compared by
792 template <typename T>
793 struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
794 typedef typename ImutProfileInfo<T*>::value_type value_type;
795 typedef typename ImutProfileInfo<T*>::value_type_ref value_type_ref;
796 typedef value_type key_type;
797 typedef value_type_ref key_type_ref;
799 static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
801 static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
805 static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
810 //===----------------------------------------------------------------------===//
812 //===----------------------------------------------------------------------===//
814 template <typename ValT, typename ValInfo = ImutContainerInfo<ValT> >
817 typedef typename ValInfo::value_type value_type;
818 typedef typename ValInfo::value_type_ref value_type_ref;
821 typedef ImutAVLTree<ValInfo> TreeTy;
824 ImmutableSet(TreeTy* R) : Root(R) {}
829 typename TreeTy::Factory F;
834 /// GetEmptySet - Returns an immutable set that contains no elements.
835 ImmutableSet GetEmptySet() { return ImmutableSet(F.GetEmptyTree()); }
837 /// Add - Creates a new immutable set that contains all of the values
838 /// of the original set with the addition of the specified value. If
839 /// the original set already included the value, then the original set is
840 /// returned and no memory is allocated. The time and space complexity
841 /// of this operation is logarithmic in the size of the original set.
842 /// The memory allocated to represent the set is released when the
843 /// factory object that created the set is destroyed.
844 ImmutableSet Add(ImmutableSet Old, value_type_ref V) {
845 return ImmutableSet(F.Add(Old.Root,V));
848 /// Remove - Creates a new immutable set that contains all of the values
849 /// of the original set with the exception of the specified value. If
850 /// the original set did not contain the value, the original set is
851 /// returned and no memory is allocated. The time and space complexity
852 /// of this operation is logarithmic in the size of the original set.
853 /// The memory allocated to represent the set is released when the
854 /// factory object that created the set is destroyed.
855 ImmutableSet Remove(ImmutableSet Old, value_type_ref V) {
856 return ImmutableSet(F.Remove(Old.Root,V));
859 BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
862 Factory(const Factory& RHS) {};
863 void operator=(const Factory& RHS) {};
866 friend class Factory;
868 /// contains - Returns true if the set contains the specified value.
869 bool contains(const value_type_ref V) const {
870 return Root ? Root->contains(V) : false;
873 bool operator==(ImmutableSet RHS) const {
874 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
877 bool operator!=(ImmutableSet RHS) const {
878 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
881 /// isEmpty - Return true if the set contains no elements.
882 bool isEmpty() const { return !Root; }
884 template <typename Callback>
885 void foreach(Callback& C) { if (Root) Root->foreach(C); }
887 template <typename Callback>
888 void foreach() { if (Root) { Callback C; Root->foreach(C); } }
890 //===--------------------------------------------------===//
892 //===--------------------------------------------------===//
895 typename TreeTy::iterator itr;
898 iterator(TreeTy* t) : itr(t) {}
899 friend class ImmutableSet<ValT,ValInfo>;
901 inline value_type_ref operator*() const { return itr->getValue(); }
902 inline iterator& operator++() { ++itr; return *this; }
903 inline iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
904 inline iterator& operator--() { --itr; return *this; }
905 inline iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
906 inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
907 inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
910 iterator begin() const { return iterator(Root); }
911 iterator end() const { return iterator(); }
913 //===--------------------------------------------------===//
915 //===--------------------------------------------------===//
917 void verify() const { if (Root) Root->verify(); }
918 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
921 } // end namespace llvm