/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- 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 "FlatOutput.H" #include "ListOps.H" #include "labelRange.H" #include "labelIndList.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() << "\""; if (lst.cdata()) { Info<< " ptr=\"" << name(lst.cdata()) << "\""; } else { Info<< " ptr=\"nullptr\""; } } Info<< ">" << nl << flatOutput(lst) << nl << "" << 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() << "\""; if (lst.cdata()) { Info<< " ptr=\"" << name(lst.cdata()) << "\""; } else { Info<< " ptr=\"nullptr\""; } } Info<< ">" << nl << flatOutput(lst) << nl << "" << 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 #ifndef FULLDEBUG // Accessing an out-of-bounds address, but writing into allocated memory. // No segfault, doesn't change the list size. Nonetheless not a good idea. ldl[0][4] = 4; #endif ldl[1] = 3; Info<< "" << flatOutput(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<< "" << flatOutput(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