-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathBelt_HashSetString.js
207 lines (183 loc) · 4.4 KB
/
Belt_HashSetString.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as Primitive_hash from "./Primitive_hash.js";
import * as Belt_internalSetBuckets from "./Belt_internalSetBuckets.js";
import * as Belt_internalBucketsType from "./Belt_internalBucketsType.js";
function copyBucket(h_buckets, ndata_tail, _old_bucket) {
while (true) {
let old_bucket = _old_bucket;
if (old_bucket === undefined) {
return;
}
let nidx = Primitive_hash.hash_final_mix(Primitive_hash.hash_mix_string(0, old_bucket.key)) & (h_buckets.length - 1 | 0);
let tail = ndata_tail[nidx];
if (tail !== undefined) {
tail.next = old_bucket;
} else {
h_buckets[nidx] = old_bucket;
}
ndata_tail[nidx] = old_bucket;
_old_bucket = old_bucket.next;
continue;
};
}
function remove(h, key) {
let h_buckets = h.buckets;
let i = Primitive_hash.hash_final_mix(Primitive_hash.hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
let l = h_buckets[i];
if (l === undefined) {
return;
}
let next_cell = l.next;
if (l.key === key) {
h.size = h.size - 1 | 0;
h_buckets[i] = next_cell;
return;
} else if (next_cell !== undefined) {
let _prec = l;
let _cell = next_cell;
while (true) {
let cell = _cell;
let prec = _prec;
let cell_next = cell.next;
if (cell.key === key) {
prec.next = cell_next;
h.size = h.size - 1 | 0;
return;
}
if (cell_next === undefined) {
return;
}
_cell = cell_next;
_prec = cell;
continue;
};
} else {
return;
}
}
function addBucket(h, key, _cell) {
while (true) {
let cell = _cell;
if (cell.key === key) {
return;
}
let n = cell.next;
if (n !== undefined) {
_cell = n;
continue;
}
h.size = h.size + 1 | 0;
cell.next = {
key: key,
next: undefined
};
return;
};
}
function add(h, key) {
let h_buckets = h.buckets;
let buckets_len = h_buckets.length;
let i = Primitive_hash.hash_final_mix(Primitive_hash.hash_mix_string(0, key)) & (buckets_len - 1 | 0);
let l = h_buckets[i];
if (l !== undefined) {
addBucket(h, key, l);
} else {
h_buckets[i] = {
key: key,
next: undefined
};
h.size = h.size + 1 | 0;
}
if (h.size > (buckets_len << 1)) {
let odata = h.buckets;
let osize = odata.length;
let nsize = (osize << 1);
if (nsize < osize) {
return;
}
let h_buckets$1 = new Array(nsize);
let ndata_tail = new Array(nsize);
h.buckets = h_buckets$1;
for (let i$1 = 0; i$1 < osize; ++i$1) {
copyBucket(h_buckets$1, ndata_tail, odata[i$1]);
}
for (let i$2 = 0; i$2 < nsize; ++i$2) {
let tail = ndata_tail[i$2];
if (tail !== undefined) {
tail.next = undefined;
}
}
return;
}
}
function has(h, key) {
let h_buckets = h.buckets;
let nid = Primitive_hash.hash_final_mix(Primitive_hash.hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
let bucket = h_buckets[nid];
if (bucket !== undefined) {
let _cell = bucket;
while (true) {
let cell = _cell;
if (cell.key === key) {
return true;
}
let nextCell = cell.next;
if (nextCell === undefined) {
return false;
}
_cell = nextCell;
continue;
};
} else {
return false;
}
}
function make(hintSize) {
return Belt_internalBucketsType.make(undefined, undefined, hintSize);
}
function size(h) {
return h.size;
}
function fromArray(arr) {
let len = arr.length;
let v = Belt_internalBucketsType.make(undefined, undefined, len);
for (let i = 0; i < len; ++i) {
add(v, arr[i]);
}
return v;
}
function mergeMany(h, arr) {
let len = arr.length;
for (let i = 0; i < len; ++i) {
add(h, arr[i]);
}
}
let clear = Belt_internalBucketsType.clear;
let isEmpty = Belt_internalBucketsType.isEmpty;
let copy = Belt_internalSetBuckets.copy;
let forEachU = Belt_internalSetBuckets.forEach;
let forEach = Belt_internalSetBuckets.forEach;
let reduceU = Belt_internalSetBuckets.reduce;
let reduce = Belt_internalSetBuckets.reduce;
let logStats = Belt_internalSetBuckets.logStats;
let toArray = Belt_internalSetBuckets.toArray;
let getBucketHistogram = Belt_internalSetBuckets.getBucketHistogram;
export {
make,
clear,
isEmpty,
add,
copy,
has,
remove,
forEachU,
forEach,
reduceU,
reduce,
size,
logStats,
toArray,
fromArray,
mergeMany,
getBucketHistogram,
}
/* No side effect */