1//===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===//
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
7//===----------------------------------------------------------------------===//
10/// This file defines the SmallSet class.
12//===----------------------------------------------------------------------===//
14#ifndef LLVM_ADT_SMALLSET_H
15#define LLVM_ADT_SMALLSET_H
24#include <initializer_list>
30/// SmallSetIterator - This class implements a const_iterator for SmallSet by
31/// delegating to the underlying SmallVector or Set iterators.
32template <
typename T,
unsigned N,
typename C>
35 std::forward_iterator_tag, T> {
37 using SetIterTy =
typename std::set<T, C>::const_iterator;
40 /// Iterators to the parts of the SmallSet containing the data. They are set
41 /// depending on isSmall.
54 // Spell out destructor, copy/move constructor and assignment operators for
55 // MSVC STL, where set<T>::const_iterator is not trivially copy constructible.
67 // Use placement new, to make sure SetIter is properly constructed, even
68 // if it is not trivially copy-able (e.g. in MSVC).
76 // Use placement new, to make sure SetIter is properly constructed, even
77 // if it is not trivially copy-able (e.g. in MSVC).
82 // Call destructor for SetIter, so it gets properly destroyed if it is
83 // not trivially destructible in case we are setting VecIter.
87 IsSmall =
Other.IsSmall;
96 // Call destructor for SetIter, so it gets properly destroyed if it is
97 // not trivially destructible in case we are setting VecIter.
101 IsSmall =
Other.IsSmall;
110 if (IsSmall !=
RHS.IsSmall)
128/// SmallSet - This maintains a set of unique values, optimizing for the case
129/// when the set is small (less than N). In this case, the set can be
130/// maintained with no mallocs. If the set gets large, we expand to using an
131/// std::set to maintain reasonable lookup times.
132template <
typename T,
unsigned N,
typename C = std::less<T>>
134 /// Use a SmallVector to hold the elements here (even though it will never
135 /// reach its 'large' stage) to avoid calling the default ctors of elements
136 /// we will never use.
140 // In small mode SmallPtrSet uses linear search for the elements, so it is
141 // not a good idea to choose this value too high. You may consider using a
142 // DenseSet<> instead if you expect many elements in the set.
143 static_assert(
N <= 32,
"N should be small");
155 template <
typename IterT>
SmallSet(IterT Begin, IterT End) {
159 template <
typename Range>
168 [[nodiscard]]
bool empty()
const {
return Vector.empty() && Set.empty(); }
171 return isSmall() ? Vector.size() : Set.size();
174 /// count - Return 1 if the element is in the set, 0 otherwise.
179 /// insert - Insert an element into the set if it isn't already there.
180 /// Returns a pair. The first value of it is an iterator to the inserted
181 /// element or the existing element in the set. The second value is true
182 /// if the element is inserted (it was not in the set before).
183 std::pair<const_iterator, bool>
insert(
const T &V) {
return insertImpl(V); }
185 std::pair<const_iterator, bool>
insert(
T &&V) {
186 return insertImpl(std::move(V));
189 template <
typename IterT>
203 if (
I != Vector.end()) {
217 return {Vector.begin()};
218 return {Set.begin()};
223 return {Vector.end()};
227 /// Check if the SmallSet contains the given element.
230 return vfind(V) != Vector.end();
231 return Set.find(V) != Set.end();
235 bool isSmall()
const {
return Set.empty(); }
237 template <
typename ArgType>
238 std::pair<const_iterator, bool> insertImpl(ArgType &&V) {
239 static_assert(std::is_convertible_v<ArgType, T>,
240 "ArgType must be convertible to T!");
242 auto [
I, Inserted] = Set.insert(std::forward<ArgType>(V));
247 if (
I != Vector.end())
// Don't reinsert if it already exists.
249 if (Vector.size() <
N) {
250 Vector.push_back(std::forward<ArgType>(V));
253 // Otherwise, grow from vector to set.
254 Set.insert(std::make_move_iterator(Vector.begin()),
255 std::make_move_iterator(Vector.end()));
257 return {
const_iterator(Set.insert(std::forward<ArgType>(V)).first),
true};
260 // Handwritten linear search. The use of std::find might hurt performance as
261 // its implementation may be optimized for larger containers.
263 for (
auto I = Vector.begin(),
E = Vector.end();
I !=
E; ++
I)
270/// If this set is of pointer values, transparently switch over to using
271/// SmallPtrSet for performance.
272template <
typename Po
inteeType,
unsigned N>
275/// Equality comparison for SmallSet.
277/// Iterates over elements of LHS confirming that each element is also a member
278/// of RHS, and that RHS contains no additional values.
279/// Equivalent to N calls to RHS.count.
280/// For small-set mode amortized complexity is O(N^2)
281/// For large-set mode amortized complexity is linear, worst case is O(N^2) (if
282/// every hash collides).
283template <
typename T,
unsigned LN,
unsigned RN,
typename C>
286 if (
LHS.size() !=
RHS.size())
289 // All elements in LHS must also be in RHS
293/// Inequality comparison for SmallSet.
295/// Equivalent to !(LHS == RHS). See operator== for performance notes.
296template <
typename T,
unsigned LN,
unsigned RN,
typename C>
302}
// end namespace llvm
304#endif // LLVM_ADT_SMALLSET_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file contains library features backported from future STL versions.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
SmallSetIterator - This class implements a const_iterator for SmallSet by delegating to the underlyin...
SmallSetIterator & operator=(SmallSetIterator &&Other)
SmallSetIterator & operator++()
bool operator==(const SmallSetIterator &RHS) const
SmallSetIterator(SetIterTy SetIter)
SmallSetIterator & operator=(const SmallSetIterator &Other)
SmallSetIterator(const SmallSetIterator &Other)
const T & operator*() const
SmallSetIterator(VecIterTy VecIter)
SmallSetIterator(SmallSetIterator &&Other)
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
const_iterator begin() const
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
SmallSetIterator< T, N, C > const_iterator
void insert(IterT I, IterT E)
void insert_range(Range &&R)
std::pair< const_iterator, bool > insert(T &&V)
SmallSet(std::initializer_list< T > L)
SmallSet(llvm::from_range_t, Range &&R)
SmallSet & operator=(const SmallSet &)=default
SmallSet(SmallSet &&)=default
SmallSet(const SmallSet &)=default
const_iterator end() const
SmallSet(IterT Begin, IterT End)
bool contains(const T &V) const
Check if the SmallSet contains the given element.
SmallSet & operator=(SmallSet &&)=default
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
bool operator!=(uint64_t V1, const APInt &V2)
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)