cityhash 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -9,12 +9,16 @@ Ruby wrapper for google's cityhash.
9
9
  ### Usage
10
10
 
11
11
  require 'cityhash'
12
-
13
- CityHash.hash64("test") # => 17703940110308125106
14
- CityHash.hash64("test", 12345) # => 14900027982776226655
15
- CityHash.hash64("test", 12345, 54321) # => 11136353178704814373
16
- CityHash.hash128("test") # => 130554790001792308794529643941127319555
17
- CityHash.hash128("test", [123,123]) # => 183946415266487905135364192266033899899
12
+
13
+ text = "test"
14
+ seed1 = 12345
15
+ seed2 = 54321
16
+
17
+ CityHash.hash64(text) # => 17703940110308125106
18
+ CityHash.hash64(text, seed1) # => 14900027982776226655
19
+ CityHash.hash64(text, seed1, seed2) # => 11136353178704814373
20
+ CityHash.hash128(text) # => 130554790001792308794529643941127319555
21
+ CityHash.hash128(text, seed1) # => 1238187875708097619810923284135194760226
18
22
 
19
23
  ### Contributing to cityhash
20
24
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cityhash}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["nashby"]
@@ -1,6 +1,10 @@
1
1
  require 'ffi'
2
2
 
3
3
  module CityHash
4
+
5
+ LOW64_MASK = 0x0000000000000000ffffffffffffffff
6
+ HIGH64_MASK = 0xffffffffffffffff0000000000000000
7
+
4
8
  module Internal
5
9
  extend FFI::Library
6
10
 
@@ -16,40 +20,34 @@ module CityHash
16
20
  input_str = input.to_s
17
21
 
18
22
  # Ruby 1.8 compatibility
19
- len = 0
20
23
  if input_str.respond_to?(:bytesize)
21
24
  len = input_str.bytesize
22
25
  else
23
26
  len = input_str.size
24
27
  end
25
28
 
26
- if seed1.nil?
27
- return CityHash::Internal.city_hash64(input_str, len)
28
- end
29
+ return CityHash::Internal.city_hash64(input_str, len) if seed1.nil?
30
+ return CityHash::Internal.city_hash64_with_seed(input_str, len, seed1.to_i & LOW64_MASK) if seed2.nil?
29
31
 
30
- if seed2.nil?
31
- return CityHash::Internal.city_hash64_with_seed(input_str, len, seed1.to_i)
32
- end
33
-
34
- return CityHash::Internal.city_hash64_with_seeds(input_str, len, seed1.to_i, seed2.to_i)
32
+ CityHash::Internal.city_hash64_with_seeds(input_str, len, seed1.to_i & LOW64_MASK, seed2.to_i & LOW64_MASK)
35
33
  end
36
34
 
37
- def self.hash128(input, seed = [])
35
+ def self.hash128(input, seed = nil)
38
36
  input_str = input.to_s
39
37
 
40
38
  # Ruby 1.8 compatibility
41
- len = 0
42
39
  if input_str.respond_to?(:bytesize)
43
40
  len = input_str.bytesize
44
41
  else
45
42
  len = input_str.size
46
43
  end
47
44
 
48
- if seed.empty?
49
- return CityHash::Internal.city_hash128(input_str, len).to_i
50
- end
45
+ return CityHash::Internal.city_hash128(input_str, len).to_i if seed.nil?
46
+
47
+ seed_low = seed.to_i & LOW64_MASK
48
+ seed_high = (seed.to_i & HIGH64_MASK) >> 64
51
49
 
52
- return CityHash::Internal.city_hash128_with_seed(input_str, len, seed[0].to_i, seed[1].to_i).to_i
50
+ CityHash::Internal.city_hash128_with_seed(input_str, len, seed_low, seed_high).to_i
53
51
  end
54
52
 
55
53
  end
@@ -2,7 +2,7 @@ module CityHash
2
2
 
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 3
5
+ MINOR = 4
6
6
  PATCH = 0
7
7
  BUILD = nil
8
8
 
@@ -19,7 +19,8 @@ class TestCityhash < Test::Unit::TestCase
19
19
  end
20
20
 
21
21
  should "return 128bit hash with seed" do
22
- assert_equal 183946415266487905135364192266033899899, CityHash.hash128("test", [123,123])
22
+ seed = (123 << 64) | 123
23
+ assert_equal 183946415266487905135364192266033899899, CityHash.hash128("test", seed)
23
24
  end
24
25
 
25
26
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cityhash
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - nashby
@@ -120,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
120
  requirements:
121
121
  - - ">="
122
122
  - !ruby/object:Gem::Version
123
- hash: -308570463
123
+ hash: 282303183
124
124
  segments:
125
125
  - 0
126
126
  version: "0"