Permalink
Browse files

pure ruby works

  • Loading branch information...
funny-falcon committed Aug 2, 2012
0 parents commit a48a01624a142280dd7b4b595aa2cde2860c5c76
Showing with 313 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +29 −0 README.md
  5. +2 −0 Rakefile
  6. +5 −0 lib/murmurhash3.rb
  7. +155 −0 lib/murmurhash3/pure_ruby.rb
  8. +3 −0 lib/murmurhash3/version.rb
  9. +17 −0 murmurhash3.gemspec
  10. +59 −0 test/test_murmur.rb
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in murmurhash3.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Sokolov Yura 'funny-falcon'
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Murmurhash3
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'murmurhash3'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install murmurhash3
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
+require "murmurhash3/version"
+
+module MurmurHash3
+
+end
@@ -0,0 +1,155 @@
+require 'digest'
+module MurmurHash3
+ module PureRuby32
+ MASK32 = 0xffffffff
+ def murmur3_32_rotl(x, r)
+ ((x << r) | (x >> (32 - r))) & MASK32
+ end
+
+
+ def murmur3_32_fmix(h)
+ h &= MASK32
+ h ^= h >> 16
+ h = (h * 0x85ebca6b) & MASK32
+ h ^= h >> 13
+ h = (h * 0xc2b2ae35) & MASK32
+ h ^ (h >> 16)
+ end
+
+ def murmur3_32__mmix(k1)
+ k1 = (k1 * 0xcc9e2d51) & MASK32
+ k1 = murmur3_32_rotl(k1, 15)
+ (k1 * 0x1b873593) & MASK32
+ end
+
+ def murmur3_32_str_hash(str, seed=0)
+ h1 = seed
+ numbers = str.unpack('V*C*')
+ tailn = str.bytesize % 4
+ tail = numbers.slice!(numbers.size - tailn, tailn)
+ for k1 in numbers
+ h1 ^= murmur3_32__mmix(k1)
+ h1 = murmur3_32_rotl(h1, 13)
+ h1 = (h1*5 + 0xe6546b64) & MASK32
+ end
+
+ unless tail.empty?
+ k1 = 0
+ tail.reverse_each do |c1|
+ k1 = (k1 << 8) | c1
+ end
+ h1 ^= murmur3_32__mmix(k1)
+ end
+
+ h1 ^= str.bytesize
+ murmur3_32_fmix(h1)
+ end
+
+ def murmur3_32_int32_hash(i, seed=0)
+ str_hash([i].pack("V"), seed)
+ end
+
+ def murmur3_32_int64_hash(i, seed=0)
+ str_hash([i].pack("Q>"), seed)
+ end
+
+ class << self
+ include MurmurHash3::PureRuby32
+ alias fmix murmur3_32_fmix
+ alias str_hash murmur3_32_str_hash
+ alias int32_hash murmur3_32_int32_hash
+ alias int64_hash murmur3_32_int64_hash
+ end
+ end
+
+ module PureRuby128
+ MASK64 = 0xffff_ffff_ffff_ffff
+
+ def murmur3_128_rotl(x, r)
+ ((x << r) | (x >> (64 - r))) & MASK64
+ end
+
+ def murmur3_128_fmix(h)
+ h &= MASK64
+ h ^= h >> 33
+ h = (h * 0xff51afd7_ed558ccd) & MASK64
+ h ^= h >> 33
+ h = (h * 0xc4ceb9fe_1a85ec53) & MASK64
+ h ^ (h >> 33)
+ end
+
+ C1 = 0x87c37b91_114253d5
+ C2 = 0x4cf5ad43_2745937f
+ def murmur3_128__mmix1(k1)
+ k1 = (k1 * C1) & MASK64
+ k1 = murmur3_128_rotl(k1, 31)
+ (k1 * C2) & MASK64
+ end
+
+ def murmur3_128__mmix2(k2)
+ k2 = (k2 * C2) & MASK64
+ k2 = murmur3_128_rotl(k2, 33)
+ (k2 * C1) & MASK64
+ end
+
+ def murmur3_128_str_hash(str, seed=0)
+ h1 = h2 = seed
+ fast_part = ((str.bytesize / 16) * 16)
+ numbers = str.byteslice(0, fast_part).unpack('Q<*')
+ tail = str.byteslice(fast_part, str.bytesize - fast_part).unpack('C*')
+
+ numbers.each_slice(2) do |k1, k2|
+ h1 ^= murmur3_128__mmix1(k1)
+ h1 = murmur3_128_rotl(h1, 27)
+ h1 = (h1 + h2) & MASK64
+ h1 = (h1*5 + 0x52dce729) & MASK64
+ h2 ^= murmur3_128__mmix2(k2)
+ h2 = murmur3_128_rotl(h2, 31)
+ h2 = (h1 + h2) & MASK64
+ h2 = (h2*5 + 0x38495ab5) & MASK64
+ end
+
+ unless tail.empty?
+ if tail.size > 8
+ k2 = 0
+ tail[8,8].reverse_each do |c2|
+ k2 = (k2 << 8) | c2
+ end
+ h2 ^= murmur3_128__mmix2(k2)
+ end
+ k1 = 0
+ tail[0,8].reverse_each do |c1|
+ k1 = (k1 << 8) | c1
+ end
+ h1 ^= murmur3_128__mmix1(k1)
+ end
+
+ h1 ^= str.bytesize
+ h2 ^= str.bytesize
+ h1 = (h1 + h2) & MASK64
+ h2 = (h1 + h2) & MASK64
+ h1 = murmur3_128_fmix(h1)
+ h2 = murmur3_128_fmix(h2)
+
+ h1 = (h1 + h2) & MASK64
+ h2 = (h1 + h2) & MASK64
+ [h1 & 0xffffffff, h1 >> 32, h2 & 0xffffffff, h2 >> 32]
+ end
+
+ def murmur3_128_int32_hash(i, seed=0)
+ str_hash([i].pack("V"), seed)
+ end
+
+ def murmur3_128_int64_hash(i, seed=0)
+ str_hash([i].pack("Q>"), seed)
+ end
+
+ class << self
+ include MurmurHash3::PureRuby128
+ alias fmix murmur3_128_fmix
+ alias str_hash murmur3_128_str_hash
+ alias int32_hash murmur3_128_int32_hash
+ alias int64_hash murmur3_128_int64_hash
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module MurmurHash3
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,17 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/murmurhash3/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Sokolov Yura 'funny-falcon'"]
+ gem.email = ["funny.falcon@gmail.com"]
+ gem.description = %q{implementation of murmur3 hashing function}
+ gem.summary = %q{implements mumur3 hashing function}
+ gem.homepage = "https://github.com/funny-falcon/murmurhash3"
+
+ gem.files = Dir['ext/**/*'].grep(/\.(rb|c)$/) +
+ (Dir['lib/**/*'] + Dir['test/**/*']).grep(/\.rb$/)
+ gem.test_files = gem.files.grep(%r{^test/})
+ gem.name = "murmurhash3"
+ gem.require_paths = ["lib", "ext"]
+ gem.version = Murmurhash3::VERSION
+end
@@ -0,0 +1,59 @@
+require 'minitest/spec'
+require 'minitest/autorun'
+
+shared_examples_128 = proc do
+ it 'should make correct hash for string' do
+ murmur.str_hash('asdfqwer', 0).must_equal [0xd6d7d367, 0xcb41f064, 0x8973cd72, 0xc345e72e]
+ murmur.str_hash('asdfqwerzxcvyui', 0).must_equal [0x007b2172f, 0x64ecae1b, 0x1813b5a5, 0x9c674ee6]
+ murmur.str_hash('asdfqwerzxcvyuio', 0).must_equal [0xf508df57, 0xbb38f3fd, 0xf48c9d98, 0xb65c36cd]
+ murmur.str_hash('asdfqwerzxcvyuio!', 0).must_equal [0x8a011755, 0xb13d463f, 0x8386d32a, 0x0df8884c]
+ end
+
+ it 'should make correct hash for 32bit integer' do
+ murmur.int32_hash(1717859169).must_equal [0x20b48108, 0x10369ceb, 0x3ad523cc, 0xdacb587f]
+ murmur.int32_hash(1717859169).must_equal murmur.str_hash('asdf')
+ end
+
+ it 'should make correct hash for 64bit integer' do
+ murmur.int64_hash(0x12345678).must_equal murmur.str_hash("\x00\x00\x00\x00\x12\x34\x56\x78")
+ murmur.int64_hash(0x1234567812345678).must_equal murmur.str_hash("\x12\x34\x56\x78\x12\x34\x56\x78")
+ end
+
+ it 'should make correct fmix for 64bit integer' do
+ murmur.fmix(1717859169).must_equal 0xbefb9076a3712207
+ murmur.fmix(12345678912345678).must_equal 0x197ef59146f5221c
+ end
+end
+
+shared_examples_32 = proc do
+ it 'should make correct hash for string' do
+ murmur.str_hash('asdfqwer', 0).must_equal 0xa46b5209
+ murmur.str_hash('asdfqwerty', 0).must_equal 0xa3cfe04b
+ murmur.str_hash('asd', 0).must_equal 0x14570c6f
+ end
+
+ it 'should make correct hash for 32bit integer' do
+ murmur.int32_hash(1717859169).must_equal 0x1b20e026
+ murmur.int32_hash(1717859169).must_equal murmur.str_hash('asdf')
+ end
+
+ it 'should make correct hash for 64bit integer' do
+ murmur.int64_hash(0x12345678).must_equal murmur.str_hash("\x00\x00\x00\x00\x12\x34\x56\x78")
+ murmur.int64_hash(0x1234567812345678).must_equal murmur.str_hash("\x12\x34\x56\x78\x12\x34\x56\x78")
+ end
+
+ it 'should make correct fmix for 32bit integer' do
+ murmur.fmix(1717859169).must_equal 0x17561734
+ end
+end
+
+require 'murmurhash3/pure_ruby'
+describe "Pure ruby 32" do
+ let(:murmur) { MurmurHash3::PureRuby32 }
+ class_exec &shared_examples_32
+end
+
+describe "Pure ruby 128" do
+ let(:murmur) { MurmurHash3::PureRuby128 }
+ class_exec &shared_examples_128
+end

0 comments on commit a48a016

Please sign in to comment.