1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2010 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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
26 \*---------------------------------------------------------------------------*/
28 #include "DynamicList.H"
29 #include "IOstreams.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 int main(int argc, char *argv[])
38 List<DynamicList<label, 1, 0> > ldl(2);
45 ldl[0].setCapacity(5); // increase allocated size
46 ldl[1].setCapacity(10); // increase allocated size
47 ldl[0].reserve(15); // should increase allocated size
48 ldl[1].reserve(5); // should not decrease allocated size
49 ldl[1](3) = 2; // allocates space and sets value
51 // this works without a segfault, but doesn't change the list size
56 Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
59 Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
63 List<List<label> > ll(2);
64 ll[0].transfer(ldl[0]);
65 ll[1].transfer(ldl[1].shrink());
67 Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
70 Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
74 Info<< "<ll>" << ll << "</ll>" << nl << endl;
77 // test the transfer between DynamicLists
78 DynamicList<label, 1, 0> dlA;
79 DynamicList<label, 1, 0> dlB;
81 for (label i = 0; i < 5; i++)
87 Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
88 << " " << dlA.size() << "/" << dlA.capacity() << endl;
92 // provokes memory error if previous transfer did not maintain
93 // the correct allocated space
96 Info<< "Transferred to dlB" << endl;
97 Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
98 << " " << dlA.size() << "/" << dlA.capacity() << endl;
99 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
100 << " " << dlB.size() << "/" << dlB.capacity() << endl;
102 // try with a normal list:
105 Info<< "Transferred to normal list" << endl;
106 Info<< "<lstA>" << lstA << "</lstA>" << nl << "sizes: "
107 << " " << lstA.size() << endl;
108 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
109 << " " << dlB.size() << "/" << dlB.capacity() << endl;
111 // Copy back and append a few time
112 for (label i=0; i < 3; i++)
117 Info<< "appended list a few times" << endl;
118 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
119 << " " << dlB.size() << "/" << dlB.capacity() << endl;
121 // assign the list (should maintain allocated space)
123 Info<< "assigned list" << endl;
124 Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
125 << " " << dlB.size() << "/" << dlB.capacity() << endl;
128 // Copy back and append a few time
129 for (label i=0; i < 3; i++)
135 // check allocation granularity
136 DynamicList<label, 6, 0> dlC;
138 Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
139 << " " << dlC.size() << "/" << dlC.capacity() << endl;
141 dlC.reserve(dlB.size());
144 Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
145 << " " << dlC.size() << "/" << dlC.capacity() << endl;
147 List<label> lstB(dlC.xfer());
149 Info<< "Transferred to normal list via the xfer() method" << endl;
150 Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
151 << " " << lstB.size() << endl;
152 Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
153 << " " << dlC.size() << "/" << dlC.capacity() << endl;
155 DynamicList<label> dlD(lstB.xfer());
157 Info<< "Transfer construct from normal list" << endl;
158 Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
159 << " " << lstB.size() << endl;
160 Info<< "<dlD>" << dlD << "</dlD>" << nl << "sizes: "
161 << " " << dlD.size() << "/" << dlD.capacity() << endl;
163 DynamicList<label,10> dlE1(10);
164 DynamicList<label> dlE2(dlE1); // construct dissimilar
166 Info<< "<dlE1>" << dlE1 << "</dlE1>" << nl << "sizes: "
167 << " " << dlE1.size() << "/" << dlE1.capacity() << endl;
168 Info<< "<dlE2>" << dlE2 << "</dlE2>" << nl << "sizes: "
169 << " " << dlE2.size() << "/" << dlE2.capacity() << endl;
171 for (label elemI=0; elemI < 5; ++elemI)
173 dlE1.append(4 - elemI);
177 Info<< "<dlE2>" << dlE2 << "</dlE2>" << endl;
179 DynamicList<label> dlE3(dlE2); // construct identical
180 Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
182 dlE3 = dlE1; // assign dissimilar
183 Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
185 dlE3 = dlE2; // assign identical
186 Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
195 // ************************************************************************* //