1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 \*---------------------------------------------------------------------------*/
29 #include "DynamicList.H"
30 #include "IOstreams.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 int main(int argc, char *argv[])
39 List<DynamicList<label, 1, 0> > ldl(2);
46 ldl[0].setCapacity(5); // increase allocated size
47 ldl[1].setCapacity(10); // increase allocated size
48 ldl[0].reserve(15); // should increase allocated size
49 ldl[1].reserve(5); // should not decrease allocated size
50 ldl[1](3) = 2; // allocates space and sets value
52 // this works without a segfault, but doesn't change the list size
57 Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
60 Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
64 List<List<label> > ll(2);
65 ll[0].transfer(ldl[0]);
66 ll[1].transfer(ldl[1].shrink());
68 Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
71 Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
75 Info<< "<ll>" << ll << "</ll>" << nl << endl;
78 // test the transfer between DynamicLists
79 DynamicList<label, 1, 0> dlA;
80 DynamicList<label, 1, 0> dlB;
82 for (label i = 0; i < 5; i++)
88 Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
89 << " " << dlA.size() << "/" << dlA.capacity() << endl;
93 // provokes memory error if previous transfer did not maintain
94 // the correct allocated space
97 Info<< "Transferred to dlB" << endl;
98 Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
99 << " " << dlA.size() << "/" << dlA.capacity() << endl;
100 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
101 << " " << dlB.size() << "/" << dlB.capacity() << endl;
103 // try with a normal list:
106 Info<< "Transferred to normal list" << endl;
107 Info<< "<lstA>" << lstA << "</lstA>" << nl << "sizes: "
108 << " " << lstA.size() << endl;
109 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
110 << " " << dlB.size() << "/" << dlB.capacity() << endl;
112 // Copy back and append a few time
113 for (label i=0; i < 3; i++)
118 Info<< "appended list a few times" << endl;
119 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
120 << " " << dlB.size() << "/" << dlB.capacity() << endl;
122 // assign the list (should maintain allocated space)
124 Info<< "assigned list" << endl;
125 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
126 << " " << dlB.size() << "/" << dlB.capacity() << endl;
129 // Copy back and append a few time
130 for (label i=0; i < 3; i++)
136 // check allocation granularity
137 DynamicList<label, 6, 0> dlC;
139 Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
140 << " " << dlC.size() << "/" << dlC.capacity() << endl;
142 dlC.reserve(dlB.size());
145 Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
146 << " " << dlC.size() << "/" << dlC.capacity() << endl;
148 List<label> lstB(dlC.xfer());
150 Info<< "Transferred to normal list via the xfer() method" << endl;
151 Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
152 << " " << lstB.size() << endl;
153 Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
154 << " " << dlC.size() << "/" << dlC.capacity() << endl;
156 DynamicList<label> dlD(lstB.xfer());
158 Info<< "Transfer construct from normal list" << endl;
159 Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
160 << " " << lstB.size() << endl;
161 Info<< "<dlD>" << dlD << "</dlD>" << nl << "sizes: "
162 << " " << dlD.size() << "/" << dlD.capacity() << endl;
164 DynamicList<label,10> dlE1(10);
165 DynamicList<label> dlE2(dlE1); // construct dissimilar
167 Info<< "<dlE1>" << dlE1 << "</dlE1>" << nl << "sizes: "
168 << " " << dlE1.size() << "/" << dlE1.capacity() << endl;
169 Info<< "<dlE2>" << dlE2 << "</dlE2>" << nl << "sizes: "
170 << " " << dlE2.size() << "/" << dlE2.capacity() << endl;
172 for (label elemI=0; elemI < 5; ++elemI)
174 dlE1.append(4 - elemI);
178 Info<< "<dlE2>" << dlE2 << "</dlE2>" << endl;
180 DynamicList<label> dlE3(dlE2); // construct identical
181 Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
183 dlE3 = dlE1; // assign dissimilar
184 Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
186 dlE3 = dlE2; // assign identical
187 Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
196 // ************************************************************************* //