Test-DynamicList.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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "DynamicList.H"
29 #include "IOstreams.H"
30 #include "ListOps.H"
31 
32 using namespace Foam;
33 
34 template<class T>
35 void printInfo
36 (
37  const word& tag,
38  const UList<T>& lst,
39  const bool showSize = false
40 )
41 {
42  Info<< "<" << tag;
43  if (showSize)
44  {
45  Info<< " size=\"" << lst.size() << "\"";
46  }
47  Info<< ">" << lst << "</" << tag << ">" << endl;
48 }
49 
50 
51 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
52 void printInfo
53 (
54  const word& tag,
56  const bool showSize = false
57 )
58 {
59  Info<< "<" << tag;
60  if (showSize)
61  {
62  Info<< " size=\"" << lst.size()
63  << "\" capacity=\"" << lst.capacity() << "\"";
64  }
65  Info<< ">" << lst << "</" << tag << ">" << endl;
66 }
67 
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 // Main program:
71 
72 int main(int argc, char *argv[])
73 {
75 
76  ldl[0](0) = 0;
77  ldl[0](2) = 2;
78  ldl[0](3) = 3;
79  ldl[0](1) = 1;
80 
81  ldl[0].setCapacity(5); // increase allocated size
82  ldl[1].setCapacity(10); // increase allocated size
83  ldl[0].reserve(15); // should increase allocated size
84  ldl[1].reserve(5); // should not decrease allocated size
85  ldl[1](3) = 2; // allocates space and sets value
86 
87  // this works without a segfault, but doesn't change the list size
88  ldl[0][4] = 4;
89 
90  ldl[1] = 3;
91 
92  Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
93  forAll(ldl, i)
94  {
95  Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
96  }
97  Info<< endl;
98 
99  List<List<label> > ll(2);
100  ll[0].transfer(ldl[0]);
101  ll[1].transfer(ldl[1].shrink());
102 
103  Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
104  forAll(ldl, i)
105  {
106  Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
107  }
108  Info<< endl;
109 
110  Info<< "<ll>" << ll << "</ll>" << nl << endl;
111 
112 
113  // test the transfer between DynamicLists
116 
117  for (label i = 0; i < 5; i++)
118  {
119  dlA.append(i);
120  }
121  dlA.setCapacity(10);
122 
123  Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
124  << " " << dlA.size() << "/" << dlA.capacity() << endl;
125 
126  dlB.transfer(dlA);
127 
128  // provokes memory error if previous transfer did not maintain
129  // the correct allocated space
130  dlB[6] = 6;
131 
132  Info<< "Transferred to dlB" << endl;
133  Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
134  << " " << dlA.size() << "/" << dlA.capacity() << endl;
135  Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
136  << " " << dlB.size() << "/" << dlB.capacity() << endl;
137 
138  // try with a normal list:
139  List<label> lstA;
140  lstA.transfer(dlB);
141  Info<< "Transferred to normal list" << endl;
142  printInfo("lstA", lstA, true);
143  printInfo("dlB", dlB, true);
144 
145  // Copy back and append a few time
146  for (label i=0; i < 3; i++)
147  {
148  dlB.append(lstA);
149  }
150 
151  Info<< "appended list a few times" << endl;
152  printInfo("dlB", dlB, true);
153 
154  // assign the list (should maintain allocated space)
155  dlB = lstA;
156  Info<< "assigned list" << endl;
157  printInfo("dlB", dlB, true);
158 
159  // Copy back and append a few time
160  for (label i=0; i < 3; i++)
161  {
162  dlB.append(lstA);
163  }
164 
165 
166  // check allocation granularity
168 
169  printInfo("dlC", dlC, true);
170 
171  dlC.reserve(dlB.size());
172  dlC = dlB;
173 
174  printInfo("dlC", dlC, true);
175 
176  List<label> lstB(dlC.xfer());
177 
178  Info<< "Transferred to normal list via the xfer() method" << endl;
179  printInfo("lstB", lstB, true);
180  printInfo("dlC", dlC, true);
181 
182  DynamicList<label> dlD(lstB.xfer());
183 
184  Info<< "Transfer construct from normal list" << endl;
185  printInfo("lstB", lstB, true);
186  printInfo("dlD", dlD, true);
187 
188  DynamicList<label,10> dlE1(10);
189  DynamicList<label> dlE2(dlE1); // construct dissimilar
190 
191  printInfo("dlE1", dlE1, true);
192  printInfo("dlE2", dlE2, true);
193 
194  for (label elemI=0; elemI < 5; ++elemI)
195  {
196  dlE1.append(4 - elemI);
197  dlE2.append(elemI);
198  }
199 
200  printInfo("dlE2", dlE2, true);
201 
202  DynamicList<label> dlE3(dlE2); // construct identical
203  printInfo("dlE3", dlE3, true);
204 
205  dlE3 = dlE1; // assign dissimilar
206  printInfo("dlE3", dlE3, true);
207 
208  dlE3 = dlE2; // assign identical
209  printInfo("dlE3", dlE3, true);
210 
211  DynamicList<label> dlE4(reorder(identity(dlE3.size()), dlE3));
212  printInfo("dlE4", dlE4, true);
213 
214  printInfo("dlE3", dlE3, true);
215 
216 
217  {
218  DynamicList<label> addr(10);
219  addr.append(3);
220  addr.append(1);
221  addr.append(2);
222 
223  forAll(dlE2, i)
224  {
225  dlE2[i] *= 10;
226  }
227 
229  (
230  dlE2, addr
231  );
232  Info<< "use UIndirectList " << uil << " remapped from " << dlE2 << endl;
233  dlE4 = uil;
234  printInfo("dlE4", dlE4, true);
235  }
236 
237 
238  Info<< "\nEnd\n";
239 
240  return 0;
241 }
242 
243 
244 // ************************************************************************* //
Foam::DynamicList::xfer
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: DynamicListI.H:301
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::reorder
ListType reorder(const labelUList &oldToNew, const ListType &)
Reorder the elements (indices, not values) of a list.
Definition: ListOpsTemplates.C:74
Foam::DynamicList::capacity
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:100
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::List::xfer
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:90
Foam::List::transfer
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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
Foam::DynamicList::setCapacity
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:109
Foam::identity
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
main
int main(int argc, char *argv[])
Definition: Test-DynamicList.C:72
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
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::UList< T >
Foam::DynamicList::transfer
void transfer(List< T > &)
Transfer contents of the argument List into this.
Foam::DynamicList::reserve
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:130
Foam::UIndirectList
A List with indirect addressing.
Definition: fvMatrix.H:106
DynamicList.H
ListOps.H
Various functions to operate on Lists.
Foam::UList::size
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
printInfo
void printInfo(const word &tag, const UList< T > &lst, const bool showSize=false)
Definition: Test-DynamicList.C:36