14 #ifndef LLVM_ADT_IMMUTABLESET_H
15 #define LLVM_ADT_IMMUTABLESET_H
20 #include "llvm/Support/DataTypes.h"
37 template <
typename ImutInfo >
77 if (ImutInfo::isEqual(K,CurrentKey))
79 else if (ImutInfo::isLess(K,CurrentKey))
92 while (Right) { T = Right; Right = T->
getRight(); }
118 if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(
getValue()),
119 ImutInfo::KeyOfValue(V)))
123 if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(
getValue()),
124 ImutInfo::DataOfValue(V)))
144 while (LItr != LEnd && RItr != REnd) {
145 if (&*LItr == &*RItr) {
158 return LItr == LEnd && RItr == REnd;
173 template <
typename Callback>
174 void foreach(Callback&
C) {
196 assert(
getHeight() == ( HL > HR ? HL : HR ) + 1
197 &&
"Height calculation wrong");
199 assert((HL > HR ? HL-HR : HR-HL) <= 2
200 &&
"Balancing invariant violated");
204 ImutInfo::KeyOfValue(
getValue()))) &&
205 "Value in left child is not less that current value");
209 ImutInfo::isLess(ImutInfo::KeyOfValue(
getValue()),
211 "Current value is not less that value of right child");
227 unsigned height : 28;
228 unsigned IsMutable : 1;
229 unsigned IsDigestCached : 1;
230 unsigned IsCanonicalized : 1;
245 : factory(f), left(l), right(r), prev(nullptr), next(nullptr),
246 height(height), IsMutable(
true), IsDigestCached(
false),
247 IsCanonicalized(0), value(v), digest(0), refCount(0)
250 if (right) right->
retain();
259 bool isMutable()
const {
return IsMutable; }
263 bool hasCachedDigest()
const {
return IsDigestCached; }
278 void markImmutable() {
279 assert(isMutable() &&
"Mutable flag already removed.");
284 void markedCachedDigest() {
285 assert(!hasCachedDigest() &&
"NoCachedDigest flag already removed.");
286 IsDigestCached =
true;
291 void setHeight(
unsigned h) {
292 assert(isMutable() &&
"Only a mutable tree can have its height changed.");
296 static uint32_t computeDigest(ImutAVLTree *L, ImutAVLTree *R,
301 digest += L->computeDigest();
305 ImutInfo::Profile(ID,V);
306 digest += ID.ComputeHash();
309 digest += R->computeDigest();
314 uint32_t computeDigest() {
317 if (hasCachedDigest())
322 markedCachedDigest();
333 assert(refCount > 0);
342 if (IsCanonicalized) {
355 factory->freeNodes.push_back(
this);
363 template <
typename ImutInfo >
364 class ImutAVLFactory {
374 std::vector<TreeTy*> createdNodes;
375 std::vector<TreeTy*> freeNodes;
377 bool ownsAllocator()
const {
378 return Allocator & 0x1 ?
false :
true;
394 : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
397 if (ownsAllocator())
delete &getAllocator();
437 return (hl > hr ? hl : hr) + 1;
444 for ( ; I!=E ; ++
I, ++TI) {
464 if (!freeNodes.empty()) {
465 T = freeNodes.back();
466 freeNodes.pop_back();
473 createdNodes.push_back(T);
482 for (
unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
484 if (N->isMutable() && N->refCount == 0)
487 createdNodes.clear();
497 assert(!
isEmpty(L) &&
"Left tree cannot be empty to have a height >= 2");
505 assert(!
isEmpty(LR) &&
"LR cannot be empty because it has a height >= 1");
514 assert(!
isEmpty(R) &&
"Right tree cannot be empty to have a height >= 2");
522 assert(!
isEmpty(RL) &&
"RL cannot be empty because it has a height >= 1");
539 assert(!T->isMutable());
541 key_type_ref K = ImutInfo::KeyOfValue(V);
542 key_type_ref KCurrent = ImutInfo::KeyOfValue(
getValue(T));
544 if (ImutInfo::isEqual(K,KCurrent))
546 else if (ImutInfo::isLess(K,KCurrent))
560 assert(!T->isMutable());
562 key_type_ref KCurrent = ImutInfo::KeyOfValue(
getValue(T));
564 if (ImutInfo::isEqual(K,KCurrent)) {
566 }
else if (ImutInfo::isLess(K,KCurrent)) {
598 if (!T || !T->isMutable())
610 if (TNew->IsCanonicalized)
615 unsigned digest = TNew->computeDigest();
620 for (
TreeTy *
T = entry ;
T !=
nullptr;
T =
T->next) {
628 if (TNew->refCount == 0)
638 TNew->IsCanonicalized =
true;
647 template <
typename ImutInfo>
648 class ImutAVLTreeGenericIterator
649 :
public std::iterator<std::bidirectional_iterator_tag,
650 ImutAVLTree<ImutInfo>> {
651 SmallVector<uintptr_t,20> stack;
660 if (Root) stack.
push_back(reinterpret_cast<uintptr_t>(Root));
664 assert(!stack.
empty());
670 assert(!stack.
empty());
682 assert(!stack.
empty());
699 return stack == x.stack;
703 return !(*
this == x);
707 assert(!stack.
empty());
713 stack.
push_back(reinterpret_cast<uintptr_t>(L));
719 stack.
push_back(reinterpret_cast<uintptr_t>(R));
733 assert(!stack.
empty());
758 template <
typename ImutInfo>
759 class ImutAVLTreeInOrderIterator
760 :
public std::iterator<std::bidirectional_iterator_tag,
761 ImutAVLTree<ImutInfo>> {
762 typedef ImutAVLTreeGenericIterator<ImutInfo> InternalIteratorTy;
763 InternalIteratorTy InternalItr;
776 return InternalItr == x.InternalItr;
780 return !(*
this == x);
788 while (!InternalItr.atEnd() &&
789 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
796 while (!InternalItr.atBeginning() &&
797 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
803 InternalItr.skipToParent();
805 while (!InternalItr.atEnd() &&
806 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
813 template <
typename T>
816 ImutAVLValueIterator<T>, typename T::TreeTy::iterator,
817 typename std::iterator_traits<
818 typename T::TreeTy::iterator>::iterator_category,
819 const typename T::value_type> {
824 typename ImutAVLValueIterator::reference
operator*()
const {
825 return this->
I->getValue();
836 template <
typename T>
847 template <
typename T>
857 #define PROFILE_INTEGER_INFO(X)\
858 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
871 #undef PROFILE_INTEGER_INFO
887 template <
typename T>
908 template <
typename T>
921 return std::equal_to<key_type>()(LHS,RHS);
925 return std::less<key_type>()(LHS,RHS);
934 template <
typename T>
943 static key_type_ref
KeyOfValue(value_type_ref D) {
return D; }
946 static bool isEqual(key_type_ref LHS, key_type_ref RHS) {
return LHS == RHS; }
948 static bool isLess(key_type_ref LHS, key_type_ref RHS) {
return LHS < RHS; }
950 static bool isDataEqual(data_type_ref, data_type_ref) {
return true; }
957 template <
typename ValT,
typename ValInfo = ImutContainerInfo<ValT> >
973 if (Root) { Root->
retain(); }
976 if (Root) { Root->
retain(); }
979 if (Root != X.Root) {
980 if (X.Root) { X.Root->
retain(); }
992 const bool Canonicalize;
996 : Canonicalize(canonicalize) {}
999 : F(Alloc), Canonicalize(canonicalize) {}
1038 void operator=(
const Factory& RHS) =
delete;
1045 return Root ? Root->
contains(V) :
false;
1049 return Root && RHS.Root ? Root->
isEqual(*RHS.Root) : Root == RHS.Root;
1053 return Root && RHS.Root ? Root->
isNotEqual(*RHS.Root) : Root != RHS.Root;
1057 if (Root) { Root->
retain(); }
1072 template <
typename Callback>
1073 void foreach(Callback& C) {
if (Root) Root->
foreach(C); }
1075 template <
typename Callback>
1076 void foreach() {
if (Root) { Callback
C; Root->
foreach(C); } }
1107 template <
typename ValT,
typename ValInfo = ImutContainerInfo<ValT> >
1127 if (Root) { Root->
retain(); }
1131 Factory(X.Factory) {
1132 if (Root) { Root->
retain(); }
1135 if (Root != X.Root) {
1136 if (X.Root) { X.Root->
retain(); }
1137 if (Root) { Root->
release(); }
1139 Factory = X.Factory;
1144 if (Root) { Root->
release(); }
1161 return Root ? Root->
contains(V) :
false;
1174 return Root && RHS.Root ? Root->
isEqual(*RHS.Root) : Root == RHS.Root;
1178 return Root && RHS.Root ? Root->
isNotEqual(*RHS.Root) : Root != RHS.Root;
Generic iterator that wraps a T::TreeTy::iterator and exposes iterator::getValue() on dereference...
unsigned getHeight() const
getHeight - Returns the height of the tree.
ImutAVLTree< ValInfo > TreeTy
void validateTree() const
ImmutableSet add(ImmutableSet Old, value_type_ref V)
add - Creates a new immutable set that contains all of the values of the original set with the additi...
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
ImmutableSetRef(const ImmutableSetRef &X)
void push_back(const T &Elt)
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
ImutAVLValueIterator()=default
unsigned validateTree() const
validateTree - A utility method that checks that the balancing and ordering invariants of the tree ar...
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
static bool isDataEqual(data_type_ref, data_type_ref)
Generic profile trait for pointer types.
ValInfo::value_type value_type
TreeTy * balanceTree(TreeTy *L, value_type_ref V, TreeTy *R)
balanceTree - Used by add_internal and remove_internal to balance a newly created tree...
ImutAVLTree * getLeft() const
Return a pointer to the left subtree.
ValInfo::value_type_ref value_type_ref
static data_type_ref DataOfValue(value_type_ref)
bool operator==(const ImmutableSetRef &RHS) const
TreeTy * add(TreeTy *T, value_type_ref V)
ImutInfo::key_type_ref key_type_ref
ImutProfileInfo< T * >::value_type value_type
bool contains(value_type_ref V) const
Returns true if the set contains the specified value.
ImutAVLTree< ValInfo > TreeTy
BumpPtrAllocator & getAllocator()
TreeTy * remove_internal(key_type_ref K, TreeTy *T)
remove_internal - Creates a new tree that includes all the data from the original tree except the spe...
ImutAVLTreeInOrderIterator & operator--()
bool contains(value_type_ref V) const
Returns true if the set contains the specified value.
void Profile(FoldingSetNodeID &ID) const
TreeTy * getRootWithoutRetain() const
TreeTy & operator*() const
TreeTy * createNode(TreeTy *newLeft, TreeTy *oldTree, TreeTy *newRight)
ImutAVLTreeGenericIterator(const TreeTy *Root)
bool isNotEqual(const ImutAVLTree &RHS) const
isNotEqual - Compares two trees for structural inequality.
bool isSingleton() const
isSingleton - Return true if the set contains exactly one element.
static void Profile(const T &X, FoldingSetNodeID &ID)
ImmutableSet & operator=(const ImmutableSet &X)
TreeTy::Factory * getTreeFactory() const
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
bool isElementEqual(const ImutAVLTree *RHS) const
ImutAVLTree * getMaxElement()
getMaxElement - Find the subtree associated with the highest ranged key value.
static ImmutableSetRef getEmptySet(FactoryTy *F)
TreeTy * createNode(TreeTy *L, value_type_ref V, TreeTy *R)
ImutAVLTreeInOrderIterator & operator++()
Generic profile template.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void AddInteger(signed I)
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
TreeTy * operator->() const
static void Profile(FoldingSetNodeID &ID, const ImmutableSet &S)
static bool compareTreeWithSection(TreeTy *T, typename TreeTy::iterator &TI, typename TreeTy::iterator &TE)
static data_type_ref DataOfValue(value_type_ref)
ImmutableSetRef add(value_type_ref V)
unsigned getHeight() const
bool isSingleton() const
isSingleton - Return true if the set contains exactly one element.
ImutInfo::value_type_ref value_type_ref
#define PROFILE_INTEGER_INFO(X)
ImmutableSet getEmptySet()
getEmptySet - Returns an immutable set that contains no elements.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
iterator begin() const
begin - Returns an iterator that iterates over the nodes of the tree in an inorder traversal...
ImutAVLFactory< ImutInfo > Factory
ImmutableSetRef(TreeTy *R, FactoryTy *F)
Constructs a set from a pointer to a tree root.
ImutAVLValueIterator(typename T::TreeTy *Tree)
ImutAVLValueIterator< ImmutableSetRef > iterator
value_type_ref key_type_ref
Profile traits for integers.
ImutAVLTree< ImutInfo > TreeTy
void foreach(Callback &C)
foreach - A member template the accepts invokes operator() on a functor object (specifed by Callback)...
ImutAVLTreeGenericIterator()
static void Profile(FoldingSetNodeID &ID, const ImmutableSetRef &S)
ImutAVLTreeInOrderIterator()
bool operator!=(const ImutAVLTreeInOrderIterator &x) const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Factory(bool canonicalize=true)
const bool & value_type_ref
TreeTy * add_internal(value_type_ref V, TreeTy *T)
add_internal - Creates a new tree that includes the specified data and the data from the original tre...
bool operator!=(const ImmutableSetRef &RHS) const
TreeTy * getEmptyTree() const
TreeTy * remove(TreeTy *T, key_type_ref V)
ImutAVLTreeGenericIterator & operator--()
Allocate memory in an ever growing pool, as if by bump-pointer.
CRTP base class for adapting an iterator to a different type.
TreeTy * operator->() const
bool operator==(const ImutAVLTreeInOrderIterator &x) const
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
ImutAVLTree * find(key_type_ref K)
find - Finds the subtree associated with the specified key value.
ImutAVLTree * getRight() const
Return a pointer to the right subtree.
void markImmutable(TreeTy *T)
markImmutable - Clears the mutable bits of a root and all of its descendants.
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
bool isElementEqual(value_type_ref V) const
ImutAVLTreeInOrderIterator(const TreeTy *Root)
unsigned size() const
size - Returns the number of nodes in the tree, which includes both leaves and non-leaf nodes...
bool isEmpty() const
isEmpty - Return true if the set contains no elements.
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
TreeTy * getRight(TreeTy *T) const
value_type_ref getValue(TreeTy *T) const
bool contains(key_type_ref K)
contains - Returns true if this tree contains a subtree (node) that has an data element that matches ...
bool operator!=(const ImmutableSet &RHS) const
TreeTy & operator*() const
ImutAVLTreeInOrderIterator< ImutInfo > iterator
TreeTy * getRootWithoutRetain() const
unsigned getHeight() const
ImutProfileInfo< T >::value_type value_type
Factory(BumpPtrAllocator &Alloc, bool canonicalize=true)
bool operator==(const ImutAVLTreeGenericIterator &x) const
iterator end() const
end - Returns an iterator for the tree that denotes the end of an inorder traversal.
ImutProfileInfo< T * >::value_type_ref value_type_ref
ImutInfo::value_type value_type
bool isEmpty() const
isEmpty - Return true if the set contains no elements.
ImmutableSetRef & operator=(const ImmutableSetRef &X)
static key_type_ref KeyOfValue(value_type_ref D)
void validateTree() const
unsigned incrementHeight(TreeTy *L, TreeTy *R) const
TreeTy * getCanonicalTree(TreeTy *TNew)
TreeTy::Factory FactoryTy
ImutProfileInfo< T >::value_type_ref value_type_ref
static bool isDataEqual(data_type_ref, data_type_ref)
TreeTy * combineTrees(TreeTy *L, TreeTy *R)
TreeTy * removeMinBinding(TreeTy *T, TreeTy *&Noderemoved)
ImutAVLFactory(BumpPtrAllocator &Alloc)
ImutAVLTree< ImutInfo > TreeTy
bool operator!=(const ImutAVLTreeGenericIterator &x) const
ImutAVLTreeGenericIterator & operator++()
static key_type_ref KeyOfValue(value_type_ref D)
ImmutableSet(TreeTy *R)
Constructs a set from a pointer to a tree root.
uintptr_t getVisitState() const
const value_type & getValue() const
getValue - Returns the data value associated with the tree node.
static bool isLess(key_type_ref LHS, key_type_ref RHS)
ImmutableSet(const ImmutableSet &X)
ValInfo::value_type_ref value_type_ref
bool isEmpty(TreeTy *T) const
void Profile(FoldingSetNodeID &ID) const
static bool isLess(key_type_ref LHS, key_type_ref RHS)
bool operator==(const ImmutableSet &RHS) const
ValInfo::value_type value_type
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
value_type value_type_ref
ImutAVLValueIterator::reference operator*() const
ImutAVLValueIterator< ImmutableSet > iterator
bool isEqual(const ImutAVLTree &RHS) const
isEqual - Compares two trees for structural equality and returns true if they are equal...
value_type_ref key_type_ref
TreeTy * getLeft(TreeTy *T) const
ImmutableSet< ValT > asImmutableSet(bool canonicalize=true) const
unsigned getHeight(TreeTy *T) const
ImutContainerInfo - Generic definition of comparison operations for elements of immutable containers ...
static unsigned maskCacheIndex(unsigned I)