• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the SmallSet class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_SMALLSET_H
15 #define LLVM_ADT_SMALLSET_H
16 
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include <set>
20 
21 namespace llvm {
22 
23 /// SmallSet - This maintains a set of unique values, optimizing for the case
24 /// when the set is small (less than N).  In this case, the set can be
25 /// maintained with no mallocs.  If the set gets large, we expand to using an
26 /// std::set to maintain reasonable lookup times.
27 ///
28 /// Note that this set does not provide a way to iterate over members in the
29 /// set.
30 template <typename T, unsigned N>
31 class SmallSet {
32   /// Use a SmallVector to hold the elements here (even though it will never
33   /// reach its 'large' stage) to avoid calling the default ctors of elements
34   /// we will never use.
35   SmallVector<T, N> Vector;
36   std::set<T> Set;
37   typedef typename SmallVector<T, N>::const_iterator VIterator;
38   typedef typename SmallVector<T, N>::iterator mutable_iterator;
39 public:
SmallSet()40   SmallSet() {}
41 
empty()42   bool empty() const { return Vector.empty() && Set.empty(); }
size()43   unsigned size() const {
44     return isSmall() ? Vector.size() : Set.size();
45   }
46 
47   /// count - Return true if the element is in the set.
count(const T & V)48   bool count(const T &V) const {
49     if (isSmall()) {
50       // Since the collection is small, just do a linear search.
51       return vfind(V) != Vector.end();
52     } else {
53       return Set.count(V);
54     }
55   }
56 
57   /// insert - Insert an element into the set if it isn't already there.
insert(const T & V)58   bool insert(const T &V) {
59     if (!isSmall())
60       return Set.insert(V).second;
61 
62     VIterator I = vfind(V);
63     if (I != Vector.end())    // Don't reinsert if it already exists.
64       return false;
65     if (Vector.size() < N) {
66       Vector.push_back(V);
67       return true;
68     }
69 
70     // Otherwise, grow from vector to set.
71     while (!Vector.empty()) {
72       Set.insert(Vector.back());
73       Vector.pop_back();
74     }
75     Set.insert(V);
76     return true;
77   }
78 
79   template <typename IterT>
insert(IterT I,IterT E)80   void insert(IterT I, IterT E) {
81     for (; I != E; ++I)
82       insert(*I);
83   }
84 
erase(const T & V)85   bool erase(const T &V) {
86     if (!isSmall())
87       return Set.erase(V);
88     for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
89       if (*I == V) {
90         Vector.erase(I);
91         return true;
92       }
93     return false;
94   }
95 
clear()96   void clear() {
97     Vector.clear();
98     Set.clear();
99   }
100 private:
isSmall()101   bool isSmall() const { return Set.empty(); }
102 
vfind(const T & V)103   VIterator vfind(const T &V) const {
104     for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
105       if (*I == V)
106         return I;
107     return Vector.end();
108   }
109 };
110 
111 /// If this set is of pointer values, transparently switch over to using
112 /// SmallPtrSet for performance.
113 template <typename PointeeType, unsigned N>
114 class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
115 
116 } // end namespace llvm
117 
118 #endif
119