Test-PackedList4.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 | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25 
26 Description
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "uLabel.H"
31 #include "IOstreams.H"
32 #include "PackedBoolList.H"
33 #include "IStringStream.H"
34 
35 using namespace Foam;
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 // Main program:
39 
40 int main(int argc, char *argv[])
41 {
42  PackedBoolList list1(20);
43  // set every third one on
44  forAll(list1, i)
45  {
46  list1[i] = !(i % 3);
47  }
48 
49  Info<< "\nalternating bit pattern\n";
50  list1.printInfo(Info, true);
51 
52  PackedBoolList list2 = ~list1;
53 
54  Info<< "\ncomplementary bit pattern\n";
55  list2.printBits(Info);
56 
57  // set every other on
58  forAll(list2, i)
59  {
60  list2[i] = !(i % 2);
61  }
62 
63  Info<< "\nalternating bit pattern\n";
64  list2.printBits(Info);
65 
66  list2.resize(28, false);
67  list2.resize(34, true);
68  list2.resize(40, false);
69  for (label i=0; i < 4; ++i)
70  {
71  list2[i] = true;
72  }
73 
74  Info<< "\nresized with false, 6 true + 6 false, bottom 4 bits true\n";
75  list2.printInfo(Info, true);
76 
77  labelList list2Labels = list2.used();
78 
79  Info<< "\noperator|\n";
80 
81  list1.printBits(Info);
82  list2.printBits(Info);
83  Info<< "==\n";
84  (list1 | list2).printBits(Info);
85 
86  Info<< "\noperator& : does trim\n";
87  (list1 & list2).printBits(Info);
88 
89  Info<< "\noperator^\n";
90  (list1 ^ list2).printBits(Info);
91 
92 
93  Info<< "\noperator|=\n";
94  {
95  PackedBoolList list3 = list1;
96  (list3 |= list2).printBits(Info);
97  }
98 
99  Info<< "\noperator|= with labelUList\n";
100  {
101  PackedBoolList list3 = list1;
102  (list3 |= list2Labels).printBits(Info);
103  }
104 
105  Info<< "\noperator&=\n";
106  {
107  PackedBoolList list3 = list1;
108  (list3 &= list2).printBits(Info);
109  }
110 
111  Info<< "\noperator+=\n";
112  {
113  PackedBoolList list3 = list1;
114  (list3 += list2).printBits(Info);
115  }
116 
117  Info<< "\noperator+= with labelUList\n";
118  {
119  PackedBoolList list3 = list1;
120  (list3 += list2Labels).printBits(Info);
121  }
122 
123  Info<< "\noperator-=\n";
124  {
125  PackedBoolList list3 = list1;
126  (list3 -= list2).printBits(Info);
127  }
128 
129  Info<< "\noperator-= with labelUList\n";
130  {
131  PackedBoolList list3 = list1;
132  (list3 -= list2Labels).printBits(Info);
133  }
134 
135  PackedBoolList list4
136  (
138  (
139  "(1 n 1 n 1 n 1 1 off 0 0 f f 0 y yes y true y false on t)"
140  )()
141  );
142 
143  Info<< "\ntest Istream constructor\n";
144 
145  list4.printInfo(Info, true);
146  Info<< list4 << " indices: " << list4.used()() <<endl;
147 
148  Info<< "\nassign from labelList\n";
149  list4 = labelList
150  (
152  (
153  "(0 1 2 3 12 13 14 19 20 21)"
154  )()
155  );
156 
157  list4.printInfo(Info, true);
158  Info<< list4 << " indices: " << list4.used()() <<endl;
159 
160  Info<< "\nassign from indices\n";
161  list4.read
162  (
164  (
165  "{0 1 2 3 12 13 14 19 20 21}"
166  )()
167  );
168 
169 
170  list4.printInfo(Info, true);
171  Info<< list4 << " indices: " << list4.used()() <<endl;
172 
173  List<bool> boolLst(list4.size());
174  forAll(list4, i)
175  {
176  boolLst[i] = list4[i];
177  }
178 
179  Info<< "List<bool>: " << boolLst <<endl;
180 
181 
182  // check roundabout assignments
183  PackedList<2> pl2
184  (
186  (
187  "{(0 3)(1 3)(2 3)(3 3)(12 3)(13 3)(14 3)(19 3)(20 3)(21 3)}"
188  )()
189  );
190 
191  Info<< "roundabout assignment: " << pl2 << endl;
192 
193  list4.clear();
194  forAll(pl2, i)
195  {
196  list4[i] = pl2[i];
197  }
198 
199  list4.write(Info, true) << endl;
200 
201  list4.writeEntry("PackedBoolList", Info);
202 
203  return 0;
204 }
205 
206 
207 // ************************************************************************* //
Foam::PackedBoolList
A bit-packed bool list.
Definition: PackedBoolList.H:63
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::labelList
List< label > labelList
A List of labels.
Definition: labelList.H:56
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::PackedList::printBits
Ostream & printBits(Ostream &, const bool fullOutput=false) const
Print bit patterns, optionally output unused elements.
Definition: PackedList.C:166
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::PackedList::writeEntry
void writeEntry(Ostream &) const
Write as a dictionary entry.
IStringStream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Info
messageStream Info
PackedBoolList.H
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::PackedList::printInfo
Ostream & printInfo(Ostream &, const bool fullOutput=false) const
Print information and bit patterns (with printBits)
Definition: PackedList.C:235
main
int main(int argc, char *argv[])
Definition: Test-PackedList4.C:37
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::PackedList< 2 >
Foam::PackedList::read
Istream & read(Istream &)
Clear list and read from stream.
Definition: PackedList.C:254
Foam::PackedList::write
Ostream & write(Ostream &, const bool indexedOutput=false) const
Write, optionally with indexedOutput.
Definition: PackedList.C:399
Foam::PackedList::resize
void resize(const label, const unsigned int &val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:729
Foam::PackedList::size
label size() const
Number of entries.
Definition: PackedListI.H:714
uLabel.H
Foam::PackedBoolList::used
Xfer< labelList > used() const
Return indices of the used (true) elements as a list of labels.
Definition: PackedBoolList.C:264
Foam::PackedList::clear
void clear()
Clear the list, i.e. set addressable size to zero.
Definition: PackedListI.H:892