/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . Description \*---------------------------------------------------------------------------*/ #include "DynamicList.H" #include "IOstreams.H" #include "ListOps.H" using namespace Foam; template void printInfo ( const word& tag, const UList& lst, const bool showSize = false ) { Info<< "<" << tag; if (showSize) { Info<< " size=\"" << lst.size() << "\""; } Info<< ">" << lst << "" << endl; } template void printInfo ( const word& tag, const DynamicList& lst, const bool showSize = false ) { Info<< "<" << tag; if (showSize) { Info<< " size=\"" << lst.size() << "\" capacity=\"" << lst.capacity() << "\""; } Info<< ">" << lst << "" << endl; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main(int argc, char *argv[]) { List > ldl(2); ldl[0](0) = 0; ldl[0](2) = 2; ldl[0](3) = 3; ldl[0](1) = 1; ldl[0].setCapacity(5); // increase allocated size ldl[1].setCapacity(10); // increase allocated size ldl[0].reserve(15); // should increase allocated size ldl[1].reserve(5); // should not decrease allocated size ldl[1](3) = 2; // allocates space and sets value // this works without a segfault, but doesn't change the list size ldl[0][4] = 4; ldl[1] = 3; Info<< "" << ldl << "" << nl << "sizes: "; forAll(ldl, i) { Info<< " " << ldl[i].size() << "/" << ldl[i].capacity(); } Info<< endl; List > ll(2); ll[0].transfer(ldl[0]); ll[1].transfer(ldl[1].shrink()); Info<< "" << ldl << "" << nl << "sizes: "; forAll(ldl, i) { Info<< " " << ldl[i].size() << "/" << ldl[i].capacity(); } Info<< endl; Info<< "" << ll << "" << nl << endl; // test the transfer between DynamicLists DynamicList dlA; DynamicList dlB; for (label i = 0; i < 5; i++) { dlA.append(i); } dlA.setCapacity(10); Info<< "" << dlA << "" << nl << "sizes: " << " " << dlA.size() << "/" << dlA.capacity() << endl; dlB.transfer(dlA); // provokes memory error if previous transfer did not maintain // the correct allocated space dlB[6] = 6; Info<< "Transferred to dlB" << endl; Info<< "" << dlA << "" << nl << "sizes: " << " " << dlA.size() << "/" << dlA.capacity() << endl; Info<< "" << dlB << "" << nl << "sizes: " << " " << dlB.size() << "/" << dlB.capacity() << endl; // try with a normal list: List