Commit 3e3c9739 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: simplify hashing to use struct instead of class

- more consistent with STL practices for function classes.

- string::hash function class now operates on std::string rather
  than Foam::string since we have now avoided inadvertent use of
  string conversion from int in more places.
parent 09a6e940
......@@ -62,15 +62,8 @@ public:
{
public:
class hash
:
public Hash<interfacePair>
struct hash
{
public:
hash()
{}
label operator()(const interfacePair& key) const
{
return word::hash()(key.first()) + word::hash()(key.second());
......@@ -80,8 +73,7 @@ public:
// Constructors
interfacePair()
{}
interfacePair() {} // = default
interfacePair(const word& alpha1Name, const word& alpha2Name)
:
......
......@@ -74,30 +74,16 @@ public:
{
public:
class symmHash
:
public Hash<interfacePair>
struct symmHash
{
public:
symmHash()
{}
label operator()(const interfacePair& key) const
{
return word::hash()(key.first()) + word::hash()(key.second());
}
};
class hash
:
public Hash<interfacePair>
struct hash
{
public:
hash()
{}
label operator()(const interfacePair& key) const
{
return word::hash()(key.first(), word::hash()(key.second()));
......@@ -107,8 +93,7 @@ public:
// Constructors
interfacePair()
{}
interfacePair() {} // = default
interfacePair(const word& alpha1Name, const word& alpha2Name)
:
......@@ -233,8 +218,7 @@ public:
//- Destructor
virtual ~multiphaseSystem()
{}
virtual ~multiphaseSystem() = default;
// Member Functions
......
......@@ -71,15 +71,8 @@ public:
{
public:
class hash
:
public Hash<interfacePair>
struct hash
{
public:
hash()
{}
label operator()(const interfacePair& key) const
{
return word::hash()(key.first()) + word::hash()(key.second());
......@@ -89,8 +82,7 @@ public:
// Constructors
interfacePair()
{}
interfacePair() {} // = default
interfacePair(const word& alpha1Name, const word& alpha2Name)
:
......@@ -196,8 +188,7 @@ public:
//- Destructor
virtual ~multiphaseMixture()
{}
virtual ~multiphaseMixture() = default;
// Member Functions
......
......@@ -27,14 +27,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::phasePairKey::hash::hash()
{}
Foam::phasePairKey::phasePairKey()
{}
Foam::phasePairKey::phasePairKey
(
const word& name1,
......@@ -47,12 +39,6 @@ Foam::phasePairKey::phasePairKey
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::phasePairKey::~phasePairKey()
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Foam::phasePairKey::ordered() const
......@@ -94,13 +80,13 @@ bool Foam::operator==
const phasePairKey& b
)
{
const label c = Pair<word>::compare(a,b);
const label cmp = Pair<word>::compare(a,b);
return
(a.ordered_ == b.ordered_)
&& (
(a.ordered_ && (c == 1))
|| (!a.ordered_ && (c != 0))
(a.ordered_ && (cmp == 1))
|| (!a.ordered_ && (cmp != 0))
);
}
......@@ -127,7 +113,7 @@ Foam::Istream& Foam::operator>>(Istream& is, phasePairKey& key)
{
key.ordered_ = false;
}
else if(temp[1] == "in")
else if (temp[1] == "in")
{
key.ordered_ = true;
}
......
......@@ -40,7 +40,7 @@ SourceFiles
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declarations
class phasePairKey;
......@@ -59,41 +59,24 @@ class phasePairKey
:
public Pair<word>
{
public:
class hash
:
public Hash<phasePairKey>
{
public:
// Constructors
// Construct null
hash();
// Member operators
// Generate a hash from a phase pair key
label operator()(const phasePairKey& key) const;
};
private:
// Private data
//- Flag to indicate whether ordering is important
bool ordered_;
public:
struct hash
{
// Generate a hash from a phase pair key
label operator()(const phasePairKey& key) const;
};
// Constructors
//- Construct null
phasePairKey();
phasePairKey() {} // = default
//- Construct from names and the ordering flag
phasePairKey
......@@ -105,7 +88,7 @@ public:
// Destructor
virtual ~phasePairKey();
virtual ~phasePairKey() = default;
// Access
......
......@@ -27,14 +27,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::phasePairKey::hash::hash()
{}
Foam::phasePairKey::phasePairKey()
{}
Foam::phasePairKey::phasePairKey
(
const word& name1,
......@@ -47,12 +39,6 @@ Foam::phasePairKey::phasePairKey
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::phasePairKey::~phasePairKey()
{}
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
Foam::label Foam::phasePairKey::hash::operator()
......@@ -86,13 +72,13 @@ bool Foam::operator==
const phasePairKey& b
)
{
const label c = Pair<word>::compare(a,b);
const label cmp = Pair<word>::compare(a,b);
return
(a.ordered_ == b.ordered_)
&& (
(a.ordered_ && (c == 1))
|| (!a.ordered_ && (c != 0))
(a.ordered_ && (cmp == 1))
|| (!a.ordered_ && (cmp != 0))
);
}
......@@ -119,7 +105,7 @@ Foam::Istream& Foam::operator>>(Istream& is, phasePairKey& key)
{
key.ordered_ = false;
}
else if(temp[1] == "in")
else if (temp[1] == "in")
{
key.ordered_ = true;
}
......
......@@ -40,7 +40,7 @@ SourceFiles
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declarations
class phasePairKey;
......@@ -59,48 +59,36 @@ class phasePairKey
:
public Pair<word>
{
public:
class hash
:
public Hash<phasePairKey>
{
public:
// Constructors
// Construct null
hash();
// Member operators
// Generate a hash from a phase pair key
label operator()(const phasePairKey& key) const;
};
private:
// Private data
//- Flag to indicate whether ordering is important
bool ordered_;
public:
struct hash
{
// Generate a hash from a phase pair key
label operator()(const phasePairKey& key) const;
};
// Constructors
//- Construct null
phasePairKey();
phasePairKey() {} // = default
//- Construct from names and the ordering flag
phasePairKey(const word& name1, const word& name2, const bool ordered);
phasePairKey
(
const word& name1,
const word& name2,
const bool ordered
);
// Destructor
virtual ~phasePairKey();
virtual ~phasePairKey() = default;
// Friend Operators
......
......@@ -101,12 +101,8 @@ public:
//- Hashing function class.
// Use Hasher directly for contiguous data. Otherwise hash incrementally.
template<class HashT=Hash<T>>
class Hash
struct Hash
{
public:
Hash()
{}
inline unsigned operator()
(
const FixedList<T, Size>&,
......
......@@ -546,18 +546,16 @@ inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator()
// Hash directly
return Hasher(lst.v_, sizeof(lst.v_), seed);
}
else
{
// Hash incrementally
unsigned val = seed;
for (unsigned i=0; i<Size; ++i)
{
val = HashT()(lst[i], val);
}
// Hash incrementally
unsigned val = seed;
return val;
for (unsigned i=0; i<Size; ++i)
{
val = HashT()(lst[i], val);
}
return val;
}
......
......@@ -44,21 +44,16 @@ Description
namespace Foam
{
//template<class Type> class Hash;
//template<> class Hash<label>;
//template<class Type> struct Hash;
//template<> struct Hash<label>;
/*---------------------------------------------------------------------------*\
Class Hash Declaration
\*---------------------------------------------------------------------------*/
template<class PrimitiveType>
class Hash
struct Hash
{
public:
Hash()
{}
unsigned operator()(const PrimitiveType& p, unsigned seed) const
{
return Hasher(&p, sizeof(p), seed);
......@@ -76,13 +71,8 @@ public:
//- Hash specialization for hashing labels
template<>
class Hash<Foam::label>
struct Hash<Foam::label>
{
public:
Hash()
{}
//- Incrementally hash a label.
// This will necessarily return a different value than the
// non-incremental version.
......@@ -103,13 +93,8 @@ public:
//- Hash specialization for hashing strings
template<>
class Hash<Foam::string>
struct Hash<Foam::string>
{
public:
Hash()
{}
unsigned operator()(const string& p, unsigned seed) const
{
return string::hash()(p, seed);
......@@ -123,13 +108,8 @@ public:
//- Hash specialization for hashing words
template<>
class Hash<Foam::word>
struct Hash<Foam::word>
{
public:
Hash()
{}
unsigned operator()(const word& p, unsigned seed) const
{
return word::hash()(p, seed);
......@@ -143,13 +123,8 @@ public:
//- Hash specialization for hashing fileNames
template<>
class Hash<Foam::fileName>
struct Hash<Foam::fileName>
{
public:
Hash()
{}
unsigned operator()(const fileName& p, unsigned seed) const
{
return fileName::hash()(p, seed);
......@@ -163,13 +138,8 @@ public:
//- Hash specialization for hashing wordRes
template<>
class Hash<Foam::wordRe>
struct Hash<Foam::wordRe>
{
public:
Hash()
{}
unsigned operator()(const wordRe& p, unsigned seed) const
{
return wordRe::hash()(p, seed);
......@@ -183,13 +153,8 @@ public:
//- Hash specialization for hashing keyTypes
template<>
class Hash<Foam::keyType>
struct Hash<Foam::keyType>
{
public:
Hash()
{}
unsigned operator()(const keyType& p, unsigned seed) const
{
return keyType::hash()(p, seed);
......@@ -201,18 +166,12 @@ public:
};
//- Hash specialization for hashing pointer addresses.
// Treat a pointer like a long.
// This should work for both 32-bit and 64-bit pointers.
template<>
class Hash<void*>
struct Hash<void*>
{
public:
Hash()
{}
unsigned operator()(const void* const& p, unsigned seed) const
{
return Hash<long>()(long(p), seed);
......
......@@ -143,15 +143,14 @@ public:
static const string null;
//- Hashing function class, shared by all the derived classes
class hash
//- Hashing function for string and derived string classes
struct hash
{
public:
hash() = default;
//- Hash for string.
// Uses Foam::string instead of std::string for automatic conversions.
inline unsigned operator()(const string& str, unsigned seed = 0) const;
inline unsigned operator()
(
const std::string& str,
unsigned seed = 0
) const;
};
......
......@@ -254,7 +254,7 @@ inline bool Foam::string::operator()(const std::string& text) const
inline unsigned Foam::string::hash::operator()
(
const string& str,
const std::string& str,
unsigned seed
) const
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment