hashword 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('hashword_core')
@@ -0,0 +1,64 @@
1
+ #include <stdint.h>
2
+ #include <ruby/ruby.h>
3
+
4
+ #define rot(x,k) (((x) << (k)) | ((x) >> (32 - (k))))
5
+
6
+ #define mix(a,b,c) \
7
+ { \
8
+ a -= c; a ^= rot(c, 4); c += b; \
9
+ b -= a; b ^= rot(a, 6); a += c; \
10
+ c -= b; c ^= rot(b, 8); b += a; \
11
+ a -= c; a ^= rot(c,16); c += b; \
12
+ b -= a; b ^= rot(a,19); a += c; \
13
+ c -= b; c ^= rot(b, 4); b += a; \
14
+ }
15
+
16
+ #define final(a,b,c) \
17
+ { \
18
+ c ^= b; c -= rot(b,14); \
19
+ a ^= c; a -= rot(c,11); \
20
+ b ^= a; b -= rot(a,25); \
21
+ c ^= b; c -= rot(b,16); \
22
+ a ^= c; a -= rot(c,4); \
23
+ b ^= a; b -= rot(a,14); \
24
+ c ^= b; c -= rot(b,24); \
25
+ }
26
+
27
+ #ifndef RARRAY_PTR
28
+ # define RARRAY_PTR(a) RARRAY(a)->ptr
29
+ #endif
30
+
31
+ VALUE
32
+ rb_cObject_hashword(VALUE self, VALUE key, VALUE len, VALUE init) {
33
+ VALUE *k = RARRAY_PTR(key);
34
+ size_t length = (size_t) NUM2UINT(len);
35
+ uint32_t initval = NUM2UINT(init);
36
+ uint32_t a, b, c;
37
+
38
+ a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
39
+
40
+ while (length > 3) {
41
+ a += NUM2UINT(k[0]);
42
+ b += NUM2UINT(k[1]);
43
+ c += NUM2UINT(k[2]);
44
+ mix(a, b, c);
45
+ length -= 3;
46
+ k += 3;
47
+ }
48
+
49
+ switch (length) {
50
+ case 3: c += NUM2UINT(k[2]);
51
+ case 2: b += NUM2UINT(k[1]);
52
+ case 1:
53
+ a += NUM2UINT(k[0]);
54
+ final(a, b, c);
55
+ break;
56
+ }
57
+
58
+ return UINT2NUM(c);
59
+ }
60
+
61
+ void
62
+ Init_hashword_core(void) {
63
+ rb_define_singleton_method(rb_cObject, "hashword", rb_cObject_hashword, 3);
64
+ }
@@ -0,0 +1,7 @@
1
+ require 'hashword_core.so'
2
+
3
+ class Array
4
+ def hashword(initval=0)
5
+ ::Object.hashword(self, size, initval)
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashword
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - shura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Add a method to Array:Class to get hashword's hash
15
+ email: shura1991@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ext/hashword_core.c
22
+ - ext/extconf.rb
23
+ - lib/hashword.rb
24
+ homepage: http://github.com/shurizzle/ruby-hashword
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.10
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Hashword algorithm implementation
48
+ test_files: []