Skip to content

Commit daaaa58

Browse files
committed
update HashTable
1 parent 3b02c52 commit daaaa58

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Other data structures/HashTable.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
</head>
77
<body>
88
<script>
9+
const capacity = [53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
10+
49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
11+
12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741];
912
const upperTol = 10;
1013
const lowerTol = 2;
11-
const initCapacity = 7;
14+
let capacityIndex = 0;
1215

1316
class HashTable {
14-
constructor(M) {
17+
constructor() {
1518
this.hashTable = Array(M);
16-
this.M = M;
19+
this.M = capacity[capacityIndex];
1720
this.size = 0;
1821
for (let i = 0; i < M; i++) {
1922
this.hashTable[i] = new Map()
@@ -29,8 +32,9 @@
2932
this.size++;
3033
}
3134

32-
if (this.size >= upperTol * this.M) {
33-
this.resize(this.M * lowerTol)
35+
if (this.size >= upperTol * this.M && capacityIndex + 1 < capacity.length) {
36+
capacityIndex++;
37+
this.resize(capacity[capacityIndex])
3438
}
3539
}
3640

@@ -43,8 +47,9 @@
4347
this.size--
4448
}
4549

46-
if (this.size < upperTol * this.M && this.M / 2 >= initCapacity) {
47-
this.resize(this.M / 2)
50+
if (this.size < lowerTol * this.M && capacityIndex - 1 >= 0) {
51+
capacityIndex--;
52+
this.resize(capacity[capacityIndex])
4853
}
4954
return ret
5055
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy