Class: VariantAssigner
- Inherits:
-
Object
- Object
- VariantAssigner
- Defined in:
- lib/variant_assigner.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
- #assign(split, seed_hi, seed_lo) ⇒ Object
-
#initialize(key) ⇒ VariantAssigner
constructor
A new instance of VariantAssigner.
- #probability(seed_hi, seed_lo) ⇒ Object
- #put_uint32(buffer, x) ⇒ Object
Constructor Details
#initialize(key) ⇒ VariantAssigner
Returns a new instance of VariantAssigner.
9 10 11 12 13 |
# File 'lib/variant_assigner.rb', line 9 def initialize(key) md5 = Hashing.hash_unit(key.to_s) @key = MurmurHash3::V32.str_hash(md5) @normalizer = 1.0 / 0xffffffff end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/variant_assigner.rb', line 7 def key @key end |
Class Method Details
.choose_variant(split, prob) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/variant_assigner.rb', line 26 def self.choose_variant(split, prob) sum = 0 split.each_with_index do |s, i| sum += s return i if prob < sum end split.count - 1 end |
Instance Method Details
#assign(split, seed_hi, seed_lo) ⇒ Object
36 37 38 39 |
# File 'lib/variant_assigner.rb', line 36 def assign(split, seed_hi, seed_lo) prob = probability(seed_hi, seed_lo) self.class.choose_variant(split, prob) end |
#probability(seed_hi, seed_lo) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/variant_assigner.rb', line 15 def probability(seed_hi, seed_lo) buffer = Array.new put_uint32(buffer, seed_lo) put_uint32(buffer, seed_hi) put_uint32(buffer, key) hash = MurmurHash3::V32.str_hash(buffer.pack("C*")) prob = (hash & 0xffffffff) * @normalizer prob end |
#put_uint32(buffer, x) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/variant_assigner.rb', line 41 def put_uint32(buffer, x) buffer << (x & 0xff) buffer << ((x >> 8) & 0xff) buffer << ((x >> 16) & 0xff) buffer << ((x >> 24) & 0xff) end |