Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
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 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 "llvm/ADT/iterator.h"
20#include <cstddef>
21#include <functional>
22#include <initializer_list>
23#include <set>
24#include <utility>
25
26namespacellvm {
27
28/// SmallSetIterator - This class implements a const_iterator for SmallSet by
29/// delegating to the underlying SmallVector or Set iterators.
30template <typename T,unsigned N,typename C>
31classSmallSetIterator
32 :publiciterator_facade_base<SmallSetIterator<T, N, C>,
33 std::forward_iterator_tag, T> {
34private:
35usingSetIterTy =typename std::set<T, C>::const_iterator;
36usingVecIterTy =typenameSmallVector<T, N>::const_iterator;
37usingSelfTy =SmallSetIterator<T, N, C>;
38
39 /// Iterators to the parts of the SmallSet containing the data. They are set
40 /// depending on isSmall.
41union{
42 SetIterTySetIter;
43 VecIterTyVecIter;
44 };
45
46bool IsSmall;
47
48public:
49SmallSetIterator(SetIterTySetIter) :SetIter(SetIter), IsSmall(false) {}
50
51SmallSetIterator(VecIterTyVecIter) :VecIter(VecIter), IsSmall(true) {}
52
53// Spell out destructor, copy/move constructor and assignment operators for
54// MSVC STL, where set<T>::const_iterator is not trivially copy constructible.
55~SmallSetIterator() {
56if (IsSmall)
57VecIter.~VecIterTy();
58else
59SetIter.~SetIterTy();
60 }
61
62SmallSetIterator(constSmallSetIterator &Other) : IsSmall(Other.IsSmall) {
63if (IsSmall)
64VecIter =Other.VecIter;
65else
66// Use placement new, to make sure SetIter is properly constructed, even
67// if it is not trivially copy-able (e.g. in MSVC).
68new (&SetIter) SetIterTy(Other.SetIter);
69 }
70
71SmallSetIterator(SmallSetIterator &&Other) : IsSmall(Other.IsSmall) {
72if (IsSmall)
73VecIter = std::move(Other.VecIter);
74else
75// Use placement new, to make sure SetIter is properly constructed, even
76// if it is not trivially copy-able (e.g. in MSVC).
77new (&SetIter) SetIterTy(std::move(Other.SetIter));
78 }
79
80SmallSetIterator&operator=(constSmallSetIterator&Other) {
81// Call destructor for SetIter, so it gets properly destroyed if it is
82// not trivially destructible in case we are setting VecIter.
83if (!IsSmall)
84SetIter.~SetIterTy();
85
86 IsSmall =Other.IsSmall;
87if (IsSmall)
88VecIter =Other.VecIter;
89else
90new (&SetIter) SetIterTy(Other.SetIter);
91return *this;
92 }
93
94SmallSetIterator&operator=(SmallSetIterator&&Other) {
95// Call destructor for SetIter, so it gets properly destroyed if it is
96// not trivially destructible in case we are setting VecIter.
97if (!IsSmall)
98SetIter.~SetIterTy();
99
100 IsSmall =Other.IsSmall;
101if (IsSmall)
102VecIter = std::move(Other.VecIter);
103else
104new (&SetIter) SetIterTy(std::move(Other.SetIter));
105return *this;
106 }
107
108booloperator==(constSmallSetIterator &RHS) const{
109if (IsSmall !=RHS.IsSmall)
110returnfalse;
111if (IsSmall)
112returnVecIter ==RHS.VecIter;
113returnSetIter ==RHS.SetIter;
114 }
115
116SmallSetIterator &operator++() {// Preincrement
117if (IsSmall)
118 ++VecIter;
119else
120 ++SetIter;
121return *this;
122 }
123
124constT &operator*() const{return IsSmall ? *VecIter : *SetIter; }
125};
126
127/// SmallSet - This maintains a set of unique values, optimizing for the case
128/// when the set is small (less than N). In this case, the set can be
129/// maintained with no mallocs. If the set gets large, we expand to using an
130/// std::set to maintain reasonable lookup times.
131template <typename T,unsigned N,typename C = std::less<T>>
132classSmallSet {
133 /// Use a SmallVector to hold the elements here (even though it will never
134 /// reach its 'large' stage) to avoid calling the default ctors of elements
135 /// we will never use.
136SmallVector<T, N> Vector;
137 std::set<T, C> Set;
138
139// In small mode SmallPtrSet uses linear search for the elements, so it is
140// not a good idea to choose this value too high. You may consider using a
141// DenseSet<> instead if you expect many elements in the set.
142static_assert(N <= 32,"N should be small");
143
144public:
145usingkey_type =T;
146usingsize_type = size_t;
147usingvalue_type =T;
148usingconst_iterator =SmallSetIterator<T, N, C>;
149
150SmallSet() =default;
151SmallSet(constSmallSet &) =default;
152SmallSet(SmallSet &&) =default;
153
154template <typename IterT>SmallSet(IterT Begin, IterTEnd) {
155insert(Begin,End);
156 }
157
158template <typename RangeT>
159explicitSmallSet(constiterator_range<RangeT> &R) {
160insert(R.begin(), R.end());
161 }
162
163SmallSet(std::initializer_list<T> L) {insert(L.begin(), L.end()); }
164
165SmallSet &operator=(constSmallSet &) =default;
166SmallSet &operator=(SmallSet &&) =default;
167
168 [[nodiscard]]boolempty() const{return Vector.empty() && Set.empty(); }
169
170size_typesize() const{
171return isSmall() ? Vector.size() : Set.size();
172 }
173
174 /// count - Return 1 if the element is in the set, 0 otherwise.
175size_typecount(constT &V) const{returncontains(V) ? 1 : 0; }
176
177 /// insert - Insert an element into the set if it isn't already there.
178 /// Returns a pair. The first value of it is an iterator to the inserted
179 /// element or the existing element in the set. The second value is true
180 /// if the element is inserted (it was not in the set before).
181 std::pair<const_iterator, bool>insert(constT &V) {return insertImpl(V); }
182
183 std::pair<const_iterator, bool>insert(T &&V) {
184return insertImpl(std::move(V));
185 }
186
187template <typename IterT>
188voidinsert(IterTI, IterTE) {
189for (;I !=E; ++I)
190insert(*I);
191 }
192
193boolerase(constT &V) {
194if (!isSmall())
195return Set.erase(V);
196autoI = vfind(V);
197if (I != Vector.end()) {
198 Vector.erase(I);
199returntrue;
200 }
201returnfalse;
202 }
203
204voidclear() {
205 Vector.clear();
206 Set.clear();
207 }
208
209const_iteratorbegin() const{
210if (isSmall())
211return {Vector.begin()};
212return {Set.begin()};
213 }
214
215const_iteratorend() const{
216if (isSmall())
217return {Vector.end()};
218return {Set.end()};
219 }
220
221 /// Check if the SmallSet contains the given element.
222boolcontains(constT &V) const{
223if (isSmall())
224return vfind(V) != Vector.end();
225return Set.find(V) != Set.end();
226 }
227
228private:
229bool isSmall() const{return Set.empty(); }
230
231template <typename ArgType>
232 std::pair<const_iterator, bool> insertImpl(ArgType &&V) {
233static_assert(std::is_convertible_v<ArgType, T>,
234"ArgType must be convertible to T!");
235if (!isSmall()) {
236auto [I, Inserted] = Set.insert(std::forward<ArgType>(V));
237return {const_iterator(I), Inserted};
238 }
239
240autoI = vfind(V);
241if (I !=Vector.end())// Don't reinsert if it already exists.
242return {const_iterator(I),false};
243if (Vector.size() <N) {
244Vector.push_back(std::forward<ArgType>(V));
245return {const_iterator(std::prev(Vector.end())),true};
246 }
247// Otherwise, grow from vector to set.
248Set.insert(std::make_move_iterator(Vector.begin()),
249 std::make_move_iterator(Vector.end()));
250Vector.clear();
251return {const_iterator(Set.insert(std::forward<ArgType>(V)).first),true};
252 }
253
254// Handwritten linear search. The use of std::find might hurt performance as
255// its implementation may be optimized for larger containers.
256typename SmallVector<T, N>::const_iterator vfind(constT &V) const{
257for (autoI =Vector.begin(),E =Vector.end();I !=E; ++I)
258if (*I == V)
259returnI;
260returnVector.end();
261 }
262};
263
264/// If this set is of pointer values, transparently switch over to using
265/// SmallPtrSet for performance.
266template <typename PointeeType,unsigned N>
267classSmallSet<PointeeType*,N> :publicSmallPtrSet<PointeeType*, N> {};
268
269/// Equality comparison for SmallSet.
270///
271/// Iterates over elements of LHS confirming that each element is also a member
272/// of RHS, and that RHS contains no additional values.
273/// Equivalent to N calls to RHS.count.
274/// For small-set mode amortized complexity is O(N^2)
275/// For large-set mode amortized complexity is linear, worst case is O(N^2) (if
276/// every hash collides).
277template <typename T,unsigned LN,unsigned RN,typename C>
278booloperator==(constSmallSet<T, LN, C> &LHS,constSmallSet<T, RN, C> &RHS) {
279if (LHS.size() !=RHS.size())
280returnfalse;
281
282// All elements in LHS must also be in RHS
283returnall_of(LHS, [&RHS](constT &E) {returnRHS.count(E); });
284}
285
286/// Inequality comparison for SmallSet.
287///
288/// Equivalent to !(LHS == RHS). See operator== for performance notes.
289template <typename T,unsigned LN,unsigned RN,typename C>
290booloperator!=(constSmallSet<T, LN, C> &LHS,constSmallSet<T, RN, C> &RHS) {
291return !(LHS ==RHS);
292}
293
294}// end namespace llvm
295
296#endif// LLVM_ADT_SMALLSET_H
true
basic Basic Alias true
Definition:BasicAliasAnalysis.cpp:1981
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
End
bool End
Definition:ELF_riscv.cpp:480
I
#define I(x, y, z)
Definition:MD5.cpp:58
T
#define T
Definition:Mips16ISelLowering.cpp:341
SmallPtrSet.h
This file defines the SmallPtrSet class.
SmallVector.h
This file defines the SmallVector class.
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
T
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallSetIterator
SmallSetIterator - This class implements a const_iterator for SmallSet by delegating to the underlyin...
Definition:SmallSet.h:33
llvm::SmallSetIterator::operator=
SmallSetIterator & operator=(SmallSetIterator &&Other)
Definition:SmallSet.h:94
llvm::SmallSetIterator::operator++
SmallSetIterator & operator++()
Definition:SmallSet.h:116
llvm::SmallSetIterator::operator==
bool operator==(const SmallSetIterator &RHS) const
Definition:SmallSet.h:108
llvm::SmallSetIterator::SetIter
SetIterTy SetIter
Definition:SmallSet.h:42
llvm::SmallSetIterator::SmallSetIterator
SmallSetIterator(SetIterTy SetIter)
Definition:SmallSet.h:49
llvm::SmallSetIterator::operator=
SmallSetIterator & operator=(const SmallSetIterator &Other)
Definition:SmallSet.h:80
llvm::SmallSetIterator::SmallSetIterator
SmallSetIterator(const SmallSetIterator &Other)
Definition:SmallSet.h:62
llvm::SmallSetIterator::operator*
const T & operator*() const
Definition:SmallSet.h:124
llvm::SmallSetIterator::VecIter
VecIterTy VecIter
Definition:SmallSet.h:43
llvm::SmallSetIterator::~SmallSetIterator
~SmallSetIterator()
Definition:SmallSet.h:55
llvm::SmallSetIterator::SmallSetIterator
SmallSetIterator(VecIterTy VecIter)
Definition:SmallSet.h:51
llvm::SmallSetIterator::SmallSetIterator
SmallSetIterator(SmallSetIterator &&Other)
Definition:SmallSet.h:71
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition:SmallSet.h:132
llvm::SmallSet::SmallSet
SmallSet(const iterator_range< RangeT > &R)
Definition:SmallSet.h:159
llvm::SmallSet::begin
const_iterator begin() const
Definition:SmallSet.h:209
llvm::SmallSet::count
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition:SmallSet.h:175
llvm::SmallSet::const_iterator
SmallSetIterator< T, N, C > const_iterator
Definition:SmallSet.h:148
llvm::SmallSet::empty
bool empty() const
Definition:SmallSet.h:168
llvm::SmallSet::insert
void insert(IterT I, IterT E)
Definition:SmallSet.h:188
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(T &&V)
Definition:SmallSet.h:183
llvm::SmallSet::SmallSet
SmallSet(std::initializer_list< T > L)
Definition:SmallSet.h:163
llvm::SmallSet::SmallSet
SmallSet()=default
llvm::SmallSet::erase
bool erase(const T &V)
Definition:SmallSet.h:193
llvm::SmallSet::size_type
size_t size_type
Definition:SmallSet.h:146
llvm::SmallSet::clear
void clear()
Definition:SmallSet.h:204
llvm::SmallSet::operator=
SmallSet & operator=(const SmallSet &)=default
llvm::SmallSet::SmallSet
SmallSet(SmallSet &&)=default
llvm::SmallSet::SmallSet
SmallSet(const SmallSet &)=default
llvm::SmallSet::end
const_iterator end() const
Definition:SmallSet.h:215
llvm::SmallSet::SmallSet
SmallSet(IterT Begin, IterT End)
Definition:SmallSet.h:154
llvm::SmallSet::contains
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition:SmallSet.h:222
llvm::SmallSet::operator=
SmallSet & operator=(SmallSet &&)=default
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition:SmallSet.h:181
llvm::SmallSet::size
size_type size() const
Definition:SmallSet.h:170
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::iterator_facade_base
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition:iterator.h:80
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition:iterator_range.h:42
iterator.h
false
Definition:StackSlotColoring.cpp:193
llvm::AMDGPU::Vector
@ Vector
Definition:AMDGPURegBankLegalizeRules.h:178
llvm::objcopy::AdjustKind::Set
@ Set
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1739
llvm::operator!=
bool operator!=(uint64_t V1, const APInt &V2)
Definition:APInt.h:2082
llvm::operator==
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
Definition:AddressRanges.h:153
llvm::IRMemLocation::Other
@ Other
Any other memory.
N
#define N

Generated on Sun Jul 20 2025 02:34:07 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp