BitOps.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-2020 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 "BitOps.H"
29 #include "bitSet.H"
30 #include "HashSet.H"
31 #include "List.H"
32 #include "labelRange.H"
33 
34 // * * * * * * * * * * * * * * * * * BitOps * * * * * * * * * * * * * * * * //
35 
36 // See bitSet::set(labelRange) for original implementation
38 {
39  labelRange slice(range);
40  slice.adjust(); // No negative start, size adjusted accordingly
41 
42  // Range is invalid (zero-sized or entirely negative) - noop
43  if (slice.empty())
44  {
45  return;
46  }
47 
48  // Range finishes at or beyond the right side.
49  // - zero fill any gaps that we might create.
50  // - flood-fill the rest, which now corresponds to the full range.
51  //
52  // NB: use labelRange after() for the exclusive end-value, which
53  // corresponds to our new set size.
54  if (slice.after() >= bools.size())
55  {
56  label i = bools.size();
57 
58  bools.resize(slice.after(), true);
59 
60  // Backfill with false
61  while (i < slice.start())
62  {
63  bools.unset(i);
64  ++i;
65  }
66  return;
67  }
68 
69  for (label i = slice.first(); i <= slice.last(); ++i)
70  {
71  bools.set(i);
72  }
73 }
74 
75 
76 // See bitSet::set(labelRange) for original implementation
78 {
79  labelRange slice(range);
80  slice.adjust(); // No negative start, size adjusted accordingly
81 
82  for (label i = slice.first(); i <= slice.last(); ++i)
83  {
84  hashset.set(i);
85  }
86 }
87 
88 
90 {
91  bitset.set(range);
92 }
93 
94 
95 // See bitSet::unset(labelRange) for original implementation
97 {
98  for (label i = range.first(); i <= range.last(); ++i)
99  {
100  bools.unset(i);
101  }
102 }
103 
104 
106 {
107  for (label i = range.first(); i <= range.last(); ++i)
108  {
109  hashset.unset(i);
110  }
111 }
112 
113 
115 {
116  bitset.unset(range);
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * BitSetOps * * * * * * * * * * * * * * * * //
121 
123 (
124  const label n,
125  const labelHashSet& locations,
126  const bool on
127 )
128 {
129  bitSet output(n, !on);
130 
131  for (const label idx : locations)
132  {
133  // Restrict the input size
134  if (idx < n)
135  {
136  output.set(idx, on);
137  }
138  }
139 
140  return output;
141 }
142 
143 
145 (
146  const label n,
147  const labelUList& locations,
148  const bool on
149 )
150 {
151  bitSet output(n, !on);
152 
153  for (const label idx : locations)
154  {
155  // Restrict the input size
156  if (idx < n)
157  {
158  output.set(idx, on);
159  }
160  }
161 
162  return output;
163 }
164 
165 
167 (
168  const label n,
169  const label select,
170  const labelUList& values,
171  const bool on
172 )
173 {
174  bitSet output(n, !on);
175 
176  // Restrict the input size
177  const label len = std::min(n, values.size());
178 
179  for (label idx = 0; idx < len; ++idx)
180  {
181  if (select == values[idx])
182  {
183  output.set(idx, on);
184  }
185  }
186 
187  return output;
188 }
189 
190 
191 // ************************************************************************* //
BitOps.H
Foam::BitSetOps::create
bitSet create(const label n, const labelHashSet &locations, const bool on=true)
Create a bitSet with length n with the specified on locations.
Definition: BitOps.C:123
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
List.H
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::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::bitSet::unset
bitSet & unset(const bitSet &other)
Definition: bitSetI.H:612
Foam::labelRange::adjust
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:81
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:574
Foam::BitOps::unset
void unset(List< bool > &bools, const labelRange &range)
Unset the specified range 'on' in a boolList.
Definition: BitOps.C:96
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::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::labelRange::after
label after() const noexcept
The value after the last element in the range.
Definition: labelRangeI.H:95
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::IntRange::empty
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: IntRangeI.H:436
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::HashSet::unset
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:204
HashSet.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
labelRange.H
Foam::List< bool >
Foam::IntRange::first
IntType first() const noexcept
The (inclusive) lower value of the range. Same as start()
Definition: IntRangeI.H:443
Foam::UList< label >
Foam::List::set
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:341
Foam::IntRange::start
IntType start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRangeI.H:408
Foam::HashSet::set
bool set(const Key &key)
Same as insert (no value to overwrite)
Definition: HashSet.H:197
Foam::IntRange::last
IntType last() const noexcept
The (inclusive) upper value of the range.
Definition: IntRangeI.H:450