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-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 \*---------------------------------------------------------------------------*/
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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
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
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  this->parent_type::erase(rhs);
361 
362  return *this;
363 }
364 
365 
366 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
367 
368 template<class Key, class Hash>
370 {
372 }
373 
374 
375 /* * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * */
376 
377 template<class Key, class Hash>
378 Foam::HashSet<Key, Hash> Foam::operator|
379 (
380  const HashSet<Key, Hash>& hash1,
381  const HashSet<Key, Hash>& hash2
382 )
383 {
384  HashSet<Key, Hash> out(hash1);
385  out |= hash2;
386  return out;
387 }
388 
389 
390 template<class Key, class Hash>
391 Foam::HashSet<Key, Hash> Foam::operator&
392 (
393  const HashSet<Key, Hash>& hash1,
394  const HashSet<Key, Hash>& hash2
395 )
396 {
397  HashSet<Key, Hash> out(hash1);
398  out &= hash2;
399  return out;
400 }
401 
402 
403 template<class Key, class Hash>
404 Foam::HashSet<Key, Hash> Foam::operator^
405 (
406  const HashSet<Key, Hash>& hash1,
407  const HashSet<Key, Hash>& hash2
408 )
409 {
410  HashSet<Key, Hash> out(hash1);
411  out ^= hash2;
412  return out;
413 }
414 
415 
416 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
417 
418 template<class Key, class Hash>
421 {
422  return HashTableCore::iterator_begin<iterator>
423  (
424  static_cast<parent_type&>(*this)
425  );
426 }
427 
428 
429 template<class Key, class Hash>
432 {
433  return HashTableCore::iterator_cbegin<const_iterator>
434  (
435  static_cast<const parent_type&>(*this)
436  );
437 }
438 
439 
440 template<class Key, class Hash>
443 {
444  return HashTableCore::iterator_cbegin<const_iterator>
445  (
446  static_cast<const parent_type&>(*this)
447  );
448 }
449 
450 
451 template<class Key, class Hash>
452 inline const typename Foam::HashSet<Key, Hash>::iterator&
454 {
455  return HashTableCore::iterator_end<iterator>();
456 }
457 
458 
459 template<class Key, class Hash>
460 inline const typename Foam::HashSet<Key, Hash>::const_iterator&
462 {
463  return HashTableCore::iterator_cend<const_iterator>();
464 }
465 
466 
467 template<class Key, class Hash>
468 inline const typename Foam::HashSet<Key, Hash>::const_iterator&
470 {
471  return HashTableCore::iterator_cend<const_iterator>();
472 }
473 
474 
475 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
476 
477 #endif
478 
479 // ************************************************************************* //
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:337
Foam::HashSet::HashSet
HashSet()
Construct null with default (128) table capacity.
Definition: HashSet.H:118
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:473
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::cbegin
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableIterI.H:268
Foam::FixedList::size
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition: FixedList.H:368
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:84
Foam::HashTable::cend
const const_iterator & cend() const
const_iterator to signal the end for any HashTable
Definition: HashTableIterI.H:292
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.
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
resize
patchWriters resize(patchIds.size())
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::HashSet::end
const iterator & end()
Definition: HashSet.C:453
Foam::Detail::ListPolicy::short_length
Number of items before requiring line-breaks in the list ouput.
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:195
HashSet.H
Foam::HashSet::operator-=
this_type & operator-=(const this_type &rhs)
Remove entries from this HashSet.
Definition: HashSet.C:358
Foam::IndirectListBase::begin
iterator begin()
Return an iterator at begin of list.
Definition: IndirectListBase.H:331
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::FixedList::end
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:497
Foam::HashTable< zero::null, Foam::string, string::hash >
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::HashSet::operator[]
bool operator[](const Key &key) const
Return true if the entry exists, same as found().
Definition: HashSet.C:256
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:442
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::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::iterator
typename parent_type::key_iterator iterator
An iterator, returning reference to the key.
Definition: HashSet.H:109
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:57
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
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102