diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index 2a79ae32b2..4143d25d85 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -31,7 +31,7 @@ License template Foam::HashPtrTable::HashPtrTable(const label size) : - HashTable(size) + parent_type(size) {} @@ -41,7 +41,7 @@ Foam::HashPtrTable::HashPtrTable const HashPtrTable& ht ) : - HashTable() + parent_type(ht.capacity()) { for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter) { @@ -73,7 +73,7 @@ template T* Foam::HashPtrTable::remove(iterator& iter) { T* ptr = iter.object(); - HashTable::erase(iter); + this->parent_type::erase(iter); return ptr; } @@ -83,7 +83,7 @@ bool Foam::HashPtrTable::erase(iterator& iter) { T* ptr = iter.object(); - if (HashTable::erase(iter)) + if (this->parent_type::erase(iter)) { if (ptr) { @@ -107,7 +107,7 @@ void Foam::HashPtrTable::clear() delete iter.object(); } - HashTable::clear(); + this->parent_type::clear(); } diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index 863d308cf2..7cdf3743e3 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -25,7 +25,7 @@ Class Foam::HashPtrTable Description - A HashTable specialization for hashing pointers. + A HashTable of pointers to objects of type \. SourceFiles HashPtrTable.C @@ -51,7 +51,7 @@ class Ostream; template class HashPtrTable; template -Istream& operator>>(Istream& is, HashPtrTable& L); +Istream& operator>>(Istream& is, HashPtrTable& tbl); template Ostream& operator<<(Ostream& os, const HashPtrTable& tbl); @@ -77,11 +77,16 @@ class HashPtrTable void read(const dictionary& dict, const INew& inewt); - public: - using iterator = typename HashTable::iterator; - using const_iterator = typename HashTable::const_iterator; + //- The template instance used for this table + typedef HashPtrTable this_type; + + //- The template instance used for the parent HashTable + typedef HashTable parent_type; + + using iterator = typename parent_type::iterator; + using const_iterator = typename parent_type::const_iterator; // Constructors @@ -100,7 +105,7 @@ public: HashPtrTable(const dictionary& dict); //- Construct as copy - HashPtrTable(const HashPtrTable& ht); + HashPtrTable(const this_type& ht); //- Destructor @@ -109,25 +114,25 @@ public: // Member Functions - // Edit + // Edit - //- Remove and return the pointer specified by given iterator - T* remove(iterator& iter); + //- Remove and return the pointer specified by given iterator + T* remove(iterator& iter); - //- Erase an hashedEntry specified by given iterator - bool erase(iterator& iter); + //- Erase an hashedEntry specified by given iterator + bool erase(iterator& iter); - //- Clear all entries from table - void clear(); + //- Clear all entries from table + void clear(); - //- Write - void write(Ostream& os) const; + //- Write + void write(Ostream& os) const; // Member Operators //- Copy assignment - void operator=(const HashPtrTable& rhs); + void operator=(const this_type& rhs); // IOstream Operators @@ -135,7 +140,7 @@ public: friend Istream& operator>> ( Istream& is, - HashPtrTable& L + HashPtrTable& tbl ); friend Ostream& operator<< diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C index 5d41d32a8b..7a679ef149 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -35,7 +35,7 @@ template template void Foam::HashPtrTable::read(Istream& is, const INew& inewt) { - is.fatalCheck("HashPtrTable::read(Istream&, const INew&)"); + is.fatalCheck(FUNCTION_NAME); token firstToken(is); @@ -131,7 +131,7 @@ void Foam::HashPtrTable::read(Istream& is, const INew& inewt) << exit(FatalIOError); } - is.fatalCheck("HashPtrTable::read(Istream&, const INew&)"); + is.fatalCheck(FUNCTION_NAME); } @@ -145,11 +145,9 @@ void Foam::HashPtrTable::read { forAllConstIter(dictionary, dict, iter) { - this->insert - ( - iter().keyword(), - inewt(dict.subDict(iter().keyword())).ptr() - ); + const word& k = iter().keyword(); + + this->insert(k, inewt(dict.subDict(k)).ptr()); } } @@ -195,10 +193,10 @@ Foam::HashPtrTable::HashPtrTable(const dictionary& dict) // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template -Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable& L) +Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable& tbl) { - L.clear(); - L.read(is, INew()); + tbl.clear(); + tbl.read(is, INew()); return is; } @@ -232,8 +230,7 @@ Foam::Ostream& Foam::operator<< // Write end delimiter os << token::END_LIST; - // Check state of IOstream - os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)"); + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/containers/HashTables/Map/Map.H b/src/OpenFOAM/containers/HashTables/Map/Map.H index 8ef2f560bc..4ac6e90e60 100644 --- a/src/OpenFOAM/containers/HashTables/Map/Map.H +++ b/src/OpenFOAM/containers/HashTables/Map/Map.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,50 +51,54 @@ class Map : public HashTable> { - public: - typedef typename HashTable>::iterator iterator; + //- The template instance used for this Map + typedef Map this_type; + + //- The template instance used for the parent HashTable + typedef HashTable> parent_type; + + using iterator = typename parent_type::iterator; + using const_iterator = typename parent_type::const_iterator; - typedef typename HashTable>::const_iterator - const_iterator; // Constructors //- Construct given initial size Map(const label size = 128) : - HashTable>(size) + parent_type(size) {} //- Construct from Istream Map(Istream& is) : - HashTable>(is) + parent_type(is) {} //- Construct as copy Map(const Map& map) : - HashTable>(map) + parent_type(map) {} //- Construct by transferring the parameter contents Map(const Xfer>& map) : - HashTable>(map) + parent_type(map) {} //- Construct by transferring the parameter contents Map(const Xfer>>& map) : - HashTable>(map) + parent_type(map) {} //- Construct from an initializer list Map(std::initializer_list> map) : - HashTable>(map) + parent_type(map) {} }; diff --git a/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H b/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H index 134a7fa9b0..178bc67098 100644 --- a/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H +++ b/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,27 +51,33 @@ class PtrMap : public HashPtrTable> { - public: + //- The template instance used for this PtrMap + typedef PtrMap this_type; + + //- The template instance used for the parent HashTable + typedef HashPtrTable> parent_type; + + // Constructors //- Construct given initial map size PtrMap(const label size = 128) : - HashPtrTable>(size) + parent_type(size) {} //- Construct from Istream PtrMap(Istream& is) : - HashPtrTable>(is) + parent_type(is) {} //- Construct as copy - PtrMap(const PtrMap& map) + PtrMap(const this_type& map) : - HashPtrTable>(map) + parent_type(map) {} }; diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C index 05ad88f33e..948a451d47 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C @@ -30,32 +30,6 @@ License #include "List.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 * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H index 03321c686d..9614b82fbb 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H @@ -79,7 +79,7 @@ template Ostream& operator<< struct StaticHashTableCore { //- Return a canonical (power-of-two) size - static label canonicalSize(const label); + static label canonicalSize(const label size); //- Construct null StaticHashTableCore() diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C index 760839b0c6..9f24c86e63 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "StaticHashTable.H" +#include "uLabel.H" // * * * * * * * * * * * * * * 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; + } +} + + // ************************************************************************* //