Commit dd9ecd49 authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent 82bad81d
......@@ -54,20 +54,13 @@ class multiphaseSystem
{
protected:
typedef
HashTable
<
volScalarField::Internal,
word,
word::hash
>
SuSpTable;
typedef HashTable<volScalarField::Internal> SuSpTable;
typedef HashTable<scalar, phasePairKey, phasePairKey::hash>
scalarTable;
typedef HashTable<surfaceScalarField, word, word::hash>
compressionFluxTable;
typedef HashTable<surfaceScalarField> compressionFluxTable;
// Protected data
......
......@@ -75,8 +75,7 @@ public:
phasePairTable;
typedef
HashTable<autoPtr<phaseModel>, word, word::hash> phaseModelTable;
typedef HashTable<autoPtr<phaseModel>> phaseModelTable;
protected:
......
......@@ -139,13 +139,16 @@ int main(int argc, char *argv[])
FixedList<label, 4> list1{2, 3, 4, 5};
Info<< "list1:" << list1
<< " hash:" << FixedList<label, 4>::Hash<>()(list1) << nl;
<< " hash:" << FixedList<label,4>::Hash<>()(list1) << nl
<< " hash:" << Hash<FixedList<label,4>>()(list1) << nl;
label a[4] = {0, 1, 2, 3};
FixedList<label, 4> list2(a);
Info<< "list2:" << list2
<< " hash:" << FixedList<label, 4>::Hash<>()(list2) << nl;
<< " hash:" << FixedList<label,4>::Hash<>()(list2) << nl
<< " hash:" << Hash<FixedList<label,4>>()(list2) << nl;
// Using FixedList for content too
{
......
......@@ -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;
}
......
......@@ -24,6 +24,7 @@ labelList
-1
-10
-100
100
)
labelListList
......@@ -34,6 +35,16 @@ labelListList
(0 1)
(100 1000)
(0 1 100 1000)
(100 1000)
)
faceList
(
(0 1 2 3 4 5)
(5 4 3 1 2 0)
(0 1 2 3 4 5)
(100 0 15)
(15 0 100)
)
// edges are hashed commutatively
......@@ -45,6 +56,7 @@ edgeList
(100 45)
(128 1000)
(1000 128)
(100 45)
)
// triFaces are also hashed commutatively (via multiply/add)
......@@ -56,4 +68,34 @@ triFaceList
)
labelPairList
(
(0 1)
(1 0)
(45 100)
(100 45)
(128 1000)
(1000 128)
(100 45)
)
labelPairPairList
(
((0 1) (1 0))
((45 100) (100 45))
((128 1000) (1000 128))
((45 100) (100 45))
)
wordPairList
(
("Yes" "No")
("True" "False")
("No" "Yes")
("False" "True")
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -598,6 +598,20 @@ int main(int argc, char *argv[])
<< "-wordList: " << flatOutput(wLst) << nl
<< "-stringList: " << flatOutput(sLst) << endl;
// Hash values
{
labelList list1(identity(5));
labelList list2(identity(5));
Info<<"hash of " << flatOutput(list1)
<< " = " << Hash<labelList>()(list1) << " or "
<< labelList::Hash<>()(list1) << nl;
Info<<"hash of " << flatOutput(list2) << " = "
<< Hash<labelList>()(list2) << " or "
<< labelList::Hash<>()(list2) << nl;
}
return 0;
}
......
......@@ -54,24 +54,8 @@ using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
template<>
inline unsigned Hash<face>::operator()(const face& t, unsigned seed) const
{
return Hasher(t.cdata(),t.size()*sizeof(label), seed);
}
template<>
inline unsigned Hash<face>::operator()(const face& t) const
{
return Hash<face>::operator()(t, 0);
}
}
const string SEPARATOR(" -1");
bool isSeparator(const std::string& line)
{
return line.substr(0, 6) == SEPARATOR;
......@@ -861,7 +845,7 @@ int main(int argc, char *argv[])
Map<label> faceToCell[2];
{
HashTable<label, face, Hash<face>> faceToFaceID(boundaryFaces.size());
HashTable<label, face, face::Hash<>> faceToFaceID(boundaryFaces.size());
forAll(boundaryFaces, facei)
{
SortableList<label> sortedVerts(boundaryFaces[facei]);
......@@ -874,12 +858,11 @@ int main(int argc, char *argv[])
forAll(faces, i)
{
SortableList<label> sortedVerts(faces[i]);
HashTable<label, face, Hash<face>>::const_iterator fnd =
faceToFaceID.find(face(sortedVerts));
const auto fnd = faceToFaceID.find(face(sortedVerts));
if (fnd != faceToFaceID.end())
if (fnd.found())
{
label facei = fnd();
label facei = *fnd;
int stat = face::compare(faces[i], boundaryFaces[facei]);
if (stat == 1)
......
......@@ -166,7 +166,7 @@ int main(int argc, char *argv[])
label maxPatch = 0;
// Boundary faces as three vertices
HashTable<label, triFace, Hash<triFace>> vertsToBoundary(nFaces);
HashTable<label, triFace, triFace::Hash<>> vertsToBoundary(nFaces);
forAll(boundaryFaces, facei)
{
......@@ -215,12 +215,11 @@ int main(int argc, char *argv[])
// Is there any boundary face with same vertices?
// (uses commutative hash)
HashTable<label, triFace, Hash<triFace>>::iterator iter =
vertsToBoundary.find(triFace(f[0], f[1], f[2]));
auto iter = vertsToBoundary.find(triFace(f[0], f[1], f[2]));
if (iter != vertsToBoundary.end())
if (iter.found())
{
label facei = iter();
label facei = iter.object();
const triFace& tri = iter.key();
// Determine orientation of tri v.s. cell centre.
......
......@@ -41,9 +41,10 @@ SourceFiles
#include "label.H"
#include "uLabel.H"
#include "zero.H"
#include "Hash.H"
#include "contiguous.H"
#include "autoPtr.H"