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