-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathbelt_internalSetBuckets.res
161 lines (145 loc) · 4.38 KB
/
belt_internalSetBuckets.res
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
/* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* We do dynamic hashing, and resize the table and rehash the elements
when buckets become too long. */
module C = Belt_internalBucketsType
/* TODO:
the current implementation relies on the fact that bucket
empty value is `undefined` in both places,
in theory, it can be different
*/
type rec bucket<'a> = {
mutable key: 'a,
mutable next: C.opt<bucket<'a>>,
}
and t<'hash, 'eq, 'a> = C.container<'hash, 'eq, bucket<'a>>
module A = Belt_Array
let rec copy = (x: t<_>): t<_> => {
hash: x.hash,
eq: x.eq,
size: x.size,
buckets: copyBuckets(x.buckets),
}
and copyBuckets = (buckets: array<C.opt<bucket<_>>>) => {
let len = A.length(buckets)
let newBuckets = A.makeUninitializedUnsafe(len)
for i in 0 to len - 1 {
A.setUnsafe(newBuckets, i, copyBucket(A.getUnsafe(buckets, i)))
}
newBuckets
}
and copyBucket = c =>
switch C.toOpt(c) {
| None => c
| Some(c) =>
let head = {
key: c.key,
next: C.emptyOpt,
}
copyAuxCont(c.next, head)
C.return(head)
}
and copyAuxCont = (c, prec) =>
switch C.toOpt(c) {
| None => ()
| Some(nc) =>
let ncopy = {key: nc.key, next: C.emptyOpt}
prec.next = C.return(ncopy)
copyAuxCont(nc.next, ncopy)
}
let rec bucketLength = (accu, buckets) =>
switch C.toOpt(buckets) {
| None => accu
| Some(cell) => bucketLength(accu + 1, cell.next)
}
let rec doBucketIter = (~f, buckets) =>
switch C.toOpt(buckets) {
| None => ()
| Some(cell) =>
f(. cell.key)
doBucketIter(~f, cell.next)
}
let forEachU = (h, f) => {
let d = h.C.buckets
for i in 0 to A.length(d) - 1 {
doBucketIter(~f, A.getUnsafe(d, i))
}
}
let forEach = (h, f) => forEachU(h, (. a) => f(a))
let rec fillArray = (i, arr, cell) => {
A.setUnsafe(arr, i, cell.key)
switch C.toOpt(cell.next) {
| None => i + 1
| Some(v) => fillArray(i + 1, arr, v)
}
}
let toArray = h => {
let d = h.C.buckets
let current = ref(0)
let arr = A.makeUninitializedUnsafe(h.C.size)
for i in 0 to A.length(d) - 1 {
let cell = A.getUnsafe(d, i)
switch C.toOpt(cell) {
| None => ()
| Some(cell) => current.contents = fillArray(current.contents, arr, cell)
}
}
arr
}
let rec doBucketFold = (~f, b, accu) =>
switch C.toOpt(b) {
| None => accu
| Some(cell) => doBucketFold(~f, cell.next, f(. accu, cell.key))
}
let reduceU = (h, init, f) => {
let d = h.C.buckets
let accu = ref(init)
for i in 0 to A.length(d) - 1 {
accu.contents = doBucketFold(~f, A.getUnsafe(d, i), accu.contents)
}
accu.contents
}
let reduce = (h, init, f) => reduceU(h, init, (. a, b) => f(a, b))
let getMaxBucketLength = h =>
A.reduceU(h.C.buckets, 0, (. m, b) => {
let len = bucketLength(0, b)
Pervasives.max(m, len)
})
let getBucketHistogram = h => {
let mbl = getMaxBucketLength(h)
let histo = A.makeByU(mbl + 1, (. _) => 0)
A.forEachU(h.C.buckets, (. b) => {
let l = bucketLength(0, b)
A.setUnsafe(histo, l, A.getUnsafe(histo, l) + 1)
})
histo
}
let logStats = h => {
let histogram = getBucketHistogram(h)
Js.log({
"bindings": h.C.size,
"buckets": A.length(h.C.buckets),
"histogram": histogram,
})
}