* HashMap.jsjavascript
/** * Created by Mch on 8/27/18. */ function KeyValuePair(key, value) { this.key = key; this.value = value; } KeyValuePair.prototype.toString = function() { return '['+ this.key +'-'+ this.value +']'; }; function HashMap() { this.map = []; } // 2 hash functions var hash = { loselose: function(key) { var hash = 0; for (var i = 0; i < key.length; i++) { hash += key.charCodeAt(i); } return hash % 37; }, djb2: function(key) { var hash = 5381; for (var i = 0; i < key.length; i++) { hash = hash * 33 + key.charCodeAt(i); } return hash % 1013; } }; // pick a hash function HashMap.hashCode = hash.loselose; // 線性探索 HashMap.prototype = { put: function(key, value) { var position = HashMap.hashCode(key); if (this.map[position] === undefined) { this.map[position] = new KeyValuePair(key, value); } else { var index = ++position; while (this.map[index] !== undefined) { index++; // Array index out of bounds! realloc if (index >= this.map.length) { var a = new Array(this.map.length*2); console.debug("realloc - " + a.length); this.map.forEach(function(e, i) { a[i] = e; }); delete this.map; this.map = a; } } this.map[index] = new KeyValuePair(key, value); } }, get: function(key) { var position = HashMap.hashCode(key); if (this.map[position] !== undefined) { if (this.map[position].key === key) { return this.map[position].value; } else { var index = ++position; while (index < this.map.length && (this.map[index] === undefined || this.map[index].key !== key)) { index++; } if (index >= this.map.length || this.map[index] === undefined) { return undefined; } if (this.map[index].key === key) { return this.map[index].value; } } } return undefined; }, remove: function(key) { var position = HashMap.hashCode(key); if (this.map[position] === undefined) { return false; } if (this.map[position].key === key) { this.map[position] = undefined; } else { var index = ++position; while (this.map[index] === undefined || this.map[index].key !== key) { index++; } if (this.map[index].key === key) { this.map[index] = undefined; } } return true; }, print: function() { for (var i = 0; i < this.map.length; ++i) { if (this.map[i] !== undefined) { console.log(i + ": " + this.map[i]); } } } }; exports.HashMap = HashMap;
* TestHashMap.jsjava
/** * Created by Mch on 8/27/18. */ var HashMap = require('./Collection/HashMap').HashMap; function TestHashMap() {} TestHashMap.main = function() { var hash = new HashMap(); hash.put('Gandalf', 'gandalf@email.com'); hash.put('John', 'johnsnow@email.com'); hash.put('Tyrion', 'tyrion@email.com'); hash.put('Aaron', 'aaron@email.com'); hash.put('Donnie', 'donnie@email.com'); hash.put('Ana', 'ana@email.com'); hash.put('Jonathan', 'jonathan@email.com'); hash.put('Jamie', 'jamie@email.com'); hash.put('Sue', 'sue@email.com'); hash.put('Mindy', 'mindy@email.com'); hash.put('Paul', 'paul@email.com'); hash.put('Nathan', 'nathan@email.com'); hash.print(); console.log(hash.get('Sue')); hash.remove('Sue'); console.log(hash.get('Sue')); }; TestHashMap.main();
5: [Jonathan-jonathan@email.com]
6: [Jamie-jamie@email.com]
7: [Sue-sue@email.com]
10: [Nathan-nathan@email.com]
13: [Donnie-donnie@email.com]
14: [Ana-ana@email.com]
16: [Tyrion-tyrion@email.com]
17: [Aaron-aaron@email.com]
19: [Gandalf-gandalf@email.com]
29: [John-johnsnow@email.com]
32: [Mindy-mindy@email.com]
33: [Paul-paul@email.com]
sue@email.com
undefinedide
* 分離連接法函數
使用loselose這個hash函數是爲了演示 解決衝突,ui
djb2這個hash函數更好this
var djb2HashCode = function(key) { var hash = 5381; for (var i = 0; i < key.length; i++) { hash = hash * 33 + key.charCodeAt(i); } return hash % 1013; }