diff --git a/applications/test/UIndirectListTest/UIndirectListTest.C b/applications/test/UIndirectListTest/UIndirectListTest.C index 575173c51e095e2488e7ad118f1699683c6e318b..9c619a4b991988b061141386aea1e70ca5947db4 100644 --- a/applications/test/UIndirectListTest/UIndirectListTest.C +++ b/applications/test/UIndirectListTest/UIndirectListTest.C @@ -28,6 +28,8 @@ Description #include "UIndirectList.H" #include "IOstreams.H" +#include "ListOps.H" +#include "OFstream.H" using namespace Foam; @@ -52,29 +54,32 @@ int main(int argc, char *argv[]) UIndirectList<double> idl(completeList, addresses); - forAll(idl, i) - { - Info<< idl[i] << token::SPACE; - } - - Info<< endl; + Info<< idl << "\n"; idl[1] = -666; - Info<< "idl[1] changed:" << idl() << endl; + Info<< "idl[1] changed:" << idl << endl; idl = -999; - Info<< "idl changed:" << idl() << endl; + Info<< "idl changed:" << idl << endl; UIndirectList<double> idl2(idl); - Info<< "idl2:" << idl2() << endl; + Info<< "idl2: " << idl2 << endl; - idl = idl2(); - Info<< "idl assigned from UList:" << idl() << endl; + { + List<double> ident(idl.size()); + + forAll(ident, i) + { + ident[i] = ident.size() - i; + } + idl = ident; + } + Info<< "idl assigned from UList:" << idl << endl; List<double> realList = UIndirectList<double>(completeList, addresses); diff --git a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H index c762bdba7d57da1ca12e5b0d8dff12e94a6f5bf7..ec6a1b9fd6f330f6d5feb3c5e7bd4d78a8b411af 100644 --- a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H +++ b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectList.H @@ -26,8 +26,8 @@ Class Foam::UIndirectList Description - A List with indirect addressing. Like IndirectList but does not store - addressing. + A List with indirect addressing. + Like IndirectList but does not store addressing. SourceFiles UIndirectListI.H @@ -44,8 +44,12 @@ SourceFiles namespace Foam { +// Forward declaration of friend functions and operators +template<class T> class UIndirectList; +template<class T> Ostream& operator<<(Ostream&, const UIndirectList<T>&); + /*---------------------------------------------------------------------------*\ - Class UIndirectList Declaration + Class UIndirectList Declaration \*---------------------------------------------------------------------------*/ template<class T> @@ -92,6 +96,17 @@ public: //- Assignment of all entries to the given value inline void operator=(const T&); + + // Ostream operator + + //- Write UIndirectList to Ostream + // Binary output is currently still a bit of a problem + friend Ostream& operator<< + #ifndef __CINT__ + <T> + #endif + (Ostream&, const UIndirectList<T>&); + }; @@ -103,6 +118,10 @@ public: #include "UIndirectListI.H" +#ifdef NoRepository +# include "UIndirectListIO.C" +#endif + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C new file mode 100644 index 0000000000000000000000000000000000000000..0c3be0e7697a8a7264d6192976cbe26c9665a46d --- /dev/null +++ b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C @@ -0,0 +1,126 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +#include "UIndirectList.H" +#include "Ostream.H" +#include "token.H" +#include "contiguous.H" + +// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // + +template<class T> +Foam::Ostream& Foam::operator<< +( + Foam::Ostream& os, + const Foam::UIndirectList<T>& L +) +{ + // Write list contents depending on data format + if (os.format() == IOstream::ASCII || !contiguous<T>()) + { + bool uniform = false; + + if (L.size() > 1 && contiguous<T>()) + { + uniform = true; + + forAll(L, i) + { + if (L[i] != L[0]) + { + uniform = false; + break; + } + } + } + + if (uniform) + { + // Write size and start delimiter + os << L.size() << token::BEGIN_BLOCK; + + // Write contents + os << L[0]; + + // Write end delimiter + os << token::END_BLOCK; + } + else if (L.size() < 11 && contiguous<T>()) + { + // Write size and start delimiter + os << L.size() << token::BEGIN_LIST; + + // Write contents + forAll(L, i) + { + if (i) os << token::SPACE; + os << L[i]; + } + + // Write end delimiter + os << token::END_LIST; + } + else + { + // Write size and start delimiter + os << nl << L.size() << nl << token::BEGIN_LIST; + + // Write contents + forAll(L, i) + { + os << nl << L[i]; + } + + // Write end delimiter + os << nl << token::END_LIST << nl; + } + } + else + { + // this is annoying, and wasteful, but there's currently no alternative + + os << nl << L.size() << nl; + + if (L.size()) + { + List<T> lst = L(); + + os.write + ( + reinterpret_cast<const char*>(lst.cdata()), + lst.byteSize() + ); + } + } + + // Check state of IOstream + os.check("Ostream& operator<<(Ostream&, const UIndirectList&)"); + + return os; +} + + +// ************************************************************************* //