pearson-hashing 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pearson-hashing.rb +22 -3
- data/lib/pearson-hashing/version.rb +1 -1
- data/spec/lib/pearson_hashing_spec.rb +13 -1
- metadata +2 -2
data/lib/pearson-hashing.rb
CHANGED
@@ -18,19 +18,38 @@ module PearsonHashing
|
|
18
18
|
120, 140, 138, 28, 84, 186, 198, 131, 54, 2, 56, 78, 173, 151, 83, 27,
|
19
19
|
255, 144, 249, 189, 104, 4, 168, 98, 162, 150, 254, 242, 109, 34, 133, 224,
|
20
20
|
228, 79, 103, 201, 160, 90, 18, 61, 10, 233, 91, 80, 124, 96, 244, 36]
|
21
|
-
|
22
|
-
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
# Calculates a 8bit hash based on pearson hashing algorithm
|
25
|
+
# (values range from 0 to 256)
|
23
26
|
#
|
24
27
|
# Further descriptions can be found here:
|
25
28
|
# * http://en.wikipedia.org/wiki/Pearson_hashing
|
26
29
|
# * http://cs.mwsu.edu/~griffin/courses/2133/downloads/Old_Assignments/p677-pearson.pdf
|
27
30
|
# @param [String] string
|
28
31
|
# @return [Fixnum] hash
|
29
|
-
def self.
|
32
|
+
def self.digest8(string)
|
30
33
|
hash = string.size % 256
|
31
34
|
string.each_byte do |byte|
|
32
35
|
hash = TABLE[ (hash + byte) % 256 ]
|
33
36
|
end
|
34
37
|
hash
|
35
38
|
end
|
39
|
+
class << self
|
40
|
+
alias :digest :digest8
|
41
|
+
end
|
42
|
+
|
43
|
+
# Calculates a bit more complex 16bit hash, as describe in page 679 of:
|
44
|
+
# (values range from 0 to 65536)
|
45
|
+
#
|
46
|
+
# http://cs.mwsu.edu/~griffin/courses/2133/downloads/Old_Assignments/p677-pearson.pdf
|
47
|
+
# @param [String] string
|
48
|
+
# @return [Fixnum] hash
|
49
|
+
def self.digest16(string)
|
50
|
+
h1 = PearsonHashing.digest(string)
|
51
|
+
string2 = [((string.bytes.first+1)%256)].pack('U*') + string[1,string.size]
|
52
|
+
h2 = PearsonHashing.digest(string2)
|
53
|
+
h1 * h2
|
54
|
+
end
|
36
55
|
end
|
@@ -1,9 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PearsonHashing do
|
4
|
-
describe '#
|
4
|
+
describe '#digest8' do
|
5
5
|
it 'should return 96 for "foo"' do
|
6
6
|
PearsonHashing.digest('foo').should == 96
|
7
7
|
end
|
8
8
|
end
|
9
|
+
|
10
|
+
describe '#digest16' do
|
11
|
+
it 'should return 96 for "foo"' do
|
12
|
+
PearsonHashing.digest16('foo').should == 9504
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#digest' do
|
17
|
+
it 'should return the same as #digest8' do
|
18
|
+
PearsonHashing.digest('foo').should == PearsonHashing.digest8('foo')
|
19
|
+
end
|
20
|
+
end
|
9
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pearson-hashing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-17 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: pearson hasing provides "fast hashing of variable-length text strings
|
15
15
|
email:
|