mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add missing Hash function for List/UList (issue #966)
- there were previously no hashing mechanisms for lists so they would fall back to the definition for primitives and hash the memory location of the allocated List object. - provide a UList::Hash<> sub-class for inheritance, and also a global specialization for UList<T>, List<T> such that the hash value for List<List<T>> cascades properly. - provide similar function in triFace to ensure that it remains similar in behaviour to face. - added SymmHash to Pair, for use when order is unimportant. STYLE: use string::hash() more consistently - no particular reason to use Hash<word>() which forwards to string::hash() anyhow
This commit is contained in:
@ -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) 2018 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -36,6 +36,7 @@ Description
|
||||
#include "labelList.H"
|
||||
#include "labelPair.H"
|
||||
#include "edgeList.H"
|
||||
#include "faceList.H"
|
||||
#include "triFaceList.H"
|
||||
|
||||
#include "Hash.H"
|
||||
@ -50,7 +51,7 @@ void infoHashString
|
||||
{
|
||||
if (modulus)
|
||||
{
|
||||
Info<< "basic string hashing (mod " << label(modulus) << ")" << endl;
|
||||
Info<< "basic string hashing (mod " << label(modulus) << ")" << nl;
|
||||
|
||||
for (const auto& str : lst)
|
||||
{
|
||||
@ -72,6 +73,190 @@ void infoHashString
|
||||
}
|
||||
|
||||
|
||||
|
||||
void reportHashList(const UList<string>& list)
|
||||
{
|
||||
Info<< "contiguous = " << contiguous<string>() << nl << nl;
|
||||
|
||||
for (const string& val : list)
|
||||
{
|
||||
unsigned hash1 = string::hash()(val);
|
||||
|
||||
Info<< hex << hash1 << ": " << val << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<label>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<label>() << nl << nl;
|
||||
|
||||
for (const label val : list)
|
||||
{
|
||||
// Direct value
|
||||
unsigned hash1 = Hash<label>()(val);
|
||||
|
||||
// Hashed byte-wise
|
||||
unsigned hash2 = Hash<label>()(val, 0);
|
||||
|
||||
Info<< hex << hash1
|
||||
<< " (seeded: " << hash2 << ")"
|
||||
<< ": " << dec << val << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<face>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<label>() << nl << nl;
|
||||
|
||||
for (const face& f : list)
|
||||
{
|
||||
// Direct value
|
||||
unsigned hash1 = face::Hash<>()(f);
|
||||
|
||||
unsigned hash2 = Hash<face>()(f);
|
||||
|
||||
Info<< hex << "face::Hash<> " << hash1
|
||||
<< " Hash<face> " << hash2
|
||||
<< ": " << dec << flatOutput(f) << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<labelList>& list)
|
||||
{
|
||||
for (const labelList& val : list)
|
||||
{
|
||||
unsigned hash1 = Hasher
|
||||
(
|
||||
val.cdata(),
|
||||
val.size() * sizeof(label)
|
||||
);
|
||||
|
||||
unsigned hash2 = Hash<labelList>()(val);
|
||||
unsigned hash2b = labelList::Hash<>()(val);
|
||||
|
||||
Info<< hex << hash1 << " or " << hash2
|
||||
<< "(" << hash2b << ") "
|
||||
<< ": " << dec << val << nl;
|
||||
}
|
||||
|
||||
unsigned hash2 = Hash<labelListList>()(list);
|
||||
unsigned hash2bad = HasherT(list);
|
||||
|
||||
Info<< hex << hash2 << " : " << dec << flatOutput(list) << nl
|
||||
<< hex << hash2bad << " as direct hash would be wrong"
|
||||
<< dec << nl;
|
||||
}
|
||||
|
||||
|
||||
typedef Pair<word> wordPair;
|
||||
|
||||
void reportHashList(const UList<wordPair>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<wordPair>() << nl << nl;
|
||||
|
||||
for (const wordPair& pr : list)
|
||||
{
|
||||
unsigned hash1 = Hash<wordPair>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash2 = wordPair::Hash<>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash2sym = wordPair::SymmHash<>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash3 = Hash<FixedList<word,2>>()(pr);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< ") or " << hash3
|
||||
<< " symm-hash:" << hash2sym
|
||||
<< " : "<< dec << flatOutput(pr) << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<labelPair>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<labelPair>() << nl << nl;
|
||||
|
||||
for (const labelPair& pr : list)
|
||||
{
|
||||
unsigned hash1 = Hash<labelPair>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash2 = labelPair::Hash<>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash3 = Hash<labelPair>()(pr);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< ") or " << hash3
|
||||
<< " : "<< dec << pr << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<labelPairPair>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<labelPairPair>() << nl << nl;
|
||||
|
||||
for (const labelPairPair& pr : list)
|
||||
{
|
||||
unsigned hash1 = Hash<labelPairPair>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash2 = labelPairPair::Hash<>()(pr);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash3 = Hash<labelPairPair>()(pr);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< ") or " << hash3
|
||||
<< " : "<< dec << pr << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<edge>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<edge>() << nl << nl;
|
||||
|
||||
for (const edge& e : list)
|
||||
{
|
||||
unsigned hash1 = Hash<edge>()(e);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash2 = labelPair::Hash<>()(e);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash3 = Hash<labelPair>()(e);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< ") or " << hash3
|
||||
<< " : "<< dec << e << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reportHashList(const UList<triFace>& list)
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<triFace>() << nl << nl;
|
||||
|
||||
for (const triFace& f : list)
|
||||
{
|
||||
// direct value
|
||||
unsigned hash1 = Hash<triFace>()(f);
|
||||
unsigned hash2 = FixedList<label, 3>::Hash<>()(f);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< "): " << dec << f << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
@ -81,114 +266,74 @@ int main(int argc, char *argv[])
|
||||
|
||||
IFstream is("hashingTests");
|
||||
|
||||
if (!is.good())
|
||||
{
|
||||
Info<< "No \"hashingTests\" file found ...\n";
|
||||
}
|
||||
|
||||
while (is.good())
|
||||
{
|
||||
const word listType(is);
|
||||
|
||||
Info<< endl;
|
||||
IOobject::writeDivider(Info) << listType << endl;
|
||||
if (listType.empty()) continue;
|
||||
|
||||
Info<< nl;
|
||||
IOobject::writeDivider(Info) << listType << nl;
|
||||
|
||||
if (listType == "stringList")
|
||||
{
|
||||
Info<< "contiguous = " << contiguous<string>() << endl << endl;
|
||||
|
||||
stringList lst(is);
|
||||
|
||||
forAll(lst, i)
|
||||
{
|
||||
unsigned hash1 = string::hash()(lst[i]);
|
||||
|
||||
Info<< hex << hash1 << ": " << lst[i] << endl;
|
||||
}
|
||||
stringList list(is);
|
||||
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "labelList")
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<label>() << endl << endl;
|
||||
|
||||
labelList lst(is);
|
||||
|
||||
forAll(lst, i)
|
||||
{
|
||||
// direct value
|
||||
unsigned hash1 = Hash<label>()(lst[i]);
|
||||
|
||||
// hashed byte-wise
|
||||
unsigned hash2 = Hash<label>()(lst[i], 0);
|
||||
|
||||
Info<< hex << hash1
|
||||
<< " (seeded: " << hash2 << ")"
|
||||
<< ": " << dec << lst[i] << endl;
|
||||
}
|
||||
|
||||
if (contiguous<label>())
|
||||
{
|
||||
unsigned hash3 = Hasher
|
||||
(
|
||||
lst.cdata(),
|
||||
lst.size() * sizeof(label)
|
||||
);
|
||||
|
||||
Info<<"contiguous hashed value " << hex << hash3 << endl;
|
||||
}
|
||||
labelList list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "faceList")
|
||||
{
|
||||
faceList list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "labelListList")
|
||||
{
|
||||
List<List<label>> lst(is);
|
||||
|
||||
forAll(lst, i)
|
||||
{
|
||||
unsigned hash1 = Hasher
|
||||
(
|
||||
lst[i].cdata(),
|
||||
lst[i].size() * sizeof(label)
|
||||
);
|
||||
|
||||
Info<< hex << hash1
|
||||
<< ": " << dec << lst[i] << endl;
|
||||
}
|
||||
|
||||
List<labelList> list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "wordPairList")
|
||||
{
|
||||
List<wordPair> list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "labelPairList")
|
||||
{
|
||||
labelPairList list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "labelPairPairList")
|
||||
{
|
||||
List<labelPairPair> list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "edgeList")
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<edge>() << endl << endl;
|
||||
|
||||
edgeList lst(is);
|
||||
|
||||
forAll(lst, i)
|
||||
{
|
||||
unsigned hash1 = Hash<edge>()(lst[i]);
|
||||
|
||||
// as FixedList
|
||||
unsigned hash2 = labelPair::Hash<>()(lst[i]);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< "): " << dec << lst[i] << endl;
|
||||
}
|
||||
edgeList list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else if (listType == "triFaceList")
|
||||
{
|
||||
Info<<"contiguous = " << contiguous<triFace>() << endl << endl;
|
||||
|
||||
triFaceList lst(is);
|
||||
|
||||
forAll(lst, i)
|
||||
{
|
||||
// direct value
|
||||
unsigned hash1 = Hash<triFace>()(lst[i]);
|
||||
unsigned hash2 = FixedList<label, 3>::Hash<>()(lst[i]);
|
||||
|
||||
Info<< hex << hash1 << " (as FixedList: " << hash2
|
||||
<< "): " << dec << lst[i] << endl;
|
||||
}
|
||||
triFaceList list(is);
|
||||
reportHashList(list);
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "unknown type: " << listType << endl;
|
||||
Info<< "unknown type: " << listType << nl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Info<< "\nEnd\n" << nl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user