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 with default (128) table capacity
128  HashSet()
129  :
130  parent_type()
131  {}
132 
133  //- Copy construct
134  HashSet(const this_type& rhs)
135  :
136  parent_type(rhs)
137  {}
138 
139  //- Move construct
140  HashSet(this_type&& rhs)
141  :
142  parent_type(std::move(rhs))
143  {}
144 
145  //- Construct given initial table capacity
146  explicit HashSet(const label size)
147  :
149  {}
150 
151  //- Construct from Istream with default 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);
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
182  {
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)
193  {
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);
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);
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);
257  }
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);
264  }
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)
275  {
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);
310 
311  // STL iterators
312 
313  inline iterator begin();
314  inline const_iterator begin() const;
315  inline const_iterator cbegin() const;
316 
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
328  {
329  return this->writeKeys(os, shortLen);
330  }
331 
332 
333  // Member Operators
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 
355  // Comparison
356 
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
381  this_type& operator|=(const this_type& rhs);
382 
383  //- Only retain entries contained in both HashSets
384  inline this_type& operator&=(const this_type& rhs);
385 
386  //- Only retain unique entries (xor)
387  this_type& operator^=(const this_type& rhs);
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
397 
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;
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).
430 label 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).
434 label max(const labelHashSet& set, label maxValue = labelMin);
435 
436 //- Find the min/max values of labelHashSet
437 MinMax<label> minMax(const labelHashSet& set);
438 
439 
440 // Global Operators
441 
442 //- Write the list of HashSet keys
443 template<class Key, class Hash>
444 Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& rhs);
445 
446 
447 //- Combine entries (OR) for two HashSets
448 // See HashSet::operator|= for more details.
449 template<class Key, class Hash>
450 HashSet<Key, Hash> operator|
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.
458 template<class Key, class Hash>
459 HashSet<Key, Hash> operator&
460 (
461  const HashSet<Key, Hash>& a,
462  const HashSet<Key, Hash>& b
463 );
464 
465 //- Create a HashSet that only contains unique entries (XOR)
466 // See HashSet::operator^= for more details.
467 template<class Key, class Hash>
468 HashSet<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.
476 template<class Key, class Hash>
477 HashSet<Key, Hash> operator-
478 (
479  const HashSet<Key, Hash>& a,
480  const HashSet<Key, Hash>& b
481 );
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 // ************************************************************************* //
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:101
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:245
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:213
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:417
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:281
this_type & operator|=(const this_type &rhs)
Add entries to this HashSet.
Definition: HashSet.C:310
void operator=(const this_type &rhs)
Copy assign.
Definition: HashSet.H:440
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:229
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:227
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
HashSet()
Default construct with default (128) table capacity.
Definition: HashSet.H:139
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
label size() const noexcept
The number of elements in table.
Definition: HashTable.H:331
scalar maxValue
key_iterator_base< iterator > key_iterator
Forward iterator returning the key.
Definition: HashTable.H:1248
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:245
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:90
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:102
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:99
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
key_iterator_base< const_iterator > const_key_iterator
Forward const iterator returning the key.
Definition: HashTable.H:1253
OBJstream os(runTime.globalPath()/outputName)
label setMany(InputIter first, InputIter last)
Same as insert (no value to overwrite)
Definition: HashSet.H:354
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:473
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:252
constexpr label labelMax
Definition: label.H:55
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:302
Namespace for OpenFOAM.