|
| 1 | +function HashTable() { |
| 2 | + this.table = new Array(137); |
| 3 | + this.simpleHash = simpleHash; |
| 4 | + this.betterHash = betterHash; |
| 5 | + this.showDistro = showDistro; |
| 6 | + this.put = put; |
| 7 | + this.get = get; |
| 8 | + // for linear probing |
| 9 | + this.values = []; |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +// put for linear probing |
| 14 | +function put(key, data) { |
| 15 | + var pos = this.betterHash(key); |
| 16 | + if(this.table[pos] == undefined) { |
| 17 | + this.table[pos] = key; |
| 18 | + this.values[pos] = data; |
| 19 | + } |
| 20 | + else { |
| 21 | + while ( this.table[pos] != undefined ) { |
| 22 | + pos++; |
| 23 | + } |
| 24 | + this.table[pos] = key; |
| 25 | + this.values[pos] = data; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +// put for separate chaining |
| 31 | +function put(key, data) { |
| 32 | + var pos = this.betterHash(key); |
| 33 | + var index = 0; |
| 34 | + if (this.table[pos][index] == undefined) { |
| 35 | + this.table[pos][index] = key; |
| 36 | + this.table[pos][index+1] = data; |
| 37 | + } |
| 38 | + index += 2; |
| 39 | + else { |
| 40 | + while ( this.table[pos][index] != undefined) { |
| 41 | + index += 2; |
| 42 | + } |
| 43 | + this.table[pos][index] = key; |
| 44 | + this.table[pos][index+1] = data; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// modulo |
| 49 | +function simpleHash( data ) { |
| 50 | + var total = 0; |
| 51 | + for (var i = 0 ; i < data.length ; ++i ) { |
| 52 | + total += data.charCodeAt(i); |
| 53 | + } |
| 54 | + print( "Hash value: " + data + " -> " + total ); |
| 55 | + return total % this.table.length; |
| 56 | +} |
| 57 | + |
| 58 | +function betterHash(string) { |
| 59 | + const H = 37; |
| 60 | + var total = 0; |
| 61 | + for (var i=0; i < string.length; ++i) { |
| 62 | + // Horner's method |
| 63 | + total += H * total + string.charCodeAt(i); |
| 64 | + } |
| 65 | + total = total % this.table.length; |
| 66 | + if (total < 0) { |
| 67 | + total += this.table.length-1; |
| 68 | + } |
| 69 | + return parseInt(total); |
| 70 | +} |
| 71 | + |
| 72 | +function showDistro() { |
| 73 | + var n = 0; |
| 74 | + for( var i = 0 ; i < this.table.length; ++i) { |
| 75 | + if(this.table[i] != undefined) { |
| 76 | + print( i + ": " + this.table[i] ); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function getRandomInt ( min, max) { |
| 82 | + return Math.floor(Math.random() * (max - min +1)) + min; |
| 83 | +} |
| 84 | + |
| 85 | +function genStruData(arr) { |
| 86 | + for (var i = 0; i < arr.length; ++i) { |
| 87 | + var num = ""; |
| 88 | + for (var j = 1 ; j <= 9; ++j ) { |
| 89 | + num += Math.floor(Math.random() * 10); |
| 90 | + } |
| 91 | + num += getRandomInt(50,100); |
| 92 | + arr[i] = num; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function buildChains(arr) { |
| 97 | + for ( var i = 0; i < arr.length; ++i) { |
| 98 | + arr[i] = new Array(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +function inHash(key, arr) { |
| 104 | + var hash = simpleHash(key, arr); |
| 105 | + var n = 0; |
| 106 | + if ( key == arr[hash][n]) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + else { |
| 110 | + while ( arr[hash][n] != undefined) { |
| 111 | + if ( arr[hash][n] == key) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + ++n; |
| 115 | + } |
| 116 | + } |
| 117 | + return false; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +// get for separate chaining |
| 122 | +function get(key) { |
| 123 | + var index = 0; |
| 124 | + var hash = this.betterHash(key); |
| 125 | + if ( this.table[hash][index] = key) { |
| 126 | + return this.table[hash][index+1]; |
| 127 | + } |
| 128 | + index += 2; |
| 129 | + else { |
| 130 | + while (this.table[hash][index] != key){ |
| 131 | + index += 2; |
| 132 | + } |
| 133 | + return this.table[hash][index+1]; |
| 134 | + } |
| 135 | + return undefined; |
| 136 | +} |
| 137 | + |
| 138 | +// get for linear probing |
| 139 | +function get(key) { |
| 140 | + var hash = -1; |
| 141 | + hash = this.betterHash(key); |
| 142 | + if (hash > -1 ) { |
| 143 | + for (var i = hash; this.table[hash] != undefined;i++) { |
| 144 | + if ( this.table[hash] == key) { |
| 145 | + return this.values[hash]; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return undefined; |
| 150 | +} |
| 151 | + |
| 152 | +function get(key) { |
| 153 | + return this.table[this.betterHash(key)]; |
| 154 | +} |
0 commit comments