Recommit r208506: DebugInfo: Include lexical scopes in inlined subroutines.
[oota-llvm.git] / include / llvm / ADT / SmallSet.h
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/SmallPtrSet.h"
18 #include "llvm/ADT/SmallVector.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,  typename C = std::less<T> >
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, C> Set;
37   typedef typename SmallVector<T, N>::const_iterator VIterator;
38   typedef typename SmallVector<T, N>::iterator mutable_iterator;
39 public:
40   SmallSet() {}
41
42   bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const {
43     return Vector.empty() && Set.empty();
44   }
45
46   unsigned size() const {
47     return isSmall() ? Vector.size() : Set.size();
48   }
49
50   /// count - Return 1 if the element is in the set, 0 otherwise.
51   unsigned count(const T &V) const {
52     if (isSmall()) {
53       // Since the collection is small, just do a linear search.
54       return vfind(V) == Vector.end() ? 0 : 1;
55     } else {
56       return Set.count(V);
57     }
58   }
59
60   /// insert - Insert an element into the set if it isn't already there.
61   /// Returns true if the element is inserted (it was not in the set before).
62   bool insert(const T &V) {
63     if (!isSmall())
64       return Set.insert(V).second;
65
66     VIterator I = vfind(V);
67     if (I != Vector.end())    // Don't reinsert if it already exists.
68       return false;
69     if (Vector.size() < N) {
70       Vector.push_back(V);
71       return true;
72     }
73
74     // Otherwise, grow from vector to set.
75     while (!Vector.empty()) {
76       Set.insert(Vector.back());
77       Vector.pop_back();
78     }
79     Set.insert(V);
80     return true;
81   }
82
83   template <typename IterT>
84   void insert(IterT I, IterT E) {
85     for (; I != E; ++I)
86       insert(*I);
87   }
88   
89   bool erase(const T &V) {
90     if (!isSmall())
91       return Set.erase(V);
92     for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
93       if (*I == V) {
94         Vector.erase(I);
95         return true;
96       }
97     return false;
98   }
99
100   void clear() {
101     Vector.clear();
102     Set.clear();
103   }
104 private:
105   bool isSmall() const { return Set.empty(); }
106
107   VIterator vfind(const T &V) const {
108     for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
109       if (*I == V)
110         return I;
111     return Vector.end();
112   }
113 };
114
115 /// If this set is of pointer values, transparently switch over to using
116 /// SmallPtrSet for performance.
117 template <typename PointeeType, unsigned N>
118 class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
119
120 } // end namespace llvm
121
122 #endif