2023-01-06 03:39:19 +08:00
|
|
|
|
/**
|
2022-12-26 23:02:39 +08:00
|
|
|
|
* File: array_hash_map.js
|
|
|
|
|
* Created Time: 2022-12-26
|
|
|
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* 键值对 Number -> String */
|
2023-06-14 02:01:06 +08:00
|
|
|
|
class Pair {
|
2022-12-26 23:02:39 +08:00
|
|
|
|
constructor(key, val) {
|
|
|
|
|
this.key = key;
|
|
|
|
|
this.val = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
|
class ArrayHashMap {
|
2023-03-15 03:11:43 +08:00
|
|
|
|
#buckets;
|
2022-12-26 23:02:39 +08:00
|
|
|
|
constructor() {
|
2023-03-15 03:48:31 +08:00
|
|
|
|
// 初始化数组,包含 100 个桶
|
2023-03-15 03:11:43 +08:00
|
|
|
|
this.#buckets = new Array(100).fill(null);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
#hashFunc(key) {
|
|
|
|
|
return key % 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
get(key) {
|
|
|
|
|
let index = this.#hashFunc(key);
|
2023-06-14 02:01:06 +08:00
|
|
|
|
let pair = this.#buckets[index];
|
|
|
|
|
if (pair === null) return null;
|
|
|
|
|
return pair.val;
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
set(key, val) {
|
|
|
|
|
let index = this.#hashFunc(key);
|
2023-06-14 02:01:06 +08:00
|
|
|
|
this.#buckets[index] = new Pair(key, val);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
delete(key) {
|
|
|
|
|
let index = this.#hashFunc(key);
|
|
|
|
|
// 置为 null ,代表删除
|
2023-03-15 03:11:43 +08:00
|
|
|
|
this.#buckets[index] = null;
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取所有键值对 */
|
|
|
|
|
entries() {
|
|
|
|
|
let arr = [];
|
2023-03-15 03:11:43 +08:00
|
|
|
|
for (let i = 0; i < this.#buckets.length; i++) {
|
|
|
|
|
if (this.#buckets[i]) {
|
|
|
|
|
arr.push(this.#buckets[i]);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取所有键 */
|
|
|
|
|
keys() {
|
|
|
|
|
let arr = [];
|
2023-03-15 03:11:43 +08:00
|
|
|
|
for (let i = 0; i < this.#buckets.length; i++) {
|
|
|
|
|
if (this.#buckets[i]) {
|
2023-08-10 23:04:41 +08:00
|
|
|
|
arr.push(this.#buckets[i].key);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取所有值 */
|
|
|
|
|
values() {
|
|
|
|
|
let arr = [];
|
2023-03-15 03:11:43 +08:00
|
|
|
|
for (let i = 0; i < this.#buckets.length; i++) {
|
|
|
|
|
if (this.#buckets[i]) {
|
2023-08-10 23:04:41 +08:00
|
|
|
|
arr.push(this.#buckets[i].val);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
|
print() {
|
2023-06-14 02:01:06 +08:00
|
|
|
|
let pairSet = this.entries();
|
|
|
|
|
for (const pair of pairSet) {
|
|
|
|
|
if (!pair) continue;
|
|
|
|
|
console.info(`${pair.key} -> ${pair.val}`);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
const map = new ArrayHashMap();
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map.set(12836, '小哈');
|
|
|
|
|
map.set(15937, '小啰');
|
|
|
|
|
map.set(16750, '小算');
|
|
|
|
|
map.set(13276, '小法');
|
|
|
|
|
map.set(10583, '小鸭');
|
|
|
|
|
console.info('\n添加完成后,哈希表为\nKey -> Value');
|
|
|
|
|
map.print();
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
let name = map.get(15937);
|
|
|
|
|
console.info('\n输入学号 15937 ,查询到姓名 ' + name);
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.delete(10583);
|
|
|
|
|
console.info('\n删除 10583 后,哈希表为\nKey -> Value');
|
|
|
|
|
map.print();
|
|
|
|
|
|
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
console.info('\n遍历键值对 Key->Value');
|
2023-06-14 02:01:06 +08:00
|
|
|
|
for (const pair of map.entries()) {
|
|
|
|
|
if (!pair) continue;
|
|
|
|
|
console.info(pair.key + ' -> ' + pair.val);
|
2022-12-26 23:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
console.info('\n单独遍历键 Key');
|
|
|
|
|
for (const key of map.keys()) {
|
|
|
|
|
console.info(key);
|
|
|
|
|
}
|
|
|
|
|
console.info('\n单独遍历值 Value');
|
|
|
|
|
for (const val of map.values()) {
|
|
|
|
|
console.info(val);
|
|
|
|
|
}
|