Test-PackedList1.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 
34 using namespace Foam;
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // Main program:
38 
39 int main(int argc, char *argv[])
40 {
41  Info<< "PackedList max_bits() = " << PackedList<>::max_bits() << nl;
42 
43  Info<< "\ntest allocation with value\n";
44  PackedList<3> list1(5,1);
45  list1.printInfo(Info, true);
46 
47  Info<< "\ntest assign uniform value\n";
48  list1 = 3;
49  list1.printInfo(Info, true);
50 
51  Info<< "\ntest assign uniform value (with overflow)\n";
52  list1 = -1;
53  list1.printInfo(Info, true);
54 
55  Info<< "\ntest zero\n";
56  list1 = 0;
57  list1.printInfo(Info, true);
58 
59  Info<< "\ntest set() with default argument (max_value)\n";
60  list1.set(1);
61  list1.set(3);
62  list1.printInfo(Info, true);
63 
64  Info<< "\ntest unset() with in-range and out-of-range\n";
65  list1.unset(3);
66  list1.unset(100000);
67  list1.printInfo(Info, true);
68 
69  Info<< "\ntest assign between references\n";
70  list1[2] = 3;
71  list1[4] = list1[2];
72  list1.printInfo(Info, true);
73 
74  Info<< "\ntest assign between references, with chaining\n";
75  list1[0] = list1[4] = 1;
76  list1.printInfo(Info, true);
77 
78  Info<< "\ntest assign between references, with chaining and auto-vivify\n";
79  list1[1] = list1[8] = list1[10] = list1[14] = 2;
80  list1.printInfo(Info, true);
81 
82 
83  Info<< "\ntest operator== between references\n";
84  if (list1[1] == list1[8])
85  {
86  Info<< "[1] == [8] (expected)\n";
87  }
88  else
89  {
90  Info<< "[1] != [8] (unexpected)\n";
91  }
92 
93  if (list1[0] != list1[1])
94  {
95  Info<< "[0] != [1] (expected)\n";
96  }
97  else
98  {
99  Info<< "[0] == [1] (unexpected)\n";
100  }
101 
102  Info<< "\ntest operator== with iterator\n";
103  {
104  PackedList<3>::iterator iter = list1[1];
105 
106  if (iter != list1[8])
107  {
108  Info<< "iter != [8] (expected)\n";
109  }
110  else
111  {
112  Info<< "iter == [8] (unexpected)\n";
113  }
114 
115  if (*iter != list1[8])
116  {
117  Info<< "*iter != [8] (unexpected)\n";
118  }
119  else
120  {
121  Info<< "*iter == [8] (expected)\n";
122  }
123  }
124 
125 
126  {
127  const PackedList<3>& constLst = list1;
128  Info<< "\ntest operator[] const with out-of-range index\n";
129  constLst.printInfo(Info, true);
130  if (constLst[20])
131  {
132  Info<< "[20] is true (unexpected)\n";
133  }
134  else
135  {
136  Info<< "[20] is false (expected) list size should be unchanged "
137  << "(const)\n";
138  }
139  constLst.printInfo(Info, true);
140 
141  Info<< "\ntest operator[] non-const with out-of-range index\n";
142  if (list1[20])
143  {
144  Info<< "[20] is true (unexpected)\n";
145  }
146  else
147  {
148  Info<< "[20] is false (expected) but list was resized?? "
149  << "(non-const)\n";
150  }
151  list1.printInfo(Info, true);
152  }
153 
154 
155  Info<< "\ntest operator[] with out-of-range index\n";
156  if (!list1[20])
157  {
158  Info<< "[20] is false, as expected\n";
159  }
160  list1.printInfo(Info, true);
161 
162  Info<< "\ntest resize with value (without reallocation)\n";
163  list1.resize(8, list1.max_value());
164  list1.printInfo(Info, true);
165 
166  Info<< "\ntest flip() function\n";
167  list1.flip();
168  list1.printInfo(Info, true);
169 
170  Info<< "\nre-flip()\n";
171  list1.flip();
172  list1.printInfo(Info, true);
173 
174  Info<< "\ntest set() function\n";
175  list1.set(1, 5);
176  list1.printInfo(Info, true);
177 
178  Info<< "\ntest assign bool\n";
179  list1 = false;
180  list1.printInfo(Info, true);
181 
182  Info<< "\ntest assign bool\n";
183  list1 = true;
184  list1.printInfo(Info, true);
185 
186  Info<< "\ntest resize without value (with reallocation)\n";
187  list1.resize(12);
188  list1.printInfo(Info, true);
189 
190  Info<< "\ntest resize with value (with reallocation)\n";
191  list1.resize(25, list1.max_value());
192  list1.printInfo(Info, true);
193 
194  Info<< "\ntest resize smaller (should not touch allocation)\n";
195  list1.resize(8);
196  list1.printInfo(Info, true);
197 
198  Info<< "\ntest append() operation\n";
199  list1.append(2);
200  list1.append(3);
201  list1.append(4);
202  list1.printInfo(Info, true);
203 
204  Info<< "\ntest reserve() operation\n";
205  list1.reserve(32);
206  list1.printInfo(Info, true);
207 
208  Info<< "\ntest shrink() operation\n";
209  list1.shrink();
210  list1.printInfo(Info, true);
211 
212  Info<< "\ntest setCapacity() operation\n";
213  list1.setCapacity(15);
214  list1.printInfo(Info, true);
215 
216  Info<< "\ntest setCapacity() operation\n";
217  list1.setCapacity(100);
218  list1.printInfo(Info, true);
219 
220  Info<< "\ntest operator[] assignment\n";
221  list1[16] = 5;
222  list1.printInfo(Info, true);
223 
224  Info<< "\ntest operator[] assignment with auto-vivify\n";
225  list1[36] = list1.max_value();
226  list1.printInfo(Info, true);
227 
228  Info<< "\ntest setCapacity smaller\n";
229  list1.setCapacity(24);
230  list1.printInfo(Info, true);
231 
232  Info<< "\ntest resize much smaller\n";
233  list1.resize(150);
234  list1.printInfo(Info, true);
235 
236  Info<< "\ntest trim\n";
237  list1.trim();
238  list1.printInfo(Info, true);
239 
240  // add in some misc values
241  list1[31] = 1;
242  list1[32] = 2;
243  list1[33] = 3;
244 
245  Info<< "\ntest iterator\n";
246  PackedList<3>::iterator iter = list1.begin();
247  Info<< "begin():";
248  iter.printInfo(Info) << "\n";
249 
250  Info<< "iterator:" << iter() << "\n";
251  iter() = 5;
252  iter.printInfo(Info);
253  list1.printInfo(Info, true);
254 
255  iter = list1[31];
256  Info<< "iterator:" << iter() << "\n";
257  iter.printInfo(Info);
258 
259 
260  Info<< "\ntest get() method\n";
261  Info<< "get(10):" << list1.get(10) << " and list[10]:" << list1[10] << "\n";
262  list1.printInfo(Info, true);
263 
264  Info<< "\ntest iterator indexing\n";
265  Info<< "cend() ";
266  list1.cend().printInfo(Info) << "\n";
267 
268  {
269  Info<< "\ntest assignment of iterator\n";
270  list1.printInfo(Info, true);
271  Info<< "cend()\n";
272  list1.end().printInfo(Info);
273  PackedList<3>::iterator cit = list1[100];
274  Info<< "out-of-range: ";
275  cit.printInfo(Info);
276  cit = list1[15];
277  Info<< "in-range: ";
278  cit.printInfo(Info);
279  Info<< "out-of-range: ";
280  cit = list1[1000];
281  cit.printInfo(Info);
282  }
283 
284 
285  for
286  (
287  PackedList<3>::iterator cit = list1[30];
288  cit != list1.end();
289  ++cit
290  )
291  {
292  cit.printInfo(Info);
293  }
294 
295  Info<< "\ntest operator[] auto-vivify\n";
296  Info<< "size:" << list1.size() << "\n";
297 
298  const unsigned int val = list1[45];
299 
300  Info<< "list[45]:" << val << "\n";
301  Info<< "size after read:" << list1.size() << "\n";
302 
303  list1[45] = list1.max_value();
304  Info<< "size after write:" << list1.size() << "\n";
305  Info<< "list[45]:" << list1[45] << "\n";
306  list1[49] = list1[100];
307  list1.printInfo(Info, true);
308 
309 
310  Info<< "\ntest copy constructor + append\n";
311  PackedList<3> list2(list1);
312  list2.append(4);
313  Info<< "source list:\n";
314  list1.printInfo(Info, true);
315  Info<< "destination list:\n";
316  list2.printInfo(Info, true);
317 
318  Info<< "\ntest pattern that fills all bits\n";
319  PackedList<4> list3(8, 8);
320 
321  label pos = list3.size() - 1;
322 
323  list3[pos--] = list3.max_value();
324  list3[pos--] = 0;
325  list3[pos--] = list3.max_value();
326  list3.printInfo(Info, true);
327 
328  Info<< "removed final value: " << list3.remove() << endl;
329  list3.printInfo(Info, true);
330 
331  Info<<"list: " << list3 << endl;
332 
333 
334  List<bool> list4(16, false);
335  {
336  // fill with some values
337  forAll(list4, i)
338  {
339  list4[i] = i % 3;
340  }
341 
342  const UList<bool>& constLst = list4;
343  Info<< "\ntest operator[] const with out-of-range index\n";
344  Info<< constLst << endl;
345  if (constLst[100])
346  {
347  Info<< "[100] is true (unexpected)\n";
348  }
349  else
350  {
351  Info<< "[100] is false (expected) "
352  << "list size should be unchanged (const)\n";
353  }
354  Info<< constLst << endl;
355  }
356 
357 
358  PackedBoolList listb(list4);
359 
360  Info<< "copied from bool list " << endl;
361  listb.printInfo(Info, true);
362 
363  {
364  labelList indices = listb.used();
365 
366  Info<< "indices: " << indices << endl;
367  }
368 
369 
370  Info<< "\n\nDone.\n";
371 
372  return 0;
373 }
374 
375 
376 // ************************************************************************* //
Foam::PackedList::unset
bool unset(const label)
Unset the entry at index I. Return true if value changed.
Definition: PackedListI.H:1016
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...
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::PackedList::cend
const_iterator cend() const
const_iterator set to beyond the end of the PackedList
Definition: PackedListI.H:705
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::PackedList::setCapacity
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: PackedListI.H:841
Foam::PackedList::max_bits
static unsigned int max_bits()
The max. number of bits that can be templated.
Definition: PackedListI.H:32
Foam::PackedList::reserve
void reserve(const label)
Reserve allocation space for at least this size.
Definition: PackedListI.H:863
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::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
PackedBoolList.H
Foam::PackedList::iterator
The iterator class used for PackedList.
Definition: PackedList.H:486
Foam::PackedList::flip
void flip()
Invert the bits in the addressable region.
Definition: PackedList.C:104
Foam::PackedList::remove
unsigned int remove()
Remove and return the last element.
Definition: PackedListI.H:1044
Foam::PackedList::begin
iterator begin()
Iterator set to the beginning of the PackedList.
Definition: PackedListI.H:665
Foam::PackedList::set
bool set(const label, const unsigned int val=~0u)
Set value at index I. Return true if value changed.
Definition: PackedListI.H:995
Foam::PackedList::trim
bool trim()
Trim any trailing zero elements.
Definition: PackedList.C:74
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::PackedList::get
unsigned int get(const label) const
Get value at index I.
Definition: PackedListI.H:964
Foam::PackedList::printInfo
Ostream & printInfo(Ostream &, const bool fullOutput=false) const
Print information and bit patterns (with printBits)
Definition: PackedList.C:235
Foam::PackedList::iteratorBase::printInfo
Ostream & printInfo(Ostream &) const
Print information and values.
Definition: PackedList.C:149
Foam::PackedList::max_value
static unsigned int max_value()
The max. value for an entry, which simultaneously the bit-mask.
Definition: PackedListI.H:39
Foam::PackedList::end
iterator end()
Iterator set to beyond the end of the PackedList.
Definition: PackedListI.H:689
Foam::PackedList::append
PackedList< nBits > & append(const unsigned int val)
Append a value at the end of the list.
Definition: PackedListI.H:1032
main
int main(int argc, char *argv[])
Definition: Test-PackedList1.C:36
Foam::List< bool >
Foam::PackedList
A dynamically allocatable list of packed unsigned integers.
Definition: PackedList.H:117
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
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::shrink
void shrink()
Shrink the allocated space to what is actually used.
Definition: PackedListI.H:908
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::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:190