bitSetTemplates.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-2019 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 "FixedList.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<unsigned N>
33 Foam::bitSet::bitSet(const label n, const FixedList<label, N>& locations)
34 :
35  bitSet(n)
36 {
37 
38  setMany(locations.begin(), locations.end());
39 }
40 
41 
42 template<unsigned N>
44 :
45  bitSet()
46 {
47 
48  setMany(locations.begin(), locations.end());
49 }
50 
51 
52 template<class Addr>
54 (
55  const bitSet& bitset,
57 )
58 :
59  bitSet(addr.size())
60 {
61  const label len = addr.size();
62 
63  for (label i = 0; i < len; ++i)
64  {
65  set(i, bitset.get(addr[i]));
66  }
67 }
68 
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
73 template<class InputIter>
74 Foam::label Foam::bitSet::setMany(InputIter first, InputIter last)
75 {
76  // Check the max expected value first
77  const auto max = std::max_element(first, last);
78  const label len = (max != last ? (1 + *max) : 0);
79 
80  label changed = 0;
81 
82  if (len > 0)
83  {
84  reserve(len);
85 
86  for (; first != last; ++first)
87  {
88  if (set(*first))
89  {
90  ++changed;
91  }
92  }
93  }
94 
95  return changed;
96 }
97 
98 
99 template<class InputIter>
100 Foam::label Foam::bitSet::unset(InputIter first, InputIter last)
101 {
102  label changed = 0;
103 
104  for (; first != last; ++first)
105  {
106  if (unset(*first))
107  {
108  ++changed;
109  }
110  }
111 
112  return changed;
113 }
114 
115 
116 template<unsigned N>
117 Foam::label Foam::bitSet::set(const FixedList<label, N>& locations)
118 {
119  return setMany(locations.begin(), locations.end());
120 }
121 
122 
123 template<unsigned N>
124 Foam::label Foam::bitSet::unset(const FixedList<label, N>& locations)
125 {
126  return unset(locations.begin(), locations.end());
127 }
128 
129 
130 // ************************************************************************* //
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:504
label setMany(InputIter first, InputIter last)
Set the locations listed by the iterator range, auto-vivify entries if needed.
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:482
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:506
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
void unset(List< bool > &bools, const labelUList &locations)
Unset the listed locations (assign &#39;false&#39;).
Definition: BitOps.C:99
bitSet & unset(const bitSet &other)
Unset (subtract) the bits specified in the other bitset, which is a set difference corresponds to the...
Definition: bitSetI.H:542
label size() const noexcept
The number of elements in the list.
constexpr bitSet() noexcept
Default construct an empty, zero-sized bitSet.
Definition: bitSetI.H:23
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
triangles reserve(surf.size())
label n
unsigned int get(const label i) const
Get value at index i or 0 for out-of-range.
Definition: PackedListI.H:683
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:63