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 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 #include <algorithm>
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
36 {
38 
39  if (select.any())
40  {
41  output.resize(2*select.count());
42 
43  for (label i = select.find_first(); i >= 0; i = select.find_next(i))
44  {
45  output.insert(i);
46  }
47  }
48 
49  return output;
50 }
51 
52 
54 {
55  const label len = select.size();
56 
57  // No idea of the sparseness, just assume 1/8
58  labelHashSet output(len/4);
59 
60  for (label i = 0; i < len; ++i)
61  {
62  if (select[i])
63  {
64  output.insert(i);
65  }
66  }
67 
68  return output;
69 }
70 
71 
73 {
74  bitSet output;
75  output.setMany(locations.begin(), locations.end());
76 
77  return output;
78 }
79 
80 
82 {
83  auto const max = std::max_element(locations.begin(), locations.end());
84  const label len = (max != locations.end() ? (1 + *max) : 0);
85 
86  if (len <= 0)
87  {
88  return List<bool>();
89  }
90 
91  List<bool> output(len, false);
92 
93  for (const label i : locations)
94  {
95  if (i >= 0)
96  {
97  output[i] = true;
98  }
99  }
100 
101  return output;
102 }
103 
104 
105 // ************************************************************************* //
Foam::bitSet::find_first
label find_first() const
Locate the first bit that is set.
Definition: bitSetI.H:314
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::HashSetOps::bools
List< bool > bools(const labelHashSet &locations)
Definition: HashOps.C:81
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::HashSet::end
iterator end() noexcept
Definition: HashSet.C:479
Foam::bitSet::any
bool any() const
True if any bits in this bitset are set.
Definition: bitSetI.H:468
Foam::HashSetOps::bitset
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:72
Foam::HashSet< label, Hash< label > >
bitSet.H
Foam::bitSet::count
unsigned int count(const bool on=true) const
Count number of bits set.
Definition: bitSetI.H:499
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::bitSet::find_next
label find_next(label pos) const
Locate the next bit set, starting one beyond the specified position.
Definition: bitSetI.H:400
Foam::HashSet::begin
iterator begin()
Definition: HashSet.C:446
Foam::List< bool >
Foam::HashSetOps::used
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:35
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
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
HashOps.H