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