digest-murmurhash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +21 -0
- data/digest-murmurhash.gemspec +27 -0
- data/ext/digest/murmurhash/extconf.rb +7 -0
- data/ext/digest/murmurhash/murmurhash.c +163 -0
- data/lib/digest/murmurhash/version.rb +5 -0
- data/spec/digest_spec.rb +56 -0
- data/spec/mem_spec.rb +10 -0
- data/spec/spec_helper.rb +1 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6eb394fa67dc2adc52961e3d8de2faeef0826f68
|
4
|
+
data.tar.gz: 1ee189aa44955238877fcfd5978ae13fe2007185
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 438420bdde5f3cbd0082274133c1dcdf61970a427d06592be020b93120cc8b9dcf8b87f21b93647aef26bbd9b59b65ad072fbaa8984b5886a7e20b6dd06d9c1c
|
7
|
+
data.tar.gz: 06d713bb5248de042c7539f99258b4d642bc4d941145ccf07a4cd08e8d62eb4e96c14031b8eb44eb516cb9a234dcccc23906158afad8ef40f76b9295818809eb
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 ksss
|
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,46 @@
|
|
1
|
+
# Digest::MurmurHash
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ksss/digest-murmurhash.png?branch=master)](https://travis-ci.org/ksss/digest-murmurhash)
|
4
|
+
|
5
|
+
Digest::MurmurHash is a class use algorithm of MurmurHash(MurmurHash1 32-bit) desiged by Austin Appleby.
|
6
|
+
|
7
|
+
Digest::MurmurHash compliance Digest API of Ruby.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
All interface compliance Digest::Base.
|
12
|
+
|
13
|
+
You can use same interface built in Digest::XXX classes.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'digest/murmurhash'
|
17
|
+
|
18
|
+
p Digest::MurmurHash.hexdigest('murmurhash') #=> 'c709abd5'
|
19
|
+
p Digest::MurmurHash.file("./LICENSE.txt") #=> '712e9641'
|
20
|
+
```
|
21
|
+
|
22
|
+
## Class tree
|
23
|
+
|
24
|
+
Digest::MurmurHash < Digest::Base < Digest::Instance < Digest::Class < Object < Kernel < BasicObject
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
gem 'digest-murmurhash'
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install digest-murmurhash
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
See the file LICENSE.txt.
|
43
|
+
|
44
|
+
## See also
|
45
|
+
|
46
|
+
[https://sites.google.com/site/murmurhash/](https://sites.google.com/site/murmurhash/)
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rspec/core'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
7
|
+
t.rspec_opts = ["-c", "-f progress", "-Ilib"]
|
8
|
+
t.pattern = "spec/**/*_spec.rb"
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
task :spec => :compile
|
12
|
+
|
13
|
+
require 'rake/extensiontask'
|
14
|
+
spec = Bundler::GemHelper.gemspec
|
15
|
+
Rake::ExtensionTask.new('murmurhash', spec) do |ext|
|
16
|
+
ext.ext_dir = 'ext/digest/murmurhash'
|
17
|
+
ext.lib_dir = 'lib/digest'
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
task :default => [:spec]
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
# require 'digest/murmurhash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "digest-murmurhash"
|
8
|
+
# FIXME replace Digest::MurmurHash::VERSION but raise NameError
|
9
|
+
spec.version = "0.0.1"
|
10
|
+
spec.author = "ksss"
|
11
|
+
spec.email = "co000ri@gmail.com"
|
12
|
+
spec.description = %q{The MurmurHash.}
|
13
|
+
spec.summary = %q{The MurmurHash create by Austin Appleby.}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
spec.extensions = ["ext/digest/murmurhash/extconf.rb"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec", ['~> 2.11']
|
26
|
+
spec.add_development_dependency "rake-compiler", ["~> 0.8.3"]
|
27
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#ifdef HAVE_RUBY_DIGEST_H
|
3
|
+
#include "ruby/digest.h"
|
4
|
+
#else
|
5
|
+
#include "digest.h"
|
6
|
+
#endif
|
7
|
+
|
8
|
+
#define MURMURHASH_DIGEST_LENGTH 4
|
9
|
+
#define MURMURHASH_BLOCK_LENGTH 4
|
10
|
+
|
11
|
+
|
12
|
+
ID id_concat;
|
13
|
+
|
14
|
+
typedef struct {
|
15
|
+
VALUE data;
|
16
|
+
} murmur_t;
|
17
|
+
|
18
|
+
#define MURMURHASH(self, name) \
|
19
|
+
murmur_t* name; \
|
20
|
+
Data_Get_Struct(self, murmur_t, name); \
|
21
|
+
if (name == NULL) { \
|
22
|
+
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be.'"); \
|
23
|
+
}
|
24
|
+
|
25
|
+
static void
|
26
|
+
murmur_init(murmur_t* ptr)
|
27
|
+
{
|
28
|
+
ptr->data = rb_str_new("", 0);
|
29
|
+
}
|
30
|
+
|
31
|
+
static void
|
32
|
+
murmur_mark(murmur_t* ptr)
|
33
|
+
{
|
34
|
+
rb_gc_mark(ptr->data);
|
35
|
+
}
|
36
|
+
|
37
|
+
static void
|
38
|
+
murmur_free(murmur_t* ptr)
|
39
|
+
{
|
40
|
+
xfree(ptr);
|
41
|
+
}
|
42
|
+
|
43
|
+
static VALUE
|
44
|
+
murmur_alloc(VALUE self)
|
45
|
+
{
|
46
|
+
murmur_t* ptr = ALLOC(murmur_t);
|
47
|
+
murmur_init(ptr);
|
48
|
+
return Data_Wrap_Struct(self, murmur_mark, murmur_free, ptr);
|
49
|
+
}
|
50
|
+
|
51
|
+
static VALUE
|
52
|
+
murmur_initialize_copy(VALUE self, VALUE obj)
|
53
|
+
{
|
54
|
+
if (self == obj) return self;
|
55
|
+
|
56
|
+
MURMURHASH(self, pctx1);
|
57
|
+
MURMURHASH(obj, pctx2);
|
58
|
+
|
59
|
+
rb_check_frozen(self);
|
60
|
+
|
61
|
+
memcpy(pctx1, pctx2, sizeof(murmur_t));
|
62
|
+
|
63
|
+
return self;
|
64
|
+
}
|
65
|
+
|
66
|
+
static VALUE
|
67
|
+
murmur_reset(VALUE self)
|
68
|
+
{
|
69
|
+
MURMURHASH(self, ptr);
|
70
|
+
murmur_init(ptr);
|
71
|
+
return self;
|
72
|
+
}
|
73
|
+
|
74
|
+
static VALUE
|
75
|
+
murmur_update(VALUE self, VALUE str)
|
76
|
+
{
|
77
|
+
MURMURHASH(self, ptr);
|
78
|
+
StringValue(str);
|
79
|
+
rb_funcall(ptr->data, id_concat, 1, str);
|
80
|
+
return self;
|
81
|
+
}
|
82
|
+
|
83
|
+
static VALUE
|
84
|
+
murmur_finish(VALUE self)
|
85
|
+
{
|
86
|
+
const uint32_t m = 0x5bd1e995;
|
87
|
+
const uint8_t r = 16;
|
88
|
+
uint32_t length, h;
|
89
|
+
const char* p;
|
90
|
+
uint8_t digest[MURMURHASH_DIGEST_LENGTH];
|
91
|
+
|
92
|
+
MURMURHASH(self, ptr);
|
93
|
+
|
94
|
+
p = RSTRING_PTR(ptr->data);
|
95
|
+
length = RSTRING_LEN(ptr->data);
|
96
|
+
h = length * m;
|
97
|
+
|
98
|
+
while (4 <= length) {
|
99
|
+
h += *(uint32_t*)p;
|
100
|
+
h *= m;
|
101
|
+
h ^= h >> r;
|
102
|
+
p += 4;
|
103
|
+
length -= 4;
|
104
|
+
}
|
105
|
+
|
106
|
+
switch (length) {
|
107
|
+
case 3:
|
108
|
+
h += p[2] << 16;
|
109
|
+
case 2:
|
110
|
+
h += p[1] << 8;
|
111
|
+
case 1:
|
112
|
+
h += p[0];
|
113
|
+
h *= m;
|
114
|
+
h ^= h >> r;
|
115
|
+
}
|
116
|
+
|
117
|
+
h *= m;
|
118
|
+
h ^= h >> 10;
|
119
|
+
h *= m;
|
120
|
+
h ^= h >> 17;
|
121
|
+
|
122
|
+
digest[0] = (h >> 24);
|
123
|
+
digest[1] = (h >> 16);
|
124
|
+
digest[2] = (h >> 8);
|
125
|
+
digest[3] = (h);
|
126
|
+
|
127
|
+
return rb_str_new((const char*) digest, 4);
|
128
|
+
}
|
129
|
+
|
130
|
+
static VALUE
|
131
|
+
murmur_digest_length(VALUE self)
|
132
|
+
{
|
133
|
+
return INT2NUM(MURMURHASH_DIGEST_LENGTH);
|
134
|
+
}
|
135
|
+
|
136
|
+
static VALUE
|
137
|
+
murmur_block_length(VALUE self)
|
138
|
+
{
|
139
|
+
return INT2NUM(MURMURHASH_BLOCK_LENGTH);
|
140
|
+
}
|
141
|
+
|
142
|
+
void
|
143
|
+
Init_murmurhash()
|
144
|
+
{
|
145
|
+
VALUE mDigest, cDigest_Base, cDigest_MurmurHash;
|
146
|
+
|
147
|
+
id_concat = rb_intern("concat");
|
148
|
+
|
149
|
+
rb_require("digest");
|
150
|
+
|
151
|
+
mDigest = rb_path2class("Digest");
|
152
|
+
cDigest_Base = rb_path2class("Digest::Base");
|
153
|
+
|
154
|
+
cDigest_MurmurHash = rb_define_class_under(mDigest, "MurmurHash", cDigest_Base);
|
155
|
+
|
156
|
+
rb_define_alloc_func(cDigest_MurmurHash, murmur_alloc);
|
157
|
+
rb_define_method(cDigest_MurmurHash, "initialize_copy", murmur_initialize_copy, 1);
|
158
|
+
rb_define_method(cDigest_MurmurHash, "reset", murmur_reset, 0);
|
159
|
+
rb_define_method(cDigest_MurmurHash, "update", murmur_update, 1);
|
160
|
+
rb_define_private_method(cDigest_MurmurHash, "finish", murmur_finish, 0);
|
161
|
+
rb_define_method(cDigest_MurmurHash, "digest_length", murmur_digest_length, 0);
|
162
|
+
rb_define_method(cDigest_MurmurHash, "block_length", murmur_block_length, 0);
|
163
|
+
}
|
data/spec/digest_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Digest::MurmurHash do
|
4
|
+
let :murmur do
|
5
|
+
Digest::MurmurHash.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "initialize" do
|
9
|
+
expect(Digest::MurmurHash.new).to be_a_kind_of(Digest::Base)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "hexdigest" do
|
13
|
+
expect(Digest::MurmurHash.hexdigest("a" * 1024)).to eq("a1522e5f")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "update and reset and hexdigest" do
|
17
|
+
murmur.update("m")
|
18
|
+
murmur.update("u")
|
19
|
+
murmur.update("r")
|
20
|
+
murmur.update("m")
|
21
|
+
murmur.update("u")
|
22
|
+
murmur.update("r")
|
23
|
+
murmur.update("hash")
|
24
|
+
expect(murmur.hexdigest).to eq("c709abd5");
|
25
|
+
expect(murmur.hexdigest).to eq("c709abd5");
|
26
|
+
expect(murmur.hexdigest!).to eq("c709abd5");
|
27
|
+
expect(murmur.hexdigest).to eq("00000000");
|
28
|
+
end
|
29
|
+
|
30
|
+
it "dup" do
|
31
|
+
murmur1 = Digest::MurmurHash.new
|
32
|
+
murmur2 = Digest::MurmurHash.new
|
33
|
+
murmur1.update("murmur")
|
34
|
+
murmur2 = murmur1.dup
|
35
|
+
murmur2.update("hash")
|
36
|
+
expect(murmur2.hexdigest).to eq("c709abd5")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "==" do
|
40
|
+
["", "murmur", "murmurhash" * 1024].each do |str|
|
41
|
+
murmur1 = Digest::MurmurHash.new
|
42
|
+
murmur2 = Digest::MurmurHash.new
|
43
|
+
expect(murmur1.update(str) == murmur2.update(str)).to be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "length" do
|
48
|
+
expect(murmur.length).to eq(4);
|
49
|
+
end
|
50
|
+
|
51
|
+
it "block_length" do
|
52
|
+
# MurmurHash don't use block.
|
53
|
+
# Therefore `block_length` return chunk size for calculate MurmurHash (equal 4)
|
54
|
+
expect(murmur.block_length).to eq(4);
|
55
|
+
end
|
56
|
+
end
|
data/spec/mem_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'digest/murmurhash'
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digest-murmurhash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.8.3
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.3
|
69
|
+
description: The MurmurHash.
|
70
|
+
email: co000ri@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions:
|
73
|
+
- ext/digest/murmurhash/extconf.rb
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .travis.yml
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- digest-murmurhash.gemspec
|
83
|
+
- ext/digest/murmurhash/extconf.rb
|
84
|
+
- ext/digest/murmurhash/murmurhash.c
|
85
|
+
- lib/digest/murmurhash/version.rb
|
86
|
+
- spec/digest_spec.rb
|
87
|
+
- spec/mem_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.1.6
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: The MurmurHash create by Austin Appleby.
|
113
|
+
test_files:
|
114
|
+
- spec/digest_spec.rb
|
115
|
+
- spec/mem_spec.rb
|
116
|
+
- spec/spec_helper.rb
|