14#ifndef WPIUTIL_WPI_SMALLSET_H
15#define WPIUTIL_WPI_SMALLSET_H
18#include "wpi/util/SmallVector.hpp"
22#include <initializer_list>
30template <
typename T,
unsigned N,
typename C>
33 std::forward_iterator_tag, T> {
35 using SetIterTy =
typename std::set<T, C>::const_iterator;
36 using VecIterTy =
typename SmallVector<T, N>::const_iterator;
73 VecIter = std::move(Other.VecIter);
77 new (&
SetIter) SetIterTy(std::move(Other.SetIter));
86 IsSmall = Other.IsSmall;
100 IsSmall = Other.IsSmall;
102 VecIter = std::move(Other.VecIter);
104 new (&
SetIter) SetIterTy(std::move(Other.SetIter));
109 if (IsSmall != RHS.IsSmall)
131template <
typename T,
unsigned N,
typename C = std::less<T>>
136 SmallVector<T, N> Vector;
142 static_assert(N <= 32,
"N should be small");
154 template <
typename IterT>
SmallSet(IterT Begin, IterT End) {
158 template <
typename RangeT>
168 [[nodiscard]]
bool empty()
const {
return Vector.empty() && Set.empty(); }
171 return isSmall() ? Vector.size() : Set.size();
181 std::pair<const_iterator, bool>
insert(
const T &V) {
return insertImpl(V); }
183 std::pair<const_iterator, bool>
insert(T &&V) {
184 return insertImpl(std::move(V));
187 template <
typename IterT>
197 if (I != Vector.end()) {
211 return {Vector.begin()};
212 return {Set.begin()};
217 return {Vector.end()};
224 return vfind(V) != Vector.end();
225 return Set.find(V) != Set.end();
229 bool isSmall()
const {
return Set.empty(); }
231 template <
typename ArgType>
232 std::pair<const_iterator, bool> insertImpl(ArgType &&V) {
233 static_assert(std::is_convertible_v<ArgType, T>,
234 "ArgType must be convertible to T!");
236 auto [I, Inserted] = Set.insert(std::forward<ArgType>(V));
241 if (I != Vector.end())
243 if (Vector.size() < N) {
244 Vector.push_back(std::forward<ArgType>(V));
248 Set.insert(std::make_move_iterator(Vector.begin()),
249 std::make_move_iterator(Vector.end()));
251 return {
const_iterator(Set.insert(std::forward<ArgType>(V)).first),
true};
256 typename SmallVector<T, N>::const_iterator vfind(
const T &V)
const {
257 for (
auto I = Vector.begin(), E = Vector.end(); I != E; ++I)
266template <
typename Po
inteeType,
unsigned N>
277template <
typename T,
unsigned LN,
unsigned RN,
typename C>
283 return std::all_of(LHS.
begin(), LHS.
end(), [&RHS](
const T &E) { return RHS.count(E); });
289template <
typename T,
unsigned LN,
unsigned RN,
typename C>
291 return !(LHS == RHS);
This file defines the SmallPtrSet class.
SmallPtrSet()
Definition SmallPtrSet.hpp:543
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.hpp:132
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.hpp:175
std::pair< const_iterator, bool > insert(T &&V)
Definition SmallSet.hpp:183
void clear()
Definition SmallSet.hpp:204
SmallSetIterator< T, N, C > const_iterator
Definition SmallSet.hpp:148
SmallSet(const iterator_range< RangeT > &R)
Definition SmallSet.hpp:159
SmallSet & operator=(SmallSet &&)=default
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.hpp:222
const_iterator begin() const
Definition SmallSet.hpp:209
bool erase(const T &V)
Definition SmallSet.hpp:193
SmallSet & operator=(const SmallSet &)=default
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.hpp:181
const_iterator end() const
Definition SmallSet.hpp:215
size_t size_type
Definition SmallSet.hpp:146
void insert(IterT I, IterT E)
Definition SmallSet.hpp:188
size_type size() const
Definition SmallSet.hpp:170
SmallSet(IterT Begin, IterT End)
Definition SmallSet.hpp:154
T key_type
Definition SmallSet.hpp:145
T value_type
Definition SmallSet.hpp:147
SmallSet(SmallSet &&)=default
SmallSet(std::initializer_list< T > L)
Definition SmallSet.hpp:163
SmallSet(const SmallSet &)=default
bool empty() const
Definition SmallSet.hpp:168
SmallSetIterator - This class implements a const_iterator for SmallSet by delegating to the underlyin...
Definition SmallSet.hpp:33
~SmallSetIterator()
Definition SmallSet.hpp:55
SmallSetIterator & operator++()
Definition SmallSet.hpp:116
SmallSetIterator(const SmallSetIterator &Other)
Definition SmallSet.hpp:62
SmallSetIterator & operator=(const SmallSetIterator &Other)
Definition SmallSet.hpp:80
SetIterTy SetIter
Definition SmallSet.hpp:42
SmallSetIterator(SetIterTy SetIter)
Definition SmallSet.hpp:49
SmallSetIterator & operator=(SmallSetIterator &&Other)
Definition SmallSet.hpp:94
bool operator==(const SmallSetIterator &RHS) const
Definition SmallSet.hpp:108
SmallSetIterator(SmallSetIterator &&Other)
Definition SmallSet.hpp:71
const T & operator*() const
Definition SmallSet.hpp:124
SmallSetIterator(VecIterTy VecIter)
Definition SmallSet.hpp:51
VecIterTy VecIter
Definition SmallSet.hpp:43
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.hpp:80
A range adaptor for a pair of iterators.
Definition iterator_range.hpp:42
IteratorT end() const
Definition iterator_range.hpp:65
IteratorT begin() const
Definition iterator_range.hpp:64
Definition raw_os_ostream.hpp:19
constexpr bool operator!=(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition expected:180
constexpr bool operator==(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition expected:176