-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathHashMap.ts
More file actions
150 lines (127 loc) · 3.22 KB
/
Copy pathHashMap.ts
File metadata and controls
150 lines (127 loc) · 3.22 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
* @description: 哈希表数据结构 - TypeScript实现
*/
const INITIAL_CAPACITY = 10;
const LOAD_FACTOR = 0.75;
interface Entry {
key: string;
value: number;
next: Entry | null;
}
class HashMap {
private table: (Entry | null)[];
private size: number;
private capacity: number;
constructor() {
this.capacity = INITIAL_CAPACITY;
this.table = new Array(INITIAL_CAPACITY).fill(null);
this.size = 0;
}
private hash(key: string): number {
let hash = 0;
for (let i = 0; i < key.length; i++) {
const char = key.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return Math.abs(hash) % this.capacity;
}
private resizeMap(): void {
const newCapacity = this.capacity * 2;
const newTable: (Entry | null)[] = new Array(newCapacity).fill(null);
for (let i = 0; i < this.capacity; i++) {
let entry = this.table[i];
while (entry !== null) {
const newIndex = this.hash(entry.key);
const newEntry: Entry = {
key: entry.key,
value: entry.value,
next: newTable[newIndex]
};
newTable[newIndex] = newEntry;
entry = entry.next;
}
}
this.table = newTable;
this.capacity = newCapacity;
}
put(key: string, value: number): void {
if (this.size / this.capacity > LOAD_FACTOR) {
this.resizeMap();
}
const index = this.hash(key);
let entry = this.table[index];
while (entry !== null) {
if (entry.key === key) {
entry.value = value;
return;
}
entry = entry.next;
}
const newEntry: Entry = {
key: key,
value: value,
next: this.table[index]
};
this.table[index] = newEntry;
this.size++;
}
get(key: string): number {
const index = this.hash(key);
let entry = this.table[index];
while (entry !== null) {
if (entry.key === key) {
return entry.value;
}
entry = entry.next;
}
return -1;
}
delete(key: string): void {
const index = this.hash(key);
let entry = this.table[index];
let prev: Entry | null = null;
while (entry !== null) {
if (entry.key === key) {
if (prev !== null) {
prev.next = entry.next;
} else {
this.table[index] = entry.next;
}
this.size--;
return;
}
prev = entry;
entry = entry.next;
}
}
freeHashMap(): void {
for (let i = 0; i < this.capacity; i++) {
let entry = this.table[i];
while (entry !== null) {
const next = entry.next;
entry = next;
}
}
}
}
const hashMapObj = new HashMap();
hashMapObj.put("apple", 10);
hashMapObj.put("banana", 20);
hashMapObj.put("orange", 30);
console.log("apple:", hashMapObj.get("apple"));
console.log("banana:", hashMapObj.get("banana"));
console.log("grape:", hashMapObj.get("grape"));
hashMapObj.delete("banana");
console.log("banana after delete:", hashMapObj.get("banana"));
hashMapObj.freeHashMap();
/*
* 输出结果:
* apple: 10
* banana: 20
* grape: -1
* banana after delete: -1
*/