1 //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SmallSet class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLSET_H
15 #define LLVM_ADT_SMALLSET_H
17 #include "llvm/ADT/None.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/type_traits.h"
26 #include <type_traits>
31 /// SmallSetIterator - This class implements a const_iterator for SmallSet by
32 /// delegating to the underlying SmallVector or Set iterators.
33 template <typename T, unsigned N, typename C>
34 class SmallSetIterator
35 : public iterator_facade_base<SmallSetIterator<T, N, C>,
36 std::forward_iterator_tag, T> {
38 using SetIterTy = typename std::set<T, C>::const_iterator;
39 using VecIterTy = typename SmallVector<T, N>::const_iterator;
40 using SelfTy = SmallSetIterator<T, N, C>;
42 /// Iterators to the parts of the SmallSet containing the data. They are set
43 /// depending on isSmall.
52 SmallSetIterator(SetIterTy SetIter) : SetIter(SetIter), isSmall(false) {}
54 SmallSetIterator(VecIterTy VecIter) : VecIter(VecIter), isSmall(true) {}
56 // Spell out destructor, copy/move constructor and assignment operators for
57 // MSVC STL, where set<T>::const_iterator is not trivially copy constructible.
65 SmallSetIterator(const SmallSetIterator &Other) : isSmall(Other.isSmall) {
67 VecIter = Other.VecIter;
69 // Use placement new, to make sure SetIter is properly constructed, even
70 // if it is not trivially copy-able (e.g. in MSVC).
71 new (&SetIter) SetIterTy(Other.SetIter);
74 SmallSetIterator(SmallSetIterator &&Other) : isSmall(Other.isSmall) {
76 VecIter = std::move(Other.VecIter);
78 // Use placement new, to make sure SetIter is properly constructed, even
79 // if it is not trivially copy-able (e.g. in MSVC).
80 new (&SetIter) SetIterTy(std::move(Other.SetIter));
83 SmallSetIterator& operator=(const SmallSetIterator& Other) {
84 // Call destructor for SetIter, so it gets properly destroyed if it is
85 // not trivially destructible in case we are setting VecIter.
89 isSmall = Other.isSmall;
91 VecIter = Other.VecIter;
93 new (&SetIter) SetIterTy(Other.SetIter);
97 SmallSetIterator& operator=(SmallSetIterator&& Other) {
98 // Call destructor for SetIter, so it gets properly destroyed if it is
99 // not trivially destructible in case we are setting VecIter.
101 SetIter.~SetIterTy();
103 isSmall = Other.isSmall;
105 VecIter = std::move(Other.VecIter);
107 new (&SetIter) SetIterTy(std::move(Other.SetIter));
111 bool operator==(const SmallSetIterator &RHS) const {
112 if (isSmall != RHS.isSmall)
115 return VecIter == RHS.VecIter;
116 return SetIter == RHS.SetIter;
119 SmallSetIterator &operator++() { // Preincrement
127 const T &operator*() const { return isSmall ? *VecIter : *SetIter; }
130 /// SmallSet - This maintains a set of unique values, optimizing for the case
131 /// when the set is small (less than N). In this case, the set can be
132 /// maintained with no mallocs. If the set gets large, we expand to using an
133 /// std::set to maintain reasonable lookup times.
134 template <typename T, unsigned N, typename C = std::less<T>>
136 /// Use a SmallVector to hold the elements here (even though it will never
137 /// reach its 'large' stage) to avoid calling the default ctors of elements
138 /// we will never use.
139 SmallVector<T, N> Vector;
142 using VIterator = typename SmallVector<T, N>::const_iterator;
143 using mutable_iterator = typename SmallVector<T, N>::iterator;
145 // In small mode SmallPtrSet uses linear search for the elements, so it is
146 // not a good idea to choose this value too high. You may consider using a
147 // DenseSet<> instead if you expect many elements in the set.
148 static_assert(N <= 32, "N should be small");
151 using size_type = size_t;
152 using const_iterator = SmallSetIterator<T, N, C>;
154 SmallSet() = default;
156 LLVM_NODISCARD bool empty() const {
157 return Vector.empty() && Set.empty();
160 size_type size() const {
161 return isSmall() ? Vector.size() : Set.size();
164 /// count - Return 1 if the element is in the set, 0 otherwise.
165 size_type count(const T &V) const {
167 // Since the collection is small, just do a linear search.
168 return vfind(V) == Vector.end() ? 0 : 1;
174 /// insert - Insert an element into the set if it isn't already there.
175 /// Returns true if the element is inserted (it was not in the set before).
176 /// The first value of the returned pair is unused and provided for
177 /// partial compatibility with the standard library self-associative container
179 // FIXME: Add iterators that abstract over the small and large form, and then
180 // return those here.
181 std::pair<NoneType, bool> insert(const T &V) {
183 return std::make_pair(None, Set.insert(V).second);
185 VIterator I = vfind(V);
186 if (I != Vector.end()) // Don't reinsert if it already exists.
187 return std::make_pair(None, false);
188 if (Vector.size() < N) {
190 return std::make_pair(None, true);
193 // Otherwise, grow from vector to set.
194 while (!Vector.empty()) {
195 Set.insert(Vector.back());
199 return std::make_pair(None, true);
202 template <typename IterT>
203 void insert(IterT I, IterT E) {
208 bool erase(const T &V) {
211 for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
224 const_iterator begin() const {
226 return {Vector.begin()};
227 return {Set.begin()};
230 const_iterator end() const {
232 return {Vector.end()};
237 bool isSmall() const { return Set.empty(); }
239 VIterator vfind(const T &V) const {
240 for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
247 /// If this set is of pointer values, transparently switch over to using
248 /// SmallPtrSet for performance.
249 template <typename PointeeType, unsigned N>
250 class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
252 } // end namespace llvm
254 #endif // LLVM_ADT_SMALLSET_H