Class: Hasher

Inherits:
Object
  • Object
show all
Defined in:
lib/hasher.rb

Defined Under Namespace

Classes: NonPrimeContainer

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Hasher

Returns a new instance of Hasher.

Raises:



4
5
6
7
# File 'lib/hasher.rb', line 4

def initialize(size)
  raise NonPrimeContainer unless FIRST_1K_PRIMES.include?(size)
  @size = size
end

Instance Method Details

#hash(input, length = input.length) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/hasher.rb', line 9

def hash(input, length=input.length)
  hash = 0
  length.times do |i|
    new_hash = hash << 0x7 ^ input[i]
    overflow = hash >> 0x15 & 0x1fc
    hash = new_hash ^ overflow
  end
  hash &= 0x7fffffff
  hash % @size
end