Loading...
Searching...
No Matches
HashOps.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) 2018-2023 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "HashOps.H"
29#include "bitSet.H"
30
31// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32
34{
35 labelHashSet output;
36
37 if (select.any())
38 {
39 output.reserve(select.count());
40
41 for (label i = select.find_first(); i >= 0; i = select.find_next(i))
42 {
43 output.insert(i);
44 }
45 }
46
47 return output;
48}
49
50
51Foam::labelHashSet Foam::HashSetOps::used(const UList<bool>& select)
52{
53 const label len = select.size();
54
55 // No idea of the sparseness, just assume 1/8
56 labelHashSet output(len/4);
57
58 for (label i = 0; i < len; ++i)
59 {
60 if (select[i])
61 {
62 output.insert(i);
63 }
64 }
65
66 return output;
67}
68
69
70Foam::bitSet Foam::HashSetOps::bitset(const labelHashSet& locations)
71{
72 bitSet output;
73 output.setMany(locations.begin(), locations.end());
74
75 return output;
76}
77
78
80{
81 auto const max = std::max_element(locations.begin(), locations.end());
82 const label len = (max != locations.end() ? (1 + *max) : 0);
83
84 if (len <= 0)
85 {
86 return List<bool>();
87 }
88
89 List<bool> output(len, false);
90
91 for (const label i : locations)
92 {
93 if (i >= 0)
94 {
95 output[i] = true;
96 }
97 }
98
99 return output;
100}
101
102
103// ************************************************************************* //
iterator end() noexcept
Definition HashSet.C:486
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
iterator begin()
Definition HashSet.C:453
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition HashTable.C:729
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
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
bool any() const
True if any entries are 'true'.
Definition UList.H:821
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
label setMany(InputIter first, InputIter last)
Set the locations listed by the iterator range, auto-vivify entries if needed.
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition BitOps.C:139
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition HashOps.C:26
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition HashOps.C:63
List< bool > bools(const labelHashSet &locations)
Transform the on locations to a boolList, with true for each non-negative location and false for all ...
Definition HashOps.C:72
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition HashSet.H:85