Sha256: 14d6af5a127c48707285a047f3f17d0cd863a3258f64b39e303e11a77fd2d954

Contents?: true

Size: 1.06 KB

Versions: 43

Compression:

Stored size: 1.06 KB

Contents

class Murmur3
  ## MurmurHash3 was written by Austin Appleby, and is placed in the public
  ## domain. The author hereby disclaims copyright to this source code.

  MASK32 = 0xffffffff

  def self.murmur3_32_rotl(x, r)
    ((x << r) | (x >> (32 - r))) & MASK32
  end


  def self.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 self.murmur3_32__mmix(k1)
    k1 = (k1 * 0xcc9e2d51) & MASK32
    k1 = murmur3_32_rotl(k1, 15)
    (k1 * 0x1b873593) & MASK32
  end

  def self.murmur3_32(str, seed=0)
    h1 = seed
    numbers = str.unpack('V*C*')
    tailn = str.length % 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.length
    murmur3_32_fmix(h1)
  end

end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
prefab-cloud-ruby-0.13.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.12.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.11.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.10.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.9.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.8.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.7.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.6.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.5.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.5.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.4.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.3.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.2.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.1.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.1.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.1.0.alpha.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.0.28 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.0.27 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.0.26 lib/prefab/murmer3.rb
prefab-cloud-ruby-0.0.25 lib/prefab/murmer3.rb