ruby-xxHash 0.0.2 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/ruby-xxhash.rb +128 -0
- data/lib/ruby-xxhash/version.rb +3 -0
- data/ruby-xxHash.gemspec +1 -1
- data/spec/results.yaml +16 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/xxhash_spec.rb +10 -14
- metadata +5 -3
- data/lib/xxhash.rb +0 -128
- data/lib/xxhash/version.rb +0 -3
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
This gem provides a pure Ruby implementation of the XXhash32 hashing algorithm described here: https://code.google.com/p/xxhash/.
|
4
4
|
|
5
|
-
It's intended to be source compatible with the Gem provided by nashby: https://github.com/nashby/xxhash
|
5
|
+
It's intended to be (mostly) source compatible with the Gem provided by nashby: https://github.com/nashby/xxhash
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -21,7 +21,7 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
require 'xxhash'
|
24
|
+
require 'ruby-xxhash'
|
25
25
|
|
26
26
|
text = "test"
|
27
27
|
seed = 12345
|
data/lib/ruby-xxhash.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require "ruby-xxhash/version"
|
2
|
+
|
3
|
+
class XXhash
|
4
|
+
@@mem_total_size = 16
|
5
|
+
@@prime32_1 = 2654435761
|
6
|
+
@@prime32_2 = 2246822519
|
7
|
+
@@prime32_3 = 3266489917
|
8
|
+
@@prime32_4 = 668265263
|
9
|
+
@@prime32_5 = 374761393
|
10
|
+
|
11
|
+
@@thirtytwo1s = (2**32-1)
|
12
|
+
|
13
|
+
def XXhash.xxh32(input, seed = 0)
|
14
|
+
xxh = XXhash.new(seed)
|
15
|
+
xxh.feed(input)
|
16
|
+
xxh.sum32
|
17
|
+
end
|
18
|
+
|
19
|
+
def XXhash.xxh32_stream(io, seed = 0, chunk = 32)
|
20
|
+
xxh = XXhash.new(seed)
|
21
|
+
|
22
|
+
while(data = io.read(chunk))
|
23
|
+
xxh.feed(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
xxh.sum32
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize seed
|
30
|
+
@seed = seed
|
31
|
+
@v1 = seed + @@prime32_1 + @@prime32_2
|
32
|
+
@v2 = seed + @@prime32_2
|
33
|
+
@v3 = seed + 0
|
34
|
+
@v4 = seed - @@prime32_1
|
35
|
+
@total_len = 0
|
36
|
+
@memory = Array.new(@@mem_total_size)
|
37
|
+
@memsize = 0
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def feed bytes
|
42
|
+
if String === bytes
|
43
|
+
bytes = bytes.unpack("C*")
|
44
|
+
end
|
45
|
+
|
46
|
+
@total_len += bytes.length
|
47
|
+
|
48
|
+
p = 0
|
49
|
+
|
50
|
+
while (remaining = (bytes.length - p)) > 0
|
51
|
+
|
52
|
+
mem_avail = @@mem_total_size - @memsize
|
53
|
+
|
54
|
+
if(remaining < mem_avail)
|
55
|
+
@memory[@memsize, remaining] = bytes[p, remaining]
|
56
|
+
@memsize += remaining
|
57
|
+
break
|
58
|
+
end
|
59
|
+
|
60
|
+
@memory[@memsize, mem_avail] = bytes[p, mem_avail]
|
61
|
+
|
62
|
+
i = 0
|
63
|
+
[:v1, :v2, :v3, :v4].each do |m|
|
64
|
+
p32 = uint32(
|
65
|
+
@memory[i] |
|
66
|
+
(@memory[i+1] << 8) |
|
67
|
+
(@memory[i+2] << 16) |
|
68
|
+
(@memory[i+3] << 24))
|
69
|
+
|
70
|
+
v = uint32(self.send(m) + p32 * @@prime32_2)
|
71
|
+
v = uint32(uint32((v << 13) | (v >> (32 - 13))) * @@prime32_1)
|
72
|
+
self.send((m.to_s + "=").to_sym, v)
|
73
|
+
i += 4
|
74
|
+
end
|
75
|
+
|
76
|
+
p += mem_avail
|
77
|
+
@memsize = 0
|
78
|
+
end
|
79
|
+
|
80
|
+
return true
|
81
|
+
end
|
82
|
+
|
83
|
+
def sum32
|
84
|
+
if @total_len >= 16
|
85
|
+
h32 = ((@v1 << 1) | (@v1 >> (32 - 1))) +
|
86
|
+
((@v2 << 7) | (@v2 >> (32 - 7))) +
|
87
|
+
((@v3 << 12) | (@v3 >> (32 - 12))) +
|
88
|
+
((@v4 << 18) | (@v4 >> (32 - 18)))
|
89
|
+
else
|
90
|
+
h32 = @seed + @@prime32_5
|
91
|
+
end
|
92
|
+
|
93
|
+
h32 = uint32(h32 + @total_len)
|
94
|
+
|
95
|
+
p = 0
|
96
|
+
while p <= (@memsize - 4)
|
97
|
+
p32 = uint32(@memory[p] |
|
98
|
+
(@memory[p+1] << 8) |
|
99
|
+
(@memory[p+2] << 16) |
|
100
|
+
(@memory[p+3] << 24))
|
101
|
+
h32 = uint32(h32 + p32 * @@prime32_3)
|
102
|
+
h32 = uint32(uint32((h32 << 17) | (h32 >> (32 - 17))) * @@prime32_4)
|
103
|
+
p += 4
|
104
|
+
end
|
105
|
+
|
106
|
+
while p < @memsize
|
107
|
+
h32 = uint32(h32 + @memory[p] * @@prime32_5)
|
108
|
+
h32 = uint32(uint32((h32 << 11) | (h32 >> (32 - 11))) * @@prime32_1)
|
109
|
+
p += 1
|
110
|
+
end
|
111
|
+
|
112
|
+
h32 ^= h32 >> 15
|
113
|
+
h32 = uint32(h32 * @@prime32_2)
|
114
|
+
h32 ^= h32 >> 13
|
115
|
+
h32 = uint32(h32 * @@prime32_3)
|
116
|
+
h32 ^= h32 >> 16
|
117
|
+
|
118
|
+
h32
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
attr_accessor :v1, :v2, :v3, :v4
|
124
|
+
|
125
|
+
def uint32(x)
|
126
|
+
x & @@thirtytwo1s
|
127
|
+
end
|
128
|
+
end
|
data/ruby-xxHash.gemspec
CHANGED
data/spec/results.yaml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
---
|
2
|
+
? - test
|
3
|
+
- 123
|
4
|
+
: 2758658570
|
5
|
+
? - ! 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, '
|
6
|
+
- 0
|
7
|
+
: 288417748
|
8
|
+
? - ! 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, '
|
9
|
+
- 1471
|
10
|
+
: 3126436440
|
11
|
+
? - ! 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'
|
12
|
+
- 0
|
13
|
+
: 222574394
|
14
|
+
? - ! 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'
|
15
|
+
- 1471
|
16
|
+
: 845537824
|
data/spec/spec_helper.rb
CHANGED
data/spec/xxhash_spec.rb
CHANGED
@@ -2,31 +2,27 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
require 'stringio'
|
5
|
+
require 'yaml'
|
5
6
|
|
6
7
|
describe XXhash do
|
7
|
-
|
8
|
-
XXhash.xxh32('test', 123).should eq(2758658570)
|
9
|
-
end
|
8
|
+
hash = YAML.load(IO.read "spec/results.yaml")
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
hash.each do |key, value|
|
11
|
+
it 'returns correct hash' do
|
12
|
+
XXhash.xxh32(key[0], key[1]).should eq(value)
|
13
|
+
end
|
13
14
|
end
|
14
15
|
|
15
|
-
it 'returns correct hash' do
|
16
|
-
XXhash.xxh32("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ", 1471).should eq(0xba59a258)
|
17
|
-
end
|
18
16
|
|
19
17
|
describe 'StreamingHash' do
|
20
18
|
it 'raises error if argument is not an IO object' do
|
21
19
|
expect { XXhash.xxh32_stream('test', 123)}.to raise_error
|
22
20
|
end
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
it 'returns the correct hash for streamed strings' do
|
29
|
-
XXhash.xxh32_stream(StringIO.new("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, "), 1471).should eq(0xba59a258)
|
22
|
+
hash.each do |key, value|
|
23
|
+
it 'returns correct hash' do
|
24
|
+
XXhash.xxh32_stream(StringIO.new(key[0]), key[1]).should eq(value)
|
25
|
+
end
|
30
26
|
end
|
31
27
|
|
32
28
|
it 'returns same hash for streamed files' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-xxHash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -71,9 +71,10 @@ files:
|
|
71
71
|
- LICENSE.txt
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
|
-
- lib/xxhash.rb
|
75
|
-
- lib/xxhash/version.rb
|
74
|
+
- lib/ruby-xxhash.rb
|
75
|
+
- lib/ruby-xxhash/version.rb
|
76
76
|
- ruby-xxHash.gemspec
|
77
|
+
- spec/results.yaml
|
77
78
|
- spec/spec_helper.rb
|
78
79
|
- spec/xxhash_spec.rb
|
79
80
|
homepage: https://github.com/justinwsmith/ruby-xxhash
|
@@ -102,5 +103,6 @@ signing_key:
|
|
102
103
|
specification_version: 3
|
103
104
|
summary: A pure Ruby implementation of xxhash.
|
104
105
|
test_files:
|
106
|
+
- spec/results.yaml
|
105
107
|
- spec/spec_helper.rb
|
106
108
|
- spec/xxhash_spec.rb
|
data/lib/xxhash.rb
DELETED
@@ -1,128 +0,0 @@
|
|
1
|
-
require "xxhash/version"
|
2
|
-
|
3
|
-
class XXhash
|
4
|
-
@@mem_total_size = 16
|
5
|
-
@@prime32_1 = 2654435761
|
6
|
-
@@prime32_2 = 2246822519
|
7
|
-
@@prime32_3 = 3266489917
|
8
|
-
@@prime32_4 = 668265263
|
9
|
-
@@prime32_5 = 374761393
|
10
|
-
|
11
|
-
@@thirtytwo1s = (2**32-1)
|
12
|
-
|
13
|
-
def XXhash.xxh32(input, seed = 0)
|
14
|
-
xxh = XXhash.new(seed)
|
15
|
-
xxh.feed(input)
|
16
|
-
xxh.sum32
|
17
|
-
end
|
18
|
-
|
19
|
-
def XXhash.xxh32_stream(io, seed = 0, chunk = 1024)
|
20
|
-
xxh = XXhash.new(seed)
|
21
|
-
|
22
|
-
while(data = io.read(chunk))
|
23
|
-
xxh.feed(data)
|
24
|
-
end
|
25
|
-
|
26
|
-
xxh.sum32
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize seed
|
30
|
-
@seed = seed
|
31
|
-
@v1 = seed + @@prime32_1 + @@prime32_2
|
32
|
-
@v2 = seed + @@prime32_2
|
33
|
-
@v3 = seed + 0
|
34
|
-
@v4 = seed - @@prime32_1
|
35
|
-
@total_len = 0
|
36
|
-
@memory = Array.new(@@mem_total_size)
|
37
|
-
@memsize = 0
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
def feed bytes
|
42
|
-
if String === bytes
|
43
|
-
bytes = bytes.unpack("C*")
|
44
|
-
end
|
45
|
-
|
46
|
-
@total_len += bytes.length
|
47
|
-
|
48
|
-
p = 0
|
49
|
-
|
50
|
-
while (remaining = (bytes.length - p)) > 0
|
51
|
-
|
52
|
-
mem_avail = @@mem_total_size - @memsize
|
53
|
-
|
54
|
-
if(remaining < mem_avail)
|
55
|
-
@memory[@memsize, remaining] = bytes[p, remaining]
|
56
|
-
@memsize += remaining
|
57
|
-
break
|
58
|
-
end
|
59
|
-
|
60
|
-
@memory[@memsize, mem_avail] = bytes[p, mem_avail]
|
61
|
-
|
62
|
-
i = 0
|
63
|
-
[:v1, :v2, :v3, :v4].each do |m|
|
64
|
-
p32 = uint32(
|
65
|
-
@memory[i] |
|
66
|
-
(@memory[i+1] << 8) |
|
67
|
-
(@memory[i+2] << 16) |
|
68
|
-
(@memory[i+3] << 24))
|
69
|
-
|
70
|
-
v = uint32(self.send(m) + p32 * @@prime32_2)
|
71
|
-
v = uint32(uint32((v << 13) | (v >> (32 - 13))) * @@prime32_1)
|
72
|
-
self.send((m.to_s + "=").to_sym, v)
|
73
|
-
i += 4
|
74
|
-
end
|
75
|
-
|
76
|
-
p += mem_avail
|
77
|
-
@memsize = 0
|
78
|
-
end
|
79
|
-
|
80
|
-
return true
|
81
|
-
end
|
82
|
-
|
83
|
-
def sum32
|
84
|
-
if @total_len >= 16
|
85
|
-
h32 = ((@v1 << 1) | (@v1 >> (32 - 1))) +
|
86
|
-
((@v2 << 7) | (@v2 >> (32 - 7))) +
|
87
|
-
((@v3 << 12) | (@v3 >> (32 - 12))) +
|
88
|
-
((@v4 << 18) | (@v4 >> (32 - 18)))
|
89
|
-
else
|
90
|
-
h32 = @seed + @@prime32_5
|
91
|
-
end
|
92
|
-
|
93
|
-
h32 = uint32(h32 + @total_len)
|
94
|
-
|
95
|
-
p = 0
|
96
|
-
while p <= (@memsize - 4)
|
97
|
-
p32 = uint32(@memory[p] |
|
98
|
-
(@memory[p+1] << 8) |
|
99
|
-
(@memory[p+2] << 16) |
|
100
|
-
(@memory[p+3] << 24))
|
101
|
-
h32 = uint32(h32 + p32 * @@prime32_3)
|
102
|
-
h32 = uint32(uint32((h32 << 17) | (h32 >> (32 - 17))) * @@prime32_4)
|
103
|
-
p += 4
|
104
|
-
end
|
105
|
-
|
106
|
-
while p < @memsize
|
107
|
-
h32 = uint32(h32 + @memory[p] * @@prime32_5)
|
108
|
-
h32 = uint32(uint32((h32 << 11) | (h32 >> (32 - 11))) * @@prime32_1)
|
109
|
-
p += 1
|
110
|
-
end
|
111
|
-
|
112
|
-
h32 ^= h32 >> 15
|
113
|
-
h32 = uint32(h32 * @@prime32_2)
|
114
|
-
h32 ^= h32 >> 13
|
115
|
-
h32 = uint32(h32 * @@prime32_3)
|
116
|
-
h32 ^= h32 >> 16
|
117
|
-
|
118
|
-
h32
|
119
|
-
end
|
120
|
-
|
121
|
-
private
|
122
|
-
|
123
|
-
attr_accessor :v1, :v2, :v3, :v4
|
124
|
-
|
125
|
-
def uint32(x)
|
126
|
-
x & @@thirtytwo1s
|
127
|
-
end
|
128
|
-
end
|
data/lib/xxhash/version.rb
DELETED