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  for (const auto& k : list)
69  {
70  this->insert(k);
71  }
72 }
73 
74 
75 template<class Key, class Hash>
77 :
78  parent_type(2*list.size())
79 {
80  for (const auto& k : list)
81  {
82  this->insert(k);
83  }
84 }
85 
86 
87 template<class Key, class Hash>
88 template<class Addr>
90 :
91  parent_type(2*list.size())
92 {
93  for (const auto& k : list)
94  {
95  this->insert(k);
96  }
97 }
98 
99 
100 template<class Key, class Hash>
101 Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> list)
102 :
103  parent_type(2*list.size())
104 {
105  for (const auto& k : list)
106  {
107  this->insert(k);
108  }
109 }
110 
111 
112 template<class Key, class Hash>
113 template<class AnyType, class AnyHash>
115 (
117 )
118 :
119  parent_type(tbl.capacity())
120 {
121  for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
122  {
123  this->insert(iter.key());
124  }
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
130 template<class Key, class Hash>
131 template<class InputIter>
132 inline Foam::label Foam::HashSet<Key, Hash>::insert
133 (
134  InputIter first,
135  InputIter last
136 )
137 {
138  label changed = 0;
139  for (; first != last; ++first)
140  {
141  if (insert(*first))
142  {
143  ++changed;
144  }
145  }
146  return changed;
147 }
148 
149 
150 template<class Key, class Hash>
151 inline Foam::label Foam::HashSet<Key, Hash>::insert
152 (
153  std::initializer_list<Key> list
154 )
155 {
156  return insert(list.begin(), list.end());
157 }
158 
159 
160 template<class Key, class Hash>
161 template<unsigned N>
162 inline Foam::label Foam::HashSet<Key, Hash>::insert
163 (
164  const FixedList<Key, N>& list
165 )
166 {
167  return insert(list.begin(), list.end());
168 }
169 
170 
171 template<class Key, class Hash>
172 inline Foam::label Foam::HashSet<Key, Hash>::insert
173 (
174  const UList<Key>& list
175 )
176 {
177  return insert(list.begin(), list.end());
178 }
179 
180 
181 template<class Key, class Hash>
182 template<class Addr>
183 inline Foam::label Foam::HashSet<Key, Hash>::insert
184 (
185  const IndirectListBase<Key, Addr>& list
186 )
187 {
188  return insert(list.begin(), list.end());
189 }
190 
191 
192 template<class Key, class Hash>
193 template<class InputIter>
194 inline Foam::label Foam::HashSet<Key, Hash>::unset
195 (
196  InputIter first,
197  InputIter last
198 )
199 {
200  return this->parent_type::erase(first, last);
201 }
202 
203 
204 template<class Key, class Hash>
205 inline Foam::label Foam::HashSet<Key, Hash>::unset
206 (
207  std::initializer_list<Key> list
208 )
209 {
210  return unset(list.begin(), list.end());
211 }
212 
213 
214 template<class Key, class Hash>
215 template<unsigned N>
216 inline Foam::label Foam::HashSet<Key, Hash>::unset
217 (
218  const FixedList<Key, N>& list
219 )
220 {
221  return unset(list.begin(), list.end());
222 }
223 
224 
225 template<class Key, class Hash>
226 inline Foam::label Foam::HashSet<Key, Hash>::unset
227 (
228  const UList<Key>& list
229 )
230 {
231  return unset(list.begin(), list.end());
232 }
233 
234 
235 template<class Key, class Hash>
236 template<class Addr>
237 inline Foam::label Foam::HashSet<Key, Hash>::unset
238 (
239  const IndirectListBase<Key, Addr>& list
240 )
241 {
242  return unset(list.begin(), list.end());
243 }
244 
245 
246 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
247 
248 template<class Key, class Hash>
249 inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const noexcept
250 {
251  return this->found(key);
252 }
253 
254 
255 template<class Key, class Hash>
256 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const noexcept
257 {
258  return this->found(key);
259 }
260 
261 
262 template<class Key, class Hash>
263 template<unsigned N>
265 {
266  assignMany(rhs.size(), rhs.begin(), rhs.end());
267 }
268 
269 
270 template<class Key, class Hash>
272 {
273  assignMany(rhs.size(), rhs.begin(), rhs.end());
274 }
275 
276 
277 template<class Key, class Hash>
278 void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs)
279 {
280  assignMany(rhs.size(), rhs.begin(), rhs.end());
281 }
282 
283 
284 template<class Key, class Hash>
286 {
287  // Sizes (number of keys) must match
288  if (this->size() != rhs.size())
289  {
290  return false;
291  }
292 
293  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
294  {
295  if (!this->found(iter.key()))
296  {
297  return false;
298  }
299  }
300 
301  return true;
302 }
303 
304 
305 template<class Key, class Hash>
307 {
308  return !operator==(rhs);
309 }
310 
311 
312 template<class Key, class Hash>
315 {
316  // Add rhs elements into lhs
317  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
318  {
319  this->insert(iter.key());
320  }
321 
322  return *this;
323 }
324 
325 
326 template<class Key, class Hash>
329 {
330  this->parent_type::retain(rhs);
331  return *this;
332 }
333 
334 
335 template<class Key, class Hash>
338 {
339  // Add missed rhs elements, remove duplicate elements
340  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
341  {
342  if (this->found(iter.key()))
343  {
344  this->erase(iter.key());
345  }
346  else
347  {
348  this->insert(iter.key());
349  }
350  }
351 
352  return *this;
353 }
354 
355 
356 template<class Key, class Hash>
359 {
360  return this->operator|=(rhs);
361 }
362 
363 
364 template<class Key, class Hash>
367 {
368  this->parent_type::erase(rhs);
369 
370  return *this;
371 }
372 
373 
374 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
375 
376 template<class Key, class Hash>
378 {
380 }
381 
382 
383 /* * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * */
384 
385 template<class Key, class Hash>
386 Foam::HashSet<Key, Hash> Foam::operator|
387 (
388  const HashSet<Key, Hash>& hash1,
389  const HashSet<Key, Hash>& hash2
390 )
391 {
392  HashSet<Key, Hash> out(hash1);
393  out |= hash2;
394  return out;
395 }
396 
397 
398 template<class Key, class Hash>
399 Foam::HashSet<Key, Hash> Foam::operator&
400 (
401  const HashSet<Key, Hash>& hash1,
402  const HashSet<Key, Hash>& hash2
403 )
404 {
405  HashSet<Key, Hash> out(hash1);
406  out &= hash2;
407  return out;
408 }
409 
410 
411 template<class Key, class Hash>
412 Foam::HashSet<Key, Hash> Foam::operator^
413 (
414  const HashSet<Key, Hash>& hash1,
415  const HashSet<Key, Hash>& hash2
416 )
417 {
418  HashSet<Key, Hash> out(hash1);
419  out ^= hash2;
420  return out;
421 }
422 
423 
424 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
425 
426 template<class Key, class Hash>
429 {
430  return iterator
431  (
432  static_cast<parent_type&>(*this).begin()
433  );
434 }
435 
436 
437 template<class Key, class Hash>
440 {
441  return const_iterator
442  (
443  static_cast<const parent_type&>(*this).begin()
444  );
445 }
446 
447 
448 template<class Key, class Hash>
451 {
452  return const_iterator
453  (
454  static_cast<const parent_type&>(*this).cbegin()
455  );
456 }
457 
458 
459 template<class Key, class Hash>
462 {
463  return iterator();
464 }
465 
466 
467 template<class Key, class Hash>
470 {
471  return const_iterator();
472 }
473 
474 
475 template<class Key, class Hash>
476 inline constexpr typename Foam::HashSet<Key, Hash>::const_iterator
478 {
479  return const_iterator();
480 }
481 
482 
483 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
484 
485 #endif
486 
487 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::HashTable< zero::null, Key, Hash >::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::IndirectListBase::end
iterator end()
Return an iterator at end of list.
Definition: IndirectListBase.H:338
Foam::HashSet::HashSet
HashSet()
Default construct with default (128) table capacity.
Definition: HashSet.H:117
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:487
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::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:461
Foam::FixedList::size
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition: FixedList.H:384
Foam::HashTable::cend
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
Definition: HashTableIterI.H:272
erase
srcOptions erase("case")
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::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
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
resize
patchWriters resize(patchIds.size())
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:328
Foam::HashSet::unset
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:194
HashSet.H
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::IndirectListBase::begin
iterator begin()
Return an iterator at begin of list.
Definition: IndirectListBase.H:332
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::FixedList::end
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:511
Foam::HashSet::operator()
bool operator()(const Key &key) const noexcept
Return true if the entry exists, same as found()
Definition: HashSet.C:249
Foam::HashTable< zero::null, Foam::string, string::hash >
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:337
Foam::HashSet::cbegin
const_iterator cbegin() const
Definition: HashSet.C:450
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:181
Foam::HashSet::iterator
typename parent_type::key_iterator iterator
An iterator, returning reference to the key.
Definition: HashSet.H:108
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
FixedList.H
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:297