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  inline bool operator()(const Key& key) const noexcept;
317 
318  //- Return true if the entry exists, same as found().
319  inline bool operator[](const Key& key) const noexcept;
320 
321  using parent_type::operator=;
322 
323  //- Copy assignment
324  void operator=(const this_type& rhs)
325  {
327  }
328 
329  //- Move assignment
330  void operator=(this_type&& rhs)
331  {
332  parent_type::operator=(std::move(rhs));
333  }
334 
335 
336  // Comparison
337 
338  //- Sets are equal if all keys are equal,
339  //- independent of order or underlying storage size.
340  bool operator==(const this_type& rhs) const;
341 
342  //- The opposite of the equality operation.
343  bool operator!=(const this_type& rhs) const;
344 
345 
346  // Assignment
347 
348  //- Assignment from a UList of keys
349  void operator=(const UList<Key>& rhs);
350 
351  //- Assignment from a FixedList of keys
352  template<unsigned N>
353  void operator=(const FixedList<Key, N>& rhs);
354 
355  //- Assignment from an initializer list of keys
356  void operator=(std::initializer_list<Key> rhs);
357 
358 
359  // Logical and set operations
360 
361  //- Add entries to this HashSet
362  this_type& operator|=(const this_type& rhs);
363 
364  //- Only retain entries found in both HashSets
365  inline this_type& operator&=(const this_type& rhs);
366 
367  //- Only retain unique entries (xor)
368  this_type& operator^=(const this_type& rhs);
369 
370  //- Add entries to this HashSet. Same as the '|=' operator
371  inline this_type& operator+=(const this_type& rhs);
372 
373  //- Remove entries from this HashSet. Uses erase()
374  inline this_type& operator-=(const this_type& rhs);
375 
376 
377  // Housekeeping
378 
379  //- Not applicable for HashSet
380  template<class UnaryPredicate>
381  List<Key> tocValues(const UnaryPredicate&, const bool) = delete;
382 
383  //- Not applicable for HashSet
384  template<class BinaryPredicate>
385  List<Key> tocEntries(const BinaryPredicate&, const bool) = delete;
386 
387  //- Not applicable for HashSet
388  template<class UnaryPredicate>
389  label countValues(const UnaryPredicate&, const bool) = delete;
390 
391  //- Not applicable for HashSet
392  template<class BinaryPredicate>
393  label countEntries(const BinaryPredicate&, const bool) = delete;
394 
395  //- Not applicable for HashSet
396  template<class UnaryPredicate>
397  label filterValues(const UnaryPredicate&, const bool) = delete;
398 
399  //- Not applicable for HashSet
400  template<class BinaryPredicate>
401  label filterEntries(const BinaryPredicate&, const bool) = delete;
402 };
403 
404 
405 // Typedefs
406 
407 //- A HashSet with word keys.
408 typedef HashSet<word> wordHashSet;
409 
410 //- A HashSet with label keys and label hasher.
412 
413 
414 // Global Functions
415 
416 //- Find the min value in labelHashSet, optionally limited by second argument.
417 // For an empty set, returns the second argument (eg, labelMax).
418 label min(const labelHashSet& set, label minValue = labelMax);
419 
420 //- Find the max value in labelHashSet, optionally limited by second argument.
421 // For an empty set, returns the second argument (eg, labelMin).
422 label max(const labelHashSet& set, label maxValue = labelMin);
423 
424 //- Find the min/max values of labelHashSet
425 MinMax<label> minMax(const labelHashSet& set);
426 
427 
428 // Global Operators
429 
430 //- Write the list of HashSet keys
431 template<class Key, class Hash>
432 Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& tbl);
433 
434 
435 //- Combine entries from HashSets
436 template<class Key, class Hash>
437 HashSet<Key, Hash> operator|
438 (
439  const HashSet<Key, Hash>& hash1,
440  const HashSet<Key, Hash>& hash2
441 );
442 
443 
444 //- Create a HashSet that only contains entries found in both HashSets
445 template<class Key, class Hash>
446 HashSet<Key, Hash> operator&
447 (
448  const HashSet<Key, Hash>& hash1,
449  const HashSet<Key, Hash>& hash2
450 );
451 
452 
453 //- Create a HashSet that only contains unique entries (xor)
454 template<class Key, class Hash>
455 HashSet<Key, Hash> operator^
456 (
457  const HashSet<Key, Hash>& hash1,
458  const HashSet<Key, Hash>& hash2
459 );
460 
461 
462 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
463 
464 } // End namespace Foam
465 
466 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
467 
468 #ifdef NoRepository
469  #include "HashSet.C"
470 #endif
471 
472 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
473 
474 #endif
475 
476 // ************************************************************************* //
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 assignment.
Definition: HashSet.H:329
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::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:256
Foam::HashSet::operator=
void operator=(const this_type &rhs)
Copy assignment.
Definition: HashSet.H:323
Foam::HashTable< zero::null, Key, Hash >::key_iterator
key_iterator_base< iterator > key_iterator
Forward iterator returning the key.
Definition: HashTable.H:857
Foam::HashSet::end
iterator end() noexcept
Definition: HashSet.C:461
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:285
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:358
Foam::HashSet::operator|=
this_type & operator|=(const this_type &rhs)
Add entries to this HashSet.
Definition: HashSet.C:314
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:306
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:860
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:328
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:477
Foam::HashSet::operator-=
this_type & operator-=(const this_type &rhs)
Remove entries from this HashSet. Uses erase()
Definition: HashSet.C:366
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:428
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:249
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:337
Foam::HashSet::cbegin
const_iterator cbegin() const
Definition: HashSet.C:450
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
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:407
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:410
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