1 /*
2     SuperCollider real time audio synthesis system
3     Copyright (c) 2002 James McCartney. All rights reserved.
4     http://www.audiosynth.com
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 
21 
22 #pragma once
23 
24 #include "SC_Types.h"
25 #include "SC_Endian.h"
26 
27 // These hash functions are among the best there are in terms of both speed and quality.
28 // A good hash function makes a lot of difference.
29 // I have not used Bob Jenkins own hash function because the keys I use are relatively short.
30 
31 
32 // hash function for a string
Hash(const char * inKey)33 inline int32 Hash(const char* inKey) {
34     // the one-at-a-time hash.
35     // a very good hash function. ref: a web page by Bob Jenkins.
36     // http://www.burtleburtle.net/bob/hash/doobs.html
37     int32 hash = 0;
38     while (*inKey) {
39         hash += *inKey++;
40         hash += hash << 10;
41         hash ^= hash >> 6;
42     }
43     hash += hash << 3;
44     hash ^= hash >> 11;
45     hash += hash << 15;
46     return hash;
47 }
48 
49 // hash function for a string that also returns the length
Hash(const char * inKey,size_t * outLength)50 inline int32 Hash(const char* inKey, size_t* outLength) {
51     // the one-at-a-time hash.
52     // a very good hash function. ref: a web page by Bob Jenkins.
53     const char* origKey = inKey;
54     int32 hash = 0;
55     while (*inKey) {
56         hash += *inKey++;
57         hash += hash << 10;
58         hash ^= hash >> 6;
59     }
60     hash += hash << 3;
61     hash ^= hash >> 11;
62     hash += hash << 15;
63     *outLength = (size_t)(inKey - origKey);
64     return hash;
65 }
66 
67 // hash function for an array of char
Hash(const char * inKey,int32 inLength)68 inline int32 Hash(const char* inKey, int32 inLength) {
69     // the one-at-a-time hash.
70     // a very good hash function. ref: a web page by Bob Jenkins.
71     int32 hash = 0;
72     for (int i = 0; i < inLength; ++i) {
73         hash += *inKey++;
74         hash += hash << 10;
75         hash ^= hash >> 6;
76     }
77     hash += hash << 3;
78     hash ^= hash >> 11;
79     hash += hash << 15;
80     return hash;
81 }
82 
83 // hash function for integers
Hash(int32 inKey)84 inline int32 Hash(int32 inKey) {
85     // Thomas Wang's integer hash.
86     // http://www.concentric.net/~Ttwang/tech/inthash.htm
87     // a faster hash for integers. also very good.
88     uint32 hash = (uint32)inKey;
89     hash += ~(hash << 15);
90     hash ^= hash >> 10;
91     hash += hash << 3;
92     hash ^= hash >> 6;
93     hash += ~(hash << 11);
94     hash ^= hash >> 16;
95     return (int32)hash;
96 }
97 
Hash64(int64 inKey)98 inline int64 Hash64(int64 inKey) {
99     // Thomas Wang's 64 bit integer hash.
100     uint64 hash = (uint64)inKey;
101     hash += ~(hash << 32);
102     hash ^= (hash >> 22);
103     hash += ~(hash << 13);
104     hash ^= (hash >> 8);
105     hash += (hash << 3);
106     hash ^= (hash >> 15);
107     hash += ~(hash << 27);
108     hash ^= (hash >> 31);
109     return (int64)hash;
110 }
111 
Hash(const int32 * inKey,int32 inLength)112 inline int32 Hash(const int32* inKey, int32 inLength) {
113     // one-at-a-time hashing of a string of int32's.
114     // uses Thomas Wang's integer hash for the combining step.
115     int32 hash = 0;
116     for (int i = 0; i < inLength; ++i) {
117         hash = Hash(hash + *inKey++);
118     }
119     return hash;
120 }
121 
122 #if BYTE_ORDER == LITTLE_ENDIAN
123 const int32 kLASTCHAR = (int32)0xFF000000;
124 #else
125 const int32 kLASTCHAR = (int32)0x000000FF;
126 #endif
127 
Hash(const int32 * inKey)128 inline int32 Hash(const int32* inKey) {
129     // hashing of a string of int32's.
130     // uses Thomas Wang's integer hash for the combining step.
131     int32 hash = 0;
132     int32 c;
133     do {
134         c = *inKey++;
135         hash = Hash(hash + c);
136     } while (c & kLASTCHAR);
137     return hash;
138 }
139