1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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"
39 const bool showSize = false
45 Info<< " size=\"" << lst.size() << "\"";
47 Info<< ">" << lst << "</" << tag << ">" << endl;
51 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
55 const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst,
56 const bool showSize = false
62 Info<< " size=\"" << lst.size()
63 << "\" capacity=\"" << lst.capacity() << "\"";
65 Info<< ">" << lst << "</" << tag << ">" << endl;
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 int main(int argc, char *argv[])
74 List<DynamicList<label, 1, 0> > ldl(2);
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
87 // this works without a segfault, but doesn't change the list size
92 Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
95 Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
99 List<List<label> > ll(2);
100 ll[0].transfer(ldl[0]);
101 ll[1].transfer(ldl[1].shrink());
103 Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
106 Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
110 Info<< "<ll>" << ll << "</ll>" << nl << endl;
113 // test the transfer between DynamicLists
114 DynamicList<label, 1, 0> dlA;
115 DynamicList<label, 1, 0> dlB;
117 for (label i = 0; i < 5; i++)
123 Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
124 << " " << dlA.size() << "/" << dlA.capacity() << endl;
128 // provokes memory error if previous transfer did not maintain
129 // the correct allocated space
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;
138 // try with a normal list:
141 Info<< "Transferred to normal list" << endl;
142 printInfo("lstA", lstA, true);
143 printInfo("dlB", dlB, true);
145 // Copy back and append a few time
146 for (label i=0; i < 3; i++)
151 Info<< "appended list a few times" << endl;
152 printInfo("dlB", dlB, true);
154 // assign the list (should maintain allocated space)
156 Info<< "assigned list" << endl;
157 printInfo("dlB", dlB, true);
159 // Copy back and append a few time
160 for (label i=0; i < 3; i++)
166 // check allocation granularity
167 DynamicList<label, 6, 0> dlC;
169 printInfo("dlC", dlC, true);
171 dlC.reserve(dlB.size());
174 printInfo("dlC", dlC, true);
176 List<label> lstB(dlC.xfer());
178 Info<< "Transferred to normal list via the xfer() method" << endl;
179 printInfo("lstB", lstB, true);
180 printInfo("dlC", dlC, true);
182 DynamicList<label> dlD(lstB.xfer());
184 Info<< "Transfer construct from normal list" << endl;
185 printInfo("lstB", lstB, true);
186 printInfo("dlD", dlD, true);
188 DynamicList<label,10> dlE1(10);
189 DynamicList<label> dlE2(dlE1); // construct dissimilar
191 printInfo("dlE1", dlE1, true);
192 printInfo("dlE2", dlE2, true);
194 for (label elemI=0; elemI < 5; ++elemI)
196 dlE1.append(4 - elemI);
200 printInfo("dlE2", dlE2, true);
202 DynamicList<label> dlE3(dlE2); // construct identical
203 printInfo("dlE3", dlE3, true);
205 dlE3 = dlE1; // assign dissimilar
206 printInfo("dlE3", dlE3, true);
208 dlE3 = dlE2; // assign identical
209 printInfo("dlE3", dlE3, true);
211 DynamicList<label> dlE4(reorder(identity(dlE3.size()), dlE3));
212 printInfo("dlE4", dlE4, true);
214 printInfo("dlE3", dlE3, true);
218 DynamicList<label> addr(10);
228 UIndirectList<label> uil
232 Info<< "use UIndirectList " << uil << " remapped from " << dlE2 << endl;
234 printInfo("dlE4", dlE4, true);
244 // ************************************************************************* //