-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset.js
167 lines (146 loc) · 3.57 KB
/
set.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class Set {
constructor(hashFunc = Set.defaultHashFunc, equalsFunc = Set.defaultEqualsFunc) {
this.capacity = 16; // 初始容量
this.size = 0; // 当前元素个数
this.buckets = new Array(this.capacity).fill(null); // 初始化桶
this.hashFunc = hashFunc; // 哈希函数
this.equalsFunc = equalsFunc; // 比较函数
}
// FNV-1a 哈希函数
static defaultHashFunc(key) {
let str = key.toString();
let hash = 2166136261;
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i);
hash *= 16777619;
}
return hash >>> 0; // 保证返回正数
}
// 默认比较函数(假设 key 是数字)
static defaultEqualsFunc(a, b) {
return a === b;
}
// 扩容 Set
resize() {
const newCapacity = this.capacity * 2;
const newBuckets = new Array(newCapacity).fill(null);
// 重新哈希所有元素
for (let i = 0; i < this.capacity; i++) {
let node = this.buckets[i];
while (node) {
const index = this.hashFunc(node.key) % newCapacity;
const nextNode = node.next;
node.next = newBuckets[index];
newBuckets[index] = node;
node = nextNode;
}
}
this.buckets = newBuckets;
this.capacity = newCapacity;
}
// 添加元素到 Set
add(key) {
if (this.size / this.capacity > 0.75) {
this.resize(); // JS数组无需扩容,这里仅作演示
}
const index = this.hashFunc(key) % this.capacity;
let node = this.buckets[index];
// 检查是否已经存在
while (node) {
if (this.equalsFunc(node.key, key)) {
console.log(`Exist node: index=${index} key=${key}`);
return;
}
node = node.next;
}
// 插入新元素
node = {
key,
next: this.buckets[index]
};
this.buckets[index] = node;
this.size++;
console.log(`Adding node: index=${index} key=${key}`);
}
// 检查元素是否在 Set 中
contains(key) {
const index = this.hashFunc(key) % this.capacity;
let node = this.buckets[index];
while (node) {
if (this.equalsFunc(node.key, key)) return true;
node = node.next;
}
return false;
}
// 从 Set 中删除元素
remove(key) {
const index = this.hashFunc(key) % this.capacity;
let node = this.buckets[index];
let prev = null;
while (node) {
if (this.equalsFunc(node.key, key)) {
if (prev) {
prev.next = node.next;
} else {
this.buckets[index] = node.next;
}
this.size--;
console.log(`Removed node: index=${index} key=${key}`);
return;
}
prev = node;
node = node.next;
}
}
// 获取 Set 中的元素个数
getSize() {
return this.size;
}
// 打印 Set 中的所有元素
print() {
for (let i = 0; i < this.capacity; i++) {
let node = this.buckets[i];
while (node) {
console.log(node.key);
node = node.next;
}
}
console.log();
}
}
// 测试代码
const set = new Set();
const values = [10, 20, 20, 30, 40, 40, 50];
// 添加元素
for (let v of values) {
set.add(v);
}
// 打印 Set 内容
set.print();
// 检查元素是否存在
console.log("Contains 30?", set.contains(30));
// 删除元素
set.remove(30);
set.print();
console.log(set);
/*
jarry@MacBook-Pro set % node set.js
Adding node: index=4 key=10
Adding node: index=12 key=20
Exist node: index=12 key=20
Adding node: index=0 key=30
Adding node: index=12 key=40
Exist node: index=12 key=40
Adding node: index=0 key=50
50
30
10
40
20
Contains 30? true
Removed node: index=0 key=30
50
10
40
20
*/