Sha256: 45662824eaa8d0c3d17c67d8ce241afff38e5dd3051240c66d9869566520b190

Contents?: true

Size: 1.09 KB

Versions: 54

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

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

54 entries across 54 versions & 1 rubygems

Version Path
prefab-cloud-ruby-1.8.8.pre.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.7 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.6 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.5 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.4 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.3 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.2 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.8.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.7.2 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.7.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.7.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.6.2 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.6.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.6.0 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.5.1 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.6.0.pre2 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.6.0.pre1 lib/prefab/murmer3.rb
prefab-cloud-ruby-0 lib/prefab/murmer3.rb
prefab-cloud-ruby-1.5.0 lib/prefab/murmer3.rb