-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmap2.ts
More file actions
128 lines (108 loc) · 2.72 KB
/
Copy pathmap2.ts
File metadata and controls
128 lines (108 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
* @description: 映射数据结构(带索引优化) - TypeScript实现
*/
interface Entry {
key: string;
value: number;
}
class CustomMapWithIndex {
private entries: Entry[];
private indexMap: Map<string, number>;
private size: number;
private capacity: number;
constructor() {
this.size = 0;
this.capacity = 10;
this.entries = new Array(this.capacity);
this.indexMap = new Map();
}
// 扩容
private resizeMap(): void {
this.capacity *= 2;
}
// 插入键值对
put(key: string, value: number): void {
if (this.indexMap.has(key)) {
// 更新值
const index = this.indexMap.get(key)!;
this.entries[index].value = value;
} else {
// 如果容量已满,扩容
if (this.size >= this.capacity) {
this.resizeMap();
}
// 插入新键值对
this.entries[this.size] = { key, value };
this.indexMap.set(key, this.size);
this.size++;
}
}
// 查找键
get(key: string): number {
if (this.indexMap.has(key)) {
const index = this.indexMap.get(key)!;
return this.entries[index].value;
}
return -1; // 未找到
}
// 删除键
delete(key: string): void {
if (this.indexMap.has(key)) {
const index = this.indexMap.get(key)!;
// 从数组中移除
this.entries.splice(index, 1);
this.indexMap.delete(key);
// 更新索引
for (let i = index; i < this.size - 1; i++) {
this.indexMap.set(this.entries[i].key, i);
}
this.size--;
}
}
// 判断键是否存在
has(key: string): boolean {
return this.indexMap.has(key);
}
// 获取键值对个数
getSize(): number {
return this.size;
}
// 清空所有数据
clear(): void {
this.entries = [];
this.indexMap = new Map();
this.size = 0;
}
// 遍历所有键值对
forEach(callback: (value: number, key: string) => void): void {
for (let i = 0; i < this.size; i++) {
callback(this.entries[i].value, this.entries[i].key);
}
}
}
const printEntry = (value: number, key: string): void => {
console.log(`${key}: ${value}`);
};
const map2Obj = new CustomMapWithIndex();
map2Obj.put("apple", 10);
map2Obj.put("banana", 20);
map2Obj.put("orange", 30);
console.log("apple:", map2Obj.get("apple"));
console.log("banana:", map2Obj.get("banana"));
console.log("grape:", map2Obj.get("grape"));
map2Obj.delete("banana");
console.log("banana after delete:", map2Obj.get("banana"));
map2Obj.forEach(printEntry);
map2Obj.clear();
/*
* 输出结果:
* apple: 10
* banana: 20
* grape: -1
* banana after delete: -1
* apple: 10
* orange: 30
*/