Loading...
Searching...
No Matches
HashSet.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::HashSet
29
30Description
31 A HashTable with keys but without contents that is similar to
32 \c std::unordered_set.
33
34 The entries are considered \a unordered since their placement
35 depends on the method used to generate the hash key index, the
36 table capacity, insertion order etc. When the key order is
37 important, use the sortedToc() method to obtain a list of sorted
38 keys and use that for further access.
39
40Note
41 The HashSet iterator dereferences to the key, so the following
42 range-for works as expected:
43 \code
44 labelHashSet someLabels{10, 20, 30, 40, ...};
45 for (const label i : someLabels)
46 {
47 Info<< "val:" << i << nl;
48 }
49 \endcode
50
51Typedef
52 Foam::wordHashSet
53
54Description
55 A HashSet with word keys and string hasher.
56
57Typedef
58 Foam::labelHashSet
59
60Description
61 A HashSet with label keys and label hasher.
62
63\*---------------------------------------------------------------------------*/
64
65#ifndef Foam_HashSet_H
66#define Foam_HashSet_H
67
68#include "HashTable.H"
69#include "IndirectList.H"
70
71// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72
73namespace Foam
74{
75
76// Forward Declarations
77template<class T> class MinMax;
78template<class Key, class Hash> class HashSet;
79
80// Common hash-set types
81
82//- A HashSet of words, uses string hasher.
84
85//- A HashSet of labels, uses label hasher.
87
88
89/*---------------------------------------------------------------------------*\
90 Class HashSet Declaration
91\*---------------------------------------------------------------------------*/
92
93template<class Key, class Hash=Foam::Hash<Key>>
94class HashSet
95:
96 public HashTable<Foam::zero, Key, Hash>
97{
98 // Private Member Functions
99
100 //- Assign from the input iterator range
101 template<class InputIter>
102 inline label assignMany
103 (
104 const label nItems,
105 InputIter first,
106 InputIter last
107 );
108
109
110public:
111
112 //- The template instance used for this HashSet
114
115 //- The template instance used for the parent HashTable
117
118 //- An iterator, returning reference to the key
119 using iterator = typename parent_type::key_iterator;
120
121 //- A const_iterator, returning reference to the key
123
124
125 // Constructors
127 //- Default construct: empty without allocation (capacity=0)
128 constexpr HashSet() noexcept = default;
129
130 //- Construct empty without allocation (capacity=0)
131 explicit constexpr HashSet(Foam::zero) noexcept : this_type() {}
132
133 //- Construct empty with initial table capacity
134 explicit HashSet(const label initialCapacity)
135 :
136 parent_type(initialCapacity)
137 {}
138
139 //- Copy construct
140 HashSet(const this_type& rhs)
141 :
143 {}
145 //- Move construct
147 :
148 parent_type(std::move(rhs))
149 {}
150
151 //- Construct from Istream with default initial table capacity
152 explicit HashSet(Istream& is)
153 :
154 parent_type(is)
155 {}
156
157 //- Construct from FixedList of Key
158 template<unsigned N>
159 explicit HashSet(const FixedList<Key, N>& list);
160
161 //- Construct from UList of Key
162 explicit HashSet(const UList<Key>& list);
163
164 //- Construct from an indirect list
165 template<class Addr>
166 explicit HashSet(const IndirectListBase<Key, Addr>& list);
167
168 //- Construct from an initializer list of Key
169 HashSet(std::initializer_list<Key> list);
170
171 //- Construct from the keys of another HashTable,
172 //- the type of values held is arbitrary.
173 template<class AnyType, class AnyHash>
174 explicit HashSet(const HashTable<AnyType, Key, AnyHash>& tbl);
175
176
177 // Member Functions
178
179 //- Same as contains() - return true if key exists in the set.
180 // Method name compatibility with bitSet and boolList.
181 bool test(const Key& key) const
183 return this->contains(key);
184 }
185
186
187 // Edit
188
189 //- Insert a new entry, not overwriting existing entries.
190 // \return True if the entry inserted, which means that it did
191 // not previously exist in the set.
192 bool insert(const Key& key)
194 return this->parent_type::emplace(key);
195 }
196
197 //- Same as insert (no value to overwrite)
198 bool set(const Key& key)
199 {
200 return this->parent_type::emplace(key);
201 }
202
203 //- Unset the specified key - same as erase
204 // \return True if the entry existed and was removed
205 bool unset(const Key& key)
206 {
207 return this->parent_type::erase(key);
208 }
209
210 //- Attempts to extract entries from source parameter and insert them
211 //- into \c this, does not overwrite existing entries.
212 //- The source will contains any items that could not be merged.
213 void merge(HashSet<Key, Hash>& source);
214
215 //- Attempts to extract entries from source parameter and insert them
216 //- into \c this, does not overwrite existing entries.
217 //- The source will contains any items that could not be merged.
218 void merge(HashSet<Key, Hash>&& source);
219
220
221 // Convenience
222
223 //- Insert keys from the input iterator range
224 // \return The number of new elements inserted
225 template<class InputIter>
226 inline label insert(InputIter first, InputIter last);
227
228 //- Insert keys from a initializer list of Key
229 // \return The number of new elements inserted
230 inline label insert(std::initializer_list<Key> list);
231
232 //- Insert keys from the list of Key
233 // \return The number of new elements inserted
234 template<unsigned N>
235 inline label insert(const FixedList<Key, N>& list);
236
237 //- Insert keys from the list of Key
238 // \return The number of new elements inserted
239 inline label insert(const UList<Key>& list);
240
241 //- Insert keys from the list of Key
242 // \return The number of new elements inserted
243 template<class Addr>
244 inline label insert(const IndirectListBase<Key, Addr>& list);
245
246 //- Same as insert (no value to overwrite)
247 template<class InputIter>
248 inline label set(InputIter first, InputIter last)
249 {
250 return insert(first, last);
251 }
252
253 //- Same as insert (no value to overwrite)
254 inline label set(std::initializer_list<Key> list)
255 {
256 return insert(list);
258
259 //- Same as insert (no value to overwrite)
260 template<unsigned N>
261 inline label set(const FixedList<Key, N>& list)
262 {
263 return insert(list);
265
266 //- Same as insert (no value to overwrite)
267 inline label set(const UList<Key>& list)
268 {
269 return insert(list);
270 }
271
272 //- Same as insert (no value to overwrite)
273 template<class Addr>
274 inline label set(const IndirectListBase<Key, Addr>& list)
276 return insert(list);
277 }
278
279 //- Same as insert (no value to overwrite)
280 // \note Method name compatibility with bitSet
281 template<class InputIter>
282 inline label setMany(InputIter first, InputIter last)
283 {
284 return insert(first, last);
285 }
286
287 //- Unset the keys listed in the input iterator range
288 // \return The number of items removed
289 template<class InputIter>
290 inline label unset(InputIter first, InputIter last);
291
292 //- Unset the listed keys - same as erase
293 // \return The number of items removed
294 inline label unset(std::initializer_list<Key> list);
295
296 //- Unset the listed keys - same as erase
297 // \return The number of items removed
298 template<unsigned N>
299 inline label unset(const FixedList<Key, N>& list);
300
301 //- Unset the listed keys - same as erase
302 // \return The number of items removed
303 inline label unset(const UList<Key>& list);
304
305 //- Unset the listed keys - same as erase
306 // \return The number of items removed
307 template<class Addr>
308 inline label unset(const IndirectListBase<Key, Addr>& list);
309
310
311 // STL iterators
312
313 inline iterator begin();
314 inline const_iterator begin() const;
315 inline const_iterator cbegin() const;
316
317 inline iterator end() noexcept;
318 inline const_iterator end() const noexcept;
319 inline constexpr const_iterator cend() const noexcept;
320
321
322 // Writing
323
324 //- Write unordered keys (list), with line-breaks
325 //- when length exceeds shortLen.
326 // Using '0' suppresses line-breaks entirely.
327 Ostream& writeList(Ostream& os, const label shortLen=0) const
329 return this->writeKeys(os, shortLen);
330 }
331
332
333 // Member Operators
334
335 //- Return true if the entry exists, same as contains()
336 // \note this allows use of HashSet as a predicate test
337 inline bool operator()(const Key& key) const noexcept;
338
339 //- Return true if the entry exists, same as contains().
340 inline bool operator[](const Key& key) const noexcept;
341
342 //- Copy assign
343 void operator=(const this_type& rhs)
344 {
346 }
347
348 //- Move assign
349 void operator=(this_type&& rhs)
350 {
351 parent_type::operator=(std::move(rhs));
352 }
353
354
355 // Comparison
357 //- Sets are equal if all keys are equal,
358 //- independent of order or underlying storage size.
359 bool operator==(const this_type& rhs) const;
360
361 //- The opposite of the equality operation.
362 bool operator!=(const this_type& rhs) const;
363
364
365 // Assignment
366
367 //- Assignment from a UList of keys
368 void operator=(const UList<Key>& rhs);
369
370 //- Assignment from a FixedList of keys
371 template<unsigned N>
372 void operator=(const FixedList<Key, N>& rhs);
373
374 //- Assignment from an initializer list of keys
375 void operator=(std::initializer_list<Key> rhs);
376
377
378 // Logical and set operations
379
380 //- Add entries to this HashSet
383 //- Only retain entries contained in both HashSets
384 inline this_type& operator&=(const this_type& rhs);
385
386 //- Only retain unique entries (xor)
388
389 //- Add entries to this HashSet. Same as the '|=' operator
390 inline this_type& operator+=(const this_type& rhs);
391
392 //- Remove entries from this HashSet. Uses erase()
393 inline this_type& operator-=(const this_type& rhs);
394
395
396 // Housekeeping
398 //- Not applicable for HashSet
399 template<class UnaryPredicate>
400 List<Key> tocValues(const UnaryPredicate&, const bool) = delete;
401
402 //- Not applicable for HashSet
403 template<class BinaryPredicate>
404 List<Key> tocEntries(const BinaryPredicate&, const bool) = delete;
405
406 //- Not applicable for HashSet
407 template<class UnaryPredicate>
408 label countValues(const UnaryPredicate&, const bool) = delete;
409
410 //- Not applicable for HashSet
411 template<class BinaryPredicate>
412 label countEntries(const BinaryPredicate&, const bool) = delete;
413
414 //- Not applicable for HashSet
415 template<class UnaryPredicate>
416 label filterValues(const UnaryPredicate&, const bool) = delete;
417
418 //- Not applicable for HashSet
419 template<class BinaryPredicate>
420 label filterEntries(const BinaryPredicate&, const bool) = delete;
421};
422
423
424// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
425
426// Global Functions
427
428//- Find the min value in labelHashSet, optionally limited by second argument.
429// For an empty set, returns the second argument (eg, labelMax).
430label min(const labelHashSet& set, label minValue = labelMax);
431
432//- Find the max value in labelHashSet, optionally limited by second argument.
433// For an empty set, returns the second argument (eg, labelMin).
434label max(const labelHashSet& set, label maxValue = labelMin);
435
436//- Find the min/max values of labelHashSet
438
439
440// Global Operators
441
442//- Write the list of HashSet keys
443template<class Key, class Hash>
445
446
447//- Combine entries (OR) for two HashSets
448// See HashSet::operator|= for more details.
449template<class Key, class Hash>
451(
452 const HashSet<Key, Hash>& a,
453 const HashSet<Key, Hash>& b
454);
455
456//- Subset (AND) intersection of two HashSet
457// See HashSet::operator&= for more details.
458template<class Key, class Hash>
459HashSet<Key, Hash> operator&
460(
461 const HashSet<Key, Hash>& a,
463);
464
465//- Create a HashSet that only contains unique entries (XOR)
466// See HashSet::operator^= for more details.
467template<class Key, class Hash>
468HashSet<Key, Hash> operator^
469(
470 const HashSet<Key, Hash>& a,
471 const HashSet<Key, Hash>& b
472);
473
474//- Subset difference of two HashSets
475// See HashSet::operator-= for more details.
476template<class Key, class Hash>
477HashSet<Key, Hash> operator-
478(
479 const HashSet<Key, Hash>& a,
480 const HashSet<Key, Hash>& b
482
483
484// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
485
486} // End namespace Foam
487
488// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
489
490#ifdef NoRepository
491 #include "HashSet.C"
492#endif
493
494// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
495
496#endif
497
498// ************************************************************************* //
scalar maxValue
scalar minValue
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition HashSet.H:96
bool operator[](const Key &key) const noexcept
Return true if the entry exists, same as contains().
Definition HashSet.C:245
Ostream & writeList(Ostream &os, const label shortLen=0) const
Definition HashSet.H:419
bool operator()(const Key &key) const noexcept
Return true if the entry exists, same as contains().
Definition HashSet.C:238
HashSet(const label initialCapacity)
Construct empty with initial table capacity.
Definition HashSet.H:149
label set(std::initializer_list< Key > list)
Same as insert (no value to overwrite).
Definition HashSet.H:319
iterator end() noexcept
Definition HashSet.C:486
void operator=(std::initializer_list< Key > rhs)
Assignment from an initializer list of keys.
Definition HashSet.C:267
void merge(HashSet< Key, Hash > &&source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
Definition HashSet.C:229
label set(const UList< Key > &list)
Same as insert (no value to overwrite).
Definition HashSet.H:336
label set(InputIter first, InputIter last)
Same as insert (no value to overwrite).
Definition HashSet.H:311
label unset(std::initializer_list< Key > list)
Unset the listed keys - same as erase.
Definition HashSet.C:181
label unset(const UList< Key > &list)
Unset the listed keys - same as erase.
Definition HashSet.C:202
HashSet(this_type &&rhs) noexcept
Move construct.
Definition HashSet.H:165
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
label countEntries(const BinaryPredicate &, const bool)=delete
Not applicable for HashSet.
this_type & operator|=(const this_type &rhs)
Add entries to this HashSet.
Definition HashSet.C:307
const_iterator begin() const
Definition HashSet.C:464
HashSet(Istream &is)
Construct from Istream with default initial table capacity.
Definition HashSet.H:173
List< Key > tocValues(const UnaryPredicate &, const bool)=delete
Not applicable for HashSet.
constexpr HashSet() noexcept=default
Default construct: empty without allocation (capacity=0).
label insert(InputIter first, InputIter last)
Insert keys from the input iterator range.
HashSet(const IndirectListBase< Key, Addr > &list)
Construct from an indirect list.
Definition HashSet.C:69
label set(const FixedList< Key, N > &list)
Same as insert (no value to overwrite).
Definition HashSet.H:328
label unset(InputIter first, InputIter last)
Unset the keys listed in the input iterator range.
typename parent_type::key_iterator iterator
Definition HashSet.H:126
void operator=(this_type &&rhs)
Move assign.
Definition HashSet.H:450
bool operator==(const this_type &rhs) const
Sets are equal if all keys are equal, independent of order or underlying storage size.
Definition HashSet.C:274
typename parent_type::const_key_iterator const_iterator
Definition HashSet.H:131
void operator=(const FixedList< Key, N > &rhs)
Assignment from a FixedList of keys.
Definition HashSet.C:253
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition HashSet.H:247
label insert(std::initializer_list< Key > list)
Insert keys from a initializer list of Key.
Definition HashSet.C:127
label insert(const UList< Key > &list)
Insert keys from the list of Key.
Definition HashSet.C:148
const_iterator cbegin() const
Definition HashSet.C:475
iterator begin()
Definition HashSet.C:453
HashSet< Key, HashType > this_type
Definition HashSet.H:116
constexpr const_iterator cend() const noexcept
label filterEntries(const BinaryPredicate &, const bool)=delete
Not applicable for HashSet.
HashSet(const FixedList< Key, N > &list)
Construct from FixedList of Key.
Definition HashSet.C:50
label insert(const IndirectListBase< Key, Addr > &list)
Insert keys from the list of Key.
label unset(const IndirectListBase< Key, Addr > &list)
Unset the listed keys - same as erase.
this_type & operator-=(const this_type &rhs)
Remove entries from this HashSet. Uses erase().
Definition HashSet.C:362
label insert(const FixedList< Key, N > &list)
Insert keys from the list of Key.
label set(const IndirectListBase< Key, Addr > &list)
Same as insert (no value to overwrite).
Definition HashSet.H:345
void operator=(const UList< Key > &rhs)
Assignment from a UList of keys.
Definition HashSet.C:260
this_type & operator&=(const this_type &rhs)
Only retain entries contained in both HashSets.
Definition HashSet.C:324
HashSet(const this_type &rhs)
Copy construct.
Definition HashSet.H:157
bool test(const Key &key) const
Same as contains() - return true if key exists in the set.
Definition HashSet.H:215
label countValues(const UnaryPredicate &, const bool)=delete
Not applicable for HashSet.
this_type & operator^=(const this_type &rhs)
Only retain unique entries (xor).
Definition HashSet.C:333
bool set(const Key &key)
Same as insert (no value to overwrite).
Definition HashSet.H:237
HashSet(const UList< Key > &list)
Construct from UList of Key.
Definition HashSet.C:59
void operator=(const this_type &rhs)
Copy assign.
Definition HashSet.H:442
HashSet(std::initializer_list< Key > list)
Construct from an initializer list of Key.
Definition HashSet.C:78
label setMany(InputIter first, InputIter last)
Same as insert (no value to overwrite).
Definition HashSet.H:356
void merge(HashSet< Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
Definition HashSet.C:222
List< Key > tocEntries(const BinaryPredicate &, const bool)=delete
Not applicable for HashSet.
bool operator!=(const this_type &rhs) const
The opposite of the equality operation.
Definition HashSet.C:299
HashSet(const HashTable< AnyType, Key, AnyHash > &tbl)
Construct from the keys of another HashTable, the type of values held is arbitrary.
Definition HashSet.C:89
label unset(const FixedList< Key, N > &list)
Unset the listed keys - same as erase.
label filterValues(const UnaryPredicate &, const bool)=delete
Not applicable for HashSet.
HashTable< Foam::zero, Key, HashType > parent_type
Definition HashSet.H:121
this_type & operator+=(const this_type &rhs)
Add entries to this HashSet. Same as the '|=' operator.
Definition HashSet.C:354
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Write unordered keys (list), with line-breaks when length exceeds shortLen.
Definition HashTableIO.C:77
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
Definition HashTableI.H:72
key_iterator_base< iterator > key_iterator
Forward iterator returning the key.
Definition HashTable.H:1285
key_iterator_base< const_iterator > const_key_iterator
Forward const iterator returning the key.
Definition HashTable.H:1290
bool erase(const iterator &iter)
void operator=(const this_type &rhs)
bool emplace(const Key &key, Args &&... args)
constexpr HashTable() noexcept
Default construct: empty without allocation (capacity=0).
Definition HashTable.C:33
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition MinMax.H:106
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition HashSet.H:85
constexpr label labelMin
Definition label.H:54
constexpr label labelMax
Definition label.H:55
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
MinMax< label > minMax(const labelHashSet &set)
Find the min/max values of labelHashSet.
Definition hashSets.C:54
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
volScalarField & b
nonInt insert("surfaceSum(((S|magSf)*S)")