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