Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updates for 1.9 & cleanup

  • Loading branch information...
commit 44e2f363b70e904fa996227232ce79dd04e527aa 1 parent 4ee1a1d
@jwilkins authored
View
8 .gitignore
@@ -0,0 +1,8 @@
+.bundle
+Gemfile.lock
+*.gem
+ext/Makefile
+ext/*.o
+ext/*.bundle
+ext/*.log
+spec/reports
View
6 .travis.yml
@@ -0,0 +1,6 @@
+language: ruby
+bundler_args: --without development
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
View
14 Gemfile
@@ -0,0 +1,14 @@
+source :rubygems
+
+group :development do
+ if RUBY_VERSION =~ /^1.9/
+ gem "ruby-debug19", :require => "ruby-debug"
+ else
+ gem 'ruby-debug'
+ end
+end
+
+group :test do
+ gem "rspec"
+ gem "rake"
+end
View
19 Rakefile
@@ -0,0 +1,19 @@
+require 'bundler'
+Bundler.require
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+desc 'Default: run specs'
+task :default => :spec
+
+task :cleanbuild do
+ `rm ext/*.o ext/Makefile`
+ if RUBY_VERSION =~ /^1.9/
+ `cd ext && ruby extconf.rb && make`
+ elsif RUBY_VERSION =~ /^1.8/
+ `cd ext && ruby extconf.rb && make`
+ else
+ puts "Ruby version #{RUBY_VERSION}? Can't help you.. "
+ end
+end
View
1  bin/nilsimsa
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
-require 'rubygems'
require 'nilsimsa'
if ARGV.size > 0 then
View
7 ext/extconf.rb
@@ -1,4 +1,11 @@
#!/usr/bin/env ruby
require 'mkmf'
+require 'rbconfig'
+
+CONFIG["DLEXT"] = "bundle"
+CONFIG["LDSHARED"] = "$(CC) -shared"
+CONFIG["CCDLFLAGS"] = " -fPIC"
+
+have_header('ruby.h') or missing('ruby.h')
create_makefile( 'nilsimsa_native' )
View
33 ext/nilsimsa.c
@@ -49,6 +49,15 @@
#define tran3(a,b,c,n) (((tran[((a)+(n))&255]^tran[(b)]*((n)+(n)+1))+tran[(c)^tran[n]])&255)
+#ifdef HAVE_RUBY_IO_H
+#ifndef RSTRING_PTR
+#define RSTRING_PTR(s) (RSTRING(s))
+#endif
+#ifndef RSTRING_LEN
+#define RSTRING_LEN(s) (RSTRING(s))
+#endif
+#else
+#endif
struct nsrecord {
int acc[256]; /* counts each trigram's hash */
@@ -256,10 +265,13 @@ rbns_update(VALUE self, VALUE data) {
char *chdata;
long chdata_len;
r = get_nsr( self );
+ VALUE str;
Check_Type( data, T_STRING );
- chdata = rb_str2cstr( data, &chdata_len );
- nsr_update( r, chdata, chdata_len );
+
+ str = StringValue(data);
+ //chdata = rb_str2cstr( data, &chdata_len );
+ nsr_update( r, (RSTRING_PTR(str)), (RSTRING_LEN(str)) );
return data;
}
@@ -269,18 +281,25 @@ rbns_nilsimsa(VALUE self, VALUE other) {
char *d1;
char *d2;
- d1 = rb_str2cstr( rb_funcall( self, rb_intern( "digest" ), 0 ), &len );
- if (len < NSR_CODE_LEN) {
+ VALUE str1;
+ VALUE str2;
+
+ //d1 = rb_str2cstr( rb_funcall( self, rb_intern( "digest" ), 0 ), &len );
+ str1 = rb_funcall( self, rb_intern( "digest" ), 0 );
+ str1 = StringValue(str1);
+ if(RSTRING_LEN(str1) < NSR_CODE_LEN) {
return Qnil;
}
Check_Type( other, T_STRING );
- d2 = rb_str2cstr( other, &len );
- if (len < NSR_CODE_LEN) {
+ //d2 = rb_str2cstr( other, &len );
+ str2 = StringValue( other);
+ if (RSTRING_LEN(str2) < NSR_CODE_LEN) {
return Qnil;
}
- return INT2NUM( nilsimsa( d1, d2 ) );
+ //return INT2NUM( nilsimsa( d1, d2 ) );
+ return INT2NUM( nilsimsa( RSTRING_PTR(str1), RSTRING_PTR(str2)) );
}
VALUE
View
86 nilsimsa.rb → lib/nilsimsa.rb
@@ -5,7 +5,6 @@
# inspired by Digest::Nilsimsa-0.06 from Perl CPAN and
# the original C nilsimsa-0.2.4 implementation by cmeclax
# http://ixazon.dynip.com/~cmeclax/nilsimsa.html
-
class Nilsimsa
TRAN =
@@ -45,16 +44,20 @@ class Nilsimsa
"\x04\x05\x05\x06\x05\x06\x06\x07\x05\x06\x06\x07\x06\x07\x07\x08"
def initialize(*data)
- @threshold=0; @count=0
- @acc =Array::new(256,0)
+ @threshold=0;
+ @count=0
+ @acc = Array::new(256,0)
@lastch0=@lastch1=@lastch2=@lastch3= -1
data.each do |d| update(d) end if data && (data.size>0)
end
- def tran3(a,b,c,n)
+ def tran3_orig(a,b,c,n)
(((TRAN[(a+n)&255]^TRAN[b]*(n+n+1))+TRAN[(c)^TRAN[n]])&255)
end
+ def tran3(a,b,c,n)
+ ((((TRAN[(a+n)&255].ord)^(TRAN[b].ord)*(n+n+1))+(TRAN[(c)^(TRAN[n].ord)]).ord)&255)
+ end
def update(data)
data.each_byte do |ch|
@@ -83,19 +86,25 @@ def update(data)
def digest
@total=0;
case @count
- when 0..2:
- when 3 : @total +=1
- when 4 : @total +=4
- else
- @total +=(8*@count)-28
+ when 0..2 then ;
+ when 3 then @total +=1
+ when 4 then @total +=4
+ else @total +=(8*@count)-28
end
@threshold=@total/256
- @code=String::new(
- "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" <<
- "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
+ @code="\x00"*32
(0..255).each do |i|
- @code[i>>3]+=( ((@acc[i]>@threshold)?(1):(0))<<(i&7) )
+ offset = i>>3
+ cur_val = @code[offset].ord
+ @code[offset] = (cur_val + ( ((@acc[i].ord>@threshold)?(1):(0))<<(i&7) )).chr
+# cv = @code[i>>3].ord
+# if @acc[i] > @threshold
+# #@code[i>>3]+=( (((@acc[i])>@threshold)?(1):(0))<<(i&7) )
+# @code[cv] = (@code[cv].ord + (1 <<(i&7))).chr
+# else
+# @code[cv] = (@code[cv].ord + (0 <<(i&7))).chr
+# end
end
@code[0..31].reverse
@@ -126,57 +135,16 @@ def file(thisone)
def nilsimsa(otherdigest)
bits=0; myd=digest
(0..31).each do |i|
- bits += POPC[255&myd[i]^otherdigest[i]]
+ bits += POPC[255&myd[i].ord^otherdigest[i].ord].ord
end
(128-bits)
end
-
end
-def selftest
- n1 = Nilsimsa::new;
- n1.update("abcdefgh")
- puts "abcdefgh: #{n1.hexdigest=='14c8118000000000030800000004042004189020001308014088003280000078'}"
- n2 = Nilsimsa::new("abcd","efgh")
- puts "abcd efgh: #{n2.hexdigest=='14c8118000000000030800000004042004189020001308014088003280000078'}"
- puts "digest: #{n1 == n2.digest}"
- n1.update("ijk")
- puts "ijk: #{n1.hexdigest=='14c811840010000c0328200108040630041890200217582d4098103280000078'}"
- puts "nilsimsa: #{n1.nilsimsa(n2.digest)==109}"
- puts
+begin # load C core - if available
+ #require "#{File.join(File.dirname(__FILE__), '..', 'ext', 'nilsimsa_native')}"
+rescue LoadError => e
+ # ignore lack of native module
end
-if __FILE__ == $0 then
- if ARGV.size>0 then
- begin # load C core - if available
- require 'nilsimsa_native'
- rescue LoadError => e
- # ignore lack of native module
- end
- ARGV.each do |filename|
- if FileTest::exists?(filename) then
- n = Nilsimsa::new
- n.file(filename)
- puts n.hexdigest+" #{filename}"
- else
- puts "error: can't find '#{filename}'"
- end
- end
- else
- puts 'Running selftest using native ruby version'
- selftest
- begin # load C core - if available
- if File.exists?('./nilsimsa_native')
- require './nilsimsa_native'
- puts 'Running selftest using compiled nilsimsa in current dir'
- else
- require 'nilsimsa_native'
- puts 'Running selftest using compiled nilsimsa'
- end
- selftest
- rescue LoadError => e
- puts "Couldnt run selftest with compiled nilsimsa"
- end
- end
-end
View
9 nilsimsa.gemspec
@@ -1,15 +1,18 @@
SPEC = Gem::Specification.new do |spec|
# Descriptive and source information for this gem.
spec.name = "nilsimsa"
- spec.version = "1.0.5"
+ spec.version = "1.1.0"
spec.summary = "Computes Nilsimsa values. Nilsimsa is a distance based hash"
spec.author = "Jonathan Wilkins"
spec.email = "jwilkins[at]nospam[dot]bitland[dot]net"
spec.has_rdoc = true
spec.extra_rdoc_files = ["README"]
- spec.files = %w(README nilsimsa.gemspec nilsimsa.rb bin/nilsimsa
- examples/simple.rb ext/extconf.rb ext/nilsimsa.c)
+ spec.files = %w(Gemfile Rakefile README nilsimsa.gemspec
+ lib/nilsimsa.rb
+ bin/nilsimsa
+ examples/simple.rb
+ ext/extconf.rb ext/nilsimsa.c)
spec.executables = ['nilsimsa']
# optional native component
View
54 spec/nilsimsa_spec.rb
@@ -0,0 +1,54 @@
+require 'bundler'
+Bundler.require :test
+
+$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
+require 'nilsimsa'
+#require "#{File.join(File.dirname(__FILE__), '..', 'ext', 'nilsimsa_native')}"
+
+describe Nilsimsa do
+
+ it "new() then update() should compute hexdigests for short strings correctly" do
+ n1 = Nilsimsa::new
+ n1.update("abcdefgh")
+ #puts "abcdefgh: #{n1.hexdigest=='14c8118000000000030800000004042004189020001308014088003280000078'}"
+ n1.hexdigest.should == '14c8118000000000030800000004042004189020001308014088003280000078'
+ end
+
+ it "new() should compute hexdigests correctly for an array of strings" do
+ n1 = Nilsimsa::new
+ n1.update("abcdefgh")
+ n2 = Nilsimsa::new("abcd","efgh")
+ #puts "abcd efgh: #{n2.hexdigest=='14c8118000000000030800000004042004189020001308014088003280000078'}"
+ #puts "digest: #{n1 == n2.digest}"
+ n2.hexdigest.should =='14c8118000000000030800000004042004189020001308014088003280000078'
+ n1.should == n2.digest
+ end
+
+ it "should allow updates" do
+ n1 = Nilsimsa::new("abcdefgh")
+ n1.hexdigest.should == '14c8118000000000030800000004042004189020001308014088003280000078'
+ n1.update("ijk")
+ n1.hexdigest.should == '14c811840010000c0328200108040630041890200217582d4098103280000078'
+ n2 = Nilsimsa::new("abcd","efgh")
+ n1.nilsimsa(n2.digest).should == 109
+ end
+
+# #puts 'Running selftest using native ruby version'
+# #selftest
+# begin # load C core - if available
+# if File.exists?('./nilsimsa_native')
+# require './nilsimsa_native'
+# puts 'Running selftest using compiled nilsimsa in current dir'
+# elsif File.exists?('./ext/nilsimsa_native')
+# require './ext/nilsimsa_native'
+# puts 'Running selftest using compiled nilsimsa in current dir'
+# else
+# require 'nilsimsa_native'
+# puts 'Running selftest using compiled nilsimsa'
+# end
+# selftest
+# rescue LoadError => e
+# puts "Couldnt run selftest with compiled nilsimsa"
+# end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.