Skip to content
Snippets Groups Projects
Commit 759306a3 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

STYLE: more consistent typedefs in Map, PtrMap, HashPtrTable

parent e105e30b
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,7 @@ License
template<class T, class Key, class Hash>
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
)
:
HashTable<T*, Key, Hash>()
parent_type(ht.capacity())
{
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* ptr = iter.object();
HashTable<T*, Key, Hash>::erase(iter);
this->parent_type::erase(iter);
return ptr;
}
......@@ -83,7 +83,7 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
{
T* ptr = iter.object();
if (HashTable<T*, Key, Hash>::erase(iter))
if (this->parent_type::erase(iter))
{
if (ptr)
{
......@@ -107,7 +107,7 @@ void Foam::HashPtrTable<T, Key, Hash>::clear()
delete iter.object();
}
HashTable<T*, Key, Hash>::clear();
this->parent_type::clear();
}
......
......@@ -25,7 +25,7 @@ Class
Foam::HashPtrTable
Description
A HashTable specialization for hashing pointers.
A HashTable of pointers to objects of type \<T\>.
SourceFiles
HashPtrTable.C
......@@ -51,7 +51,7 @@ class Ostream;
template<class T, class Key, class Hash> class HashPtrTable;
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>
Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& tbl);
......@@ -77,11 +77,16 @@ class HashPtrTable
void read(const dictionary& dict, const INew& inewt);
public:
using iterator = typename HashTable<T*, Key, Hash>::iterator;
using const_iterator = typename HashTable<T*, Key, Hash>::const_iterator;
//- The template instance used for this table
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
......@@ -100,7 +105,7 @@ public:
HashPtrTable(const dictionary& dict);
//- Construct as copy
HashPtrTable(const HashPtrTable<T, Key, Hash>& 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<T, Key, Hash>& rhs);
void operator=(const this_type& rhs);
// IOstream Operators
......@@ -135,7 +140,7 @@ public:
friend Istream& operator>> <T, Key, Hash>
(
Istream& is,
HashPtrTable<T, Key, Hash>& L
HashPtrTable<T, Key, Hash>& tbl
);
friend Ostream& operator<< <T, Key, Hash>
......
......@@ -35,7 +35,7 @@ template<class T, class Key, class Hash>
template<class INew>
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);
......@@ -131,7 +131,7 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
<< 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)
{
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<T, Key, Hash>::HashPtrTable(const dictionary& dict)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
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();
L.read(is, INew<T>());
tbl.clear();
tbl.read(is, INew<T>());
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;
}
......
......@@ -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<T, label, Hash<label>>
{
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
//- Construct given initial size
Map(const label size = 128)
:
HashTable<T, label, Hash<label>>(size)
parent_type(size)
{}
//- Construct from Istream
Map(Istream& is)
:
HashTable<T, label, Hash<label>>(is)
parent_type(is)
{}
//- Construct as copy
Map(const Map<T>& map)
:
HashTable<T, label, Hash<label>>(map)
parent_type(map)
{}
//- Construct by transferring the parameter contents
Map(const Xfer<Map<T>>& map)
:
HashTable<T, label, Hash<label>>(map)
parent_type(map)
{}
//- Construct by transferring the parameter contents
Map(const Xfer<HashTable<T, label, Hash<label>>>& map)
:
HashTable<T, label, Hash<label>>(map)
parent_type(map)
{}
//- Construct from an initializer list
Map(std::initializer_list<Tuple2<label, T>> map)
:
HashTable<T, label, Hash<label>>(map)
parent_type(map)
{}
};
......
......@@ -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<T, label, Hash<label>>
{
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
//- Construct given initial map size
PtrMap(const label size = 128)
:
HashPtrTable<T, label, Hash<label>>(size)
parent_type(size)
{}
//- Construct from Istream
PtrMap(Istream& is)
:
HashPtrTable<T, label, Hash<label>>(is)
parent_type(is)
{}
//- Construct as copy
PtrMap(const PtrMap<T>& map)
PtrMap(const this_type& map)
:
HashPtrTable<T, label, Hash<label>>(map)
parent_type(map)
{}
};
......
......@@ -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<class T, class Key, class Hash>
......
......@@ -79,7 +79,7 @@ template<class T, class Key, class Hash> 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()
......
......@@ -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;
}
}
// ************************************************************************* //
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment