STYLE: more consistent typedefs in Map, PtrMap, HashPtrTable

This commit is contained in:
Mark Olesen
2017-05-04 02:47:32 +02:00
parent e105e30bfb
commit 759306a3e2
8 changed files with 102 additions and 78 deletions

View File

@ -31,7 +31,7 @@ License
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size) Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
: :
HashTable<T*, Key, Hash>(size) parent_type(size)
{} {}
@ -41,7 +41,7 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable
const HashPtrTable<T, Key, Hash>& ht const HashPtrTable<T, Key, Hash>& ht
) )
: :
HashTable<T*, Key, Hash>() parent_type(ht.capacity())
{ {
for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter) for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter)
{ {
@ -73,7 +73,7 @@ template<class T, class Key, class Hash>
T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter) T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
{ {
T* ptr = iter.object(); T* ptr = iter.object();
HashTable<T*, Key, Hash>::erase(iter); this->parent_type::erase(iter);
return ptr; return ptr;
} }
@ -83,7 +83,7 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
{ {
T* ptr = iter.object(); T* ptr = iter.object();
if (HashTable<T*, Key, Hash>::erase(iter)) if (this->parent_type::erase(iter))
{ {
if (ptr) if (ptr)
{ {
@ -107,7 +107,7 @@ void Foam::HashPtrTable<T, Key, Hash>::clear()
delete iter.object(); delete iter.object();
} }
HashTable<T*, Key, Hash>::clear(); this->parent_type::clear();
} }

View File

@ -25,7 +25,7 @@ Class
Foam::HashPtrTable Foam::HashPtrTable
Description Description
A HashTable specialization for hashing pointers. A HashTable of pointers to objects of type \<T\>.
SourceFiles SourceFiles
HashPtrTable.C HashPtrTable.C
@ -51,7 +51,7 @@ class Ostream;
template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> class HashPtrTable;
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L); Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl);
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& tbl); Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& tbl);
@ -77,11 +77,16 @@ class HashPtrTable
void read(const dictionary& dict, const INew& inewt); void read(const dictionary& dict, const INew& inewt);
public: public:
using iterator = typename HashTable<T*, Key, Hash>::iterator; //- The template instance used for this table
using const_iterator = typename HashTable<T*, Key, Hash>::const_iterator; typedef HashPtrTable<T, Key, Hash> this_type;
//- The template instance used for the parent HashTable
typedef HashTable<T*, Key, Hash> parent_type;
using iterator = typename parent_type::iterator;
using const_iterator = typename parent_type::const_iterator;
// Constructors // Constructors
@ -100,7 +105,7 @@ public:
HashPtrTable(const dictionary& dict); HashPtrTable(const dictionary& dict);
//- Construct as copy //- Construct as copy
HashPtrTable(const HashPtrTable<T, Key, Hash>& ht); HashPtrTable(const this_type& ht);
//- Destructor //- Destructor
@ -109,25 +114,25 @@ public:
// Member Functions // Member Functions
// Edit // Edit
//- Remove and return the pointer specified by given iterator //- Remove and return the pointer specified by given iterator
T* remove(iterator& iter); T* remove(iterator& iter);
//- Erase an hashedEntry specified by given iterator //- Erase an hashedEntry specified by given iterator
bool erase(iterator& iter); bool erase(iterator& iter);
//- Clear all entries from table //- Clear all entries from table
void clear(); void clear();
//- Write //- Write
void write(Ostream& os) const; void write(Ostream& os) const;
// Member Operators // Member Operators
//- Copy assignment //- Copy assignment
void operator=(const HashPtrTable<T, Key, Hash>& rhs); void operator=(const this_type& rhs);
// IOstream Operators // IOstream Operators
@ -135,7 +140,7 @@ public:
friend Istream& operator>> <T, Key, Hash> friend Istream& operator>> <T, Key, Hash>
( (
Istream& is, Istream& is,
HashPtrTable<T, Key, Hash>& L HashPtrTable<T, Key, Hash>& tbl
); );
friend Ostream& operator<< <T, Key, Hash> friend Ostream& operator<< <T, Key, Hash>

View File

