Module: GTools::GMath

Defined in:
lib/gtools.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.hash_seedObject

Returns the value of attribute hash_seed.



68
69
70
# File 'lib/gtools.rb', line 68

def hash_seed
  @hash_seed
end

Class Method Details

.fmod(v, x) ⇒ Object



77
78
79
80
81
# File 'lib/gtools.rb', line 77

def self.fmod(v, x)
  i = (v / x).floor
  r = Integer(v - Float(i * x))
  r
end

.hash(str) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gtools.rb', line 90

def self.hash(str)
  v = 16909125
  for i in 0..(str.length - 1)
    v ^= self.hash_seed[i % 87].ord ^ str[i].ord
    v = fmod(v, 0xFFFFFFFF + 1) if 0xFFFFFFFF < v || -0xFFFFFFFF > v
    v -= 0xFFFFFFFF + 1 if 0x7FFFFFFF < v
    v += 0xFFFFFFFF + 1 if -0x80000000 > v
    v = urshift(v, 23) | v << 9
  end
  "8" + hexenc(v)
end

.hexenc(v) ⇒ Object



83
84
85
86
87
88
# File 'lib/gtools.rb', line 83

def self.hexenc(v)
  x = urshift(v, 24).to_s(16).rjust(2, "0")
  x += (urshift(v, 16) & 255).to_s(16).rjust(2, "0")
  x += (urshift(v, 8) & 255).to_s(16).rjust(2, "0")
  x + (v & 255).to_s(16).rjust(2, "0")
end

.urshift(v, amt) ⇒ Object



72
73
74
75
# File 'lib/gtools.rb', line 72

def self.urshift(v, amt)
  mask = (1 << (32 - amt)) - 1
  (v >> amt) & mask
end