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-------------------------------------------------------------------------------
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 <algorithm>
29#include "FixedList.H"
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
33template<unsigned N>
34Foam::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
43template<unsigned N>
45:
46 bitSet()
47{
48
49 setMany(locations.begin(), locations.end());
50}
51
52
53template<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
74template<class InputIter>
75Foam::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
100template<class InputIter>
101Foam::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
117template<unsigned N>
118Foam::label Foam::bitSet::set(const FixedList<label, N>& locations)
119{
120 return setMany(locations.begin(), locations.end());
121}
122
123
124template<unsigned N>
125Foam::label Foam::bitSet::unset(const FixedList<label, N>& locations)
126{
127 return unset(locations.begin(), locations.end());
128}
129
130
131// ************************************************************************* //
label n
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
label size() const noexcept
The number of elements in the list.
unsigned int get(const label i) const
Get value at index i or 0 for out-of-range.
Definition: PackedListI.H:630
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:590
bitSet() noexcept
Default construct an empty, zero-sized bitSet.
Definition: bitSetI.H:77
label setMany(InputIter first, InputIter last)
static const triad unset
Definition: triad.H:97
bool set() const
Are all the vector set.
Definition: triadI.H:76
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47