Permalink
Browse files

add .hash32 method

  • Loading branch information...
1 parent 87d290b commit f5d0b63e8c43603655a4f83dd8b28e58ec022e4a @nashby committed Oct 25, 2012
Showing with 17 additions and 0 deletions.
  1. +1 −0 CHANGELOG.md
  2. +1 −0 README.md
  3. +7 −0 ext/cityhash/cityhash.cc
  4. +4 −0 lib/cityhash.rb
  5. +4 −0 test/cityhash_test.rb
View
@@ -1,4 +1,5 @@
## dev ##
### enhancements
+ * add CityHash.hash32 method
* update libcityhash to 1.1. See [NEWS](http://code.google.com/p/cityhash/source/browse/trunk/NEWS)
View
@@ -15,6 +15,7 @@ text = "test"
seed1 = 12345
seed2 = 54321
+CityHash.hash32(text) # => 1633095781
CityHash.hash64(text) # => 8581389452482819506
CityHash.hash64(text, seed1) # => 9154302171269876511
CityHash.hash64(text, seed1, seed2) # => 4854399283587686019
@@ -5,6 +5,11 @@
// calling rb_define_method()
typedef VALUE (ruby_method)(...);
+extern "C" VALUE cityhash_hash32(VALUE mod, VALUE input)
+{
+ return ULL2NUM(CityHash32(StringValuePtr(input), RSTRING_LEN(input)));
+}
+
extern "C" VALUE cityhash_hash64(VALUE mod, VALUE input)
{
return ULL2NUM(CityHash64(StringValuePtr(input), RSTRING_LEN(input)));
@@ -38,6 +43,8 @@ extern "C" void Init_cityhash()
VALUE mCityHash = rb_define_module("CityHash");
VALUE mInternal = rb_define_module_under(mCityHash, "Internal");
+ rb_define_singleton_method(mInternal, "hash32", (ruby_method*) &cityhash_hash32, 1);
+
rb_define_singleton_method(mInternal, "hash64", (ruby_method*) &cityhash_hash64, 1);
rb_define_singleton_method(mInternal, "hash64_with_seed", (ruby_method*) &cityhash_hash64_with_seed, 2);
rb_define_singleton_method(mInternal, "hash64_with_seeds", (ruby_method*) &cityhash_hash64_with_seeds, 3);
View
@@ -5,6 +5,10 @@ module CityHash
LOW64_MASK = 0x0000000000000000ffffffffffffffff
HIGH64_MASK = 0xffffffffffffffff0000000000000000
+ def self.hash32(input)
+ Internal.hash32(input)
+ end
+
def self.hash64(input, seed1=nil, seed2=nil)
return Internal.hash64(input) if seed1.nil?
return Internal.hash64_with_seed(input, seed1.to_i) if seed2.nil?
@@ -1,6 +1,10 @@
require 'test_helper'
describe CityHash do
+ it 'returns 32bit hash' do
+ assert_equal 1633095781, CityHash.hash32("test")
+ end
+
it 'returns 64bit hash' do
assert_equal 8581389452482819506, CityHash.hash64("test")
end

0 comments on commit f5d0b63

Please sign in to comment.