Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /** * Block. The object to hold the actual values. * @constructor */ function Block() { /** * The binary address of the block. * Only used for visualization. * @type {string} */ this.index = undefined; /** * An array to store the values of this block. * @type {number[]} */ this.values = []; /** * A nested Block object that holds the overflowing values. * @type {Block} */ this.overflow = undefined; } /** * LinearHashing */ export default class LinearHashing { /** * An array of Block objects to hold the values. * @type {Block[]} */ blocks = []; /** * Initially addressable number of Blocks (in bits). * Example: * d = 3 -> 3 bit addresses, 4 addressable blocks. * d = 4 -> 4 bit addresses, 8 addressable blocks. * @type {number} */ d = 3; /** * Block Size * @type {number} */ b = 4; /** * Next To Split. * @todo find out what nextToSplit means. * @type {number} */ nextToSplit = 0; /** * Initialize an empty hash table. * @param {number} addressLength * @param {number} blockSize */ init(addressLength = 2, blockSize = 2) { this.blocks = []; this.d = addressLength; this.b = blockSize; this.nextToSplit = 0; // fill the blocks array with as many blocks as should be initially addressable. for (let i = 0; i < 2 ** this.d; i += 1) { const block = new Block(); // convert i to binary, only use the d rightmost bits block.index = i.toString(2).slice(-Math.abs(this.d)); this.blocks.push(block); } } /** * The hash function is dependent on the length of the address space in bits (d). * @param {number} value */ hash(value) { // convert value to binary, only use the d rightmost bits let addressBinary = value.toString(2).slice(-Math.abs(this.d)); let addressDecimal = parseInt(addressBinary, 2); // todo: reason for the following if-condition? if (addressDecimal < this.nextToSplit) { // convert value to binary, only use the d-1 rightmost bits addressBinary = value.toString(2).slice(-Math.abs(this.d + 1)); addressDecimal = parseInt(addressBinary, 2); } return addressDecimal; } /** * Insert the given value into the hash table. */ insert(value) { if (this.blocks.length === 0) { // Table not created return; } // Calculate the address of the block where value should go let addressDecimal = this.hash(value); if (this.blocks[addressDecimal].values.length < this.b) { // free space in the designated bucket, put the value there, done. this.blocks[addressDecimal].values.push(value); } else { // designated bucket is full. // descend down in last overflow block let currentBlock = this.blocks[addressDecimal]; while (currentBlock.overflow !== undefined) { currentBlock = currentBlock.overflow; } if (currentBlock.values.length < this.b) { // free space in currentBlock, put the value there, done. currentBlock.values.push(value); } else { // this is where I lose it: // not enough space in currentBlock, split. const newOverflow = new Block(); newOverflow.values.push(value); currentBlock.overflow = newOverflow; // add new Block at the end and update vars currentBlock = this.blocks[this.nextToSplit]; const newBlock = new Block(); newBlock.index = `1${currentBlock.index}`; this.blocks.push(newBlock); // get all values from act row+overflows const vals = []; while (currentBlock !== undefined) { for (let i = 0; i < currentBlock.values.length; i += 1) { vals.push(currentBlock.values[i]); } currentBlock = currentBlock.overflow; } // delete all values overflows of act row currentBlock = this.blocks[this.nextToSplit]; currentBlock.index = `0${currentBlock.index}`; currentBlock.values = []; currentBlock.overflow = undefined; // d wird erhöht, wenn splitting für ursprünglichen Primärbereich einmal durchgeführt wurde: this.nextToSplit += 1; if (this.nextToSplit === 2 ** this.d) { this.d += 1; this.nextToSplit = 0; } // redistribute for (let i = 0; i < vals.length; i += 1) { // Calculate the address/bucket/block where value should go addressDecimal = this.hash(vals[i]); // descend down in last overflow block currentBlock = this.blocks[addressDecimal]; while (currentBlock.overflow !== undefined) { currentBlock = currentBlock.overflow; } if (currentBlock.values.length < this.b) { currentBlock.values.push(vals[i]); } else { // overflow again? const overflowBlock = new Block(); overflowBlock.values.push(vals[i]); currentBlock.overflow = overflowBlock; } } } } } /** * Search for the given value in the hash table. * @param value */ search(value) { if (this.blocks.length === 0) { // Table not created return; } const addressDecimal = this.hash(value); let currentBlock = this.blocks[addressDecimal]; let found = false; let index = 0; while (!found) { for (let i = 0; i < this.b; i += 1) { if (currentBlock.values[i] === value) { found = true; index = i; break; } else if (currentBlock.values[i] === undefined) { break; } } if (!found) { currentBlock = currentBlock.overflow; } if (currentBlock === undefined) { // reached last overflow block, value not found. break; } } // if 'found', then data is at 'index'. // else, did not find anything. } /** * Remove the given value from the hash table. * @param value */ remove(value) { if (this.blocks.length === 0) { // Table not created return; } const addressDecimal = this.hash(value); let currentBlock = this.blocks[addressDecimal]; let previousBlock; let found = false; let index = 0; while (!found) { for (let i = 0; i < this.b; i += 1) { if (currentBlock.values[i] === value) { found = true; index = i; break; } else if (currentBlock.values[i] === undefined) { break; } } if (!found) { previousBlock = currentBlock; currentBlock = currentBlock.overflow; } if (currentBlock === undefined) { // reached last overflow block, value not found. return; } } // remove value currentBlock.values.splice(index, 1); // move all values to the left let { overflow } = currentBlock; if (overflow === undefined && currentBlock.values.length < 1 && previousBlock !== undefined) { previousBlock.overflow = undefined; return; } while (overflow !== undefined) { currentBlock.values.push(overflow.values[0]); overflow.values.splice(0, 1); // overflow now empty: if (overflow.values.length < 1) { if (overflow.overflow === undefined) { currentBlock.overflow = undefined; } else { currentBlock.overflow = overflow.overflow; } } currentBlock = overflow; overflow = currentBlock.overflow; } } } |