author | Peter D. Barnes, Jr. <barnes26@llnl.gov> |
Tue, 13 Nov 2012 16:44:26 -0800 | |
branch | hash |
changeset 9939 | 8d4c99660585 |
parent 9936 | 51de54536ee3 |
child 9945 | 9a867fd76097 |
permissions | -rw-r--r-- |
9932 | 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright (c) 2012 Lawrence Livermore National Laboratory |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License version 2 as |
|
7 |
* published by the Free Software Foundation; |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
* |
|
18 |
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov> |
|
19 |
*/ |
|
20 |
||
21 |
#ifndef HASHFUNCTION_H |
|
22 |
#define HASHFUNCTION_H |
|
23 |
||
24 |
#include "simple-ref-count.h" |
|
25 |
||
26 |
namespace ns3 { |
|
27 |
||
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
28 |
namespace Hash { |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
29 |
|
9932 | 30 |
/** |
31 |
* \ingroup hash |
|
32 |
* |
|
33 |
* \brief Hash function implementation base class |
|
34 |
*/ |
|
35 |
class Implementation : public SimpleRefCount<Implementation> |
|
36 |
{ |
|
37 |
public: |
|
38 |
/** |
|
39 |
* Compute 32-bit hash of a byte buffer |
|
40 |
* |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
41 |
* Call clear () between calls to GetHash32() to reset the |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
42 |
* internal state and hash each buffer separately. |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
43 |
* |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
44 |
* If you don't call clear() between calls to GetHash32, |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
45 |
* you can hash successive buffers. The final return value |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
46 |
* will be the cumulative hash across all calls. |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
47 |
* |
9932 | 48 |
* \param [in] buffer pointer to the beginning of the buffer |
49 |
* \param [in] size length of the buffer, in bytes |
|
50 |
* \return 32-bit hash of the buffer |
|
51 |
*/ |
|
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
52 |
virtual uint32_t GetHash32 (const char * buffer, const size_t size) = 0; |
9932 | 53 |
/** |
54 |
* Compute 64-bit hash of a byte buffer. |
|
55 |
* |
|
56 |
* Default implementation returns 32-bit hash, with a warning. |
|
57 |
* |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
58 |
* Call clear () between calls to GetHash64() to reset the |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
59 |
* internal state and hash each buffer separately. |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
60 |
* |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
61 |
* If you don't call clear() between calls to GetHash64, |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
62 |
* you can hash successive buffers. The final return value |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
63 |
* will be the cumulative hash across all calls. |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
64 |
* |
9932 | 65 |
* \param [in] buffer pointer to the beginning of the buffer |
66 |
* \param [in] size length of the buffer, in bytes |
|
67 |
* \return 64-bit hash of the buffer |
|
68 |
*/ |
|
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
69 |
virtual uint64_t GetHash64 (const char * buffer, const size_t size); |
9932 | 70 |
/** |
71 |
* Restore initial state |
|
72 |
*/ |
|
73 |
virtual void clear (void) = 0; |
|
74 |
/** |
|
75 |
* Constructor |
|
76 |
*/ |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
77 |
Implementation () { }; |
9932 | 78 |
/** |
79 |
* Destructor |
|
80 |
*/ |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
81 |
virtual ~Implementation () { }; |
9932 | 82 |
}; // Hashfunction |
83 |
||
84 |
||
85 |
/*-------------------------------------- |
|
86 |
* Hash function implementation |
|
87 |
* by function pointers and templates |
|
88 |
*/ |
|
89 |
||
90 |
/** |
|
91 |
* |
|
92 |
* \ingroup hash |
|
93 |
* |
|
94 |
* \brief Basic hash function typedefs. |
|
95 |
* |
|
96 |
* See Hash32Implementation<> or Hash64Implementation<> |
|
97 |
*/ |
|
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
98 |
typedef uint32_t (*Hash32Function_ptr) (const char *, const size_t); |
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
99 |
typedef uint64_t (*Hash64Function_ptr) (const char *, const size_t); |
9932 | 100 |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
101 |
namespace Function { |
9932 | 102 |
|
103 |
/** |
|
104 |
* \ingroup hash |
|
105 |
* |
|
106 |
* \brief Template for Hashfunctions from 32-bit hash functions |
|
107 |
*/ |
|
108 |
class Hash32 : public Implementation |
|
109 |
{ |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
110 |
public: |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
111 |
Hash32 (Hash32Function_ptr hp) : m_fp (hp) { }; |
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
112 |
uint32_t GetHash32 (const char * buffer, const size_t size) |
9932 | 113 |
{ |
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
114 |
return (*m_fp) (buffer, size); |
9932 | 115 |
} |
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
116 |
void clear () { }; |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
117 |
private: |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
118 |
Hash32Function_ptr m_fp; |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
119 |
}; // Hash32 |
9932 | 120 |
|
121 |
/** |
|
122 |
* \ingroup hash |
|
123 |
* |
|
124 |
* \brief Template for Hashfunctions from 64-bit hash functions |
|
125 |
*/ |
|
126 |
class Hash64 : public Implementation |
|
127 |
{ |
|
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
128 |
public: |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
129 |
Hash64 (Hash64Function_ptr hp) : m_fp (hp) { }; |
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
130 |
uint64_t GetHash64 (const char * buffer, const size_t size) |
9932 | 131 |
{ |
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
132 |
return (*m_fp) (buffer, size); |
9932 | 133 |
} |
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
134 |
uint32_t GetHash32 (const char * buffer, const size_t size) |
9932 | 135 |
{ |
9936
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
136 |
uint64_t hash = GetHash64 (buffer, size); |
51de54536ee3
Use uint32/64/_t instead of Hash32/64_t
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9934
diff
changeset
|
137 |
return *(uint32_t *)(void *)(&hash); |
9932 | 138 |
} |
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
139 |
void clear () { }; |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
140 |
private: |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
141 |
Hash64Function_ptr m_fp; |
9932 | 142 |
}; // Hash64<Hash64Function_ptr> |
143 |
||
144 |
||
9934
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
145 |
} // namespace Function |
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
146 |
|
0a0b86bc36c3
Incremental hashing, fix hash function ptr, additional test cases, check-style.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9932
diff
changeset
|
147 |
} // namespace Hash |
9932 | 148 |
|
149 |
} // namespace ns3 |
|
150 |
||
151 |
#endif /* HASHFUNCTION_H */ |
|
152 |