WPILibC++ 2023.4.3
SmallSet.h
Go to the documentation of this file.
1//===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- 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/// \file
10/// This file defines the SmallSet class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef WPIUTIL_WPI_SMALLSET_H
15#define WPIUTIL_WPI_SMALLSET_H
16
17#include "wpi/SmallPtrSet.h"
18#include "wpi/SmallVector.h"
19#include "wpi/iterator.h"
20#include "wpi/Compiler.h"
21#include "wpi/type_traits.h"
22#include <cstddef>
23#include <functional>
24#include <optional>
25#include <set>
26#include <type_traits>
27#include <utility>
28
29namespace wpi {
30
31/// SmallSetIterator - This class implements a const_iterator for SmallSet by
32/// delegating to the underlying SmallVector or Set iterators.
33template <typename T, unsigned N, typename C>
35 : public iterator_facade_base<SmallSetIterator<T, N, C>,
36 std::forward_iterator_tag, T> {
37private:
38 using SetIterTy = typename std::set<T, C>::const_iterator;
39 using VecIterTy = typename SmallVector<T, N>::const_iterator;
41
42 /// Iterators to the parts of the SmallSet containing the data. They are set
43 /// depending on isSmall.
44 union {
45 SetIterTy SetIter;
46 VecIterTy VecIter;
47 };
48
49 bool isSmall;
50
51public:
52 SmallSetIterator(SetIterTy SetIter) : SetIter(SetIter), isSmall(false) {}
53
54 SmallSetIterator(VecIterTy VecIter) : VecIter(VecIter), isSmall(true) {}
55
56 // Spell out destructor, copy/move constructor and assignment operators for
57 // MSVC STL, where set<T>::const_iterator is not trivially copy constructible.
59 if (isSmall)
60 VecIter.~VecIterTy();
61 else
62 SetIter.~SetIterTy();
63 }
64
65 SmallSetIterator(const SmallSetIterator &Other) : isSmall(Other.isSmall) {
66 if (isSmall)
67 VecIter = Other.VecIter;
68 else
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);
72 }
73
74 SmallSetIterator(SmallSetIterator &&Other) : isSmall(Other.isSmall) {
75 if (isSmall)
76 VecIter = std::move(Other.VecIter);
77 else
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));
81 }
82
84 // Call destructor for SetIter, so it gets properly destroyed if it is
85 // not trivially destructible in case we are setting VecIter.
86 if (!isSmall)
87 SetIter.~SetIterTy();
88
89 isSmall = Other.isSmall;
90 if (isSmall)
91 VecIter = Other.VecIter;
92 else
93 new (&SetIter) SetIterTy(Other.SetIter);
94 return *this;
95 }
96
98 // Call destructor for SetIter, so it gets properly destroyed if it is
99 // not trivially destructible in case we are setting VecIter.
100 if (!isSmall)
101 SetIter.~SetIterTy();
102
103 isSmall = Other.isSmall;
104 if (isSmall)
105 VecIter = std::move(Other.VecIter);
106 else
107 new (&SetIter) SetIterTy(std::move(Other.SetIter));
108 return *this;
109 }
110
111 bool operator==(const SmallSetIterator &RHS) const {
112 if (isSmall != RHS.isSmall)
113 return false;
114 if (isSmall)
115 return VecIter == RHS.VecIter;
116 return SetIter == RHS.SetIter;
117 }
118
119 SmallSetIterator &operator++() { // Preincrement
120 if (isSmall)
121 VecIter++;
122 else
123 SetIter++;
124 return *this;
125 }
126
127 const T &operator*() const { return isSmall ? *VecIter : *SetIter; }
128};
129
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.
134template <typename T, unsigned N, typename C = std::less<T>>
135class SmallSet {
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;
140 std::set<T, C> Set;
141
142 using VIterator = typename SmallVector<T, N>::const_iterator;
143 using mutable_iterator = typename SmallVector<T, N>::iterator;
144
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");
149
150public:
151 using size_type = size_t;
153
154 SmallSet() = default;
155
156 LLVM_NODISCARD bool empty() const {
157 return Vector.empty() && Set.empty();
158 }
159
160 size_type size() const {
161 return isSmall() ? Vector.size() : Set.size();
162 }
163
164 /// count - Return 1 if the element is in the set, 0 otherwise.
165 size_type count(const T &V) const {
166 if (isSmall()) {
167 // Since the collection is small, just do a linear search.
168 return vfind(V) == Vector.end() ? 0 : 1;
169 } else {
170 return Set.count(V);
171 }
172 }
173
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
178 /// concept.
179 // FIXME: Add iterators that abstract over the small and large form, and then
180 // return those here.
181 std::pair<std::nullopt_t, bool> insert(const T &V) {
182 if (!isSmall())
183 return std::make_pair(std::nullopt, Set.insert(V).second);
184
185 VIterator I = vfind(V);
186 if (I != Vector.end()) // Don't reinsert if it already exists.
187 return std::make_pair(std::nullopt, false);
188 if (Vector.size() < N) {
189 Vector.push_back(V);
190 return std::make_pair(std::nullopt, true);
191 }
192
193 // Otherwise, grow from vector to set.
194 while (!Vector.empty()) {
195 Set.insert(Vector.back());
196 Vector.pop_back();
197 }
198 Set.insert(V);
199 return std::make_pair(std::nullopt, true);
200 }
201
202 template <typename IterT>
203 void insert(IterT I, IterT E) {
204 for (; I != E; ++I)
205 insert(*I);
206 }
207
208 bool erase(const T &V) {
209 if (!isSmall())
210 return Set.erase(V);
211 for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
212 if (*I == V) {
213 Vector.erase(I);
214 return true;
215 }
216 return false;
217 }
218
219 void clear() {
220 Vector.clear();
221 Set.clear();
222 }
223
225 if (isSmall())
226 return {Vector.begin()};
227 return {Set.begin()};
228 }
229
231 if (isSmall())
232 return {Vector.end()};
233 return {Set.end()};
234 }
235
236 /// Check if the SmallSet contains the given element.
237 bool contains(const T &V) const {
238 if (isSmall())
239 return vfind(V) != Vector.end();
240 return Set.find(V) != Set.end();
241 }
242
243private:
244 bool isSmall() const { return Set.empty(); }
245
246 VIterator vfind(const T &V) const {
247 for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
248 if (*I == V)
249 return I;
250 return Vector.end();
251 }
252};
253
254/// If this set is of pointer values, transparently switch over to using
255/// SmallPtrSet for performance.
256template <typename PointeeType, unsigned N>
257class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
258
259/// Equality comparison for SmallSet.
260///
261/// Iterates over elements of LHS confirming that each element is also a member
262/// of RHS, and that RHS contains no additional values.
263/// Equivalent to N calls to RHS.count.
264/// For small-set mode amortized complexity is O(N^2)
265/// For large-set mode amortized complexity is linear, worst case is O(N^2) (if
266/// every hash collides).
267template <typename T, unsigned LN, unsigned RN, typename C>
269 if (LHS.size() != RHS.size())
270 return false;
271
272 // All elements in LHS must also be in RHS
273 return std::all_of(LHS.begin(), LHS.end(), [&RHS](const T &E) { return RHS.count(E); });
274}
275
276/// Inequality comparison for SmallSet.
277///
278/// Equivalent to !(LHS == RHS). See operator== for performance notes.
279template <typename T, unsigned LN, unsigned RN, typename C>
281 return !(LHS == RHS);
282}
283
284} // end namespace wpi
285
286#endif // WPIUTIL_WPI_SMALLSET_H
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:177
This file defines the SmallPtrSet class.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
bool erase(const T &V)
Definition: SmallSet.h:208
const_iterator end() const
Definition: SmallSet.h:230
std::pair< std::nullopt_t, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
size_t size_type
Definition: SmallSet.h:151
void insert(IterT I, IterT E)
Definition: SmallSet.h:203
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition: SmallSet.h:237
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:165
LLVM_NODISCARD bool empty() const
Definition: SmallSet.h:156
SmallSet()=default
void clear()
Definition: SmallSet.h:219
const_iterator begin() const
Definition: SmallSet.h:224
size_type size() const
Definition: SmallSet.h:160
SmallSetIterator - This class implements a const_iterator for SmallSet by delegating to the underlyin...
Definition: SmallSet.h:36
SmallSetIterator(const SmallSetIterator &Other)
Definition: SmallSet.h:65
SmallSetIterator(SetIterTy SetIter)
Definition: SmallSet.h:52
bool operator==(const SmallSetIterator &RHS) const
Definition: SmallSet.h:111
SetIterTy SetIter
Definition: SmallSet.h:45
SmallSetIterator(VecIterTy VecIter)
Definition: SmallSet.h:54
~SmallSetIterator()
Definition: SmallSet.h:58
const T & operator*() const
Definition: SmallSet.h:127
SmallSetIterator & operator++()
Definition: SmallSet.h:119
SmallSetIterator & operator=(const SmallSetIterator &Other)
Definition: SmallSet.h:83
VecIterTy VecIter
Definition: SmallSet.h:46
SmallSetIterator(SmallSetIterator &&Other)
Definition: SmallSet.h:74
SmallSetIterator & operator=(SmallSetIterator &&Other)
Definition: SmallSet.h:97
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1186
iterator erase(const_iterator CI)
Definition: SmallVector.h:727
void clear()
Definition: SmallVector.h:594
void pop_back()
Definition: SmallVector.h:415
void push_back(const T &Elt)
Definition: SmallVector.h:403
size_t size() const
Definition: SmallVector.h:78
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:81
iterator begin()
Definition: SmallVector.h:252
iterator end()
Definition: SmallVector.h:254
reference back()
Definition: SmallVector.h:293
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
bool operator!=(PointerUnion< PTs... > lhs, PointerUnion< PTs... > rhs)
Definition: PointerUnion.h:244
bool operator==(PointerUnion< PTs... > lhs, PointerUnion< PTs... > rhs)
Definition: PointerUnion.h:239