ruby-xxHash 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-xxHash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Justin W Smith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # XXhash
2
+
3
+ This gem provides a pure Ruby implementation of the XXhash32 hashing algorithm described here: https://code.google.com/p/xxhash/.
4
+
5
+ It's intended to be source compatible with the Gem provided by nashby: https://github.com/nashby/xxhash
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ruby-xxHash'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby-xxHash
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'xxhash'
25
+
26
+ text = "test"
27
+ seed = 12345
28
+
29
+ XXhash.xxh32(text, seed) # => 3834992036
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/lib/xxhash.rb ADDED
@@ -0,0 +1,128 @@
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
@@ -0,0 +1,3 @@
1
+ class XXhash
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xxhash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-xxHash"
8
+ spec.version = XXhash::VERSION
9
+ spec.authors = ["Justin W Smith"]
10
+ spec.email = ["justin.w.smith@gmail.com"]
11
+ spec.description = %q{A pure Ruby implementation of xxhash.}
12
+ spec.summary = %q{A pure Ruby implementation of xxhash.}
13
+ spec.homepage = "https://github.com/justinwsmith/ruby-xxhash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'xxhash'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
@@ -0,0 +1,38 @@
1
+ #Copyright (c) 2012 Vasiliy Ermolovich
2
+
3
+ require 'spec_helper'
4
+ require 'stringio'
5
+
6
+ describe XXhash do
7
+ it 'returns correct hash' do
8
+ XXhash.xxh32('test', 123).should eq(2758658570)
9
+ end
10
+
11
+ it 'returns correct hash' do
12
+ XXhash.xxh32("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ").should eq(0x1130e7d4)
13
+ end
14
+
15
+ it 'returns correct hash' do
16
+ XXhash.xxh32("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ", 1471).should eq(0xba59a258)
17
+ end
18
+
19
+ describe 'StreamingHash' do
20
+ it 'raises error if argument is not an IO object' do
21
+ expect { XXhash.xxh32_stream('test', 123)}.to raise_error
22
+ end
23
+
24
+ it 'returns the correct hash for streamed strings' do
25
+ XXhash.xxh32_stream(StringIO.new('test'), 123).should eq(2758658570)
26
+ end
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)
30
+ end
31
+
32
+ it 'returns same hash for streamed files' do
33
+ h1 = XXhash.xxh32(File.read(__FILE__), 123)
34
+ h2 = XXhash.xxh32_stream(File.open(__FILE__), 123)
35
+ h1.should eq(h2)
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-xxHash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin W Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A pure Ruby implementation of xxhash.
63
+ email:
64
+ - justin.w.smith@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/xxhash.rb
75
+ - lib/xxhash/version.rb
76
+ - ruby-xxHash.gemspec
77
+ - spec/spec_helper.rb
78
+ - spec/xxhash_spec.rb
79
+ homepage: https://github.com/justinwsmith/ruby-xxhash
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: A pure Ruby implementation of xxhash.
104
+ test_files:
105
+ - spec/spec_helper.rb
106
+ - spec/xxhash_spec.rb