Module: GeneralHashFunctionsPure
- Defined in:
- lib/general-hash-functions.rb
Overview
**************************************************************************
-
*
-
General Purpose Hash Function Algorithms Library *
-
*
-
Author: Arash Partow - 2002 *
-
URL: www.partow.net *
-
*
-
Copyright notice: *
-
Free use of the General Purpose Hash Function Algorithms Library is *
-
permitted under the guidelines and in accordance with the most current *
-
version of the Common Public License. *
-
*
**************************************************************************
Class Method Summary collapse
- .aphash(str) ⇒ Object
- .bkdrhash(str) ⇒ Object
- .bphash(str) ⇒ Object
- .dekhash(str) ⇒ Object
- .djbhash(str) ⇒ Object
- .elfhash(str) ⇒ Object
- .fnvhash(str) ⇒ Object
- .jshash(str) ⇒ Object
- .rshash(str) ⇒ Object
- .sdbmhash(str) ⇒ Object
Class Method Details
.aphash(str) ⇒ Object
115 116 117 |
# File 'lib/general-hash-functions.rb', line 115 def self.aphash( str ) raise NotImplementedError::new("Arash Partow's hash is provided by C extension only.") end |
.bkdrhash(str) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/general-hash-functions.rb', line 57 def self.bkdrhash( str ) seed = 131 # 31 131 1313 13131 131313 etc.. hash = 0 str.each_byte{ |i| hash = (( hash * seed ) + i) & 0xffffffff } return hash end |
.bphash(str) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/general-hash-functions.rb', line 94 def self.bphash( str ) hash = 0 str.each_byte{ |i| hash = (hash << 7 ^ i) & 0xffffffff } return hash end |
.dekhash(str) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/general-hash-functions.rb', line 85 def self.dekhash( str ) hash = str.length str.each_byte{ |i| hash = (((hash << 5) ^ (hash >> 27)) ^ i) & 0xffffffff } return hash end |
.djbhash(str) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/general-hash-functions.rb', line 76 def self.djbhash( str ) hash = 5381 str.each_byte{ |i| hash = (((hash << 5) + hash) + i) & 0xffffffff } return hash end |
.elfhash(str) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/general-hash-functions.rb', line 42 def self.elfhash( str ) hash = 0 x = 0 str.each_byte{ |i| hash = (hash << 4) + i if (x = hash & 0xF0000000) != 0 hash ^= (x >> 24) hash &= ~x end hash &= 0xffffffff } return hash end |
.fnvhash(str) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/general-hash-functions.rb', line 103 def self.fnvhash( str ) fnv_prime = 0x811C9DC5 hash = 0 str.each_byte{ |i| hash *= fnv_prime hash ^= i hash &= 0xffffffff } return hash end |
.jshash(str) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/general-hash-functions.rb', line 33 def self.jshash( str ) hash = 1315423911 str.each_byte{ |i| hash ^= ( ( hash << 5 ) + i + ( hash >> 2 ) ) & 0xffffffff } return hash end |
.rshash(str) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/general-hash-functions.rb', line 21 def self.rshash( str ) a = 63689 b = 378551 hash = 0 str.each_byte{ |i| hash = (hash * a + i) & 0xffffffff a = (a * b) & 0xffffffff } return hash end |
.sdbmhash(str) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/general-hash-functions.rb', line 67 def self.sdbmhash( str ) hash = 0 str.each_byte{ |i| hash = (i + ( hash << 6 ) + ( hash << 16 ) - hash) & 0xffffffff } return hash end |