Loading...
Searching...
No Matches
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-2023 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.
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>
39inline Foam::label Foam::HashSet<Key, Hash>::assignMany
40(
41 const label nItems,
42 InputIter first,
43 InputIter last
44)
45{
46 this->clear();
47 this->reserve(nItems);
48
49 return insert(first, last);
51
52
53// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54
55template<class Key, class Hash>
56template<unsigned N>
58:
59 parent_type(2*list.size())
60{
61 insert(list.begin(), list.end());
62}
63
64
65template<class Key, class Hash>
67:
68 parent_type(2*list.size())
70 insert(list.begin(), list.end());
71}
72
73
74template<class Key, class Hash>
75template<class Addr>
77:
78 parent_type(2*list.size())
79{
80 insert(list.begin(), list.end());
81}
82
83
84template<class Key, class Hash>
85Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> list)
86:
87 parent_type(2*list.size())
89 insert(list.begin(), list.end());
90}
91
92
93template<class Key, class Hash>
94template<class AnyType, class AnyHash>
96(
98)
99:
100 parent_type(2*tbl.size())
101{
102 for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
103 {
104 this->insert(iter.key());
105 }
107
108
109// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
110
111template<class Key, class Hash>
112template<class InputIter>
113inline Foam::label Foam::HashSet<Key, Hash>::insert
114(
115 InputIter first,
116 InputIter last
117)
118{
119 label changed = 0;
120 while (first != last)
121 {
122 if (insert(*first))
123 {
124 ++changed;
125 }
126 ++first;
127 }
128 return changed;
129}
130
131
132template<class Key, class Hash>
133inline Foam::label Foam::HashSet<Key, Hash>::insert
134(
135 std::initializer_list<Key> list
136)
138 return insert(list.begin(), list.end());
139}
140
141
142template<class Key, class Hash>
143template<unsigned N>
144inline Foam::label Foam::HashSet<Key, Hash>::insert
145(
146 const FixedList<Key, N>& list
148{
149 return insert(list.begin(), list.end());
150}
151
152
153template<class Key, class Hash>
154inline Foam::label Foam::HashSet<Key, Hash>::insert
155(
156 const UList<Key>& list
157)
159 return insert(list.begin(), list.end());
160}
161
162
163template<class Key, class Hash>
164template<class Addr>
165inline Foam::label Foam::HashSet<Key, Hash>::insert
166(
168)
170 return insert(list.begin(), list.end());
171}
172
173
174template<class Key, class Hash>
175template<class InputIter>
176inline Foam::label Foam::HashSet<Key, Hash>::unset
177(
178 InputIter first,
179 InputIter last
181{
182 return this->parent_type::erase(first, last);
183}
184
185
186template<class Key, class Hash>
187inline Foam::label Foam::HashSet<Key, Hash>::unset
188(
189 std::initializer_list<Key> list
190)
192 return unset(list.begin(), list.end());
193}
194
195
196template<class Key, class Hash>
197template<unsigned N>
198inline Foam::label Foam::HashSet<Key, Hash>::unset
199(
200 const FixedList<Key, N>& list
202{
203 return unset(list.begin(), list.end());
204}
205
206
207template<class Key, class Hash>
208inline Foam::label Foam::HashSet<Key, Hash>::unset
209(
210 const UList<Key>& list
211)
213 return unset(list.begin(), list.end());
214}
215
216
217template<class Key, class Hash>
218template<class Addr>
219inline Foam::label Foam::HashSet<Key, Hash>::unset
220(
223{
224 return unset(list.begin(), list.end());
225}
226
227
228template<class Key, class Hash>
233
234
235template<class Key, class Hash>
237{
239}
240
241
242// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
243
244template<class Key, class Hash>
245inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const noexcept
246{
247 return this->contains(key);
248}
249
250
251template<class Key, class Hash>
252inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const noexcept
254 return this->contains(key);
255}
256
258template<class Key, class Hash>
259template<unsigned N>
261{
262 assignMany(rhs.size(), rhs.begin(), rhs.end());
263}
264
265
266template<class Key, class Hash>
268{
269 assignMany(rhs.size(), rhs.begin(), rhs.end());
270}
271
272
273template<class Key, class Hash>
274void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs)
275{
276 assignMany(rhs.size(), rhs.begin(), rhs.end());
277}
278
279
280template<class Key, class Hash>
282{
283 // Trivial checks first
284 if (this == &rhs)
285 {
286 return true;
287 }
288 else 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->contains(iter.key()))
296 {
297 return false;
298 }
300
301 return true;
302}
303
304
305template<class Key, class Hash>
306bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
308 return !operator==(rhs);
309}
310
311
312template<class Key, class Hash>
315{
316 // Add rhs elements into lhs - avoid no-ops
317 if (this != &rhs)
318 {
319 for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
320 {
321 this->insert(iter.key());
322 }
323 }
325 return *this;
326}
327
328
329template<class Key, class Hash>
332{
334 return *this;
335}
336
337
338template<class Key, class Hash>
341{
342 // Add missed rhs elements, remove duplicate elements
343 for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
344 {
345 if (this->contains(iter.key()))
346 {
347 this->erase(iter.key());
348 }
349 else
350 {
351 this->insert(iter.key());
352 }
353 }
355 return *this;
356}
357
358
359template<class Key, class Hash>
363 return this->operator|=(rhs);
364}
365
366
367template<class Key, class Hash>
370{
371 this->parent_type::erase(rhs);
372
373 return *this;
374}
375
376
377// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
378
379template<class Key, class Hash>
380Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& rhs)
381{
383}
384
385
386/* * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * */
387
388template<class Key, class Hash>
389Foam::HashSet<Key, Hash> Foam::operator|
390(
391 const HashSet<Key, Hash>& a,
392 const HashSet<Key, Hash>& b
393)
394{
395 HashSet<Key, Hash> result(a);
396 result |= b;
397 return result;
398}
399
400
401template<class Key, class Hash>
403(
405 const HashSet<Key, Hash>& b
407{
408 HashSet<Key, Hash> result(a.capacity());
409
410 for (const Key& k : a)
411 {
412 if (b.contains(k))
413 {
414 result.insert(k);
415 }
416 }
417
418 return result;
419}
420
421
422template<class Key, class Hash>
423Foam::HashSet<Key, Hash> Foam::operator^
424(
425 const HashSet<Key, Hash>& a,
426 const HashSet<Key, Hash>& b
427)
428{
429 HashSet<Key, Hash> result(a);
430 result ^= b;
431 return result;
433
434
435template<class Key, class Hash>
436Foam::HashSet<Key, Hash> Foam::operator-
438 const HashSet<Key, Hash>& a,
439 const HashSet<Key, Hash>& b
440)
441{
442 HashSet<Key, Hash> result(a.capacity());
443
444 for (const Key& k : a)
445 {
446 if (!b.contains(k))
447 {
448 result.insert(k);
449 }
450 }
451
452 return result;
454
455
456// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
457
458template<class Key, class Hash>
461{
462 return iterator
463 (
464 static_cast<parent_type&>(*this).begin()
465 );
466}
468
469template<class Key, class Hash>
472{
473 return const_iterator
474 (
475 static_cast<const parent_type&>(*this).begin()
476 );
477}
478
479
480template<class Key, class Hash>
483{
484 return const_iterator
485 (
486 static_cast<const parent_type&>(*this).cbegin()
487 );
488}
489
490
491template<class Key, class Hash>
495 return iterator();
496}
497
498
499template<class Key, class Hash>
503 return const_iterator();
505
506
507template<class Key, class Hash>
508inline constexpr typename Foam::HashSet<Key, Hash>::const_iterator
510{
511 return const_iterator();
512}
513
515// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
516
517#endif
518
519// ************************************************************************* //
label k
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition FixedListI.H:526
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition FixedListI.H:478
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 contains().
Definition HashSet.C:245
bool operator()(const Key &key) const noexcept
Return true if the entry exists, same as contains().
Definition HashSet.C:238
iterator end() noexcept
Definition HashSet.C:486
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
this_type & operator|=(const this_type &rhs)
Add entries to this HashSet.
Definition HashSet.C:307
constexpr HashSet() noexcept=default
Default construct: empty without allocation (capacity=0).
typename parent_type::key_iterator iterator
An iterator, returning reference to the key.
Definition HashSet.H:126
bool operator==(const this_type &rhs) const
Sets are equal if all keys are equal, independent of order or underlying storage size.
Definition HashSet.C:274
typename parent_type::const_key_iterator const_iterator
A const_iterator, returning reference to the key.
Definition HashSet.H:131
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition HashSet.H:247
const_iterator cbegin() const
Definition HashSet.C:475
iterator begin()
Definition HashSet.C:453
constexpr const_iterator cend() const noexcept
Definition HashSet.C:502
this_type & operator-=(const this_type &rhs)
Remove entries from this HashSet. Uses erase().
Definition HashSet.C:362
this_type & operator&=(const this_type &rhs)
Only retain entries contained in both HashSets.
Definition HashSet.C:324
this_type & operator^=(const this_type &rhs)
Only retain unique entries (xor).
Definition HashSet.C:333
void operator=(const this_type &rhs)
Copy assign.
Definition HashSet.H:442
void merge(HashSet< Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
Definition HashSet.C:222
bool operator!=(const this_type &rhs) const
The opposite of the equality operation.
Definition HashSet.C:299
HashTable< Foam::zero, Key, Hash > parent_type
The template instance used for the parent HashTable.
Definition HashSet.H:121
this_type & operator+=(const this_type &rhs)
Add entries to this HashSet. Same as the '|=' operator.
Definition HashSet.C:354
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
label retain(const HashTable< AnyType, Key, AnyHash > &other)
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
Definition HashTableI.H:72
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
void merge(HashTable< Foam::zero, Key, Hash > &source)
label capacity() const noexcept
The size of the underlying table (the number of buckets).
Definition HashTable.H:363
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition HashTable.C:489
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
constexpr HashTable() noexcept
Default construct: empty without allocation (capacity=0).
Definition HashTable.C:33
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:59
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:89
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
OBJstream os(runTime.globalPath()/outputName)
surface1 clear()
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
srcOptions erase("case")
triangles reserve(surf.size())
volScalarField & b
nonInt insert("surfaceSum(((S|magSf)*S)")
Number of items before requiring line-breaks in the list output.
Definition ListPolicy.H:56