HashSet.C
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 \*---------------------------------------------------------------------------*/
28 
29 #ifndef HashSet_C
30 #define HashSet_C
31 
32 #include "HashSet.H"
33 #include "FixedList.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 template<class Key, class Hash>
38 template<class InputIter>
39 inline Foam::label Foam::HashSet<Key, Hash>::assignMany
40 (
41  const label nItems,
42  InputIter first,
43  InputIter last
44 )
45 {
46  if (!this->capacity())
47  {
48  // Zero-sized from a previous transfer()?
49  this->resize(2*nItems);
50  }
51  else
52  {
53  this->clear();
54  }
55 
56  return insert(first, last);
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 template<class Key, class Hash>
63 template<unsigned N>
65 :
66  parent_type(2*list.size())
67 {
68  insert(list.begin(), list.end());
69 }
70 
71 
72 template<class Key, class Hash>
74 :
75  parent_type(2*list.size())
76 {
77  insert(list.begin(), list.end());
78 }
79 
80 
81 template<class Key, class Hash>
82 template<class Addr>
84 :
85  parent_type(2*list.size())
86 {
87  insert(list.begin(), list.end());
88 }
89 
90 
91 template<class Key, class Hash>
92 Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> list)
93 :
94  parent_type(2*list.size())
95 {
96  insert(list.begin(), list.end());
97 }
98 
99 
100 template<class Key, class Hash>
101 template<class AnyType, class AnyHash>
103 (
105 )
106 :
107  parent_type(tbl.capacity())
108 {
109  for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
110  {
111  this->insert(iter.key());
112  }
113 }
114 
115 
116 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
117 
118 template<class Key, class Hash>
119 template<class InputIter>
120 inline Foam::label Foam::HashSet<Key, Hash>::insert
121 (
122  InputIter first,
123  InputIter last
124 )
125 {
126  label changed = 0;
127  while (first != last)
128  {
129  if (insert(*first))
130  {
131  ++changed;
132  }
133  ++first;
134  }
135  return changed;
136 }
137 
138 
139 template<class Key, class Hash>
140 inline Foam::label Foam::HashSet<Key, Hash>::insert
141 (
142  std::initializer_list<Key> list
143 )
144 {
145  return insert(list.begin(), list.end());
146 }
147 
148 
149 template<class Key, class Hash>
150 template<unsigned N>
151 inline Foam::label Foam::HashSet<Key, Hash>::insert
152 (
153  const FixedList<Key, N>& list
154 )
155 {
156  return insert(list.begin(), list.end());
157 }
158 
159 
160 template<class Key, class Hash>
161 inline Foam::label Foam::HashSet<Key, Hash>::insert
162 (
163  const UList<Key>& list
164 )
165 {
166  return insert(list.begin(), list.end());
167 }
168 
169 
170 template<class Key, class Hash>
171 template<class Addr>
172 inline Foam::label Foam::HashSet<Key, Hash>::insert
173 (
174  const IndirectListBase<Key, Addr>& list
175 )
176 {
177  return insert(list.begin(), list.end());
178 }
179 
180 
181 template<class Key, class Hash>
182 template<class InputIter>
183 inline Foam::label Foam::HashSet<Key, Hash>::unset
184 (
185  InputIter first,
186  InputIter last
187 )
188 {
189  return this->parent_type::erase(first, last);
190 }
191 
192 
193 template<class Key, class Hash>
194 inline Foam::label Foam::HashSet<Key, Hash>::unset
195 (
196  std::initializer_list<Key> list
197 )
198 {
199  return unset(list.begin(), list.end());
200 }
201 
202 
203 template<class Key, class Hash>
204 template<unsigned N>
205 inline Foam::label Foam::HashSet<Key, Hash>::unset
206 (
207  const FixedList<Key, N>& list
208 )
209 {
210  return unset(list.begin(), list.end());
211 }
212 
213 
214 template<class Key, class Hash>
215 inline Foam::label Foam::HashSet<Key, Hash>::unset
216 (
217  const UList<Key>& list
218 )
219 {
220  return unset(list.begin(), list.end());
221 }
222 
223 
224 template<class Key, class Hash>
225 template<class Addr>
226 inline Foam::label Foam::HashSet<Key, Hash>::unset
227 (
228  const IndirectListBase<Key, Addr>& list
229 )
230 {
231  return unset(list.begin(), list.end());
232 }
233 
234 
235 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
236 
237 template<class Key, class Hash>
238 inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const noexcept
239 {
240  return this->found(key);
241 }
242 
243 
244 template<class Key, class Hash>
245 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const noexcept
246 {
247  return this->found(key);
248 }
249 
250 
251 template<class Key, class Hash>
252 template<unsigned N>
254 {
255  assignMany(rhs.size(), rhs.begin(), rhs.end());
256 }
257 
258 
259 template<class Key, class Hash>
261 {
262  assignMany(rhs.size(), rhs.begin(), rhs.end());
263 }
264 
265 
266 template<class Key, class Hash>
267 void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs)
268 {
269  assignMany(rhs.size(), rhs.begin(), rhs.end());
270 }
271 
272 
273 template<class Key, class Hash>
275 {
276  // Sizes (number of keys) must match
277  if (this->size() != rhs.size())
278  {
279  return false;
280  }
281 
282  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
283  {
284  if (!this->found(iter.key()))
285  {
286  return false;
287  }
288  }
289 
290  return true;
291 }
292 
293 
294 template<class Key, class Hash>
296 {
297  return !operator==(rhs);
298 }
299 
300 
301 template<class Key, class Hash>
304 {
305  // Add rhs elements into lhs
306  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
307  {
308  this->insert(iter.key());
309  }
310 
311  return *this;
312 }
313 
314 
315 template<class Key, class Hash>
318 {
319  this->parent_type::retain(rhs);
320  return *this;
321 }
322 
323 
324 template<class Key, class Hash>
327 {
328  // Add missed rhs elements, remove duplicate elements
329  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
330  {
331  if (this->found(iter.key()))
332  {
333  this->erase(iter.key());
334  }
335  else
336  {
337  this->insert(iter.key());
338  }
339  }
340 
341  return *this;
342 }
343 
344 
345 template<class Key, class Hash>
348 {
349  return this->operator|=(rhs);
350 }
351 
352 
353 template<class Key, class Hash>
356 {
357  this->parent_type::erase(rhs);
358 
359  return *this;
360 }
361 
362 
363 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
364 
365 template<class Key, class Hash>
367 {
368  return rhs.writeKeys(os, Detail::ListPolicy::short_length<Key>::value);
369 }
370 
371 
372 /* * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * */
373 
374 template<class Key, class Hash>
375 Foam::HashSet<Key, Hash> Foam::operator|
376 (
377  const HashSet<Key, Hash>& a,
378  const HashSet<Key, Hash>& b
379 )
380 {
381  HashSet<Key, Hash> result(a);
382  result |= b;
383  return result;
384 }
385 
386 
387 template<class Key, class Hash>
388 Foam::HashSet<Key, Hash> Foam::operator&
389 (
390  const HashSet<Key, Hash>& a,
391  const HashSet<Key, Hash>& b
392 )
393 {
394  HashSet<Key, Hash> result(a.capacity());
395 
396  for (const Key& k : a)
397  {
398  if (b.found(k))
399  {
400  result.insert(k);
401  }
402  }
403 
404  return result;
405 }
406 
407 
408 template<class Key, class Hash>
409 Foam::HashSet<Key, Hash> Foam::operator^
410 (
411  const HashSet<Key, Hash>& a,
412  const HashSet<Key, Hash>& b
413 )
414 {
415  HashSet<Key, Hash> result(a);
416  result ^= b;
417  return result;
418 }
419 
420 
421 template<class Key, class Hash>
422 Foam::HashSet<Key, Hash> Foam::operator-
423 (
424  const HashSet<Key, Hash>& a,
425  const HashSet<Key, Hash>& b
426 )
427 {
428  HashSet<Key, Hash> result(a.capacity());
429 
430  for (const Key& k : a)
431  {
432  if (!b.found(k))
433  {
434  result.insert(k);
435  }
436  }
437 
438  return result;
439 }
440 
441 
442 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
443 
444 template<class Key, class Hash>
447 {
448  return iterator
449  (
450  static_cast<parent_type&>(*this).begin()
451  );
452 }
453 
454 
455 template<class Key, class Hash>
458 {
459  return const_iterator
460  (
461  static_cast<const parent_type&>(*this).begin()
462  );
463 }
464 
465 
466 template<class Key, class Hash>
469 {
470  return const_iterator
471  (
472  static_cast<const parent_type&>(*this).cbegin()
473  );
474 }
475 
476 
477 template<class Key, class Hash>
480 {
481  return iterator();
482 }
483 
484 
485 template<class Key, class Hash>
488 {
489  return const_iterator();
490 }
491 
492 
493 template<class Key, class Hash>
494 inline constexpr typename Foam::HashSet<Key, Hash>::const_iterator
496 {
497  return const_iterator();
498 }
499 
500 
501 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
502 
503 #endif
504 
505 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::IndirectListBase::end
iterator end()
Return an iterator at end of list.
Definition: IndirectListBase.H:346
Foam::HashSet::HashSet
HashSet()
Default construct with default (128) table capacity.
Definition: HashSet.H:127
Foam::FixedList::end
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
Foam::UList::end
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
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:332
Foam::HashTable::cbegin
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableIterI.H:248
Foam::HashSet::end
iterator end() noexcept
Definition: HashSet.C:479
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::FixedList::size
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition: FixedList.H:416
Foam::HashTable::cend
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
Definition: HashTableIterI.H:272
Foam::BitOps::unset
void unset(List< bool > &bools, const labelRange &range)
Unset the specified range 'on' in a boolList.
Definition: BitOps.C:96
erase
srcOptions erase("case")
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:77
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< word, Hash< word > >::const_iterator
typename parent_type::const_key_iterator const_iterator
A const_iterator, returning reference to the key.
Definition: HashSet.H:121
Foam::FixedList::begin
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
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::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
resize
patchWriters resize(patchIds.size())
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::Detail::ListPolicy::short_length
Number of items before requiring line-breaks in the list output.
Definition: ListPolicy.H:61
Foam::HashSet::operator&=
this_type & operator&=(const this_type &rhs)
Only retain entries found in both HashSets.
Definition: HashSet.C:317
Foam::HashSet::unset
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:204
HashSet.H
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
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IndirectListBase::begin
iterator begin()
Return an iterator at begin of list.
Definition: IndirectListBase.H:340
Foam::HashSet::begin
iterator begin()
Definition: HashSet.C:446
Foam::HashSet::operator()
bool operator()(const Key &key) const noexcept
Return true if the entry exists, same as found()
Definition: HashSet.C:238
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::HashTable::begin
iterator begin()
iterator set to the beginning of the HashTable
Definition: HashTableIterI.H:232
Foam::HashTable::capacity
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:45
clear
patchWriters clear()
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::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
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
Foam::HashSet::iterator
typename parent_type::key_iterator iterator
An iterator, returning reference to the key.
Definition: HashSet.H:118
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
FixedList.H