cityhash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "ffi"
4
+
5
+ group :development do
6
+ gem "shoulda", ">= 0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.5.2"
9
+ gem "rcov", ">= 0"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ffi (1.0.7)
5
+ rake (>= 0.8.7)
6
+ git (1.2.5)
7
+ jeweler (1.5.2)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ shoulda (2.11.3)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler (~> 1.0.0)
20
+ ffi
21
+ jeweler (~> 1.5.2)
22
+ rcov
23
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 nashby
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = cityhash
2
+
3
+ Ruby wrapper for google's cityhash.
4
+
5
+ == Usage
6
+
7
+ require 'cityhash'
8
+
9
+ CityHash.city_hash64("test", 4) # => 17703940110308125106
10
+ CityHash.city_hash64_with_seed("test", 4, 12345) # => 14900027982776226655
11
+ CityHash.city_hash64_with_seeds("test", 4, 12345, 54321) # => 11136353178704814373
12
+
13
+ == Contributing to cityhash
14
+
15
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
16
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
17
+ * Fork the project
18
+ * Start a feature/bugfix branch
19
+ * Commit and push until you are happy with your contribution
20
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
21
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2011 nashby. See LICENSE.txt for
26
+ further details.
27
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ $LOAD_PATH.unshift('lib')
13
+
14
+ require 'jeweler'
15
+ require 'cityhash/version'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "cityhash"
19
+ gem.homepage = "http://github.com/nashby/cityhash"
20
+ gem.license = "MIT"
21
+ gem.version = CityHash::Version::STRING
22
+ gem.summary = "ffi wrapper for google's cityhash"
23
+ gem.description = "ffi wrapper for google's cityhash"
24
+ gem.email = "younash@gmail.com"
25
+ gem.authors = ["nashby"]
26
+ gem.add_runtime_dependency 'ffi'
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "cityhash #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/lib/cityhash.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'ffi'
2
+
3
+ module CityHash
4
+ extend FFI::Library
5
+
6
+ ffi_lib "lib/cityhash/ext/libcity.so"
7
+ attach_function :city_hash64, :CityHash64, [:string, :size_t], :uint64
8
+ attach_function :city_hash64_with_seed, :CityHash64WithSeed, [:string, :size_t, :uint64], :uint64
9
+ attach_function :city_hash64_with_seeds, :CityHash64WithSeeds, [:string, :size_t, :uint64, :uint64], :uint64
10
+ attach_function :city_hash128, :CityHash128, [:string, :size_t], :ulong
11
+ attach_function :city_hash128_with_seed, :CityHash128WithSeed, [:string, :size_t, :ulong], :ulong
12
+ end
Binary file
@@ -0,0 +1,2 @@
1
+ all:
2
+ g++ -shared city.cc -o ../libcity.so
@@ -0,0 +1,307 @@
1
+ // Copyright (c) 2011 Google, Inc.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+ //
21
+ // CityHash Version 1, by Geoff Pike and Jyrki Alakuijala
22
+ //
23
+ // This file provides CityHash64() and related functions.
24
+ //
25
+ // It's probably possible to create even faster hash functions by
26
+ // writing a program that systematically explores some of the space of
27
+ // possible hash functions, by using SIMD instructions, or by
28
+ // compromising on hash quality.
29
+
30
+ #include "city.h"
31
+
32
+ #include <algorithm>
33
+
34
+ using namespace std;
35
+
36
+ #define UNALIGNED_LOAD64(p) (*(const uint64*)(p))
37
+ #define UNALIGNED_LOAD32(p) (*(const uint32*)(p))
38
+
39
+ #if !defined(LIKELY)
40
+ #if defined(__GNUC__)
41
+ #define LIKELY(x) (__builtin_expect(!!(x), 1))
42
+ #else
43
+ #define LIKELY(x) (x)
44
+ #endif
45
+ #endif
46
+
47
+ // Some primes between 2^63 and 2^64 for various uses.
48
+ static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
49
+ static const uint64 k1 = 0xb492b66fbe98f273ULL;
50
+ static const uint64 k2 = 0x9ae16a3b2f90404fULL;
51
+ static const uint64 k3 = 0xc949d7c7509e6557ULL;
52
+
53
+ // Bitwise right rotate. Normally this will compile to a single
54
+ // instruction, especially if the shift is a manifest constant.
55
+ static uint64 Rotate(uint64 val, int shift) {
56
+ // Avoid shifting by 64: doing so yields an undefined result.
57
+ return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
58
+ }
59
+
60
+ // Equivalent to Rotate(), but requires the second arg to be non-zero.
61
+ // On x86-64, and probably others, it's possible for this to compile
62
+ // to a single instruction if both args are already in registers.
63
+ static uint64 RotateByAtLeast1(uint64 val, int shift) {
64
+ return (val >> shift) | (val << (64 - shift));
65
+ }
66
+
67
+ static uint64 ShiftMix(uint64 val) {
68
+ return val ^ (val >> 47);
69
+ }
70
+
71
+ static uint64 HashLen16(uint64 u, uint64 v) {
72
+ return Hash128to64(uint128(u, v));
73
+ }
74
+
75
+ static uint64 HashLen0to16(const char *s, size_t len) {
76
+ if (len > 8) {
77
+ uint64 a = UNALIGNED_LOAD64(s);
78
+ uint64 b = UNALIGNED_LOAD64(s + len - 8);
79
+ return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b;
80
+ }
81
+ if (len >= 4) {
82
+ uint64 a = UNALIGNED_LOAD32(s);
83
+ return HashLen16(len + (a << 3), UNALIGNED_LOAD32(s + len - 4));
84
+ }
85
+ if (len > 0) {
86
+ uint8 a = s[0];
87
+ uint8 b = s[len >> 1];
88
+ uint8 c = s[len - 1];
89
+ uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
90
+ uint32 z = len + (static_cast<uint32>(c) << 2);
91
+ return ShiftMix(y * k2 ^ z * k3) * k2;
92
+ }
93
+ return k2;
94
+ }
95
+
96
+ // This probably works well for 16-byte strings as well, but it may be overkill
97
+ // in that case.
98
+ static uint64 HashLen17to32(const char *s, size_t len) {
99
+ uint64 a = UNALIGNED_LOAD64(s) * k1;
100
+ uint64 b = UNALIGNED_LOAD64(s + 8);
101
+ uint64 c = UNALIGNED_LOAD64(s + len - 8) * k2;
102
+ uint64 d = UNALIGNED_LOAD64(s + len - 16) * k0;
103
+ return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d,
104
+ a + Rotate(b ^ k3, 20) - c + len);
105
+ }
106
+
107
+ // Return a 16-byte hash for 48 bytes. Quick and dirty.
108
+ // Callers do best to use "random-looking" values for a and b.
109
+ static pair<uint64, uint64> WeakHashLen32WithSeeds(
110
+ uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) {
111
+ a += w;
112
+ b = Rotate(b + a + z, 21);
113
+ uint64 c = a;
114
+ a += x;
115
+ a += y;
116
+ b += Rotate(a, 44);
117
+ return make_pair(a + z, b + c);
118
+ }
119
+
120
+ // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
121
+ static pair<uint64, uint64> WeakHashLen32WithSeeds(
122
+ const char* s, uint64 a, uint64 b) {
123
+ return WeakHashLen32WithSeeds(UNALIGNED_LOAD64(s),
124
+ UNALIGNED_LOAD64(s + 8),
125
+ UNALIGNED_LOAD64(s + 16),
126
+ UNALIGNED_LOAD64(s + 24),
127
+ a,
128
+ b);
129
+ }
130
+
131
+ // Return an 8-byte hash for 33 to 64 bytes.
132
+ static uint64 HashLen33to64(const char *s, size_t len) {
133
+ uint64 z = UNALIGNED_LOAD64(s + 24);
134
+ uint64 a = UNALIGNED_LOAD64(s) + (len + UNALIGNED_LOAD64(s + len - 16)) * k0;
135
+ uint64 b = Rotate(a + z, 52);
136
+ uint64 c = Rotate(a, 37);
137
+ a += UNALIGNED_LOAD64(s + 8);
138
+ c += Rotate(a, 7);
139
+ a += UNALIGNED_LOAD64(s + 16);
140
+ uint64 vf = a + z;
141
+ uint64 vs = b + Rotate(a, 31) + c;
142
+ a = UNALIGNED_LOAD64(s + 16) + UNALIGNED_LOAD64(s + len - 32);
143
+ z = UNALIGNED_LOAD64(s + len - 8);
144
+ b = Rotate(a + z, 52);
145
+ c = Rotate(a, 37);
146
+ a += UNALIGNED_LOAD64(s + len - 24);
147
+ c += Rotate(a, 7);
148
+ a += UNALIGNED_LOAD64(s + len - 16);
149
+ uint64 wf = a + z;
150
+ uint64 ws = b + Rotate(a, 31) + c;
151
+ uint64 r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0);
152
+ return ShiftMix(r * k0 + vs) * k2;
153
+ }
154
+
155
+ uint64 CityHash64(const char *s, size_t len) {
156
+ if (len <= 32) {
157
+ if (len <= 16) {
158
+ return HashLen0to16(s, len);
159
+ } else {
160
+ return HashLen17to32(s, len);
161
+ }
162
+ } else if (len <= 64) {
163
+ return HashLen33to64(s, len);
164
+ }
165
+
166
+ // For strings over 64 bytes we hash the end first, and then as we
167
+ // loop we keep 56 bytes of state: v, w, x, y, and z.
168
+ uint64 x = UNALIGNED_LOAD64(s);
169
+ uint64 y = UNALIGNED_LOAD64(s + len - 16) ^ k1;
170
+ uint64 z = UNALIGNED_LOAD64(s + len - 56) ^ k0;
171
+ pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, y);
172
+ pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, len * k1, k0);
173
+ z += ShiftMix(v.second) * k1;
174
+ x = Rotate(z + x, 39) * k1;
175
+ y = Rotate(y, 33) * k1;
176
+
177
+ // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
178
+ len = (len - 1) & ~static_cast<size_t>(63);
179
+ do {
180
+ x = Rotate(x + y + v.first + UNALIGNED_LOAD64(s + 16), 37) * k1;
181
+ y = Rotate(y + v.second + UNALIGNED_LOAD64(s + 48), 42) * k1;
182
+ x ^= w.second;
183
+ y ^= v.first;
184
+ z = Rotate(z ^ w.first, 33);
185
+ v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
186
+ w = WeakHashLen32WithSeeds(s + 32, z + w.second, y);
187
+ std::swap(z, x);
188
+ s += 64;
189
+ len -= 64;
190
+ } while (len != 0);
191
+ return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
192
+ HashLen16(v.second, w.second) + x);
193
+ }
194
+
195
+ uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) {
196
+ return CityHash64WithSeeds(s, len, k2, seed);
197
+ }
198
+
199
+ uint64 CityHash64WithSeeds(const char *s, size_t len,
200
+ uint64 seed0, uint64 seed1) {
201
+ return HashLen16(CityHash64(s, len) - seed0, seed1);
202
+ }
203
+
204
+ // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
205
+ // of any length representable in ssize_t. Based on City and Murmur.
206
+ static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
207
+ uint64 a = Uint128Low64(seed);
208
+ uint64 b = Uint128High64(seed);
209
+ uint64 c = 0;
210
+ uint64 d = 0;
211
+ ssize_t l = len - 16;
212
+ if (l <= 0) { // len <= 16
213
+ c = b * k1 + HashLen0to16(s, len);
214
+ d = Rotate(a + (len >= 8 ? UNALIGNED_LOAD64(s) : c), 32);
215
+ } else { // len > 16
216
+ c = HashLen16(UNALIGNED_LOAD64(s + len - 8) + k1, a);
217
+ d = HashLen16(b + len, c + UNALIGNED_LOAD64(s + len - 16));
218
+ a += d;
219
+ do {
220
+ a ^= ShiftMix(UNALIGNED_LOAD64(s) * k1) * k1;
221
+ a *= k1;
222
+ b ^= a;
223
+ c ^= ShiftMix(UNALIGNED_LOAD64(s + 8) * k1) * k1;
224
+ c *= k1;
225
+ d ^= c;
226
+ s += 16;
227
+ l -= 16;
228
+ } while (l > 0);
229
+ }
230
+ a = HashLen16(a, c);
231
+ b = HashLen16(d, b);
232
+ return uint128(a ^ b, HashLen16(b, a));
233
+ }
234
+
235
+ uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
236
+ if (len < 128) {
237
+ return CityMurmur(s, len, seed);
238
+ }
239
+
240
+ // We expect len >= 128 to be the common case. Keep 56 bytes of state:
241
+ // v, w, x, y, and z.
242
+ pair<uint64, uint64> v, w;
243
+ uint64 x = Uint128Low64(seed);
244
+ uint64 y = Uint128High64(seed);
245
+ uint64 z = len * k1;
246
+ v.first = Rotate(y ^ k1, 49) * k1 + UNALIGNED_LOAD64(s);
247
+ v.second = Rotate(v.first, 42) * k1 + UNALIGNED_LOAD64(s + 8);
248
+ w.first = Rotate(y + z, 35) * k1 + x;
249
+ w.second = Rotate(x + UNALIGNED_LOAD64(s + 88), 53) * k1;
250
+
251
+ // This is the same inner loop as CityHash64(), manually unrolled.
252
+ do {
253
+ x = Rotate(x + y + v.first + UNALIGNED_LOAD64(s + 16), 37) * k1;
254
+ y = Rotate(y + v.second + UNALIGNED_LOAD64(s + 48), 42) * k1;
255
+ x ^= w.second;
256
+ y ^= v.first;
257
+ z = Rotate(z ^ w.first, 33);
258
+ v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
259
+ w = WeakHashLen32WithSeeds(s + 32, z + w.second, y);
260
+ std::swap(z, x);
261
+ s += 64;
262
+ x = Rotate(x + y + v.first + UNALIGNED_LOAD64(s + 16), 37) * k1;
263
+ y = Rotate(y + v.second + UNALIGNED_LOAD64(s + 48), 42) * k1;
264
+ x ^= w.second;
265
+ y ^= v.first;
266
+ z = Rotate(z ^ w.first, 33);
267
+ v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
268
+ w = WeakHashLen32WithSeeds(s + 32, z + w.second, y);
269
+ std::swap(z, x);
270
+ s += 64;
271
+ len -= 128;
272
+ } while (LIKELY(len >= 128));
273
+ y += Rotate(w.first, 37) * k0 + z;
274
+ x += Rotate(v.first + z, 49) * k0;
275
+ // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
276
+ for (size_t tail_done = 0; tail_done < len; ) {
277
+ tail_done += 32;
278
+ y = Rotate(y - x, 42) * k0 + v.second;
279
+ w.first += UNALIGNED_LOAD64(s + len - tail_done + 16);
280
+ x = Rotate(x, 49) * k0 + w.first;
281
+ w.first += v.first;
282
+ v = WeakHashLen32WithSeeds(s + len - tail_done, v.first, v.second);
283
+ }
284
+ // At this point our 48 bytes of state should contain more than
285
+ // enough information for a strong 128-bit hash. We use two
286
+ // different 48-byte-to-8-byte hashes to get a 16-byte final result.
287
+ x = HashLen16(x, v.first);
288
+ y = HashLen16(y, w.first);
289
+ return uint128(HashLen16(x + v.second, w.second) + y,
290
+ HashLen16(x + w.second, y + v.second));
291
+ }
292
+
293
+ uint128 CityHash128(const char *s, size_t len) {
294
+ if (len >= 16) {
295
+ return CityHash128WithSeed(s + 16,
296
+ len - 16,
297
+ uint128(UNALIGNED_LOAD64(s) ^ k3,
298
+ UNALIGNED_LOAD64(s + 8)));
299
+ } else if (len >= 8) {
300
+ return CityHash128WithSeed(NULL,
301
+ 0,
302
+ uint128(UNALIGNED_LOAD64(s) ^ (len * k0),
303
+ UNALIGNED_LOAD64(s + len - 8) ^ k1));
304
+ } else {
305
+ return CityHash128WithSeed(s, len, uint128(k0, k1));
306
+ }
307
+ }
@@ -0,0 +1,90 @@
1
+ // Copyright (c) 2011 Google, Inc.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+ //
21
+ // CityHash Version 1, by Geoff Pike and Jyrki Alakuijala
22
+ //
23
+ // This file provides a few functions for hashing strings. On x86-64
24
+ // hardware in 2011, CityHash64() is faster than other high-quality
25
+ // hash functions, such as Murmur. This is largely due to higher
26
+ // instruction-level parallelism. CityHash64() and CityHash128() also perform
27
+ // well on hash-quality tests.
28
+ //
29
+ // CityHash128() is optimized for relatively long strings and returns
30
+ // a 128-bit hash. For strings more than about 2000 bytes it can be
31
+ // faster than CityHash64().
32
+ //
33
+ // Functions in the CityHash family are not suitable for cryptography.
34
+ //
35
+ // WARNING: This code has not been tested on big-endian platforms!
36
+ // It is known to work well on little-endian platforms that have a small penalty
37
+ // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
38
+ //
39
+ // By the way, for some hash functions, given strings a and b, the hash
40
+ // of a+b is easily derived from the hashes of a and b. This property
41
+ // doesn't hold for any hash functions in this file.
42
+
43
+ #ifndef CITY_HASH_H_
44
+ #define CITY_HASH_H_
45
+
46
+ #include <stdlib.h> // for size_t.
47
+ #include <stdint.h>
48
+ #include <utility>
49
+
50
+ typedef uint8_t uint8;
51
+ typedef uint32_t uint32;
52
+ typedef uint64_t uint64;
53
+ typedef std::pair<uint64, uint64> uint128;
54
+
55
+ inline uint64 Uint128Low64(const uint128& x) { return x.first; }
56
+ inline uint64 Uint128High64(const uint128& x) { return x.second; }
57
+
58
+ // Hash function for a byte array.
59
+ extern "C" uint64 CityHash64(const char *buf, size_t len);
60
+
61
+ // Hash function for a byte array. For convenience, a 64-bit seed is also
62
+ // hashed into the result.
63
+ extern "C" uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed);
64
+
65
+ // Hash function for a byte array. For convenience, two seeds are also
66
+ // hashed into the result.
67
+ extern "C" uint64 CityHash64WithSeeds(const char *buf, size_t len,
68
+ uint64 seed0, uint64 seed1);
69
+
70
+ // Hash function for a byte array.
71
+ extern "C" uint128 CityHash128(const char *s, size_t len);
72
+
73
+ // Hash function for a byte array. For convenience, a 128-bit seed is also
74
+ // hashed into the result.
75
+ extern "C" uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed);
76
+
77
+ // Hash 128 input bits down to 64 bits of output.
78
+ // This is intended to be a reasonably good hash function.
79
+ inline uint64 Hash128to64(const uint128& x) {
80
+ // Murmur-inspired hashing.
81
+ const uint64 kMul = 0x9ddfea08eb382d69ULL;
82
+ uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
83
+ a ^= (a >> 47);
84
+ uint64 b = (Uint128High64(x) ^ a) * kMul;
85
+ b ^= (b >> 47);
86
+ b *= kMul;
87
+ return b;
88
+ }
89
+
90
+ #endif // CITY_HASH_H_
@@ -0,0 +1,12 @@
1
+ module CityHash
2
+
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 0
7
+ BUILD = nil
8
+
9
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
10
+ end
11
+
12
+ end
data/lib/libcity.so ADDED
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'cityhash'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class TestCityhash < Test::Unit::TestCase
4
+ should "return 64bit hash" do
5
+ assert_equal 17703940110308125106, CityHash.city_hash64("test", 4)
6
+ end
7
+
8
+ should "return 64bit hash with a seed" do
9
+ assert_equal 14900027982776226655, CityHash.city_hash64_with_seed("test", 4, 12345)
10
+ end
11
+
12
+ should "return 64bit hash with seeds" do
13
+ assert_equal 11136353178704814373, CityHash.city_hash64_with_seeds("test", 4, 12345, 54321)
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cityhash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - nashby
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-22 00:00:00 +03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: ffi
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: shoulda
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.5.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rcov
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: ffi
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ description: ffi wrapper for google's cityhash
83
+ email: younash@gmail.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ files:
92
+ - .document
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - lib/cityhash.rb
99
+ - lib/cityhash/ext/libcity.so
100
+ - lib/cityhash/ext/src/Makefile
101
+ - lib/cityhash/ext/src/city.cc
102
+ - lib/cityhash/ext/src/city.h
103
+ - lib/cityhash/version.rb
104
+ - lib/libcity.so
105
+ - test/helper.rb
106
+ - test/test_cityhash.rb
107
+ has_rdoc: true
108
+ homepage: http://github.com/nashby/cityhash
109
+ licenses:
110
+ - MIT
111
+ post_install_message:
112
+ rdoc_options: []
113
+
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 133207375
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.6.2
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: ffi wrapper for google's cityhash
138
+ test_files:
139
+ - test/helper.rb
140
+ - test/test_cityhash.rb