2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
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.
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.
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
26 #include "SC_Endian.h"
28 // These hash functions are among the best there are in terms of both speed and quality.
29 // A good hash function makes a lot of difference.
30 // I have not used Bob Jenkins own hash function because the keys I use are relatively short.
33 // hash function for a string
34 inline int32
Hash(const char *inKey
)
36 // the one-at-a-time hash.
37 // a very good hash function. ref: a web page by Bob Jenkins.
38 // http://www.burtleburtle.net/bob/hash/doobs.html
51 // hash function for a string that also returns the length
52 inline int32
Hash(const char *inKey
, int32
*outLength
)
54 // the one-at-a-time hash.
55 // a very good hash function. ref: a web page by Bob Jenkins.
56 const char *origKey
= inKey
;
66 *outLength
= inKey
- origKey
;
70 // hash function for an array of char
71 inline int32
Hash(const char *inKey
, int32 inLength
)
73 // the one-at-a-time hash.
74 // a very good hash function. ref: a web page by Bob Jenkins.
76 for (int i
=0; i
<inLength
; ++i
) {
87 // hash function for integers
88 inline int32
Hash(int32 inKey
)
90 // Thomas Wang's integer hash.
91 // http://www.concentric.net/~Ttwang/tech/inthash.htm
92 // a faster hash for integers. also very good.
93 uint32 hash
= (uint32
)inKey
;
94 hash
+= ~(hash
<< 15);
98 hash
+= ~(hash
<< 11);
103 inline int64
Hash64(int64 inKey
)
105 // Thomas Wang's 64 bit integer hash.
106 uint64 hash
= (uint64
)inKey
;
107 hash
+= ~(hash
<< 32);
108 hash
^= (hash
>> 22);
109 hash
+= ~(hash
<< 13);
112 hash
^= (hash
>> 15);
113 hash
+= ~(hash
<< 27);
114 hash
^= (hash
>> 31);
118 inline int32
Hash(const int32
*inKey
, int32 inLength
)
120 // one-at-a-time hashing of a string of int32's.
121 // uses Thomas Wang's integer hash for the combining step.
123 for (int i
=0; i
<inLength
; ++i
) {
124 hash
= Hash(hash
+ *inKey
++);
131 #if BYTE_ORDER == LITTLE_ENDIAN
132 const int32 kLASTCHAR
= 0xFF000000;
134 const int32 kLASTCHAR
= 0x000000FF;
138 inline int32
Hash(const int32
*inKey
)
140 // hashing of a string of int32's.
141 // uses Thomas Wang's integer hash for the combining step.
146 hash
= Hash(hash
+ c
);
147 } while (c
& kLASTCHAR
);