From 63a9d5e66d463210f145fac28a5e5a0db3f2f005 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Thu, 7 Aug 2008 11:42:06 +0200 Subject: [PATCH] new container 'Keyed' - useful for tagging things with a label --- src/OpenFOAM/containers/Keyed/Keyed.H | 129 ++++++++++++++++++++++ src/OpenFOAM/containers/Keyed/KeyedI.H | 146 +++++++++++++++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 src/OpenFOAM/containers/Keyed/Keyed.H create mode 100644 src/OpenFOAM/containers/Keyed/KeyedI.H diff --git a/src/OpenFOAM/containers/Keyed/Keyed.H b/src/OpenFOAM/containers/Keyed/Keyed.H new file mode 100644 index 00000000000..2d0caa7b183 --- /dev/null +++ b/src/OpenFOAM/containers/Keyed/Keyed.H @@ -0,0 +1,129 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 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 + +Class + Foam::Keyed + +Description + A container with an integer key attached to any item. + + The key can useful for sorting. + +SourceFiles + KeyedI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef Keyed_H +#define Keyed_H + +#include "List.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of friend functions and operators + +template<class T> class Keyed; + +template<class T> Istream& operator>>(Istream&, Keyed<T>&); +template<class T> Ostream& operator<<(Ostream&, const Keyed<T>&); + +/*---------------------------------------------------------------------------*\ + Class Keyed Declaration +\*---------------------------------------------------------------------------*/ + +template<class T> +class Keyed +: + public T +{ + // Private data + + label key_; + +public: + + // Static Members + + //- Add labels to a list of values + inline static List<Keyed<T> > createList + ( + const List<T>&, + const label key=0 + ); + + //- Add labels to a list of values + inline static List<Keyed<T> > createList + ( + const List<T>&, + const List<label>& keys + ); + + + // Constructors + + //- Construct null + inline Keyed(); + + //- Construct as a copy of item, with a key + inline Keyed(const T& item, const label key=0); + + //- Construct from Istream + inline Keyed(Istream&); + + + // Member Functions + + // Access + + //- Return const access to the integer key + inline label key() const; + + //- Return non-const access to the integer key + inline label& key(); + + + // IOstream Operators + + friend Istream& operator>> <T>(Istream&, Keyed<T>&); + friend Ostream& operator<< <T>(Ostream&, const Keyed<T>&); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "KeyedI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Keyed/KeyedI.H b/src/OpenFOAM/containers/Keyed/KeyedI.H new file mode 100644 index 00000000000..ecae23da3d6 --- /dev/null +++ b/src/OpenFOAM/containers/Keyed/KeyedI.H @@ -0,0 +1,146 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 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 "IOstreams.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +//- Construct null +template<class T> +inline Foam::Keyed<T>::Keyed() +: + key_(-1) +{} + + +//- Construct from components +template<class T> +inline Foam::Keyed<T>::Keyed(const T& item, const label key) +: + T(item), + key_(key) +{} + + +template<class T> +inline Foam::Keyed<T>::Keyed(Istream& is) +{ + is >> *this; +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class T> +inline Foam::label Foam::Keyed<T>::key() const +{ + return key_; +} + +template<class T> +inline Foam::label& Foam::Keyed<T>::key() +{ + return key_; +} + + +template<class T> +inline Foam::List<Foam::Keyed<T> > +Foam::Keyed<T>::createList(const List<T>& lst, const label key) +{ + List<Keyed<T> > newList(lst.size()); + + forAll (lst, elemI) + { + newList[elemI] = Keyed(lst[elemI], key); + } + return newList; +} + + +template<class T> +inline Foam::List<Foam::Keyed<T> > +Foam::Keyed<T>::createList(const List<T>& lst, const List<label>& keys) +{ + if (lst.size() != keys.size()) + { + FatalErrorIn + ( + "Foam::Keyed<T>::createList(const List<T>&, const List<label>&)" + ) + << "size mismatch adding keys to a list:" << nl + << "List has size " << lst.size() + << " and keys has size " << keys.size() << nl + << abort(FatalError); + } + + List<Keyed<T> > newList(lst.size()); + + forAll (lst, elemI) + { + newList[elemI] = Keyed(lst[elemI], keys[elemI]); + } + return newList; +} + + +// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // + +template<class T> +inline Foam::Istream& Foam::operator>>(Istream& is, Keyed<T>& item) +{ + // Read beginning of Keyed item/key pair + is.readBegin("Keyed<T>"); + + is >> static_cast<T&>(item); + is >> item.key_; + + // Read end of Keyed item/key pair + is.readEnd("Keyed<T>"); + + // Check state of Ostream + is.check("Istream& operator>>(Istream&, Keyed&)"); + + return is; +} + + +template<class T> +inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item) +{ + os << token::BEGIN_LIST + << static_cast<const T&>(item) + << token::SPACE << item.key_ + << token::END_LIST; + + return os; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// ************************************************************************* // -- GitLab