2
* Copyright 2006-2008 The FLWOR Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
28
///////////////////////////////////////////////////////////////////////////////
30
size_t hash_bytes( void const *p, size_t len, size_t result ) {
31
unsigned char const *c = reinterpret_cast<unsigned char const*>( p );
32
unsigned char const *const end = c + len;
33
#ifdef ZORBA_HASH_FN_FNV_1a
35
// FNV-1a (Fowler/Noll/Vo) hashing algorithm.
41
#endif /* ZORBA_HASH_FN_FNV_1a */
42
#ifdef ZORBA_HASH_FN_PJW
44
// An adaptation of Peter J. Weinberger's (PJW) generic hashing algorithm
45
// based on Allen Holub's version. This version works for any hash code
46
// size (in this case, size_t) and not only 'unsigned' as in PJW's version.
48
// The original PJW algorithm can be found in:
50
// Fig. 7.35: Hash function hashpjw, written in C
51
// "Compilers: Principles, Techniques, and Tools," Section 7.6: "Symbol
52
// Tables," Alfred Aho, Ravi Sethi, and Jeffrey D. Ullman, Addison-Wesley,
53
// Reading, MA, 1986, pp. 435-436.
55
static size_t const BitsInSizeT = sizeof( size_t ) *
56
numeric_limits<unsigned char>::digits;
57
static size_t const ThreeFourths = BitsInSizeT * 3 / 4;
58
static size_t const OneEighth = BitsInSizeT / 8;
59
static size_t const HighBits = ~( (size_t)(~0ul) >> OneEighth );
62
result = (result << OneEighth) + *c++;
63
if ( size_t temp = result & HighBits )
64
result = (result ^ (temp >> ThreeFourths)) & ~HighBits;
66
#endif /* ZORBA_HASH_FN_PJW */
70
///////////////////////////////////////////////////////////////////////////////
75
/* vim:set et ts=2 sw=2: */