ImmutableSet.h revision 360784
1//===--- ImmutableSet.h - Immutable (functional) set interface --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ImutAVLTree and ImmutableSet classes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ADT_IMMUTABLESET_H
14#define LLVM_ADT_IMMUTABLESET_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/iterator.h"
20#include "llvm/Support/Allocator.h"
21#include "llvm/Support/ErrorHandling.h"
22#include <cassert>
23#include <cstdint>
24#include <functional>
25#include <iterator>
26#include <new>
27#include <vector>
28
29namespace llvm {
30
31//===----------------------------------------------------------------------===//
32// Immutable AVL-Tree Definition.
33//===----------------------------------------------------------------------===//
34
35template <typename ImutInfo> class ImutAVLFactory;
36template <typename ImutInfo> class ImutIntervalAVLFactory;
37template <typename ImutInfo> class ImutAVLTreeInOrderIterator;
38template <typename ImutInfo> class ImutAVLTreeGenericIterator;
39
40template <typename ImutInfo >
41class ImutAVLTree {
42public:
43  using key_type_ref = typename ImutInfo::key_type_ref;
44  using value_type = typename ImutInfo::value_type;
45  using value_type_ref = typename ImutInfo::value_type_ref;
46  using Factory = ImutAVLFactory<ImutInfo>;
47  using iterator = ImutAVLTreeInOrderIterator<ImutInfo>;
48
49  friend class ImutAVLFactory<ImutInfo>;
50  friend class ImutIntervalAVLFactory<ImutInfo>;
51  friend class ImutAVLTreeGenericIterator<ImutInfo>;
52
53  //===----------------------------------------------------===//
54  // Public Interface.
55  //===----------------------------------------------------===//
56
57  /// Return a pointer to the left subtree.  This value
58  ///  is NULL if there is no left subtree.
59  ImutAVLTree *getLeft() const { return left; }
60
61  /// Return a pointer to the right subtree.  This value is
62  ///  NULL if there is no right subtree.
63  ImutAVLTree *getRight() const { return right; }
64
65  /// getHeight - Returns the height of the tree.  A tree with no subtrees
66  ///  has a height of 1.
67  unsigned getHeight() const { return height; }
68
69  /// getValue - Returns the data value associated with the tree node.
70  const value_type& getValue() const { return value; }
71
72  /// find - Finds the subtree associated with the specified key value.
73  ///  This method returns NULL if no matching subtree is found.
74  ImutAVLTree* find(key_type_ref K) {
75    ImutAVLTree *T = this;
76    while (T) {
77      key_type_ref CurrentKey = ImutInfo::KeyOfValue(T->getValue());
78      if (ImutInfo::isEqual(K,CurrentKey))
79        return T;
80      else if (ImutInfo::isLess(K,CurrentKey))
81        T = T->getLeft();
82      else
83        T = T->getRight();
84    }
85    return nullptr;
86  }
87
88  /// getMaxElement - Find the subtree associated with the highest ranged
89  ///  key value.
90  ImutAVLTree* getMaxElement() {
91    ImutAVLTree *T = this;
92    ImutAVLTree *Right = T->getRight();
93    while (Right) { T = Right; Right = T->getRight(); }
94    return T;
95  }
96
97  /// size - Returns the number of nodes in the tree, which includes
98  ///  both leaves and non-leaf nodes.
99  unsigned size() const {
100    unsigned n = 1;
101    if (const ImutAVLTree* L = getLeft())
102      n += L->size();
103    if (const ImutAVLTree* R = getRight())
104      n += R->size();
105    return n;
106  }
107
108  /// begin - Returns an iterator that iterates over the nodes of the tree
109  ///  in an inorder traversal.  The returned iterator thus refers to the
110  ///  the tree node with the minimum data element.
111  iterator begin() const { return iterator(this); }
112
113  /// end - Returns an iterator for the tree that denotes the end of an
114  ///  inorder traversal.
115  iterator end() const { return iterator(); }
116
117  bool isElementEqual(value_type_ref V) const {
118    // Compare the keys.
119    if (!ImutInfo::isEqual(ImutInfo::KeyOfValue(getValue()),
120                           ImutInfo::KeyOfValue(V)))
121      return false;
122
123    // Also compare the data values.
124    if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(getValue()),
125                               ImutInfo::DataOfValue(V)))
126      return false;
127
128    return true;
129  }
130
131  bool isElementEqual(const ImutAVLTree* RHS) const {
132    return isElementEqual(RHS->getValue());
133  }
134
135  /// isEqual - Compares two trees for structural equality and returns true
136  ///   if they are equal.  This worst case performance of this operation is
137  //    linear in the sizes of the trees.
138  bool isEqual(const ImutAVLTree& RHS) const {
139    if (&RHS == this)
140      return true;
141
142    iterator LItr = begin(), LEnd = end();
143    iterator RItr = RHS.begin(), REnd = RHS.end();
144
145    while (LItr != LEnd && RItr != REnd) {
146      if (&*LItr == &*RItr) {
147        LItr.skipSubTree();
148        RItr.skipSubTree();
149        continue;
150      }
151
152      if (!LItr->isElementEqual(&*RItr))
153        return false;
154
155      ++LItr;
156      ++RItr;
157    }
158
159    return LItr == LEnd && RItr == REnd;
160  }
161
162  /// isNotEqual - Compares two trees for structural inequality.  Performance
163  ///  is the same is isEqual.
164  bool isNotEqual(const ImutAVLTree& RHS) const { return !isEqual(RHS); }
165
166  /// contains - Returns true if this tree contains a subtree (node) that
167  ///  has an data element that matches the specified key.  Complexity
168  ///  is logarithmic in the size of the tree.
169  bool contains(key_type_ref K) { return (bool) find(K); }
170
171  /// foreach - A member template the accepts invokes operator() on a functor
172  ///  object (specifed by Callback) for every node/subtree in the tree.
173  ///  Nodes are visited using an inorder traversal.
174  template <typename Callback>
175  void foreach(Callback& C) {
176    if (ImutAVLTree* L = getLeft())
177      L->foreach(C);
178
179    C(value);
180
181    if (ImutAVLTree* R = getRight())
182      R->foreach(C);
183  }
184
185  /// validateTree - A utility method that checks that the balancing and
186  ///  ordering invariants of the tree are satisifed.  It is a recursive
187  ///  method that returns the height of the tree, which is then consumed
188  ///  by the enclosing validateTree call.  External callers should ignore the
189  ///  return value.  An invalid tree will cause an assertion to fire in
190  ///  a debug build.
191  unsigned validateTree() const {
192    unsigned HL = getLeft() ? getLeft()->validateTree() : 0;
193    unsigned HR = getRight() ? getRight()->validateTree() : 0;
194    (void) HL;
195    (void) HR;
196
197    assert(getHeight() == ( HL > HR ? HL : HR ) + 1
198            && "Height calculation wrong");
199
200    assert((HL > HR ? HL-HR : HR-HL) <= 2
201           && "Balancing invariant violated");
202
203    assert((!getLeft() ||
204            ImutInfo::isLess(ImutInfo::KeyOfValue(getLeft()->getValue()),
205                             ImutInfo::KeyOfValue(getValue()))) &&
206           "Value in left child is not less that current value");
207
208    assert((!getRight() ||
209             ImutInfo::isLess(ImutInfo::KeyOfValue(getValue()),
210                              ImutInfo::KeyOfValue(getRight()->getValue()))) &&
211           "Current value is not less that value of right child");
212
213    return getHeight();
214  }
215
216  //===----------------------------------------------------===//
217  // Internal values.
218  //===----------------------------------------------------===//
219
220private:
221  Factory *factory;
222  ImutAVLTree *left;
223  ImutAVLTree *right;
224  ImutAVLTree *prev = nullptr;
225  ImutAVLTree *next = nullptr;
226
227  unsigned height : 28;
228  bool IsMutable : 1;
229  bool IsDigestCached : 1;
230  bool IsCanonicalized : 1;
231
232  value_type value;
233  uint32_t digest = 0;
234  uint32_t refCount = 0;
235
236  //===----------------------------------------------------===//
237  // Internal methods (node manipulation; used by Factory).
238  //===----------------------------------------------------===//
239
240private:
241  /// ImutAVLTree - Internal constructor that is only called by
242  ///   ImutAVLFactory.
243  ImutAVLTree(Factory *f, ImutAVLTree* l, ImutAVLTree* r, value_type_ref v,
244              unsigned height)
245    : factory(f), left(l), right(r), height(height), IsMutable(true),
246      IsDigestCached(false), IsCanonicalized(false), value(v)
247  {
248    if (left) left->retain();
249    if (right) right->retain();
250  }
251
252  /// isMutable - Returns true if the left and right subtree references
253  ///  (as well as height) can be changed.  If this method returns false,
254  ///  the tree is truly immutable.  Trees returned from an ImutAVLFactory
255  ///  object should always have this method return true.  Further, if this
256  ///  method returns false for an instance of ImutAVLTree, all subtrees
257  ///  will also have this method return false.  The converse is not true.
258  bool isMutable() const { return IsMutable; }
259
260  /// hasCachedDigest - Returns true if the digest for this tree is cached.
261  ///  This can only be true if the tree is immutable.
262  bool hasCachedDigest() const { return IsDigestCached; }
263
264  //===----------------------------------------------------===//
265  // Mutating operations.  A tree root can be manipulated as
266  // long as its reference has not "escaped" from internal
267  // methods of a factory object (see below).  When a tree
268  // pointer is externally viewable by client code, the
269  // internal "mutable bit" is cleared to mark the tree
270  // immutable.  Note that a tree that still has its mutable
271  // bit set may have children (subtrees) that are themselves
272  // immutable.
273  //===----------------------------------------------------===//
274
275  /// markImmutable - Clears the mutable flag for a tree.  After this happens,
276  ///   it is an error to call setLeft(), setRight(), and setHeight().
277  void markImmutable() {
278    assert(isMutable() && "Mutable flag already removed.");
279    IsMutable = false;
280  }
281
282  /// markedCachedDigest - Clears the NoCachedDigest flag for a tree.
283  void markedCachedDigest() {
284    assert(!hasCachedDigest() && "NoCachedDigest flag already removed.");
285    IsDigestCached = true;
286  }
287
288  /// setHeight - Changes the height of the tree.  Used internally by
289  ///  ImutAVLFactory.
290  void setHeight(unsigned h) {
291    assert(isMutable() && "Only a mutable tree can have its height changed.");
292    height = h;
293  }
294
295  static uint32_t computeDigest(ImutAVLTree *L, ImutAVLTree *R,
296                                value_type_ref V) {
297    uint32_t digest = 0;
298
299    if (L)
300      digest += L->computeDigest();
301
302    // Compute digest of stored data.
303    FoldingSetNodeID ID;
304    ImutInfo::Profile(ID,V);
305    digest += ID.ComputeHash();
306
307    if (R)
308      digest += R->computeDigest();
309
310    return digest;
311  }
312
313  uint32_t computeDigest() {
314    // Check the lowest bit to determine if digest has actually been
315    // pre-computed.
316    if (hasCachedDigest())
317      return digest;
318
319    uint32_t X = computeDigest(getLeft(), getRight(), getValue());
320    digest = X;
321    markedCachedDigest();
322    return X;
323  }
324
325  //===----------------------------------------------------===//
326  // Reference count operations.
327  //===----------------------------------------------------===//
328
329public:
330  void retain() { ++refCount; }
331
332  void release() {
333    assert(refCount > 0);
334    if (--refCount == 0)
335      destroy();
336  }
337
338  void destroy() {
339    if (left)
340      left->release();
341    if (right)
342      right->release();
343    if (IsCanonicalized) {
344      if (next)
345        next->prev = prev;
346
347      if (prev)
348        prev->next = next;
349      else
350        factory->Cache[factory->maskCacheIndex(computeDigest())] = next;
351    }
352
353    // We need to clear the mutability bit in case we are
354    // destroying the node as part of a sweep in ImutAVLFactory::recoverNodes().
355    IsMutable = false;
356    factory->freeNodes.push_back(this);
357  }
358};
359
360//===----------------------------------------------------------------------===//
361// Immutable AVL-Tree Factory class.
362//===----------------------------------------------------------------------===//
363
364template <typename ImutInfo >
365class ImutAVLFactory {
366  friend class ImutAVLTree<ImutInfo>;
367
368  using TreeTy = ImutAVLTree<ImutInfo>;
369  using value_type_ref = typename TreeTy::value_type_ref;
370  using key_type_ref = typename TreeTy::key_type_ref;
371  using CacheTy = DenseMap<unsigned, TreeTy*>;
372
373  CacheTy Cache;
374  uintptr_t Allocator;
375  std::vector<TreeTy*> createdNodes;
376  std::vector<TreeTy*> freeNodes;
377
378  bool ownsAllocator() const {
379    return (Allocator & 0x1) == 0;
380  }
381
382  BumpPtrAllocator& getAllocator() const {
383    return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
384  }
385
386  //===--------------------------------------------------===//
387  // Public interface.
388  //===--------------------------------------------------===//
389
390public:
391  ImutAVLFactory()
392    : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
393
394  ImutAVLFactory(BumpPtrAllocator& Alloc)
395    : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
396
397  ~ImutAVLFactory() {
398    if (ownsAllocator()) delete &getAllocator();
399  }
400
401  TreeTy* add(TreeTy* T, value_type_ref V) {
402    T = add_internal(V,T);
403    markImmutable(T);
404    recoverNodes();
405    return T;
406  }
407
408  TreeTy* remove(TreeTy* T, key_type_ref V) {
409    T = remove_internal(V,T);
410    markImmutable(T);
411    recoverNodes();
412    return T;
413  }
414
415  TreeTy* getEmptyTree() const { return nullptr; }
416
417protected:
418  //===--------------------------------------------------===//
419  // A bunch of quick helper functions used for reasoning
420  // about the properties of trees and their children.
421  // These have succinct names so that the balancing code
422  // is as terse (and readable) as possible.
423  //===--------------------------------------------------===//
424
425  bool            isEmpty(TreeTy* T) const { return !T; }
426  unsigned        getHeight(TreeTy* T) const { return T ? T->getHeight() : 0; }
427  TreeTy*         getLeft(TreeTy* T) const { return T->getLeft(); }
428  TreeTy*         getRight(TreeTy* T) const { return T->getRight(); }
429  value_type_ref  getValue(TreeTy* T) const { return T->value; }
430
431  // Make sure the index is not the Tombstone or Entry key of the DenseMap.
432  static unsigned maskCacheIndex(unsigned I) { return (I & ~0x02); }
433
434  unsigned incrementHeight(TreeTy* L, TreeTy* R) const {
435    unsigned hl = getHeight(L);
436    unsigned hr = getHeight(R);
437    return (hl > hr ? hl : hr) + 1;
438  }
439
440  static bool compareTreeWithSection(TreeTy* T,
441                                     typename TreeTy::iterator& TI,
442                                     typename TreeTy::iterator& TE) {
443    typename TreeTy::iterator I = T->begin(), E = T->end();
444    for ( ; I!=E ; ++I, ++TI) {
445      if (TI == TE || !I->isElementEqual(&*TI))
446        return false;
447    }
448    return true;
449  }
450
451  //===--------------------------------------------------===//
452  // "createNode" is used to generate new tree roots that link
453  // to other trees.  The functon may also simply move links
454  // in an existing root if that root is still marked mutable.
455  // This is necessary because otherwise our balancing code
456  // would leak memory as it would create nodes that are
457  // then discarded later before the finished tree is
458  // returned to the caller.
459  //===--------------------------------------------------===//
460
461  TreeTy* createNode(TreeTy* L, value_type_ref V, TreeTy* R) {
462    BumpPtrAllocator& A = getAllocator();
463    TreeTy* T;
464    if (!freeNodes.empty()) {
465      T = freeNodes.back();
466      freeNodes.pop_back();
467      assert(T != L);
468      assert(T != R);
469    } else {
470      T = (TreeTy*) A.Allocate<TreeTy>();
471    }
472    new (T) TreeTy(this, L, R, V, incrementHeight(L,R));
473    createdNodes.push_back(T);
474    return T;
475  }
476
477  TreeTy* createNode(TreeTy* newLeft, TreeTy* oldTree, TreeTy* newRight) {
478    return createNode(newLeft, getValue(oldTree), newRight);
479  }
480
481  void recoverNodes() {
482    for (unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
483      TreeTy *N = createdNodes[i];
484      if (N->isMutable() && N->refCount == 0)
485        N->destroy();
486    }
487    createdNodes.clear();
488  }
489
490  /// balanceTree - Used by add_internal and remove_internal to
491  ///  balance a newly created tree.
492  TreeTy* balanceTree(TreeTy* L, value_type_ref V, TreeTy* R) {
493    unsigned hl = getHeight(L);
494    unsigned hr = getHeight(R);
495
496    if (hl > hr + 2) {
497      assert(!isEmpty(L) && "Left tree cannot be empty to have a height >= 2");
498
499      TreeTy *LL = getLeft(L);
500      TreeTy *LR = getRight(L);
501
502      if (getHeight(LL) >= getHeight(LR))
503        return createNode(LL, L, createNode(LR,V,R));
504
505      assert(!isEmpty(LR) && "LR cannot be empty because it has a height >= 1");
506
507      TreeTy *LRL = getLeft(LR);
508      TreeTy *LRR = getRight(LR);
509
510      return createNode(createNode(LL,L,LRL), LR, createNode(LRR,V,R));
511    }
512
513    if (hr > hl + 2) {
514      assert(!isEmpty(R) && "Right tree cannot be empty to have a height >= 2");
515
516      TreeTy *RL = getLeft(R);
517      TreeTy *RR = getRight(R);
518
519      if (getHeight(RR) >= getHeight(RL))
520        return createNode(createNode(L,V,RL), R, RR);
521
522      assert(!isEmpty(RL) && "RL cannot be empty because it has a height >= 1");
523
524      TreeTy *RLL = getLeft(RL);
525      TreeTy *RLR = getRight(RL);
526
527      return createNode(createNode(L,V,RLL), RL, createNode(RLR,R,RR));
528    }
529
530    return createNode(L,V,R);
531  }
532
533  /// add_internal - Creates a new tree that includes the specified
534  ///  data and the data from the original tree.  If the original tree
535  ///  already contained the data item, the original tree is returned.
536  TreeTy* add_internal(value_type_ref V, TreeTy* T) {
537    if (isEmpty(T))
538      return createNode(T, V, T);
539    assert(!T->isMutable());
540
541    key_type_ref K = ImutInfo::KeyOfValue(V);
542    key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
543
544    if (ImutInfo::isEqual(K,KCurrent))
545      return createNode(getLeft(T), V, getRight(T));
546    else if (ImutInfo::isLess(K,KCurrent))
547      return balanceTree(add_internal(V, getLeft(T)), getValue(T), getRight(T));
548    else
549      return balanceTree(getLeft(T), getValue(T), add_internal(V, getRight(T)));
550  }
551
552  /// remove_internal - Creates a new tree that includes all the data
553  ///  from the original tree except the specified data.  If the
554  ///  specified data did not exist in the original tree, the original
555  ///  tree is returned.
556  TreeTy* remove_internal(key_type_ref K, TreeTy* T) {
557    if (isEmpty(T))
558      return T;
559
560    assert(!T->isMutable());
561
562    key_type_ref KCurrent = ImutInfo::KeyOfValue(getValue(T));
563
564    if (ImutInfo::isEqual(K,KCurrent)) {
565      return combineTrees(getLeft(T), getRight(T));
566    } else if (ImutInfo::isLess(K,KCurrent)) {
567      return balanceTree(remove_internal(K, getLeft(T)),
568                                            getValue(T), getRight(T));
569    } else {
570      return balanceTree(getLeft(T), getValue(T),
571                         remove_internal(K, getRight(T)));
572    }
573  }
574
575  TreeTy* combineTrees(TreeTy* L, TreeTy* R) {
576    if (isEmpty(L))
577      return R;
578    if (isEmpty(R))
579      return L;
580    TreeTy* OldNode;
581    TreeTy* newRight = removeMinBinding(R,OldNode);
582    return balanceTree(L, getValue(OldNode), newRight);
583  }
584
585  TreeTy* removeMinBinding(TreeTy* T, TreeTy*& Noderemoved) {
586    assert(!isEmpty(T));
587    if (isEmpty(getLeft(T))) {
588      Noderemoved = T;
589      return getRight(T);
590    }
591    return balanceTree(removeMinBinding(getLeft(T), Noderemoved),
592                       getValue(T), getRight(T));
593  }
594
595  /// markImmutable - Clears the mutable bits of a root and all of its
596  ///  descendants.
597  void markImmutable(TreeTy* T) {
598    if (!T || !T->isMutable())
599      return;
600    T->markImmutable();
601    markImmutable(getLeft(T));
602    markImmutable(getRight(T));
603  }
604
605public:
606  TreeTy *getCanonicalTree(TreeTy *TNew) {
607    if (!TNew)
608      return nullptr;
609
610    if (TNew->IsCanonicalized)
611      return TNew;
612
613    // Search the hashtable for another tree with the same digest, and
614    // if find a collision compare those trees by their contents.
615    unsigned digest = TNew->computeDigest();
616    TreeTy *&entry = Cache[maskCacheIndex(digest)];
617    do {
618      if (!entry)
619        break;
620      for (TreeTy *T = entry ; T != nullptr; T = T->next) {
621        // Compare the Contents('T') with Contents('TNew')
622        typename TreeTy::iterator TI = T->begin(), TE = T->end();
623        if (!compareTreeWithSection(TNew, TI, TE))
624          continue;
625        if (TI != TE)
626          continue; // T has more contents than TNew.
627        // Trees did match!  Return 'T'.
628        if (TNew->refCount == 0)
629          TNew->destroy();
630        return T;
631      }
632      entry->prev = TNew;
633      TNew->next = entry;
634    }
635    while (false);
636
637    entry = TNew;
638    TNew->IsCanonicalized = true;
639    return TNew;
640  }
641};
642
643//===----------------------------------------------------------------------===//
644// Immutable AVL-Tree Iterators.
645//===----------------------------------------------------------------------===//
646
647template <typename ImutInfo>
648class ImutAVLTreeGenericIterator
649    : public std::iterator<std::bidirectional_iterator_tag,
650                           ImutAVLTree<ImutInfo>> {
651  SmallVector<uintptr_t,20> stack;
652
653public:
654  enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
655                   Flags=0x3 };
656
657  using TreeTy = ImutAVLTree<ImutInfo>;
658
659  ImutAVLTreeGenericIterator() = default;
660  ImutAVLTreeGenericIterator(const TreeTy *Root) {
661    if (Root) stack.push_back(reinterpret_cast<uintptr_t>(Root));
662  }
663
664  TreeTy &operator*() const {
665    assert(!stack.empty());
666    return *reinterpret_cast<TreeTy *>(stack.back() & ~Flags);
667  }
668  TreeTy *operator->() const { return &*this; }
669
670  uintptr_t getVisitState() const {
671    assert(!stack.empty());
672    return stack.back() & Flags;
673  }
674
675  bool atEnd() const { return stack.empty(); }
676
677  bool atBeginning() const {
678    return stack.size() == 1 && getVisitState() == VisitedNone;
679  }
680
681  void skipToParent() {
682    assert(!stack.empty());
683    stack.pop_back();
684    if (stack.empty())
685      return;
686    switch (getVisitState()) {
687      case VisitedNone:
688        stack.back() |= VisitedLeft;
689        break;
690      case VisitedLeft:
691        stack.back() |= VisitedRight;
692        break;
693      default:
694        llvm_unreachable("Unreachable.");
695    }
696  }
697
698  bool operator==(const ImutAVLTreeGenericIterator &x) const {
699    return stack == x.stack;
700  }
701
702  bool operator!=(const ImutAVLTreeGenericIterator &x) const {
703    return !(*this == x);
704  }
705
706  ImutAVLTreeGenericIterator &operator++() {
707    assert(!stack.empty());
708    TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
709    assert(Current);
710    switch (getVisitState()) {
711      case VisitedNone:
712        if (TreeTy* L = Current->getLeft())
713          stack.push_back(reinterpret_cast<uintptr_t>(L));
714        else
715          stack.back() |= VisitedLeft;
716        break;
717      case VisitedLeft:
718        if (TreeTy* R = Current->getRight())
719          stack.push_back(reinterpret_cast<uintptr_t>(R));
720        else
721          stack.back() |= VisitedRight;
722        break;
723      case VisitedRight:
724        skipToParent();
725        break;
726      default:
727        llvm_unreachable("Unreachable.");
728    }
729    return *this;
730  }
731
732  ImutAVLTreeGenericIterator &operator--() {
733    assert(!stack.empty());
734    TreeTy* Current = reinterpret_cast<TreeTy*>(stack.back() & ~Flags);
735    assert(Current);
736    switch (getVisitState()) {
737      case VisitedNone:
738        stack.pop_back();
739        break;
740      case VisitedLeft:
741        stack.back() &= ~Flags; // Set state to "VisitedNone."
742        if (TreeTy* L = Current->getLeft())
743          stack.push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
744        break;
745      case VisitedRight:
746        stack.back() &= ~Flags;
747        stack.back() |= VisitedLeft;
748        if (TreeTy* R = Current->getRight())
749          stack.push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
750        break;
751      default:
752        llvm_unreachable("Unreachable.");
753    }
754    return *this;
755  }
756};
757
758template <typename ImutInfo>
759class ImutAVLTreeInOrderIterator
760    : public std::iterator<std::bidirectional_iterator_tag,
761                           ImutAVLTree<ImutInfo>> {
762  using InternalIteratorTy = ImutAVLTreeGenericIterator<ImutInfo>;
763
764  InternalIteratorTy InternalItr;
765
766public:
767  using TreeTy = ImutAVLTree<ImutInfo>;
768
769  ImutAVLTreeInOrderIterator(const TreeTy* Root) : InternalItr(Root) {
770    if (Root)
771      ++*this; // Advance to first element.
772  }
773
774  ImutAVLTreeInOrderIterator() : InternalItr() {}
775
776  bool operator==(const ImutAVLTreeInOrderIterator &x) const {
777    return InternalItr == x.InternalItr;
778  }
779
780  bool operator!=(const ImutAVLTreeInOrderIterator &x) const {
781    return !(*this == x);
782  }
783
784  TreeTy &operator*() const { return *InternalItr; }
785  TreeTy *operator->() const { return &*InternalItr; }
786
787  ImutAVLTreeInOrderIterator &operator++() {
788    do ++InternalItr;
789    while (!InternalItr.atEnd() &&
790           InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
791
792    return *this;
793  }
794
795  ImutAVLTreeInOrderIterator &operator--() {
796    do --InternalItr;
797    while (!InternalItr.atBeginning() &&
798           InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
799
800    return *this;
801  }
802
803  void skipSubTree() {
804    InternalItr.skipToParent();
805
806    while (!InternalItr.atEnd() &&
807           InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
808      ++InternalItr;
809  }
810};
811
812/// Generic iterator that wraps a T::TreeTy::iterator and exposes
813/// iterator::getValue() on dereference.
814template <typename T>
815struct ImutAVLValueIterator
816    : iterator_adaptor_base<
817          ImutAVLValueIterator<T>, typename T::TreeTy::iterator,
818          typename std::iterator_traits<
819              typename T::TreeTy::iterator>::iterator_category,
820          const typename T::value_type> {
821  ImutAVLValueIterator() = default;
822  explicit ImutAVLValueIterator(typename T::TreeTy *Tree)
823      : ImutAVLValueIterator::iterator_adaptor_base(Tree) {}
824
825  typename ImutAVLValueIterator::reference operator*() const {
826    return this->I->getValue();
827  }
828};
829
830//===----------------------------------------------------------------------===//
831// Trait classes for Profile information.
832//===----------------------------------------------------------------------===//
833
834/// Generic profile template.  The default behavior is to invoke the
835/// profile method of an object.  Specializations for primitive integers
836/// and generic handling of pointers is done below.
837template <typename T>
838struct ImutProfileInfo {
839  using value_type = const T;
840  using value_type_ref = const T&;
841
842  static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
843    FoldingSetTrait<T>::Profile(X,ID);
844  }
845};
846
847/// Profile traits for integers.
848template <typename T>
849struct ImutProfileInteger {
850  using value_type = const T;
851  using value_type_ref = const T&;
852
853  static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
854    ID.AddInteger(X);
855  }
856};
857
858#define PROFILE_INTEGER_INFO(X)\
859template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
860
861PROFILE_INTEGER_INFO(char)
862PROFILE_INTEGER_INFO(unsigned char)
863PROFILE_INTEGER_INFO(short)
864PROFILE_INTEGER_INFO(unsigned short)
865PROFILE_INTEGER_INFO(unsigned)
866PROFILE_INTEGER_INFO(signed)
867PROFILE_INTEGER_INFO(long)
868PROFILE_INTEGER_INFO(unsigned long)
869PROFILE_INTEGER_INFO(long long)
870PROFILE_INTEGER_INFO(unsigned long long)
871
872#undef PROFILE_INTEGER_INFO
873
874/// Profile traits for booleans.
875template <>
876struct ImutProfileInfo<bool> {
877  using value_type = const bool;
878  using value_type_ref = const bool&;
879
880  static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
881    ID.AddBoolean(X);
882  }
883};
884
885/// Generic profile trait for pointer types.  We treat pointers as
886/// references to unique objects.
887template <typename T>
888struct ImutProfileInfo<T*> {
889  using value_type = const T*;
890  using value_type_ref = value_type;
891
892  static void Profile(FoldingSetNodeID &ID, value_type_ref X) {
893    ID.AddPointer(X);
894  }
895};
896
897//===----------------------------------------------------------------------===//
898// Trait classes that contain element comparison operators and type
899//  definitions used by ImutAVLTree, ImmutableSet, and ImmutableMap.  These
900//  inherit from the profile traits (ImutProfileInfo) to include operations
901//  for element profiling.
902//===----------------------------------------------------------------------===//
903
904/// ImutContainerInfo - Generic definition of comparison operations for
905///   elements of immutable containers that defaults to using
906///   std::equal_to<> and std::less<> to perform comparison of elements.
907template <typename T>
908struct ImutContainerInfo : public ImutProfileInfo<T> {
909  using value_type = typename ImutProfileInfo<T>::value_type;
910  using value_type_ref = typename ImutProfileInfo<T>::value_type_ref;
911  using key_type = value_type;
912  using key_type_ref = value_type_ref;
913  using data_type = bool;
914  using data_type_ref = bool;
915
916  static key_type_ref KeyOfValue(value_type_ref D) { return D; }
917  static data_type_ref DataOfValue(value_type_ref) { return true; }
918
919  static bool isEqual(key_type_ref LHS, key_type_ref RHS) {
920    return std::equal_to<key_type>()(LHS,RHS);
921  }
922
923  static bool isLess(key_type_ref LHS, key_type_ref RHS) {
924    return std::less<key_type>()(LHS,RHS);
925  }
926
927  static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
928};
929
930/// ImutContainerInfo - Specialization for pointer values to treat pointers
931///  as references to unique objects.  Pointers are thus compared by
932///  their addresses.
933template <typename T>
934struct ImutContainerInfo<T*> : public ImutProfileInfo<T*> {
935  using value_type = typename ImutProfileInfo<T*>::value_type;
936  using value_type_ref = typename ImutProfileInfo<T*>::value_type_ref;
937  using key_type = value_type;
938  using key_type_ref = value_type_ref;
939  using data_type = bool;
940  using data_type_ref = bool;
941
942  static key_type_ref KeyOfValue(value_type_ref D) { return D; }
943  static data_type_ref DataOfValue(value_type_ref) { return true; }
944
945  static bool isEqual(key_type_ref LHS, key_type_ref RHS) { return LHS == RHS; }
946
947  static bool isLess(key_type_ref LHS, key_type_ref RHS) { return LHS < RHS; }
948
949  static bool isDataEqual(data_type_ref, data_type_ref) { return true; }
950};
951
952//===----------------------------------------------------------------------===//
953// Immutable Set
954//===----------------------------------------------------------------------===//
955
956template <typename ValT, typename ValInfo = ImutContainerInfo<ValT>>
957class ImmutableSet {
958public:
959  using value_type = typename ValInfo::value_type;
960  using value_type_ref = typename ValInfo::value_type_ref;
961  using TreeTy = ImutAVLTree<ValInfo>;
962
963private:
964  TreeTy *Root;
965
966public:
967  /// Constructs a set from a pointer to a tree root.  In general one
968  /// should use a Factory object to create sets instead of directly
969  /// invoking the constructor, but there are cases where make this
970  /// constructor public is useful.
971  explicit ImmutableSet(TreeTy* R) : Root(R) {
972    if (Root) { Root->retain(); }
973  }
974
975  ImmutableSet(const ImmutableSet &X) : Root(X.Root) {
976    if (Root) { Root->retain(); }
977  }
978
979  ~ImmutableSet() {
980    if (Root) { Root->release(); }
981  }
982
983  ImmutableSet &operator=(const ImmutableSet &X) {
984    if (Root != X.Root) {
985      if (X.Root) { X.Root->retain(); }
986      if (Root) { Root->release(); }
987      Root = X.Root;
988    }
989    return *this;
990  }
991
992  class Factory {
993    typename TreeTy::Factory F;
994    const bool Canonicalize;
995
996  public:
997    Factory(bool canonicalize = true)
998      : Canonicalize(canonicalize) {}
999
1000    Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
1001      : F(Alloc), Canonicalize(canonicalize) {}
1002
1003    Factory(const Factory& RHS) = delete;
1004    void operator=(const Factory& RHS) = delete;
1005
1006    /// getEmptySet - Returns an immutable set that contains no elements.
1007    ImmutableSet getEmptySet() {
1008      return ImmutableSet(F.getEmptyTree());
1009    }
1010
1011    /// add - Creates a new immutable set that contains all of the values
1012    ///  of the original set with the addition of the specified value.  If
1013    ///  the original set already included the value, then the original set is
1014    ///  returned and no memory is allocated.  The time and space complexity
1015    ///  of this operation is logarithmic in the size of the original set.
1016    ///  The memory allocated to represent the set is released when the
1017    ///  factory object that created the set is destroyed.
1018    LLVM_NODISCARD ImmutableSet add(ImmutableSet Old, value_type_ref V) {
1019      TreeTy *NewT = F.add(Old.Root, V);
1020      return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1021    }
1022
1023    /// remove - Creates a new immutable set that contains all of the values
1024    ///  of the original set with the exception of the specified value.  If
1025    ///  the original set did not contain the value, the original set is
1026    ///  returned and no memory is allocated.  The time and space complexity
1027    ///  of this operation is logarithmic in the size of the original set.
1028    ///  The memory allocated to represent the set is released when the
1029    ///  factory object that created the set is destroyed.
1030    LLVM_NODISCARD ImmutableSet remove(ImmutableSet Old, value_type_ref V) {
1031      TreeTy *NewT = F.remove(Old.Root, V);
1032      return ImmutableSet(Canonicalize ? F.getCanonicalTree(NewT) : NewT);
1033    }
1034
1035    BumpPtrAllocator& getAllocator() { return F.getAllocator(); }
1036
1037    typename TreeTy::Factory *getTreeFactory() const {
1038      return const_cast<typename TreeTy::Factory *>(&F);
1039    }
1040  };
1041
1042  friend class Factory;
1043
1044  /// Returns true if the set contains the specified value.
1045  bool contains(value_type_ref V) const {
1046    return Root ? Root->contains(V) : false;
1047  }
1048
1049  bool operator==(const ImmutableSet &RHS) const {
1050    return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
1051  }
1052
1053  bool operator!=(const ImmutableSet &RHS) const {
1054    return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
1055  }
1056
1057  TreeTy *getRoot() {
1058    if (Root) { Root->retain(); }
1059    return Root;
1060  }
1061
1062  TreeTy *getRootWithoutRetain() const {
1063    return Root;
1064  }
1065
1066  /// isEmpty - Return true if the set contains no elements.
1067  bool isEmpty() const { return !Root; }
1068
1069  /// isSingleton - Return true if the set contains exactly one element.
1070  ///   This method runs in constant time.
1071  bool isSingleton() const { return getHeight() == 1; }
1072
1073  template <typename Callback>
1074  void foreach(Callback& C) { if (Root) Root->foreach(C); }
1075
1076  template <typename Callback>
1077  void foreach() { if (Root) { Callback C; Root->foreach(C); } }
1078
1079  //===--------------------------------------------------===//
1080  // Iterators.
1081  //===--------------------------------------------------===//
1082
1083  using iterator = ImutAVLValueIterator<ImmutableSet>;
1084
1085  iterator begin() const { return iterator(Root); }
1086  iterator end() const { return iterator(); }
1087
1088  //===--------------------------------------------------===//
1089  // Utility methods.
1090  //===--------------------------------------------------===//
1091
1092  unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1093
1094  static void Profile(FoldingSetNodeID &ID, const ImmutableSet &S) {
1095    ID.AddPointer(S.Root);
1096  }
1097
1098  void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
1099
1100  //===--------------------------------------------------===//
1101  // For testing.
1102  //===--------------------------------------------------===//
1103
1104  void validateTree() const { if (Root) Root->validateTree(); }
1105};
1106
1107// NOTE: This may some day replace the current ImmutableSet.
1108template <typename ValT, typename ValInfo = ImutContainerInfo<ValT>>
1109class ImmutableSetRef {
1110public:
1111  using value_type = typename ValInfo::value_type;
1112  using value_type_ref = typename ValInfo::value_type_ref;
1113  using TreeTy = ImutAVLTree<ValInfo>;
1114  using FactoryTy = typename TreeTy::Factory;
1115
1116private:
1117  TreeTy *Root;
1118  FactoryTy *Factory;
1119
1120public:
1121  /// Constructs a set from a pointer to a tree root.  In general one
1122  /// should use a Factory object to create sets instead of directly
1123  /// invoking the constructor, but there are cases where make this
1124  /// constructor public is useful.
1125  explicit ImmutableSetRef(TreeTy* R, FactoryTy *F)
1126    : Root(R),
1127      Factory(F) {
1128    if (Root) { Root->retain(); }
1129  }
1130
1131  ImmutableSetRef(const ImmutableSetRef &X)
1132    : Root(X.Root),
1133      Factory(X.Factory) {
1134    if (Root) { Root->retain(); }
1135  }
1136
1137  ~ImmutableSetRef() {
1138    if (Root) { Root->release(); }
1139  }
1140
1141  ImmutableSetRef &operator=(const ImmutableSetRef &X) {
1142    if (Root != X.Root) {
1143      if (X.Root) { X.Root->retain(); }
1144      if (Root) { Root->release(); }
1145      Root = X.Root;
1146      Factory = X.Factory;
1147    }
1148    return *this;
1149  }
1150
1151  static ImmutableSetRef getEmptySet(FactoryTy *F) {
1152    return ImmutableSetRef(0, F);
1153  }
1154
1155  ImmutableSetRef add(value_type_ref V) {
1156    return ImmutableSetRef(Factory->add(Root, V), Factory);
1157  }
1158
1159  ImmutableSetRef remove(value_type_ref V) {
1160    return ImmutableSetRef(Factory->remove(Root, V), Factory);
1161  }
1162
1163  /// Returns true if the set contains the specified value.
1164  bool contains(value_type_ref V) const {
1165    return Root ? Root->contains(V) : false;
1166  }
1167
1168  ImmutableSet<ValT> asImmutableSet(bool canonicalize = true) const {
1169    return ImmutableSet<ValT>(canonicalize ?
1170                              Factory->getCanonicalTree(Root) : Root);
1171  }
1172
1173  TreeTy *getRootWithoutRetain() const {
1174    return Root;
1175  }
1176
1177  bool operator==(const ImmutableSetRef &RHS) const {
1178    return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
1179  }
1180
1181  bool operator!=(const ImmutableSetRef &RHS) const {
1182    return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
1183  }
1184
1185  /// isEmpty - Return true if the set contains no elements.
1186  bool isEmpty() const { return !Root; }
1187
1188  /// isSingleton - Return true if the set contains exactly one element.
1189  ///   This method runs in constant time.
1190  bool isSingleton() const { return getHeight() == 1; }
1191
1192  //===--------------------------------------------------===//
1193  // Iterators.
1194  //===--------------------------------------------------===//
1195
1196  using iterator = ImutAVLValueIterator<ImmutableSetRef>;
1197
1198  iterator begin() const { return iterator(Root); }
1199  iterator end() const { return iterator(); }
1200
1201  //===--------------------------------------------------===//
1202  // Utility methods.
1203  //===--------------------------------------------------===//
1204
1205  unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
1206
1207  static void Profile(FoldingSetNodeID &ID, const ImmutableSetRef &S) {
1208    ID.AddPointer(S.Root);
1209  }
1210
1211  void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
1212
1213  //===--------------------------------------------------===//
1214  // For testing.
1215  //===--------------------------------------------------===//
1216
1217  void validateTree() const { if (Root) Root->validateTree(); }
1218};
1219
1220} // end namespace llvm
1221
1222#endif // LLVM_ADT_IMMUTABLESET_H
1223