Test-Hashing.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  testHashing
26 
27 Description
28 
29 \*---------------------------------------------------------------------------*/
30 
31 #include "IOstreams.H"
32 #include "IOobject.H"
33 #include "IFstream.H"
34 
35 #include "stringList.H"
36 #include "labelList.H"
37 #include "labelPair.H"
38 #include "edgeList.H"
39 #include "triFaceList.H"
40 
41 #include "Hash.H"
42 
43 using namespace Foam;
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 // Main program:
47 
48 int main(int argc, char *argv[])
49 {
50  IFstream is("hashingTests");
51 
52 
53  while (is.good())
54  {
55  const word listType(is);
56 
57  Info<< endl;
58  IOobject::writeDivider(Info) << listType << endl;
59 
60  if (listType == "stringList")
61  {
62  Info<< "contiguous = " << contiguous<string>() << endl << endl;
63 
64  stringList lst(is);
65 
66  forAll(lst, i)
67  {
68  unsigned hash1 = string::hash()(lst[i]);
69 
70  Info<< hex << hash1 << ": " << lst[i] << endl;
71  }
72 
73  }
74  else if (listType == "labelList")
75  {
76  Info<<"contiguous = " << contiguous<label>() << endl << endl;
77 
78  labelList lst(is);
79 
80  forAll(lst, i)
81  {
82  // direct value
83  unsigned hash1 = Hash<label>()(lst[i]);
84 
85  // hashed byte-wise
86  unsigned hash2 = Hash<label>()(lst[i], 0);
87 
88  Info<< hex << hash1
89  << " (seeded: " << hash2 << ")"
90  << ": " << dec << lst[i] << endl;
91  }
92 
93  if (contiguous<label>())
94  {
95  unsigned hash3 = Hasher
96  (
97  lst.cdata(),
98  lst.size() * sizeof(label)
99  );
100 
101  Info<<"contiguous hashed value " << hex << hash3 << endl;
102  }
103  }
104  else if (listType == "labelListList")
105  {
106  List< List<label> > lst(is);
107 
108  forAll(lst, i)
109  {
110  unsigned hash1 = Hasher
111  (
112  lst[i].cdata(),
113  lst[i].size() * sizeof(label)
114  );
115 
116  Info<< hex << hash1
117  << ": " << dec << lst[i] << endl;
118  }
119 
120  }
121  else if (listType == "edgeList")
122  {
123  Info<<"contiguous = " << contiguous<edge>() << endl << endl;
124 
125  edgeList lst(is);
126 
127  forAll(lst, i)
128  {
129  unsigned hash1 = Hash<edge>()(lst[i]);
130 
131  // as FixedList
132  unsigned hash2 = labelPair::Hash<>()(lst[i]);
133 
134  Info<< hex << hash1 << " (as FixedList: " << hash2
135  << "): " << dec << lst[i] << endl;
136  }
137  }
138  else if (listType == "triFaceList")
139  {
140  Info<<"contiguous = " << contiguous<triFace>() << endl << endl;
141 
142  triFaceList lst(is);
143 
144  forAll(lst, i)
145  {
146  // direct value
147  unsigned hash1 = Hash<triFace>()(lst[i]);
148  unsigned hash2 = FixedList<label, 3>::Hash<>()(lst[i]);
149 
150  Info<< hex << hash1 << " (as FixedList: " << hash2
151  << "): " << dec << lst[i] << endl;
152  }
153  }
154  else
155  {
156  Info<< "unknown type: " << listType << endl;
157  }
158 
159  }
160 
161  return 0;
162 }
163 
164 
165 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::Hasher
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
main
int main(int argc, char *argv[])
Definition: Test-Hashing.C:45
Foam::contiguous< triFace >
bool contiguous< triFace >()
Definition: triFace.H:259
Foam::contiguous< edge >
bool contiguous< edge >()
Definition: edge.H:170
Foam::Hash
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:56
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
labelList.H
Foam::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:558
Foam::Info
messageStream Info
IFstream.H
IOobject.H
edgeList.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
Foam::string::hash
Hashing function class, shared by all the derived classes.
Definition: string.H:90
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::FixedList
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
Hash.H
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
triFaceList.H
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
stringList.H
labelPair.H