From 630bbac28580dd165cee51af75eb60aabc6aaf6f Mon Sep 17 00:00:00 2001 From: justin Date: Mon, 26 Dec 2022 23:02:39 +0800 Subject: [PATCH 1/3] Add the JavaScript code (Chapter of Hashing) --- .../chapter_hashing/array_hash_map.js | 129 ++++++++++++++++++ codes/javascript/chapter_hashing/hash_map.js | 44 ++++++ 2 files changed, 173 insertions(+) create mode 100644 codes/javascript/chapter_hashing/array_hash_map.js create mode 100644 codes/javascript/chapter_hashing/hash_map.js diff --git a/codes/javascript/chapter_hashing/array_hash_map.js b/codes/javascript/chapter_hashing/array_hash_map.js new file mode 100644 index 0000000000..056a8007e8 --- /dev/null +++ b/codes/javascript/chapter_hashing/array_hash_map.js @@ -0,0 +1,129 @@ +/* + * File: array_hash_map.js + * Created Time: 2022-12-26 + * Author: Justin (xiefahit@gmail.com) + */ + +/* 键值对 Number -> String */ +class Entry { + constructor(key, val) { + this.key = key; + this.val = val; + } +} + +/* 基于数组简易实现的哈希表 */ +class ArrayHashMap { + #bucket + constructor() { + // 初始化一个长度为 100 的桶(数组) + this.#bucket = new Array(100).fill(null); + } + + /* 哈希函数 */ + #hashFunc(key) { + return key % 100; + } + + /* 查询操作 */ + get(key) { + let index = this.#hashFunc(key); + let entry = this.#bucket[index]; + if (entry === null) return null; + return entry.val; + } + + /* 添加操作 */ + set(key, val) { + let index = this.#hashFunc(key); + this.#bucket[index] = new Entry(key, val); + } + + /* 删除操作 */ + delete(key) { + let index = this.#hashFunc(key); + // 置为 null ,代表删除 + this.#bucket[index] = null; + } + + /* 获取所有键值对 */ + entries() { + let arr = []; + for (let i = 0; i < this.#bucket.length; i++) { + if (this.#bucket[i]) { + arr.push(this.#bucket[i]); + } + } + return arr; + } + + /* 获取所有键 */ + keys() { + let arr = []; + for (let i = 0; i < this.#bucket.length; i++) { + if (this.#bucket[i]) { + arr.push(this.#bucket[i]?.key); + } + } + return arr; + } + + /* 获取所有值 */ + values() { + let arr = []; + for (let i = 0; i < this.#bucket.length; i++) { + if (this.#bucket[i]) { + arr.push(this.#bucket[i]?.val); + } + } + return arr; + } + + /* 打印哈希表 */ + print() { + let entrySet = this.entries(); + for (const entry of entrySet) { + if (!entry) continue; + console.info(`${entry.key} -> ${entry.val}`); + } + } +} + +/* 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'); +for (const entry of map.entries()) { + if (!entry) continue; + console.info(entry.key + ' -> ' + entry.val); +} +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); +} diff --git a/codes/javascript/chapter_hashing/hash_map.js b/codes/javascript/chapter_hashing/hash_map.js new file mode 100644 index 0000000000..197fb7dd95 --- /dev/null +++ b/codes/javascript/chapter_hashing/hash_map.js @@ -0,0 +1,44 @@ +/* + * File: hash_map.js + * Created Time: 2022-12-26 + * Author: Justin (xiefahit@gmail.com) + */ + +/* Driver Code */ +/* 初始化哈希表 */ +const map = new Map(); + +/* 添加操作 */ +// 在哈希表中添加键值对 (key, value) +map.set(12836, '小哈'); +map.set(15937, '小啰'); +map.set(16750, '小算'); +map.set(13276, '小法'); +map.set(10583, '小鸭'); +console.info('\n添加完成后,哈希表为\nKey -> Value'); +console.info(map); + +/* 查询操作 */ +// 向哈希表输入键 key ,得到值 value +let name = map.get(15937); +console.info('\n输入学号 15937 ,查询到姓名 ' + name); + +/* 删除操作 */ +// 在哈希表中删除键值对 (key, value) +map.delete(10583); +console.info('\n删除 10583 后,哈希表为\nKey -> Value'); +console.info(map); + +/* 遍历哈希表 */ +console.info('\n遍历键值对 Key->Value'); +for (const [k, v] of map.entries()) { + console.info(k + ' -> ' + v); +} +console.info('\n单独遍历键 Key'); +for (const k of map.keys()) { + console.info(k); +} +console.info('\n单独遍历值 Value'); +for (const v of map.values()) { + console.info(v); +} From 650872cb05bf0a8093fa11a1f78158d227e83b06 Mon Sep 17 00:00:00 2001 From: justin Date: Mon, 26 Dec 2022 23:03:12 +0800 Subject: [PATCH 2/3] Add the JavaScript code to docs (Chapter of Hashing) --- docs/chapter_hashing/hash_map.md | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index b51a1da912..c441c7187c 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -132,7 +132,23 @@ comments: true === "JavaScript" ```js title="hash_map.js" + /* 初始化哈希表 */ + const map = new ArrayHashMap(); + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.set(12836, '小哈'); + map.set(15937, '小啰'); + map.set(16750, '小算'); + map.set(13276, '小法'); + map.set(10583, '小鸭'); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + let name = map.get(15937); + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.delete(10583); ``` === "TypeScript" @@ -244,7 +260,20 @@ comments: true === "JavaScript" ```js title="hash_map.js" - + /* 遍历哈希表 */ + // 遍历键值对 key->value + for (const entry of map.entries()) { + if (!entry) continue; + console.info(entry.key + ' -> ' + entry.val); + } + // 单独遍历键 key + for (const key of map.keys()) { + console.info(key); + } + // 单独遍历值 value + for (const val of map.values()) { + console.info(val); + } ``` === "TypeScript" @@ -500,7 +529,48 @@ $$ === "JavaScript" ```js title="array_hash_map.js" + /* 键值对 Number -> String */ + class Entry { + constructor(key, val) { + this.key = key; + this.val = val; + } + } + + /* 基于数组简易实现的哈希表 */ + class ArrayHashMap { + #bucket + constructor() { + // 初始化一个长度为 100 的桶(数组) + this.#bucket = new Array(100).fill(null); + } + /* 哈希函数 */ + #hashFunc(key) { + return key % 100; + } + + /* 查询操作 */ + get(key) { + let index = this.#hashFunc(key); + let entry = this.#bucket[index]; + if (entry === null) return null; + return entry.val; + } + + /* 添加操作 */ + set(key, val) { + let index = this.#hashFunc(key); + this.#bucket[index] = new Entry(key, val); + } + + /* 删除操作 */ + delete(key) { + let index = this.#hashFunc(key); + // 置为 null ,代表删除 + this.#bucket[index] = null; + } + } ``` === "TypeScript" From 60c715b041fdc4c000b2dcdaf88c7c1c23393ea2 Mon Sep 17 00:00:00 2001 From: justin Date: Mon, 26 Dec 2022 23:45:23 +0800 Subject: [PATCH 3/3] Update JavaScript style (Chapter of Hashing) --- codes/javascript/chapter_hashing/array_hash_map.js | 2 +- docs/chapter_hashing/hash_map.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/javascript/chapter_hashing/array_hash_map.js b/codes/javascript/chapter_hashing/array_hash_map.js index 056a8007e8..f5c77aa2b2 100644 --- a/codes/javascript/chapter_hashing/array_hash_map.js +++ b/codes/javascript/chapter_hashing/array_hash_map.js @@ -14,7 +14,7 @@ class Entry { /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - #bucket + #bucket; constructor() { // 初始化一个长度为 100 的桶(数组) this.#bucket = new Array(100).fill(null); diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index c441c7187c..61f4c2293f 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -539,7 +539,7 @@ $$ /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - #bucket + #bucket; constructor() { // 初始化一个长度为 100 的桶(数组) this.#bucket = new Array(100).fill(null);