Skip to content

Commit

Permalink
Merge pull request krahets#136 from danielsss/typescript
Browse files Browse the repository at this point in the history
Add the TypeScript code and docs for Chapter of Hash Map krahets#113
  • Loading branch information
krahets authored Dec 26, 2022
2 parents 18c4356 + f628fe2 commit ac90a0f
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ docs/overrides/

# python files
__pycache__

# iml
hello-algo.iml
136 changes: 136 additions & 0 deletions codes/typescript/chapter_hashing/array_hash_map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* File: array_hash_map.ts
* Created Time: 2022-12-20
* Author: Daniel ([email protected])
*/

/* 键值对 Number -> String */
class Entry {
public key: number;
public val: string;

constructor(key: number, val: string) {
this.key = key;
this.val = val;
}
}

/* 基于数组简易实现的哈希表 */
class ArrayHashMap {

private readonly bucket: (Entry | null)[];

constructor() {
// 初始化一个长度为 100 的桶(数组)
this.bucket = (new Array(100)).fill(null);
}

/* 哈希函数 */
private hashFunc(key: number): number {
return key % 100;
}

/* 查询操作 */
public get(key: number): string | null {
let index = this.hashFunc(key);
let entry = this.bucket[index];
if (entry === null) return null;
return entry.val;
}

/* 添加操作 */
public set(key: number, val: string) {
let index = this.hashFunc(key);
this.bucket[index] = new Entry(key, val);
}

/* 删除操作 */
public delete(key: number) {
let index = this.hashFunc(key);
// 置为 null ,代表删除
this.bucket[index] = null;
}

/* 获取所有键值对 */
public entries(): (Entry | null)[] {
let arr: (Entry | null)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]);
}
}
return arr;
}

/* 获取所有键 */
public keys(): (number | undefined)[] {
let arr: (number | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.key);
}
}
return arr;
}

/* 获取所有值 */
public values(): (string | undefined)[] {
let arr: (string | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.val);
}
}
return arr;
}

/* 打印哈希表 */
public 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);
}

export {};
44 changes: 44 additions & 0 deletions codes/typescript/chapter_hashing/hash_map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* File: hash_map.ts
* Created Time: 2022-12-20
* Author: Daniel ([email protected])
*/

/* Driver Code */
/* 初始化哈希表 */
const map = new Map<number, string>();

/* 添加操作 */
// 在哈希表中添加键值对 (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);
}
116 changes: 114 additions & 2 deletions docs/chapter_hashing/hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,28 @@ comments: true
=== "TypeScript"

```typescript title="hash_map.ts"
/* 初始化哈希表 */
const map = new Map<number, string>();
/* 添加操作 */
// 在哈希表中添加键值对 (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);
```

=== "C"
Expand Down Expand Up @@ -250,7 +271,19 @@ comments: true
=== "TypeScript"

```typescript title="hash_map.ts"

/* 遍历哈希表 */
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);
}
```

=== "C"
Expand Down Expand Up @@ -506,7 +539,86 @@ $$
=== "TypeScript"

```typescript title="array_hash_map.ts"

/* 键值对 Number -> String */
class Entry {
public key: number;
public val: string;

constructor(key: number, val: string) {
this.key = key;
this.val = val;
}
}

/* 基于数组简易实现的哈希表 */
class ArrayHashMap {

private readonly bucket: (Entry | null)[];

constructor() {
// 初始化一个长度为 100 的桶(数组)
this.bucket = (new Array(100)).fill(null);
}

/* 哈希函数 */
private hashFunc(key: number): number {
return key % 100;
}

/* 查询操作 */
public get(key: number): string | null {
let index = this.hashFunc(key);
let entry = this.bucket[index];
if (entry === null) return null;
return entry.val;
}

/* 添加操作 */
public set(key: number, val: string) {
let index = this.hashFunc(key);
this.bucket[index] = new Entry(index, val);
}

/* 删除操作 */
public delete(key: number) {
let index = this.hashFunc(key);
// 置为 null ,代表删除
this.bucket[index] = null;
}

/* 获取所有键值对 */
public entries(): (Entry | null)[] {
let arr: (Entry | null)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]);
}
}
return arr;
}

/* 获取所有键 */
public keys(): (number | undefined)[] {
let arr: (number | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.key);
}
}
return arr;
}

/* 获取所有值 */
public values(): (string | undefined)[] {
let arr: (string | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.val);
}
}
return arr;
}
}
```

=== "C"
Expand Down

0 comments on commit ac90a0f

Please sign in to comment.