Class: Fairy::HValueGenerator::MurMur

Inherits:
Object
  • Object
show all
Defined in:
lib/fairy/share/hash-murmur.rb

Overview

mhash is modiftied from MurmerHash(murmurhash.googlepages.com/).

Constant Summary collapse

FIX_MASK =
0x3fff_ffff
MASK24 =
0xffff_ffff_ffff
MAGIC =
0x7fd652ad & FIX_MASK
R =
18

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ MurMur

Returns a new instance of MurMur.



17
18
19
20
21
# File 'lib/fairy/share/hash-murmur.rb', line 17

def initialize(h)
	@seed = h + 0xdeadbeef & FIX_MASK
#	@postfix = [@seed].pack("N")[1..3]
	@postfix = "000"
end

Class Method Details

.create_seedObject



54
55
56
# File 'lib/fairy/share/hash-murmur.rb', line 54

def MurMur.create_seed
	rand(FIX_MASK)
end

Instance Method Details

#str_hash(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fairy/share/hash-murmur.rb', line 32

def str_hash(data)
	h = @seed

	for k in (data+@postfix).unpack("N*")
	  k *= MAGIC
	  k ^= k >> R
	  k &= FIX_MASK
	  k *= MAGIC
	  k &= FIX_MASK

	  h = @seed * MAGIC
	  k &= FIX_MASK
	  h ^= k
	end

	h ^= h >> 13
	h *= MAGIC
	h &= FIX_MASK
	h ^= h >> 15
	h
end

#value(data) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/fairy/share/hash-murmur.rb', line 23

def value(data)
	case data
	when String
	  str_hash(data)
	else
	  ERR::Raise ERR::NoImpliment, "non-string key(#{data.inspect})"
	end
end