Module: GTools::GMath
- Defined in:
- lib/gtools.rb
Overview
:nodoc:
Class Attribute Summary collapse
-
.hash_seed ⇒ Object
Returns the value of attribute hash_seed.
Class Method Summary collapse
Class Attribute Details
.hash_seed ⇒ Object
Returns the value of attribute hash_seed.
115 116 117 |
# File 'lib/gtools.rb', line 115 def hash_seed @hash_seed end |
Class Method Details
.fmod(v, x) ⇒ Object
124 125 126 127 128 |
# File 'lib/gtools.rb', line 124 def self.fmod(v, x) i = (v / x).floor r = Integer(v - Float(i * x)) r end |
.hash(str) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gtools.rb', line 137 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
130 131 132 133 134 135 |
# File 'lib/gtools.rb', line 130 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
119 120 121 122 |
# File 'lib/gtools.rb', line 119 def self.urshift(v, amt) mask = (1 << (32 - amt)) - 1 (v >> amt) & mask end |