From bd21fd8be9e8e2a07f41658992ec012729d34495 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 19:29:06 +1100 Subject: [PATCH] fixed: several bugs --- .../chapter_hashing/array_hash_map.ts | 23 +++--- codes/typescript/tsconfig.json | 3 +- docs/chapter_hashing/hash_map.md | 71 ++++++++++++++++--- 3 files changed, 77 insertions(+), 20 deletions(-) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index d5b5a9f80..e4d1c83f5 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -1,4 +1,4 @@ -/* 键值对 int->String */ +/* 键值对 Number -> String */ class Entry { public key: number; public val: string; @@ -26,19 +26,23 @@ class ArrayList { public set(key: number, val: string | null) { if (val !== null) { this.elements[key] = new Entry(key, val); + } else { + this.elements[key] = null as any; } - this.elements[key] = null as any; } /* 获取 */ - public get(key: number): string { - return this.elements[key].val; + public get(key: number): string | null { + if (this.elements[key] instanceof Entry) { + return this.elements[key].val; + } + return null; } public entrySet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i]); } } @@ -48,7 +52,7 @@ class ArrayList { public valueSet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].val); } } @@ -58,7 +62,7 @@ class ArrayList { public keySet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].key); } } @@ -84,9 +88,7 @@ class ArrayHashMap { public get(key: number): string | null { let index = this.hashFunc(key); let val = this.bucket.get(index); - if (val === null) { - return null; - } + if (val === null) return null; return val; } @@ -122,6 +124,7 @@ class ArrayHashMap { public print() { let entrySet = this.entrySet(); for (const entry of entrySet) { + if (!entry) continue; console.info(`${entry.key} -> ${entry.val}`); } } diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json index c53bf4f72..446280b19 100644 --- a/codes/typescript/tsconfig.json +++ b/codes/typescript/tsconfig.json @@ -3,9 +3,8 @@ "target": "es6", "moduleResolution": "node", "resolveJsonModule": true, - "module": "esnext", + "module": "commonjs", "strict": true, - "importHelpers": true, "noEmit": false, "sourceMap": true, "baseUrl": ".", diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 2fed2facd..981204b71 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -510,7 +510,7 @@ $$ === "TypeScript" ```typescript title="array_hash_map.ts" - /* 键值对 int->String */ + /* 键值对 Number -> String */ class Entry { public key: number; public val: string; @@ -538,19 +538,23 @@ $$ public set(key: number, val: string | null) { if (val !== null) { this.elements[key] = new Entry(key, val); + } else { + this.elements[key] = null as any; } - this.elements[key] = null as any; } /* 获取 */ - public get(key: number): string { - return this.elements[key].val; + public get(key: number): string | null { + if (this.elements[key] instanceof Entry) { + return this.elements[key].val; + } + return null; } public entrySet() { - let arr = []; + let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i]); } } @@ -560,7 +564,7 @@ $$ public valueSet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].val); } } @@ -570,13 +574,64 @@ $$ public keySet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].key); } } return arr; } } + + /* 基于数组简易实现的哈希表 */ + class ArrayHashMap { + // 初始化一个长度为 100 的桶(数组) + private bucket: ArrayList; + + constructor() { + this.bucket = new ArrayList(100); + } + + /* 哈希函数 */ + private hashFunc(key: number): number { + return key % 100; + } + + /* 查询操作 */ + public get(key: number): string | null { + let index = this.hashFunc(key); + let val = this.bucket.get(index); + if (val === null) return null; + return val; + } + + /* 添加操作 */ + public put(key: number, val: string) { + let index = this.hashFunc(key); + this.bucket.set(index, val); + } + + /* 删除操作 */ + public remove(key: number) { + let index = this.hashFunc(key); + // 置为 null ,代表删除 + this.bucket.set(index, null); + } + + /* 获取所有键值对 */ + public entrySet(): Entry[] { + return this.bucket.entrySet(); + } + + /* 获取所有键 */ + public keySet(): number[] { + return this.bucket.keySet(); + } + + /* 获取所有值 */ + public valueSet(): string[] { + return this.bucket.valueSet(); + } + } ``` === "C"