算法练习 - 自定义散列表 hashTable
Opened this issue · 0 comments
hszy00232 commented
const HashTable = function (obj) {
let length = 0;
/**
* 将传入的对象缓存,方便操作
*/
this._items = (function (obj) {
let items = {};
for (const p in obj) {
items[p] = obj[p];
length++;
}
return items;
})(obj);
/**
* 将key和value关联
* @param {string} key
* @param {string} value
* @returns {(undefined|Object)} key已经存在是返回value值,不存在返回undefinded并添加到列表中
*/
this.set = function (key, value) {
let previous = undefined;
if (this.has(key)) {
previous = this._items[key];
} else {
length++;
}
this._items[key] = value;
return previous;
}
/**
* 返回与指定键关联的值
* @param {string} key
* @returns {(undefined|Object)} undefined或关联的值
*/
this.get = function (key) {
return this._items.hasOwnProperty(key) ? this._items[key] : undefined;
}
/**
* 返回哈希表是否包含指定的键
* @param {string} key
* @returns {boolean}
*/
this.has = function (key) {
return this._items.hasOwnProperty(key);
}
/**
* 删除指定的键及其值
* @param {string} key 要删除的键
* @returns {(undefined|string)} 如果键不存在返回undefined,或删除项的值
*/
this.remove = function (key) {
if (this._items.has(key)) {
let previous = this._items[key];
length--;
delete this._items[key];
return previous
} else {
return undefined;
}
}
/**
* 返回一个包含所有key的数组
* @returns {Array}
*/
this.getKeys = function () {
let keys = [];
for (const i in this._items) {
if (this.has(i)) {
keys.push(i)
}
}
return keys;
}
/**
* 返回一个包含所有值的数组
* @returns {Array}
*/
this.getValues = function () {
let values = [];
for (const i in this._items) {
if (this.has(i)) {
values.push(this.items[i])
}
}
return values;
}
/**
* 迭代执行回调
* @param {function} callback 有两个参数key,value
*/
this.each = function (callback) {
for (const i in this._items) {
if (this._items.has(i)) {
callback(i, this._items[i]);
}
}
}
/**
* 删除hashmap上的所有键值对
*/
this.clear = function () {
this._items = {};
length = 0;
}
/**
* 获取hash表中的条目数
*/
Object.defineProperty(this, 'length', {
get: function () {
return length;
}
})
/**
* 获取hash表中所有key的数组
*/
Object.defineProperty(this, 'keys', {
get: function () {
return this.getKeys();
}
})
/**
* 获取hash表中所有key的值
*/
Object.defineProperty(this,'values',{
get: function(){
return this.getValues();
}
})
}
const hashtable = new HashTable({one: 1, two: 2, three: 3, cuatro: 4 });
console.log("Original length: " + hashtable.length); // Original length: 4
console.log('Value of key "one": ' + hashtable.get("one")); // Value of key "one": 1
console.log('Has key "foo"? ' + hashtable.has("foo")); // Has key "foo"? false
console.log('Previous value of key "foo": ' + hashtable.set("foo", "bar")); // Previous value of key "foo": undefined
console.log("Length after set: " + hashtable.length); // Length after set: 5
console.log('Value of key "foo": ' + hashtable.get("foo")); // Value of key "foo": bar
console.log('Value of key "cuatro": ' + hashtable.get("cuatro")); // Value of key "cuatro": 4
console.log("Get keys by using property: " + hashtable.keys); // Get keys by using property: one,two,three,cuatro,foo
hashtable.clear();
console.log("Length after clear: " + hashtable.length); // Length after clear: 0