2007-10-09 21:38:09 +00:00
|
|
|
//===--- ImmutableSet.h - Immutable (functional) set interface --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-09 21:38:09 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ImutAVLTree and ImmutableSet classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_IMSET_H
|
|
|
|
#define LLVM_ADT_IMSET_H
|
|
|
|
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2008-02-11 23:11:12 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2007-10-09 21:38:09 +00:00
|
|
|
#include <cassert>
|
2008-02-22 10:11:21 +00:00
|
|
|
#include <functional>
|
2007-10-09 21:38:09 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Immutable AVL-Tree Definition.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <typename ImutInfo> class ImutAVLFactory;
|
2007-10-10 18:11:16 +00:00
|
|
|
template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
|
2008-02-04 21:15:24 +00:00
|
|
|
template <typename ImutInfo> class ImutAVLTreeGenericIterator;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename ImutInfo >
|
|
|
|
class ImutAVLTree : public FoldingSetNode {
|
|
|
|
public:
|
|
|
|
typedef typename ImutInfo::key_type_ref key_type_ref;
|
|
|
|
typedef typename ImutInfo::value_type value_type;
|
|
|
|
typedef typename ImutInfo::value_type_ref value_type_ref;
|
2007-10-10 18:11:16 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
typedef ImutAVLFactory<ImutInfo> Factory;
|
|
|
|
friend class ImutAVLFactory<ImutInfo>;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
friend class ImutAVLTreeGenericIterator<ImutInfo>;
|
|
|
|
friend class FoldingSet<ImutAVLTree>;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
typedef ImutAVLTreeInOrderIterator<ImutInfo> iterator;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Public Interface.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===----------------------------------------------------===//
|
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// getLeft - Returns a pointer to the left subtree. This value
|
|
|
|
/// is NULL if there is no left subtree.
|
2009-07-09 18:34:41 +00:00
|
|
|
ImutAVLTree *getLeft() const {
|
|
|
|
return reinterpret_cast<ImutAVLTree*>(Left & ~LeftFlags);
|
2007-10-10 20:44:21 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// getRight - Returns a pointer to the right subtree. This value is
|
|
|
|
/// NULL if there is no right subtree.
|
2009-01-09 19:25:42 +00:00
|
|
|
ImutAVLTree* getRight() const { return Right; }
|
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// getHeight - Returns the height of the tree. A tree with no subtrees
|
|
|
|
/// has a height of 1.
|
2009-01-09 19:25:42 +00:00
|
|
|
unsigned getHeight() const { return Height; }
|
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// getValue - Returns the data value associated with the tree node.
|
2007-10-09 21:38:09 +00:00
|
|
|
const value_type& getValue() const { return Value; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// find - Finds the subtree associated with the specified key value.
|
|
|
|
/// This method returns NULL if no matching subtree is found.
|
2007-10-09 21:38:09 +00:00
|
|
|
ImutAVLTree* find(key_type_ref K) {
|
|
|
|
ImutAVLTree *T = this;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
while (T) {
|
2007-10-10 16:27:33 +00:00
|
|
|
key_type_ref CurrentKey = ImutInfo::KeyOfValue(T->getValue());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (ImutInfo::isEqual(K,CurrentKey))
|
|
|
|
return T;
|
|
|
|
else if (ImutInfo::isLess(K,CurrentKey))
|
|
|
|
T = T->getLeft();
|
|
|
|
else
|
|
|
|
T = T->getRight();
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-23 17:27:18 +00:00
|
|
|
|
|
|
|
/// getMaxElement - Find the subtree associated with the highest ranged
|
|
|
|
/// key value.
|
|
|
|
ImutAVLTree* getMaxElement() {
|
|
|
|
ImutAVLTree *T = this;
|
|
|
|
ImutAVLTree *Right = T->getRight();
|
|
|
|
while (Right) { T = Right; Right = T->getRight(); }
|
|
|
|
return T;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// size - Returns the number of nodes in the tree, which includes
|
|
|
|
/// both leaves and non-leaf nodes.
|
2007-10-09 21:38:09 +00:00
|
|
|
unsigned size() const {
|
|
|
|
unsigned n = 1;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (const ImutAVLTree* L = getLeft()) n += L->size();
|
|
|
|
if (const ImutAVLTree* R = getRight()) n += R->size();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
return n;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// begin - Returns an iterator that iterates over the nodes of the tree
|
|
|
|
/// in an inorder traversal. The returned iterator thus refers to the
|
|
|
|
/// the tree node with the minimum data element.
|
2007-10-10 18:11:16 +00:00
|
|
|
iterator begin() const { return iterator(this); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// end - Returns an iterator for the tree that denotes the end of an
|
|
|
|
/// inorder traversal.
|
2007-10-10 18:11:16 +00:00
|
|
|
iterator end() const { return iterator(); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
bool ElementEqual(value_type_ref V) const {
|
|
|
|
// Compare the keys.
|
|
|
|
if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(getValue()),
|
|
|
|
ImutInfo::KeyOfValue(V)))
|
|
|
|
return false;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// Also compare the data values.
|
|
|
|
if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(getValue()),
|
|
|
|
ImutInfo::DataOfValue(V)))
|
|
|
|
return false;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
bool ElementEqual(const ImutAVLTree* RHS) const {
|
|
|
|
return ElementEqual(RHS->getValue());
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// isEqual - Compares two trees for structural equality and returns true
|
|
|
|
/// if they are equal. This worst case performance of this operation is
|
|
|
|
// linear in the sizes of the trees.
|
2007-10-09 21:38:09 +00:00
|
|
|
bool isEqual(const ImutAVLTree& RHS) const {
|
2007-10-10 18:11:16 +00:00
|
|
|
if (&RHS == this)
|
|
|
|
return true;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
iterator LItr = begin(), LEnd = end();
|
|
|
|
iterator RItr = RHS.begin(), REnd = RHS.end();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
while (LItr != LEnd && RItr != REnd) {
|
|
|
|
if (*LItr == *RItr) {
|
|
|
|
LItr.SkipSubTree();
|
|
|
|
RItr.SkipSubTree();
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
if (!LItr->ElementEqual(*RItr))
|
2008-01-17 17:36:49 +00:00
|
|
|
return false;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
++LItr;
|
|
|
|
++RItr;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
return LItr == LEnd && RItr == REnd;
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2007-10-10 20:44:21 +00:00
|
|
|
|
|
|
|
/// isNotEqual - Compares two trees for structural inequality. Performance
|
|
|
|
/// is the same is isEqual.
|
2007-10-09 21:38:09 +00:00
|
|
|
bool isNotEqual(const ImutAVLTree& RHS) const { return !isEqual(RHS); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// contains - Returns true if this tree contains a subtree (node) that
|
|
|
|
/// has an data element that matches the specified key. Complexity
|
|
|
|
/// is logarithmic in the size of the tree.
|
2009-07-19 01:42:34 +00:00
|
|
|
bool contains(key_type_ref K) { return (bool) find(K); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// foreach - A member template the accepts invokes operator() on a functor
|
|
|
|
/// object (specifed by Callback) for every node/subtree in the tree.
|
|
|
|
/// Nodes are visited using an inorder traversal.
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void foreach(Callback& C) {
|
|
|
|
if (ImutAVLTree* L = getLeft()) L->foreach(C);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
C(Value);
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (ImutAVLTree* R = getRight()) R->foreach(C);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 20:44:21 +00:00
|
|
|
/// verify - A utility method that checks that the balancing and
|
|
|
|
/// ordering invariants of the tree are satisifed. It is a recursive
|
|
|
|
/// method that returns the height of the tree, which is then consumed
|
|
|
|
/// by the enclosing verify call. External callers should ignore the
|
|
|
|
/// return value. An invalid tree will cause an assertion to fire in
|
|
|
|
/// a debug build.
|
2007-10-09 21:38:09 +00:00
|
|
|
unsigned verify() const {
|
|
|
|
unsigned HL = getLeft() ? getLeft()->verify() : 0;
|
|
|
|
unsigned HR = getRight() ? getRight()->verify() : 0;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
assert (getHeight() == ( HL > HR ? HL : HR ) + 1
|
2007-10-09 21:38:09 +00:00
|
|
|
&& "Height calculation wrong.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert ((HL > HR ? HL-HR : HR-HL) <= 2
|
|
|
|
&& "Balancing invariant violated.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!getLeft()
|
|
|
|
|| ImutInfo::isLess(ImutInfo::KeyOfValue(getLeft()->getValue()),
|
|
|
|
ImutInfo::KeyOfValue(getValue()))
|
|
|
|
&& "Value in left child is not less that current value.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!getRight()
|
|
|
|
|| ImutInfo::isLess(ImutInfo::KeyOfValue(getValue()),
|
|
|
|
ImutInfo::KeyOfValue(getRight()->getValue()))
|
|
|
|
&& "Current value is not less that value of right child.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
return getHeight();
|
2008-02-04 21:15:24 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-05 17:30:43 +00:00
|
|
|
/// Profile - Profiling for ImutAVLTree.
|
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) {
|
|
|
|
ID.AddInteger(ComputeDigest());
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Internal Values.
|
|
|
|
//===----------------------------------------------------===//
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
private:
|
|
|
|
uintptr_t Left;
|
|
|
|
ImutAVLTree* Right;
|
|
|
|
unsigned Height;
|
|
|
|
value_type Value;
|
2009-07-09 18:34:41 +00:00
|
|
|
uint32_t Digest;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Internal methods (node manipulation; used by Factory).
|
|
|
|
//===----------------------------------------------------===//
|
2008-02-04 21:15:24 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
private:
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-07-09 18:34:41 +00:00
|
|
|
enum { Mutable = 0x1, NoCachedDigest = 0x2, LeftFlags = 0x3 };
|
2008-02-04 21:15:24 +00:00
|
|
|
|
2007-10-15 18:52:34 +00:00
|
|
|
/// ImutAVLTree - Internal constructor that is only called by
|
|
|
|
/// ImutAVLFactory.
|
2007-10-09 21:38:09 +00:00
|
|
|
ImutAVLTree(ImutAVLTree* l, ImutAVLTree* r, value_type_ref v, unsigned height)
|
2009-07-09 18:34:41 +00:00
|
|
|
: Left(reinterpret_cast<uintptr_t>(l) | (Mutable | NoCachedDigest)),
|
2008-02-05 17:30:43 +00:00
|
|
|
Right(r), Height(height), Value(v), Digest(0) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
|
2007-10-15 18:52:34 +00:00
|
|
|
/// isMutable - Returns true if the left and right subtree references
|
|
|
|
/// (as well as height) can be changed. If this method returns false,
|
|
|
|
/// the tree is truly immutable. Trees returned from an ImutAVLFactory
|
|
|
|
/// object should always have this method return true. Further, if this
|
|
|
|
/// method returns false for an instance of ImutAVLTree, all subtrees
|
|
|
|
/// will also have this method return false. The converse is not true.
|
|
|
|
bool isMutable() const { return Left & Mutable; }
|
2009-07-09 18:34:41 +00:00
|
|
|
|
|
|
|
/// hasCachedDigest - Returns true if the digest for this tree is cached.
|
|
|
|
/// This can only be true if the tree is immutable.
|
|
|
|
bool hasCachedDigest() const { return !(Left & NoCachedDigest); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------===//
|
2007-10-15 18:52:34 +00:00
|
|
|
// Mutating operations. A tree root can be manipulated as
|
2009-01-09 19:25:42 +00:00
|
|
|
// long as its reference has not "escaped" from internal
|
2007-10-15 18:52:34 +00:00
|
|
|
// methods of a factory object (see below). When a tree
|
2009-01-09 19:25:42 +00:00
|
|
|
// pointer is externally viewable by client code, the
|
|
|
|
// internal "mutable bit" is cleared to mark the tree
|
2007-10-15 18:52:34 +00:00
|
|
|
// immutable. Note that a tree that still has its mutable
|
|
|
|
// bit set may have children (subtrees) that are themselves
|
2007-10-09 21:38:09 +00:00
|
|
|
// immutable.
|
2007-10-15 18:52:34 +00:00
|
|
|
//===----------------------------------------------------===//
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-15 18:52:34 +00:00
|
|
|
/// MarkImmutable - Clears the mutable flag for a tree. After this happens,
|
2009-07-09 18:34:41 +00:00
|
|
|
/// it is an error to call setLeft(), setRight(), and setHeight().
|
2007-10-15 19:15:48 +00:00
|
|
|
void MarkImmutable() {
|
2009-07-09 18:34:41 +00:00
|
|
|
assert(isMutable() && "Mutable flag already removed.");
|
2007-10-15 18:52:34 +00:00
|
|
|
Left &= ~Mutable;
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-07-09 18:34:41 +00:00
|
|
|
|
|
|
|
/// MarkedCachedDigest - Clears the NoCachedDigest flag for a tree.
|
|
|
|
void MarkedCachedDigest() {
|
|
|
|
assert(!hasCachedDigest() && "NoCachedDigest flag already removed.");
|
|
|
|
Left &= ~NoCachedDigest;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-15 18:52:34 +00:00
|
|
|
/// setLeft - Changes the reference of the left subtree. Used internally
|
|
|
|
/// by ImutAVLFactory.
|
2007-10-09 21:38:09 +00:00
|
|
|
void setLeft(ImutAVLTree* NewLeft) {
|
2009-07-09 18:34:41 +00:00
|
|
|
assert(isMutable() &&
|
|
|
|
"Only a mutable tree can have its left subtree changed.");
|
|
|
|
Left = reinterpret_cast<uintptr_t>(NewLeft) | LeftFlags;
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-15 18:52:34 +00:00
|
|
|
/// setRight - Changes the reference of the right subtree. Used internally
|
|
|
|
/// by ImutAVLFactory.
|
2007-10-09 21:38:09 +00:00
|
|
|
void setRight(ImutAVLTree* NewRight) {
|
2009-07-10 00:33:43 +00:00
|
|
|
assert(isMutable() &&
|
|
|
|
"Only a mutable tree can have its right subtree changed.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
Right = NewRight;
|
2009-07-10 00:33:43 +00:00
|
|
|
// Set the NoCachedDigest flag.
|
|
|
|
Left = Left | NoCachedDigest;
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-15 18:52:34 +00:00
|
|
|
/// setHeight - Changes the height of the tree. Used internally by
|
|
|
|
/// ImutAVLFactory.
|
2007-10-09 21:38:09 +00:00
|
|
|
void setHeight(unsigned h) {
|
2009-07-10 00:33:43 +00:00
|
|
|
assert(isMutable() && "Only a mutable tree can have its height changed.");
|
2007-10-09 21:38:09 +00:00
|
|
|
Height = h;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
static inline
|
2009-07-09 18:34:41 +00:00
|
|
|
uint32_t ComputeDigest(ImutAVLTree* L, ImutAVLTree* R, value_type_ref V) {
|
|
|
|
uint32_t digest = 0;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-07-09 18:34:41 +00:00
|
|
|
if (L)
|
|
|
|
digest += L->ComputeDigest();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-07-09 18:34:41 +00:00
|
|
|
// Compute digest of stored data.
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
ImutInfo::Profile(ID,V);
|
|
|
|
digest += ID.ComputeHash();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-07-09 18:34:41 +00:00
|
|
|
if (R)
|
|
|
|
digest += R->ComputeDigest();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-05 17:30:43 +00:00
|
|
|
return digest;
|
2008-02-04 21:15:24 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-07-09 18:34:41 +00:00
|
|
|
inline uint32_t ComputeDigest() {
|
|
|
|
// Check the lowest bit to determine if digest has actually been
|
|
|
|
// pre-computed.
|
|
|
|
if (hasCachedDigest())
|
|
|
|
return Digest;
|
|
|
|
|
|
|
|
uint32_t X = ComputeDigest(getLeft(), getRight(), getValue());
|
2009-07-10 00:33:43 +00:00
|
|
|
Digest = X;
|
2008-02-04 21:15:24 +00:00
|
|
|
return X;
|
|
|
|
}
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Immutable AVL-Tree Factory class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
template <typename ImutInfo >
|
2007-10-09 21:38:09 +00:00
|
|
|
class ImutAVLFactory {
|
|
|
|
typedef ImutAVLTree<ImutInfo> TreeTy;
|
|
|
|
typedef typename TreeTy::value_type_ref value_type_ref;
|
|
|
|
typedef typename TreeTy::key_type_ref key_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
typedef FoldingSet<TreeTy> CacheTy;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-11 23:11:12 +00:00
|
|
|
CacheTy Cache;
|
|
|
|
uintptr_t Allocator;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-11 23:11:12 +00:00
|
|
|
bool ownsAllocator() const {
|
|
|
|
return Allocator & 0x1 ? false : true;
|
|
|
|
}
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
BumpPtrAllocator& getAllocator() const {
|
2008-02-11 23:11:12 +00:00
|
|
|
return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Public interface.
|
|
|
|
//===--------------------------------------------------===//
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
public:
|
2008-02-11 23:11:12 +00:00
|
|
|
ImutAVLFactory()
|
|
|
|
: Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-11 23:11:12 +00:00
|
|
|
ImutAVLFactory(BumpPtrAllocator& Alloc)
|
|
|
|
: Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-11 23:11:12 +00:00
|
|
|
~ImutAVLFactory() {
|
|
|
|
if (ownsAllocator()) delete &getAllocator();
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* Add(TreeTy* T, value_type_ref V) {
|
|
|
|
T = Add_internal(V,T);
|
|
|
|
MarkImmutable(T);
|
|
|
|
return T;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* Remove(TreeTy* T, key_type_ref V) {
|
|
|
|
T = Remove_internal(V,T);
|
|
|
|
MarkImmutable(T);
|
|
|
|
return T;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* GetEmptyTree() const { return NULL; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// A bunch of quick helper functions used for reasoning
|
|
|
|
// about the properties of trees and their children.
|
|
|
|
// These have succinct names so that the balancing code
|
|
|
|
// is as terse (and readable) as possible.
|
|
|
|
//===--------------------------------------------------===//
|
|
|
|
private:
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 23:35:04 +00:00
|
|
|
bool isEmpty(TreeTy* T) const { return !T; }
|
2009-01-09 19:25:42 +00:00
|
|
|
unsigned Height(TreeTy* T) const { return T ? T->getHeight() : 0; }
|
2009-07-09 18:34:41 +00:00
|
|
|
TreeTy* Left(TreeTy* T) const { return T->getLeft(); }
|
2009-01-09 19:25:42 +00:00
|
|
|
TreeTy* Right(TreeTy* T) const { return T->getRight(); }
|
2007-10-10 23:35:04 +00:00
|
|
|
value_type_ref Value(TreeTy* T) const { return T->Value; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
unsigned IncrementHeight(TreeTy* L, TreeTy* R) const {
|
|
|
|
unsigned hl = Height(L);
|
|
|
|
unsigned hr = Height(R);
|
|
|
|
return ( hl > hr ? hl : hr ) + 1;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
static bool CompareTreeWithSection(TreeTy* T,
|
|
|
|
typename TreeTy::iterator& TI,
|
|
|
|
typename TreeTy::iterator& TE) {
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
typename TreeTy::iterator I = T->begin(), E = T->end();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
for ( ; I!=E ; ++I, ++TI)
|
|
|
|
if (TI == TE || !I->ElementEqual(*TI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2007-10-10 16:27:33 +00:00
|
|
|
// "CreateNode" is used to generate new tree roots that link
|
2007-10-09 21:38:09 +00:00
|
|
|
// to other trees. The functon may also simply move links
|
|
|
|
// in an existing root if that root is still marked mutable.
|
|
|
|
// This is necessary because otherwise our balancing code
|
|
|
|
// would leak memory as it would create nodes that are
|
|
|
|
// then discarded later before the finished tree is
|
|
|
|
// returned to the caller.
|
|
|
|
//===--------------------------------------------------===//
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 16:27:33 +00:00
|
|
|
TreeTy* CreateNode(TreeTy* L, value_type_ref V, TreeTy* R) {
|
2008-02-05 17:30:43 +00:00
|
|
|
// Search the FoldingSet bucket for a Tree with the same digest.
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
unsigned digest = TreeTy::ComputeDigest(L, R, V);
|
|
|
|
ID.AddInteger(digest);
|
|
|
|
unsigned hash = ID.ComputeHash();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
typename CacheTy::bucket_iterator I = Cache.bucket_begin(hash);
|
|
|
|
typename CacheTy::bucket_iterator E = Cache.bucket_end(hash);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
for (; I != E; ++I) {
|
|
|
|
TreeTy* T = &*I;
|
|
|
|
|
2008-02-05 17:30:43 +00:00
|
|
|
if (T->ComputeDigest() != digest)
|
2008-02-04 21:15:24 +00:00
|
|
|
continue;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// We found a collision. Perform a comparison of Contents('T')
|
|
|
|
// with Contents('L')+'V'+Contents('R').
|
|
|
|
typename TreeTy::iterator TI = T->begin(), TE = T->end();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// First compare Contents('L') with the (initial) contents of T.
|
|
|
|
if (!CompareTreeWithSection(L, TI, TE))
|
|
|
|
continue;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// Now compare the new data element.
|
|
|
|
if (TI == TE || !TI->ElementEqual(V))
|
|
|
|
continue;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
++TI;
|
|
|
|
|
|
|
|
// Now compare the remainder of 'T' with 'R'.
|
|
|
|
if (!CompareTreeWithSection(R, TI, TE))
|
|
|
|
continue;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-07-10 00:33:43 +00:00
|
|
|
if (TI != TE)
|
|
|
|
continue; // Contents('R') did not match suffix of 'T'.
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// Trees did match! Return 'T'.
|
2007-10-09 21:38:09 +00:00
|
|
|
return T;
|
2008-02-04 21:15:24 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// No tree with the contents: Contents('L')+'V'+Contents('R').
|
2009-07-10 00:33:43 +00:00
|
|
|
// Create it. Allocate the new tree node and insert it into the cache.
|
2008-02-11 23:11:12 +00:00
|
|
|
BumpPtrAllocator& A = getAllocator();
|
|
|
|
TreeTy* T = (TreeTy*) A.Allocate<TreeTy>();
|
2007-10-10 23:32:01 +00:00
|
|
|
new (T) TreeTy(L,R,V,IncrementHeight(L,R));
|
2007-10-17 22:17:01 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// We do not insert 'T' into the FoldingSet here. This is because
|
|
|
|
// this tree is still mutable and things may get rebalanced.
|
2008-02-05 17:30:43 +00:00
|
|
|
// Because our digest is associative and based on the contents of
|
2008-02-04 21:15:24 +00:00
|
|
|
// the set, this should hopefully not cause any strange bugs.
|
|
|
|
// 'T' is inserted by 'MarkImmutable'.
|
|
|
|
return T;
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
TreeTy* CreateNode(TreeTy* L, TreeTy* OldTree, TreeTy* R) {
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!isEmpty(OldTree));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (OldTree->isMutable()) {
|
|
|
|
OldTree->setLeft(L);
|
|
|
|
OldTree->setRight(R);
|
|
|
|
OldTree->setHeight(IncrementHeight(L,R));
|
|
|
|
return OldTree;
|
|
|
|
}
|
2009-07-10 00:33:43 +00:00
|
|
|
else
|
|
|
|
return CreateNode(L, Value(OldTree), R);
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
/// Balance - Used by Add_internal and Remove_internal to
|
|
|
|
/// balance a newly created tree.
|
|
|
|
TreeTy* Balance(TreeTy* L, value_type_ref V, TreeTy* R) {
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
unsigned hl = Height(L);
|
|
|
|
unsigned hr = Height(R);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (hl > hr + 2) {
|
|
|
|
assert (!isEmpty(L) &&
|
|
|
|
"Left tree cannot be empty to have a height >= 2.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* LL = Left(L);
|
|
|
|
TreeTy* LR = Right(L);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (Height(LL) >= Height(LR))
|
2007-10-10 16:27:33 +00:00
|
|
|
return CreateNode(LL, L, CreateNode(LR,V,R));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!isEmpty(LR) &&
|
|
|
|
"LR cannot be empty because it has a height >= 1.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* LRL = Left(LR);
|
|
|
|
TreeTy* LRR = Right(LR);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
return CreateNode(CreateNode(LL,L,LRL), LR, CreateNode(LRR,V,R));
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
|
|
|
else if (hr > hl + 2) {
|
|
|
|
assert (!isEmpty(R) &&
|
|
|
|
"Right tree cannot be empty to have a height >= 2.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* RL = Left(R);
|
|
|
|
TreeTy* RR = Right(R);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (Height(RR) >= Height(RL))
|
2007-10-10 16:27:33 +00:00
|
|
|
return CreateNode(CreateNode(L,V,RL), R, RR);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!isEmpty(RL) &&
|
|
|
|
"RL cannot be empty because it has a height >= 1.");
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* RLL = Left(RL);
|
|
|
|
TreeTy* RLR = Right(RL);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 16:27:33 +00:00
|
|
|
return CreateNode(CreateNode(L,V,RLL), RL, CreateNode(RLR,R,RR));
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
|
|
|
else
|
2007-10-10 16:27:33 +00:00
|
|
|
return CreateNode(L,V,R);
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
/// Add_internal - Creates a new tree that includes the specified
|
|
|
|
/// data and the data from the original tree. If the original tree
|
|
|
|
/// already contained the data item, the original tree is returned.
|
|
|
|
TreeTy* Add_internal(value_type_ref V, TreeTy* T) {
|
|
|
|
if (isEmpty(T))
|
2007-10-10 16:27:33 +00:00
|
|
|
return CreateNode(T, V, T);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!T->isMutable());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
key_type_ref K = ImutInfo::KeyOfValue(V);
|
|
|
|
key_type_ref KCurrent = ImutInfo::KeyOfValue(Value(T));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (ImutInfo::isEqual(K,KCurrent))
|
2007-10-10 16:27:33 +00:00
|
|
|
return CreateNode(Left(T), V, Right(T));
|
2007-10-09 21:38:09 +00:00
|
|
|
else if (ImutInfo::isLess(K,KCurrent))
|
|
|
|
return Balance(Add_internal(V,Left(T)), Value(T), Right(T));
|
|
|
|
else
|
|
|
|
return Balance(Left(T), Value(T), Add_internal(V,Right(T)));
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-03 03:50:40 +00:00
|
|
|
/// Remove_internal - Creates a new tree that includes all the data
|
2007-10-09 21:38:09 +00:00
|
|
|
/// from the original tree except the specified data. If the
|
|
|
|
/// specified data did not exist in the original tree, the original
|
|
|
|
/// tree is returned.
|
|
|
|
TreeTy* Remove_internal(key_type_ref K, TreeTy* T) {
|
|
|
|
if (isEmpty(T))
|
|
|
|
return T;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
assert (!T->isMutable());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
key_type_ref KCurrent = ImutInfo::KeyOfValue(Value(T));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (ImutInfo::isEqual(K,KCurrent))
|
|
|
|
return CombineLeftRightTrees(Left(T),Right(T));
|
|
|
|
else if (ImutInfo::isLess(K,KCurrent))
|
|
|
|
return Balance(Remove_internal(K,Left(T)), Value(T), Right(T));
|
|
|
|
else
|
|
|
|
return Balance(Left(T), Value(T), Remove_internal(K,Right(T)));
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* CombineLeftRightTrees(TreeTy* L, TreeTy* R) {
|
2009-01-09 19:25:42 +00:00
|
|
|
if (isEmpty(L)) return R;
|
2007-10-09 21:38:09 +00:00
|
|
|
if (isEmpty(R)) return L;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
TreeTy* OldNode;
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* NewRight = RemoveMinBinding(R,OldNode);
|
|
|
|
return Balance(L,Value(OldNode),NewRight);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* RemoveMinBinding(TreeTy* T, TreeTy*& NodeRemoved) {
|
|
|
|
assert (!isEmpty(T));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
if (isEmpty(Left(T))) {
|
|
|
|
NodeRemoved = T;
|
|
|
|
return Right(T);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
return Balance(RemoveMinBinding(Left(T),NodeRemoved),Value(T),Right(T));
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
/// MarkImmutable - Clears the mutable bits of a root and all of its
|
|
|
|
/// descendants.
|
|
|
|
void MarkImmutable(TreeTy* T) {
|
|
|
|
if (!T || !T->isMutable())
|
|
|
|
return;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-15 19:15:48 +00:00
|
|
|
T->MarkImmutable();
|
2007-10-09 21:38:09 +00:00
|
|
|
MarkImmutable(Left(T));
|
|
|
|
MarkImmutable(Right(T));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-04 21:15:24 +00:00
|
|
|
// Now that the node is immutable it can safely be inserted
|
|
|
|
// into the node cache.
|
2008-02-05 17:30:43 +00:00
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
ID.AddInteger(T->ComputeDigest());
|
|
|
|
Cache.InsertNode(T, (void*) &*Cache.bucket_end(ID.ComputeHash()));
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-10 18:11:16 +00:00
|
|
|
// Immutable AVL-Tree Iterators.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
template <typename ImutInfo>
|
|
|
|
class ImutAVLTreeGenericIterator {
|
|
|
|
SmallVector<uintptr_t,20> stack;
|
|
|
|
public:
|
2009-01-09 19:25:42 +00:00
|
|
|
enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
|
2007-10-10 18:11:16 +00:00
|
|
|
Flags=0x3 };
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
typedef ImutAVLTree<ImutInfo> TreeTy;
|
2007-10-10 18:11:16 +00:00
|
|
|
typedef ImutAVLTreeGenericIterator<ImutInfo> _Self;
|
|
|
|
|
|
|
|
inline ImutAVLTreeGenericIterator() {}
|
|
|
|
inline ImutAVLTreeGenericIterator(const TreeTy* Root) {
|
|
|
|
if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
TreeTy* operator*() const {
|
2009-01-09 19:25:42 +00:00
|
|
|
assert (!stack.empty());
|
2007-10-10 18:11:16 +00:00
|
|
|
return reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
uintptr_t getVisitState() {
|
|
|
|
assert (!stack.empty());
|
|
|
|
return stack.back() & Flags;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
bool AtEnd() const { return stack.empty(); }
|
2007-10-09 21:38:09 +00:00
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
bool AtBeginning() const {
|
2007-10-10 18:11:16 +00:00
|
|
|
return stack.size() == 1 && getVisitState() == VisitedNone;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
void SkipToParent() {
|
|
|
|
assert (!stack.empty());
|
|
|
|
stack.pop_back();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
if (stack.empty())
|
|
|
|
return;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
switch (getVisitState()) {
|
|
|
|
case VisitedNone:
|
|
|
|
stack.back() |= VisitedLeft;
|
|
|
|
break;
|
|
|
|
case VisitedLeft:
|
|
|
|
stack.back() |= VisitedRight;
|
|
|
|
break;
|
|
|
|
default:
|
2009-01-09 19:25:42 +00:00
|
|
|
assert (false && "Unreachable.");
|
2007-10-10 18:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
inline bool operator==(const _Self& x) const {
|
|
|
|
if (stack.size() != x.stack.size())
|
|
|
|
return false;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
for (unsigned i = 0 ; i < stack.size(); i++)
|
|
|
|
if (stack[i] != x.stack[i])
|
|
|
|
return false;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
_Self& operator++() {
|
|
|
|
assert (!stack.empty());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
|
|
|
|
assert (Current);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
switch (getVisitState()) {
|
|
|
|
case VisitedNone:
|
2009-07-09 18:34:41 +00:00
|
|
|
if (TreeTy* L = Current->getLeft())
|
2007-10-10 18:11:16 +00:00
|
|
|
stack.push_back(reinterpret_cast<uintptr_t>(L));
|
|
|
|
else
|
|
|
|
stack.back() |= VisitedLeft;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
break;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
case VisitedLeft:
|
|
|
|
if (TreeTy* R = Current->getRight())
|
|
|
|
stack.push_back(reinterpret_cast<uintptr_t>(R));
|
|
|
|
else
|
|
|
|
stack.back() |= VisitedRight;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
break;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
case VisitedRight:
|
2009-01-09 19:25:42 +00:00
|
|
|
SkipToParent();
|
2007-10-10 18:11:16 +00:00
|
|
|
break;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
default:
|
|
|
|
assert (false && "Unreachable.");
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
_Self& operator--() {
|
|
|
|
assert (!stack.empty());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
|
|
|
|
assert (Current);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
switch (getVisitState()) {
|
|
|
|
case VisitedNone:
|
|
|
|
stack.pop_back();
|
|
|
|
break;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
case VisitedLeft:
|
2007-10-10 18:11:16 +00:00
|
|
|
stack.back() &= ~Flags; // Set state to "VisitedNone."
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
if (TreeTy* L = Current->getLeft())
|
|
|
|
stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
break;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
case VisitedRight:
|
2007-10-10 18:11:16 +00:00
|
|
|
stack.back() &= ~Flags;
|
|
|
|
stack.back() |= VisitedLeft;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
if (TreeTy* R = Current->getRight())
|
|
|
|
stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
break;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
default:
|
|
|
|
assert (false && "Unreachable.");
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
template <typename ImutInfo>
|
|
|
|
class ImutAVLTreeInOrderIterator {
|
|
|
|
typedef ImutAVLTreeGenericIterator<ImutInfo> InternalIteratorTy;
|
|
|
|
InternalIteratorTy InternalItr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef ImutAVLTree<ImutInfo> TreeTy;
|
|
|
|
typedef ImutAVLTreeInOrderIterator<ImutInfo> _Self;
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
|
2007-10-10 18:11:16 +00:00
|
|
|
if (Root) operator++(); // Advance to first element.
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
ImutAVLTreeInOrderIterator() : InternalItr() {}
|
|
|
|
|
|
|
|
inline bool operator==(const _Self& x) const {
|
|
|
|
return InternalItr == x.InternalItr;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
2007-10-11 00:14:49 +00:00
|
|
|
inline TreeTy* operator*() const { return *InternalItr; }
|
|
|
|
inline TreeTy* operator->() const { return *InternalItr; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
inline _Self& operator++() {
|
2007-10-10 18:11:16 +00:00
|
|
|
do ++InternalItr;
|
2009-01-09 19:25:42 +00:00
|
|
|
while (!InternalItr.AtEnd() &&
|
2007-10-10 18:11:16 +00:00
|
|
|
InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
inline _Self& operator--() {
|
2007-10-10 18:11:16 +00:00
|
|
|
do --InternalItr;
|
2009-01-09 19:25:42 +00:00
|
|
|
while (!InternalItr.AtBeginning() &&
|
2007-10-10 18:11:16 +00:00
|
|
|
InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
inline void SkipSubTree() {
|
|
|
|
InternalItr.SkipToParent();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 18:11:16 +00:00
|
|
|
while (!InternalItr.AtEnd() &&
|
|
|
|
InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
|
2009-01-09 19:25:42 +00:00
|
|
|
++InternalItr;
|
2007-10-10 18:11:16 +00:00
|
|
|
}
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Trait classes for Profile information.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Generic profile template. The default behavior is to invoke the
|
|
|
|
/// profile method of an object. Specializations for primitive integers
|
|
|
|
/// and generic handling of pointers is done below.
|
|
|
|
template <typename T>
|
|
|
|
struct ImutProfileInfo {
|
|
|
|
typedef const T value_type;
|
|
|
|
typedef const T& value_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
|
2008-01-19 04:51:55 +00:00
|
|
|
FoldingSetTrait<T>::Profile(X,ID);
|
|
|
|
}
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Profile traits for integers.
|
|
|
|
template <typename T>
|
2009-01-09 19:25:42 +00:00
|
|
|
struct ImutProfileInteger {
|
2007-10-09 21:38:09 +00:00
|
|
|
typedef const T value_type;
|
|
|
|
typedef const T& value_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID& ID, value_type_ref X) {
|
|
|
|
ID.AddInteger(X);
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define PROFILE_INTEGER_INFO(X)\
|
|
|
|
template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
|
|
|
|
|
|
|
|
PROFILE_INTEGER_INFO(char)
|
|
|
|
PROFILE_INTEGER_INFO(unsigned char)
|
|
|
|
PROFILE_INTEGER_INFO(short)
|
|
|
|
PROFILE_INTEGER_INFO(unsigned short)
|
|
|
|
PROFILE_INTEGER_INFO(unsigned)
|
|
|
|
PROFILE_INTEGER_INFO(signed)
|
|
|
|
PROFILE_INTEGER_INFO(long)
|
|
|
|
PROFILE_INTEGER_INFO(unsigned long)
|
|
|
|
PROFILE_INTEGER_INFO(long long)
|
|
|
|
PROFILE_INTEGER_INFO(unsigned long long)
|
|
|
|
|
|
|
|
#undef PROFILE_INTEGER_INFO
|
|
|
|
|
|
|
|
/// Generic profile trait for pointer types. We treat pointers as
|
|
|
|
/// references to unique objects.
|
|
|
|
template <typename T>
|
|
|
|
struct ImutProfileInfo<T*> {
|
|
|
|
typedef const T* value_type;
|
|
|
|
typedef value_type value_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID &ID, value_type_ref X) {
|
|
|
|
ID.AddPointer(X);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Trait classes that contain element comparison operators and type
|
|
|
|
// definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap. These
|
|
|
|
// inherit from the profile traits (ImutProfileInfo) to include operations
|
|
|
|
// for element profiling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
/// ImutContainerInfo - Generic definition of comparison operations for
|
|
|
|
/// elements of immutable containers that defaults to using
|
|
|
|
/// std::equal_to<> and std::less<> to perform comparison of elements.
|
|
|
|
template <typename T>
|
|
|
|
struct ImutContainerInfo : public ImutProfileInfo<T> {
|
|
|
|
typedef typename ImutProfileInfo<T>::value_type value_type;
|
|
|
|
typedef typename ImutProfileInfo<T>::value_type_ref value_type_ref;
|
|
|
|
typedef value_type key_type;
|
|
|
|
typedef value_type_ref key_type_ref;
|
2008-01-17 17:36:49 +00:00
|
|
|
typedef bool data_type;
|
|
|
|
typedef bool data_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline data_type_ref DataOfValue(value_type_ref) { return true; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
|
2007-10-09 21:38:09 +00:00
|
|
|
return std::equal_to<key_type>()(LHS,RHS);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
|
|
|
|
return std::less<key_type>()(LHS,RHS);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline bool isDataEqual(data_type_ref,data_type_ref) { return true; }
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// ImutContainerInfo - Specialization for pointer values to treat pointers
|
|
|
|
/// as references to unique objects. Pointers are thus compared by
|
|
|
|
/// their addresses.
|
|
|
|
template <typename T>
|
|
|
|
struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
|
|
|
|
typedef typename ImutProfileInfo<T*>::value_type value_type;
|
|
|
|
typedef typename ImutProfileInfo<T*>::value_type_ref value_type_ref;
|
|
|
|
typedef value_type key_type;
|
|
|
|
typedef value_type_ref key_type_ref;
|
2008-01-17 17:36:49 +00:00
|
|
|
typedef bool data_type;
|
|
|
|
typedef bool data_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline key_type_ref KeyOfValue(value_type_ref D) { return D; }
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline data_type_ref DataOfValue(value_type_ref) { return true; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline bool isEqual(key_type_ref LHS, key_type_ref RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline bool isLess(key_type_ref LHS, key_type_ref RHS) {
|
|
|
|
return LHS < RHS;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline bool isDataEqual(data_type_ref,data_type_ref) { return true; }
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Immutable Set
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <typename ValT, typename ValInfo = ImutContainerInfo<ValT> >
|
|
|
|
class ImmutableSet {
|
|
|
|
public:
|
|
|
|
typedef typename ValInfo::value_type value_type;
|
|
|
|
typedef typename ValInfo::value_type_ref value_type_ref;
|
|
|
|
typedef ImutAVLTree<ValInfo> TreeTy;
|
2008-01-23 19:57:33 +00:00
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
private:
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* Root;
|
2008-01-23 19:57:33 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
public:
|
2008-01-23 19:57:33 +00:00
|
|
|
/// Constructs a set from a pointer to a tree root. In general one
|
|
|
|
/// should use a Factory object to create sets instead of directly
|
|
|
|
/// invoking the constructor, but there are cases where make this
|
|
|
|
/// constructor public is useful.
|
|
|
|
explicit ImmutableSet(TreeTy* R) : Root(R) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
class Factory {
|
|
|
|
typename TreeTy::Factory F;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
public:
|
|
|
|
Factory() {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-11 23:11:12 +00:00
|
|
|
Factory(BumpPtrAllocator& Alloc)
|
|
|
|
: F(Alloc) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 23:47:03 +00:00
|
|
|
/// GetEmptySet - Returns an immutable set that contains no elements.
|
2007-10-09 21:38:09 +00:00
|
|
|
ImmutableSet GetEmptySet() { return ImmutableSet(F.GetEmptyTree()); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 23:47:03 +00:00
|
|
|
/// Add - Creates a new immutable set that contains all of the values
|
|
|
|
/// of the original set with the addition of the specified value. If
|
|
|
|
/// the original set already included the value, then the original set is
|
|
|
|
/// returned and no memory is allocated. The time and space complexity
|
|
|
|
/// of this operation is logarithmic in the size of the original set.
|
|
|
|
/// The memory allocated to represent the set is released when the
|
|
|
|
/// factory object that created the set is destroyed.
|
2007-10-09 21:38:09 +00:00
|
|
|
ImmutableSet Add(ImmutableSet Old, value_type_ref V) {
|
|
|
|
return ImmutableSet(F.Add(Old.Root,V));
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 23:47:03 +00:00
|
|
|
/// Remove - Creates a new immutable set that contains all of the values
|
|
|
|
/// of the original set with the exception of the specified value. If
|
|
|
|
/// the original set did not contain the value, the original set is
|
|
|
|
/// returned and no memory is allocated. The time and space complexity
|
|
|
|
/// of this operation is logarithmic in the size of the original set.
|
|
|
|
/// The memory allocated to represent the set is released when the
|
|
|
|
/// factory object that created the set is destroyed.
|
2007-10-09 21:38:09 +00:00
|
|
|
ImmutableSet Remove(ImmutableSet Old, value_type_ref V) {
|
|
|
|
return ImmutableSet(F.Remove(Old.Root,V));
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-11 21:51:04 +00:00
|
|
|
BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
private:
|
|
|
|
Factory(const Factory& RHS) {};
|
2009-01-09 19:25:42 +00:00
|
|
|
void operator=(const Factory& RHS) {};
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
friend class Factory;
|
2007-10-10 23:47:03 +00:00
|
|
|
|
|
|
|
/// contains - Returns true if the set contains the specified value.
|
2009-07-19 01:42:34 +00:00
|
|
|
bool contains(value_type_ref V) const {
|
2007-10-09 21:38:09 +00:00
|
|
|
return Root ? Root->contains(V) : false;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
bool operator==(ImmutableSet RHS) const {
|
|
|
|
return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
bool operator!=(ImmutableSet RHS) const {
|
|
|
|
return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-23 19:57:33 +00:00
|
|
|
TreeTy* getRoot() const { return Root; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-10 23:47:03 +00:00
|
|
|
/// isEmpty - Return true if the set contains no elements.
|
2007-10-09 21:38:09 +00:00
|
|
|
bool isEmpty() const { return !Root; }
|
2009-02-20 22:54:36 +00:00
|
|
|
|
2009-02-12 05:17:02 +00:00
|
|
|
/// isSingleton - Return true if the set contains exactly one element.
|
|
|
|
/// This method runs in constant time.
|
|
|
|
bool isSingleton() const { return getHeight() == 1; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void foreach(Callback& C) { if (Root) Root->foreach(C); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void foreach() { if (Root) { Callback C; Root->foreach(C); } }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2007-10-11 00:14:49 +00:00
|
|
|
// Iterators.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
2007-10-11 00:14:49 +00:00
|
|
|
|
|
|
|
class iterator {
|
|
|
|
typename TreeTy::iterator itr;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-11 00:14:49 +00:00
|
|
|
iterator() {}
|
|
|
|
iterator(TreeTy* t) : itr(t) {}
|
|
|
|
friend class ImmutableSet<ValT,ValInfo>;
|
|
|
|
public:
|
|
|
|
inline value_type_ref operator*() const { return itr->getValue(); }
|
|
|
|
inline iterator& operator++() { ++itr; return *this; }
|
2007-10-11 21:51:04 +00:00
|
|
|
inline iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
|
2007-10-11 00:14:49 +00:00
|
|
|
inline iterator& operator--() { --itr; return *this; }
|
2007-10-11 21:51:04 +00:00
|
|
|
inline iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
|
2007-10-11 00:14:49 +00:00
|
|
|
inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
|
2009-01-09 19:25:42 +00:00
|
|
|
inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
|
2009-02-12 17:14:49 +00:00
|
|
|
inline value_type *operator->() const { return &(operator*()); }
|
2007-10-11 00:14:49 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-11 00:14:49 +00:00
|
|
|
iterator begin() const { return iterator(Root); }
|
2009-01-09 19:25:42 +00:00
|
|
|
iterator end() const { return iterator(); }
|
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2008-02-05 18:50:25 +00:00
|
|
|
// Utility methods.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
2008-02-05 18:50:25 +00:00
|
|
|
inline unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-05 18:50:25 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID& ID, const ImmutableSet& S) {
|
|
|
|
ID.AddPointer(S.Root);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-02-05 18:50:25 +00:00
|
|
|
inline void Profile(FoldingSetNodeID& ID) const {
|
|
|
|
return Profile(ID,*this);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// For testing.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
void verify() const { if (Root) Root->verify(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|