@ -35,7 +35,7 @@ template<class T, class Key, class Hash>
template<class INew> template<class INew>
void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt) void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
{ {
is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)"); is.fatalCheck(FUNCTION_NAME);
token firstToken(is); token firstToken(is);
@ -131,7 +131,7 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
<< exit(FatalIOError); << exit(FatalIOError);
} }
is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)"); is.fatalCheck(FUNCTION_NAME);
} }
@ -145,11 +145,9 @@ void Foam::HashPtrTable<T, Key, Hash>::read
{ {
forAllConstIter(dictionary, dict, iter) forAllConstIter(dictionary, dict, iter)
{ {
this->insert const word& k = iter().keyword();
(
iter().keyword(), this->insert(k, inewt(dict.subDict(k)).ptr());
inewt(dict.subDict(iter().keyword())).ptr()
);
} }
} }
@ -195,10 +193,10 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const dictionary& dict)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L) Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl)
{ {
L.clear(); tbl.clear();
L.read(is, INew<T>()); tbl.read(is, INew<T>());
return is; return is;
} }
@ -232,8 +230,7 @@ Foam::Ostream& Foam::operator<<
// Write end delimiter // Write end delimiter
os << token::END_LIST; os << token::END_LIST;
// Check state of IOstream os.check(FUNCTION_NAME);
os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)");
return os; return os;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -51,50 +51,54 @@ class Map
: :
public HashTable<T, label, Hash<label>> public HashTable<T, label, Hash<label>>
{ {
public: public:
typedef typename HashTable<T, label, Hash<label>>::iterator iterator; //- The template instance used for this Map
typedef Map<T> this_type;
//- The template instance used for the parent HashTable
typedef HashTable<T, label, Hash<label>> parent_type;
using iterator = typename parent_type::iterator;
using const_iterator = typename parent_type::const_iterator;
typedef typename HashTable<T, label, Hash<label>>::const_iterator
const_iterator;
// Constructors // Constructors
//- Construct given initial size //- Construct given initial size
Map(const label size = 128) Map(const label size = 128)
: :
HashTable<T, label, Hash<label>>(size) parent_type(size)
{} {}
//- Construct from Istream //- Construct from Istream
Map(Istream& is) Map(Istream& is)
: :
HashTable<T, label, Hash<label>>(is) parent_type(is)
{} {}
//- Construct as copy //- Construct as copy
Map(const Map<T>& map) Map(const Map<T>& map)
: :
HashTable<T, label, Hash<label>>(map) parent_type(map)
{} {}
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
Map(const Xfer<Map<T>>& map) Map(const Xfer<Map<T>>& map)
: :
HashTable<T, label, Hash<label>>(map) parent_type(map)
{} {}
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
Map(const Xfer<HashTable<T, label, Hash<label>>>& map) Map(const Xfer<HashTable<T, label, Hash<label>>>& map)
: :
HashTable<T, label, Hash<label>>(map) parent_type(map)
{} {}
//- Construct from an initializer list //- Construct from an initializer list
Map(std::initializer_list<Tuple2<label, T>> map) Map(std::initializer_list<Tuple2<label, T>> map)
: :
HashTable<T, label, Hash<label>>(map) parent_type(map)
{} {}
}; };

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -51,27 +51,33 @@ class PtrMap
: :
public HashPtrTable<T, label, Hash<label>> public HashPtrTable<T, label, Hash<label>>
{ {
public: public:
//- The template instance used for this PtrMap
typedef PtrMap<T> this_type;
//- The template instance used for the parent HashTable
typedef HashPtrTable<T, label, Hash<label>> parent_type;
// Constructors // Constructors
//- Construct given initial map size //- Construct given initial map size
PtrMap(const label size = 128) PtrMap(const label size = 128)
: :
HashPtrTable<T, label, Hash<label>>(size) parent_type(size)
{} {}
//- Construct from Istream //- Construct from Istream
PtrMap(Istream& is) PtrMap(Istream& is)
: :
HashPtrTable<T, label, Hash<label>>(is) parent_type(is)
{} {}
//- Construct as copy //- Construct as copy
PtrMap(const PtrMap<T>& map) PtrMap(const this_type& map)
: :
HashPtrTable<T, label, Hash<label>>(map) parent_type(map)
{} {}
}; };

View File

@ -30,32 +30,6 @@ License
#include "List.H" #include "List.H"
#include "IOstreams.H" #include "IOstreams.H"
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
Foam::label Foam::StaticHashTableCore::canonicalSize(const label size)
{
if (size < 1)
{
return 0;
}
// Enforce power of two
unsigned int goodSize = size;
if (goodSize & (goodSize - 1))
{
// Brute-force is fast enough
goodSize = 1;
while (goodSize < unsigned(size))
{
goodSize <<= 1;
}
}
return goodSize;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T, class Key, class Hash> template<class T, class Key, class Hash>

View File

@ -79,7 +79,7 @@ template<class T, class Key, class Hash> Ostream& operator<<
struct StaticHashTableCore struct StaticHashTableCore
{ {
//- Return a canonical (power-of-two) size //- Return a canonical (power-of-two) size
static label canonicalSize(const label); static label canonicalSize(const label size);
//- Construct null //- Construct null
StaticHashTableCore() StaticHashTableCore()

View File

@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "StaticHashTable.H" #include "StaticHashTable.H"
#include "uLabel.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -33,4 +34,41 @@ defineTypeNameAndDebug(StaticHashTableCore, 0);
} }
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
Foam::label Foam::StaticHashTableCore::canonicalSize(const label size)
{
if (size < 1)
{
return 0;
}
// Enforce power of two - makes for a vey fast modulus etc.
// The value '8' is some arbitrary lower limit.
// If the hash table is too small, there will be many table collisions!
const uLabel unsigned_size = size;
uLabel powerOfTwo = 8;
if (size < powerOfTwo)
{
return powerOfTwo;
}
else if (unsigned_size & (unsigned_size-1)) // <- Modulus of i^2
{
// Determine power-of-two. Brute-force is fast enough.
while (powerOfTwo < unsigned_size)
{
powerOfTwo <<= 1;
}
return powerOfTwo;
}
else
{
return unsigned_size;
}
}
// ************************************************************************* // // ************************************************************************* //