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-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::HashSet
29 
30 Description
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 
40 Note
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 
51 Typedef
52  Foam::wordHashSet
53 
54 Description
55  A HashSet with word keys and string hasher.
56 
57 Typedef
58  Foam::labelHashSet
59 
60 Description
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 
73 namespace Foam
74 {
75 
76 // Forward Declarations
77 template<class T> class MinMax;
78 template<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 
93 template<class Key, class Hash=Foam::Hash<Key>>
94 class 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 
110 public:
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  HashSet() noexcept = default;
129 
130  //- Construct empty without allocation (capacity=0)
131  explicit HashSet(const Foam::zero) noexcept
132  :
133  parent_type(Foam::zero{})
134  {}
135 
136  //- Construct empty with initial table capacity
137  explicit HashSet(const label initialCapacity)
138  :
139  parent_type(initialCapacity)
140  {}
141 
142  //- Copy construct
143  HashSet(const this_type& rhs)
144  :
145  parent_type(rhs)
146  {}
147 
148  //- Move construct
149  HashSet(this_type&& rhs) noexcept
150  :
151  parent_type(std::move(rhs))
152  {}
153 
154  //- Construct from Istream with default initial table capacity
155  explicit HashSet(Istream& is)
156  :
157  parent_type(is)
158  {}
159 
160  //- Construct from FixedList of Key
161  template<unsigned N>
162  explicit HashSet(const FixedList<Key, N>& list);
163 
164  //- Construct from UList of Key
165  explicit HashSet(const UList<Key>& list);
166 
167  //- Construct from an indirect list
168  template<class Addr>
169  explicit HashSet(const IndirectListBase<Key, Addr>& list);
170 
171  //- Construct from an initializer list of Key
172  HashSet(std::initializer_list<Key> list);
173 
174  //- Construct from the keys of another HashTable,
175  //- the type of values held is arbitrary.
176  template<class AnyType, class AnyHash>
177  explicit HashSet(const HashTable<AnyType, Key, AnyHash>& tbl);
178 
179 
180  // Member Functions
181 
182  //- Same as contains() - return true if key exists in the set.
183  // Method name compatibility with bitSet and boolList.
184  bool test(const Key& key) const
185  {
186  return this->contains(key);
187  }
188 
189 
190  // Edit
191 
192  //- Insert a new entry, not overwriting existing entries.
193  // \return True if the entry inserted, which means that it did
194  // not previously exist in the set.
195  bool insert(const Key& key)
196  {
197  return this->parent_type::emplace(key);
198  }
199 
200  //- Same as insert (no value to overwrite)
201  bool set(const Key& key)
202  {
203  return this->parent_type::emplace(key);
204  }
205 
206  //- Unset the specified key - same as erase
207  // \return True if the entry existed and was removed
208  bool unset(const Key& key)
209  {
210  return this->parent_type::erase(key);
211  }
212 
213  //- Attempts to extract entries from source parameter and insert them
214  //- into \c this, does not overwrite existing entries.
215  //- The source will contains any items that could not be merged.
216  void merge(HashSet<Key, Hash>& source);
217 
218  //- Attempts to extract entries from source parameter and insert them
219  //- into \c this, does not overwrite existing entries.
220  //- The source will contains any items that could not be merged.
221  void merge(HashSet<Key, Hash>&& source);
222 
223 
224  // Convenience
225 
226  //- Insert keys from the input iterator range
227  // \return The number of new elements inserted
228  template<class InputIter>
229  inline label insert(InputIter first, InputIter last);
230 
231  //- Insert keys from a initializer list of Key
232  // \return The number of new elements inserted
233  inline label insert(std::initializer_list<Key> list);
234 
235  //- Insert keys from the list of Key
236  // \return The number of new elements inserted
237  template<unsigned N>
238  inline label insert(const FixedList<Key, N>& list);
239 
240  //- Insert keys from the list of Key
241  // \return The number of new elements inserted
242  inline label insert(const UList<Key>& list);
243 
244  //- Insert keys from the list of Key
245  // \return The number of new elements inserted
246  template<class Addr>
247  inline label insert(const IndirectListBase<Key, Addr>& list);
248 
249  //- Same as insert (no value to overwrite)
250  template<class InputIter>
251  inline label set(InputIter first, InputIter last)
252  {
253  return insert(first, last);
254  }
255 
256  //- Same as insert (no value to overwrite)
257  inline label set(std::initializer_list<Key> list)
258  {
259  return insert(list);
260  }
261 
262  //- Same as insert (no value to overwrite)
263  template<unsigned N>
264  inline label set(const FixedList<Key, N>& list)
265  {
266  return insert(list);
267  }
268 
269  //- Same as insert (no value to overwrite)
270  inline label set(const UList<Key>& list)
271  {
272  return insert(list);
273  }
274 
275  //- Same as insert (no value to overwrite)
276  template<class Addr>
277  inline label set(const IndirectListBase<Key, Addr>& list)
278  {
279  return insert(list);
280  }
281 
282  //- Same as insert (no value to overwrite)
283  // \note Method name compatibility with bitSet
284  template<class InputIter>
285  inline label setMany(InputIter first, InputIter last)
286  {
287  return insert(first, last);
288  }
289 
290  //- Unset the keys listed in the input iterator range
291  // \return The number of items removed
292  template<class InputIter>
293  inline label unset(InputIter first, InputIter last);
294 
295  //- Unset the listed keys - same as erase
296  // \return The number of items removed
297  inline label unset(std::initializer_list<Key> list);
298 
299  //- Unset the listed keys - same as erase
300  // \return The number of items removed
301  template<unsigned N>
302  inline label unset(const FixedList<Key, N>& list);
303 
304  //- Unset the listed keys - same as erase
305  // \return The number of items removed
306  inline label unset(const UList<Key>& list);
307 
308  //- Unset the listed keys - same as erase
309  // \return The number of items removed
310  template<class Addr>
311  inline label unset(const IndirectListBase<Key, Addr>& list);
312 
313 
314  // STL iterators
315 
316  inline iterator begin();
317  inline const_iterator begin() const;
318  inline const_iterator cbegin() const;
319 
320  inline iterator end() noexcept;
321  inline const_iterator end() const noexcept;
322  inline constexpr const_iterator cend() const noexcept;
323 
324 
325  // Writing
326 
327  //- Write unordered keys (list), with line-breaks
328  //- when length exceeds shortLen.
329  // Using '0' suppresses line-breaks entirely.
330  Ostream& writeList(Ostream& os, const label shortLen=0) const
331  {
332  return this->writeKeys(os, shortLen);
333  }
334 
335 
336  // Member Operators
337 
338  //- Return true if the entry exists, same as contains()
339  // \note this allows use of HashSet as a predicate test
340  inline bool operator()(const Key& key) const noexcept;
341 
342  //- Return true if the entry exists, same as contains().
343  inline bool operator[](const Key& key) const noexcept;
344 
345  //- Copy assign
346  void operator=(const this_type& rhs)
347  {
349  }
350 
351  //- Move assign
352  void operator=(this_type&& rhs)
353  {
354  parent_type::operator=(std::move(rhs));
355  }
356 
357 
358  // Comparison
360  //- Sets are equal if all keys are equal,
361  //- independent of order or underlying storage size.
362  bool operator==(const this_type& rhs) const;
363 
364  //- The opposite of the equality operation.
365  bool operator!=(const this_type& rhs) const;
366 
367 
368  // Assignment
369 
370  //- Assignment from a UList of keys
371  void operator=(const UList<Key>& rhs);
372 
373  //- Assignment from a FixedList of keys
374  template<unsigned N>
375  void operator=(const FixedList<Key, N>& rhs);
376 
377  //- Assignment from an initializer list of keys
378  void operator=(std::initializer_list<Key> rhs);
379 
380 
381  // Logical and set operations
382 
383  //- Add entries to this HashSet
384  this_type& operator|=(const this_type& rhs);
385 
386  //- Only retain entries contained in both HashSets
387  inline this_type& operator&=(const this_type& rhs);
388 
389  //- Only retain unique entries (xor)
390  this_type& operator^=(const this_type& rhs);
391 
392  //- Add entries to this HashSet. Same as the '|=' operator
393  inline this_type& operator+=(const this_type& rhs);
394 
395  //- Remove entries from this HashSet. Uses erase()
396  inline this_type& operator-=(const this_type& rhs);
397 
398 
399  // Housekeeping
400 
401  //- Not applicable for HashSet
402  template<class UnaryPredicate>
403  List<Key> tocValues(const UnaryPredicate&, const bool) = delete;
404 
405  //- Not applicable for HashSet
406  template<class BinaryPredicate>
407  List<Key> tocEntries(const BinaryPredicate&, const bool) = delete;
408 
409  //- Not applicable for HashSet
410  template<class UnaryPredicate>
411  label countValues(const UnaryPredicate&, const bool) = delete;
412 
413  //- Not applicable for HashSet
414  template<class BinaryPredicate>
415  label countEntries(const BinaryPredicate&, const bool) = delete;
416 
417  //- Not applicable for HashSet
418  template<class UnaryPredicate>
419  label filterValues(const UnaryPredicate&, const bool) = delete;
420 
421  //- Not applicable for HashSet
422  template<class BinaryPredicate>
423  label filterEntries(const BinaryPredicate&, const bool) = delete;
424 };
425 
426 
427 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
428 
429 // Global Functions
430 
431 //- Find the min value in labelHashSet, optionally limited by second argument.
432 // For an empty set, returns the second argument (eg, labelMax).
433 label min(const labelHashSet& set, label minValue = labelMax);
434 
435 //- Find the max value in labelHashSet, optionally limited by second argument.
436 // For an empty set, returns the second argument (eg, labelMin).
437 label max(const labelHashSet& set, label maxValue = labelMin);
438 
439 //- Find the min/max values of labelHashSet
440 MinMax<label> minMax(const labelHashSet& set);
441 
442 
443 // Global Operators
444 
445 //- Write the list of HashSet keys
446 template<class Key, class Hash>
447 Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& rhs);
448 
449 
450 //- Combine entries (OR) for two HashSets
451 // See HashSet::operator|= for more details.
452 template<class Key, class Hash>
454 (
455  const HashSet<Key, Hash>& a,
456  const HashSet<Key, Hash>& b
457 );
458 
459 //- Subset (AND) intersection of two HashSet
460 // See HashSet::operator&= for more details.
461 template<class Key, class Hash>
462 HashSet<Key, Hash> operator&
463 (
464  const HashSet<Key, Hash>& a,
465  const HashSet<Key, Hash>& b
466 );
467 
468 //- Create a HashSet that only contains unique entries (XOR)
469 // See HashSet::operator^= for more details.
470 template<class Key, class Hash>
471 HashSet<Key, Hash> operator^
472 (
473  const HashSet<Key, Hash>& a,
474  const HashSet<Key, Hash>& b
475 );
476 
477 //- Subset difference of two HashSets
478 // See HashSet::operator-= for more details.
479 template<class Key, class Hash>
480 HashSet<Key, Hash> operator-
481 (
482  const HashSet<Key, Hash>& a,
483  const HashSet<Key, Hash>& b
484 );
485 
486 
487 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
488 
489 } // End namespace Foam
490 
491 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
492 
493 #ifdef NoRepository
494  #include "HashSet.C"
495 #endif
496 
497 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
498 
499 #endif
500 
501 // ************************************************************************* //
A HashTable with keys but without contents that is similar to std::unordered_set. ...
Definition: HashSet.H:73
List< Key > tocEntries(const BinaryPredicate &, const bool)=delete
Not applicable for HashSet.
this_type & operator^=(const this_type &rhs)
Only retain unique entries (xor)
Definition: HashSet.C:333
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:129
iterator begin()
Definition: HashSet.C:453
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
label filterValues(const UnaryPredicate &, const bool)=delete
Not applicable for HashSet.
bool operator()(const Key &key) const noexcept
Return true if the entry exists, same as contains()
Definition: HashSet.C:238
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
bool test(const Key &key) const
Same as contains() - return true if key exists in the set.
Definition: HashSet.H:218
scalar minValue
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write unordered keys (list), with line-breaks when length exceeds shortLen.
Definition: HashSet.H:422
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
this_type & operator|=(const this_type &rhs)
Add entries to this HashSet.
Definition: HashSet.C:307
void operator=(const this_type &rhs)
Copy assign.
Definition: HashSet.H:445
constexpr const_iterator cend() const noexcept
Definition: HashSet.C:502
constexpr label labelMin
Definition: label.H:54
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
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:232
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
label filterEntries(const BinaryPredicate &, const bool)=delete
Not applicable for HashSet.
MinMax< label > minMax(const labelHashSet &set)
Find the min/max values of labelHashSet.
Definition: hashSets.C:54
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition: HashSet.H:85
HashSet() noexcept=default
Default construct: empty without allocation (capacity=0)
scalar maxValue
key_iterator_base< iterator > key_iterator
Forward iterator returning the key.
Definition: HashTable.H:1269
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:250
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
Definition: HashTableI.H:72
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Write unordered keys (list), with line-breaks when length exceeds shortLen.
Definition: HashTableIO.C:77
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
void operator=(const this_type &rhs)
Copy assign.
this_type & operator-=(const this_type &rhs)
Remove entries from this HashSet. Uses erase()
Definition: HashSet.C:362
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:73
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
key_iterator_base< const_iterator > const_key_iterator
Forward const iterator returning the key.
Definition: HashTable.H:1274
OBJstream os(runTime.globalPath()/outputName)
label setMany(InputIter first, InputIter last)
Same as insert (no value to overwrite)
Definition: HashSet.H:359
iterator end() noexcept
Definition: HashSet.C:486
label countValues(const UnaryPredicate &, const bool)=delete
Not applicable for HashSet.
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:472
this_type & operator &=(const this_type &rhs)
Only retain entries contained in both HashSets.
typename parent_type::key_iterator iterator
An iterator, returning reference to the key.
Definition: HashSet.H:126
List< Key > tocValues(const UnaryPredicate &, const bool)=delete
Not applicable for HashSet.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
label countEntries(const BinaryPredicate &, const bool)=delete
Not applicable for HashSet.
HashTable< Foam::zero, Key, Hash > parent_type
The template instance used for the parent HashTable.
Definition: HashSet.H:121
HashSet< Key, Hash > this_type
The template instance used for this HashSet.
Definition: HashSet.H:116
bool operator[](const Key &key) const noexcept
Return true if the entry exists, same as contains().
Definition: HashSet.C:245
constexpr label labelMax
Definition: label.H:55
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
this_type & operator+=(const this_type &rhs)
Add entries to this HashSet. Same as the &#39;|=&#39; operator.
Definition: HashSet.C:354
typename parent_type::const_key_iterator const_iterator
A const_iterator, returning reference to the key.
Definition: HashSet.H:131
const_iterator cbegin() const
Definition: HashSet.C:475
bool operator!=(const this_type &rhs) const
The opposite of the equality operation.
Definition: HashSet.C:299
Namespace for OpenFOAM.