ENH: generalize speciesTable as hashedWordList

BUG: incorrect speciesTable indices after assignment or reading from Istream
This commit is contained in:
Mark Olesen
2010-10-05 18:19:17 +02:00
parent e251b7b677
commit 9fd0959b88
8 changed files with 341 additions and 169 deletions

View File

@ -85,6 +85,7 @@ $(primitiveLists)/vectorListIOList.C
$(primitiveLists)/sphericalTensorList.C
$(primitiveLists)/symmTensorList.C
$(primitiveLists)/tensorList.C
$(primitiveLists)/hashedWordList.C
Streams = db/IOstreams
$(Streams)/token/tokenIO.C

View File

@ -0,0 +1,159 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-2010 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "hashedWordList.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::hashedWordList::rehash()
{
indices_.clear();
forAll(*this, i)
{
indices_.insert(List<word>::operator[](i), i);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::hashedWordList::hashedWordList()
:
List<word>()
{}
Foam::hashedWordList::hashedWordList(const UList<word>& names)
:
List<word>(names)
{
rehash();
}
Foam::hashedWordList::hashedWordList(const hashedWordList& names)
:
List<word>(static_cast<const UList<word>&>(names))
{
rehash();
}
Foam::hashedWordList::hashedWordList(const Xfer< List<word> >& names)
:
List<word>(names)
{
rehash();
}
Foam::hashedWordList::hashedWordList
(
const label nNames,
const char** names
)
:
List<word>(nNames)
{
forAll(*this, i)
{
List<word>::operator[](i) = names[i];
}
rehash();
}
Foam::hashedWordList::hashedWordList
(
const char** names
)
{
// count names
label nNames = 0;
for (unsigned i = 0; names[i] && *(names[i]); ++i)
{
++nNames;
}
List<word>::setSize(nNames);
forAll(*this, i)
{
List<word>::operator[](i) = names[i];
}
rehash();
}
Foam::hashedWordList::hashedWordList(Istream& is)
{
is >> *this;
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
void Foam::hashedWordList::clear()
{
List<word>::clear();
indices_.clear();
}
void Foam::hashedWordList::append(const word& name)
{
const label idx = size();
List<word>::append(name);
indices_.insert(name, idx);
}
void Foam::hashedWordList::transfer(List<word>& lst)
{
List<word>::transfer(lst);
rehash();
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, hashedWordList& lst)
{
is >> static_cast<List<word>&>(lst);
lst.rehash();
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const hashedWordList& lst)
{
os << static_cast<const List<word>&>(lst);
return os;
}
// ************************************************************************* //

View File

@ -0,0 +1,149 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-2010 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 <http://www.gnu.org/licenses/>.
Class
Foam::hashedWordList
Description
A wordList with hashed indices for faster lookup by name.
SourceFiles
hashedWordListI.H
hashedWordList.C
\*---------------------------------------------------------------------------*/
#ifndef hashedWordList_H
#define hashedWordList_H
#include "wordList.H"
#include "HashTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
class hashedWordList;
// Forward declaration of friend functions and operators
Istream& operator>>(Istream&, hashedWordList&);
Ostream& operator<<(Ostream&, const hashedWordList&);
/*---------------------------------------------------------------------------*\
Class hashedWordList Declaration
\*---------------------------------------------------------------------------*/
class hashedWordList
:
public List<word>
{
// Private data
HashTable<label, word> indices_;
// Private Member Functions
//- rebuild the hash of indices
void rehash();
public:
// Constructors
//- Construct null
hashedWordList();
//- Copy constructor.
hashedWordList(const hashedWordList&);
//- Construct from list of names
hashedWordList(const UList<word>& names);
//- Construct by transferring the parameter contents
hashedWordList(const Xfer< List<word> >& names);
//- Construct from number and list of names
hashedWordList(const label nNames, const char** names);
//- Construct from a NULL-terminated list of names
hashedWordList(const char** names);
//- Construct from Istream
hashedWordList(Istream&);
// Member Functions
//- Clear the list, i.e. set size to zero.
void clear();
//- Append an element at the end of the list
void append(const word&);
//- Does the list contain the specified name
inline bool contains(const word&) const;
//- Transfer the contents of the argument List into this list
// and annul the argument list.
void transfer(List<word>&);
// Member Operators
//- Assignment operator from list of names
inline void operator=(const UList<word>& names);
//- Assignment operator.
inline void operator=(const hashedWordList&);
//- Allow readonly access to list of words
inline operator const Foam::UList<word>&() const;
//- Return name corresponding to specified index
inline const word& operator[](const label index) const;
//- Return index corresponding to specified name
inline label operator[](const word&) const;
// Istream operators
friend Istream& operator>>(Istream&, hashedWordList&);
friend Ostream& operator<<(Ostream&, const hashedWordList&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "hashedWordListI.H"
#endif
// ************************************************************************* //

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -21,32 +21,44 @@ License
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::speciesTable::contains(const word& specieName) const
inline bool Foam::hashedWordList::contains(const word& name) const
{
return specieIndices_.found(specieName);
return indices_.found(name);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline const Foam::word& Foam::speciesTable::operator[]
(
const label specieIndex
) const
inline void Foam::hashedWordList::operator=(const UList<word>& lst)
{
return wordList::operator[](specieIndex);
List<word>::operator=(lst);
rehash();
}
inline Foam::label Foam::speciesTable::operator[](const word& specieName) const
inline void Foam::hashedWordList::operator=(const hashedWordList& lst)
{
return specieIndices_[specieName];
operator=(static_cast<const UList<word>&>(lst));
}
inline const Foam::word& Foam::hashedWordList::operator[]
(
const label index
) const
{
return List<word>::operator[](index);
}
// could return -1 instead of bombing out
inline Foam::label Foam::hashedWordList::operator[](const word& name) const
{
return indices_[name];
}

View File

@ -859,7 +859,7 @@ Foam::chemkinReader::chemkinReader
:
lineNo_(1),
specieNames_(10),
speciesTable_(static_cast<const wordList&>(wordList()))
speciesTable_()
{
read(CHEMKINFileName, thermoFileName);
}
@ -870,7 +870,7 @@ Foam::chemkinReader::chemkinReader(const dictionary& thermoDict)
:
lineNo_(1),
specieNames_(10),
speciesTable_(static_cast<const wordList&>(wordList()))
speciesTable_()
{
fileName chemkinFile
(

View File

@ -1,12 +1,10 @@
atomicWeights = atomicWeights
specie = specie
speciesTable = speciesTable
equationOfState = equationOfState
reactions = reaction/reactions
$(atomicWeights)/atomicWeights.C
$(specie)/specie.C
$(speciesTable)/speciesTable.C
$(equationOfState)/perfectGas/perfectGas.C
$(reactions)/makeChemkinReactions.C
$(reactions)/makeReactionThermoReactions.C

View File

@ -1,83 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2010 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "speciesTable.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::speciesTable::setIndices()
{
forAll(*this, i)
{
specieIndices_.insert(wordList::operator[](i), i);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct from list of specie names
Foam::speciesTable::speciesTable(const wordList& specieNames)
:
wordList(specieNames)
{
setIndices();
}
// Construct from number of species and list of specie names
Foam::speciesTable::speciesTable(const label nSpecies, const char** specieNames)
:
wordList(nSpecies)
{
forAll(*this, i)
{
wordList::operator[](i) = specieNames[i];
}
setIndices();
}
// Construct from Istream
Foam::speciesTable::speciesTable(Istream& is)
:
wordList(is)
{
setIndices();
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, speciesTable& st)
{
is >> static_cast<wordList&>(st);
st.setIndices();
return is;
}
// ************************************************************************* //

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -21,89 +21,25 @@ License
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Typedef
Foam::speciesTable
Description
A table of species
SourceFiles
speciesTableI.H
speciesTable.C
A table of species as a hashedWordList
\*---------------------------------------------------------------------------*/
#ifndef speciesTable_H
#define speciesTable_H
#include "wordList.H"
#include "HashTable.H"
#include "hashedWordList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class speciesTable Declaration
\*---------------------------------------------------------------------------*/
class speciesTable
:
public wordList
{
// Private data
HashTable<label> specieIndices_;
// Private Member Functions
void setIndices();
public:
// Constructors
//- Construct from list of specie names
speciesTable(const wordList& specieNames);
//- Construct from number of species and list of specie names
speciesTable(const label nSpecies, const char** specieNames);
//- Construct from Istream
speciesTable(Istream&);
// Member Functions
//- Does the table contain the given specie
inline bool contains(const word& specieName) const;
// Member Operators
//- Return a specie's name
inline const word& operator[](const label) const;
//- Lookup a specie's name and return its index
inline label operator[](const word&) const;
// Istream operators
friend Istream& operator>>(Istream&, speciesTable&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "speciesTableI.H"
typedef hashedWordList speciesTable;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //