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-------------------------------------------------------------------------------
11License
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
37template<class Key, class Hash>
38template<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
62template<class Key, class Hash>
63template<unsigned N>
65:
66 parent_type(2*list.size())
67{
68 insert(list.begin(), list.end());
69}
70
71
72template<class Key, class Hash>
74:
75 parent_type(2*list.size())
76{
77 insert(list.begin(), list.end());
78}
79
80
81template<class Key, class Hash>
82template<class Addr>
84:
85 parent_type(2*list.size())
86{
87 insert(list.begin(), list.end());
88}
89
90
91template<class Key, class Hash>
92Foam::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
100template<class Key, class Hash>
101template<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
118template<class Key, class Hash>
119template<class InputIter>
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
139template<class Key, class Hash>
141(
142 std::initializer_list<Key> list
143)
144{
145 return insert(list.begin(), list.end());
146}
147
148
149template<class Key, class Hash>
150template<unsigned N>
152(
153 const FixedList<Key, N>& list
154)
155{
156 return insert(list.begin(), list.end());
157}
159
160template<class Key, class Hash>
162(
163 const UList<Key>& list
164)
166 return insert(list.begin(), list.end());
167}
169
170template<class Key, class Hash>
171template<class Addr>
175)
176{
177 return insert(list.begin(), list.end());
178}
179
180
181template<class Key, class Hash>
182template<class InputIter>
184(
185 InputIter first,
186 InputIter last
187)
188{
189 return this->parent_type::erase(first, last);
190}
191
192
193template<class Key, class Hash>
195(
196 std::initializer_list<Key> list
197)
198{
199 return unset(list.begin(), list.end());
200}
201
202
203template<class Key, class Hash>
204template<unsigned N>
206(
207 const FixedList<Key, N>& list
208)
209{
210 return unset(list.begin(), list.end());
211}
212
213
214template<class Key, class Hash>
216(
217 const UList<Key>& list
218)
220 return unset(list.begin(), list.end());
221}
222
223
224template<class Key, class Hash>
225template<class Addr>
227(
229)
230{
231 return unset(list.begin(), list.end());
232}
233
234
235// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
236
237template<class Key, class Hash>
238inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const noexcept
239{
240 return this->found(key);
241}
242
243
244template<class Key, class Hash>
245inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const noexcept
246{
247 return this->found(key);
248}
249
250
251template<class Key, class Hash>
252template<unsigned N>
254{
255 assignMany(rhs.size(), rhs.begin(), rhs.end());
256}
257
258
259template<class Key, class Hash>
261{
262 assignMany(rhs.size(), rhs.begin(), rhs.end());
263}
264
265
266template<class Key, class Hash>
267void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs)
268{
269 assignMany(rhs.size(), rhs.begin(), rhs.end());
270}
271
272
273template<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)
284 if (!this->found(iter.key()))
285 {
286 return false;
287 }
288 }
289
290 return true;
291}
293
294template<class Key, class Hash>
296{
297 return !operator==(rhs);
298}
299
300
301template<class Key, class Hash>
305 // Add rhs elements into lhs
306 for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
308 this->insert(iter.key());
309 }
310
311 return *this;
312}
313
314
315template<class Key, class Hash>
318{
319 this->parent_type::retain(rhs);
320 return *this;
321}
322
323
324template<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
345template<class Key, class Hash>
349 return this->operator|=(rhs);
350}
352
353template<class Key, class Hash>
356{
357 this->parent_type::erase(rhs);
358
359 return *this;
360}
362
363// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
365template<class Key, class Hash>
367{
369}
371
372/* * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * */
374template<class Key, class Hash>
375Foam::HashSet<Key, Hash> Foam::operator|
377 const HashSet<Key, Hash>& a,
378 const HashSet<Key, Hash>& b
380{
381 HashSet<Key, Hash> result(a);
382 result |= b;
383 return result;
384}
385
386
387template<class Key, class Hash>
388Foam::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
408template<class Key, class Hash>
409Foam::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
421template<class Key, class Hash>
422Foam::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
444template<class Key, class Hash>
447{
448 return iterator
449 (
450 static_cast<parent_type&>(*this).begin()
451 );
452}
453
454
455template<class Key, class Hash>
458{
459 return const_iterator
460 (
461 static_cast<const parent_type&>(*this).begin()
462 );
463}
464
465
466template<class Key, class Hash>
469{
470 return const_iterator
471 (
472 static_cast<const parent_type&>(*this).cbegin()
473 );
474}
475
476
477template<class Key, class Hash>
480{
481 return iterator();
482}
483
484
485template<class Key, class Hash>
488{
489 return const_iterator();
490}
491
492
493template<class Key, class Hash>
494inline constexpr typename Foam::HashSet<Key, Hash>::const_iterator
496{
497 return const_iterator();
498}
499
500
501// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
502
503#endif
504
505// ************************************************************************* //
label k
bool found
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition: FixedList.H:416
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:96
bool operator[](const Key &key) const noexcept
Return true if the entry exists, same as found().
Definition: HashSet.C:245
iterator end() noexcept
Definition: HashSet.C:479
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
this_type & operator|=(const this_type &rhs)
Add entries to this HashSet.
Definition: HashSet.C:303
typename parent_type::key_iterator iterator
An iterator, returning reference to the key.
Definition: HashSet.H:118
typename parent_type::const_key_iterator const_iterator
A const_iterator, returning reference to the key.
Definition: HashSet.H:121
const_iterator cbegin() const
Definition: HashSet.C:468
iterator begin()
Definition: HashSet.C:446
constexpr const_iterator cend() const noexcept
Definition: HashSet.C:495
HashSet()
Default construct with default (128) table capacity.
Definition: HashSet.H:127
this_type & operator-=(const this_type &rhs)
Remove entries from this HashSet. Uses erase()
Definition: HashSet.C:355
this_type & operator&=(const this_type &rhs)
Only retain entries found in both HashSets.
Definition: HashSet.C:317
this_type & operator^=(const this_type &rhs)
Only retain unique entries (xor)
Definition: HashSet.C:326
void operator=(const this_type &rhs)
Copy assign.
Definition: HashSet.H:332
this_type & operator+=(const this_type &rhs)
Add entries to this HashSet. Same as the '|=' operator.
Definition: HashSet.C:347
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Definition: HashTableIO.C:97
iterator begin()
iterator set to the beginning of the HashTable
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:45
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
iterator end()
Return an iterator at end of list.
iterator begin()
Return an iterator at begin of list.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:265
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
static const triad unset
Definition: triad.H:97
patchWriters resize(patchIds.size())
patchWriters clear()
OBJstream os(runTime.globalPath()/outputName)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const direction noexcept
Definition: Scalar.H:223
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
srcOptions erase("case")
volScalarField & b
Definition: createFields.H:27
Number of items before requiring line-breaks in the list output.
Definition: ListPolicy.H:61