Source file maps_string.ml
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
let dichotomy_threshold = 16
let indirect_threshold = 48
let () =
assert (indirect_threshold - dichotomy_threshold > 2);
assert (indirect_threshold < 255);
()
type 'a t =
| Leaf of {
value : 'a option;
}
| Prefix of {
value : 'a option;
prefix : string;
child : 'a t;
}
| Dichotomy of {
value : 'a option;
keys : string;
children : 'a t array;
}
| Indirect of {
value : 'a option;
keys : string;
children : 'a t array;
}
| Direct of {
value : 'a option;
children : 'a t array;
}
let rec find_aux key len offset t =
if offset = len then
match t with
| Leaf { value; }
| Prefix { value; _ }
| Dichotomy { value; _ }
| Indirect { value; _ }
| Direct { value; _ } -> value
else begin
assert (offset < len);
match t with
| Leaf _ -> None
| Prefix { prefix; child; value = _; } ->
let n = String.length prefix in
if offset + n > len then None
else begin
let i = ref 0 in
while !i < n && offset + !i < len &&
String.unsafe_get prefix !i =
String.unsafe_get key (offset + !i) do
i := !i + 1
done;
if !i = n
then find_aux key len (offset + !i) child
else None
end
| Dichotomy { keys; children; value = _; } ->
let c = String.unsafe_get key offset in
let n = Array.length children in
let l = ref 0 in
let r = ref n in
while !l < !r do
let m = (!l + !r) / 2 in
if String.unsafe_get keys m < c
then l := m + 1
else r := m
done;
let m = !l in
if m < n && String.unsafe_get keys m = c
then find_aux key len (offset + 1) (Array.unsafe_get children m)
else None
| Indirect { keys; children; value = _; } ->
let i = Char.code (String.unsafe_get key offset) in
let j = Char.code (String.unsafe_get keys i) in
if j > indirect_threshold then None
else find_aux key len (offset + 1) (Array.unsafe_get children j)
| Direct { children; value = _; } ->
let i = Char.code (String.unsafe_get key offset) in
find_aux key len (offset + 1) (Array.unsafe_get children i)
end
let[@inline] array_insert a m x =
let n = Array.length a in
let a' = Array.make (n + 1) x in
Array.blit a 0 a' 0 m;
Array.blit a m a' (m + 1) (n - m);
a'
let[@inline] string_insert s m c =
let n = String.length s in
let b = Bytes.make (n + 1) c in
Bytes.blit_string s 0 b 0 m;
Bytes.blit_string s m b (m + 1) (n - m);
Bytes.unsafe_to_string b
let empty = Leaf { value = None; }
let[@inline] with_value value = function
| Leaf _ -> Leaf { value; }
| Prefix { value = _; prefix; child; } -> Prefix { value; prefix; child; }
| Dichotomy { value = _; keys; children; } -> Dichotomy { value; keys; children; }
| Indirect { value = _; keys; children } -> Indirect { value; keys; children; }
| Direct { value = _; children; } -> Direct { value; children; }
let[@inline] mk_prefix value prefix child =
match prefix, value, child with
| "", None, _ -> child
| "", (Some _), _ -> with_value value child
| _, _, Prefix { prefix = p'; child; value = None; } ->
let prefix = prefix ^ p' in
Prefix { prefix; child; value; }
| _ -> Prefix { prefix; child; value; }
let sub_after s off =
if off = String.length s then ""
else String.sub s off (String.length s - off)
let rec add_aux key v len offset t =
if offset = len then
with_value (Some v) t
else begin
assert (offset < len);
match t with
| Leaf { value; } ->
let prefix = String.sub key offset (len - offset) in
let child = Leaf { value = Some v; } in
mk_prefix value prefix child
| Prefix { prefix; child; value; } ->
let i = ref 0 in
let n = String.length prefix in
while !i < n && offset + !i < len &&
prefix.[!i] = key.[offset + !i] do
i := !i + 1
done;
if !i = n then begin
let child = add_aux key v len (offset + !i) child in
mk_prefix value prefix child
end else if offset + !i = len then begin
let pre = String.sub prefix 0 !i in
let post = String.sub prefix !i (n - !i) in
mk_prefix value pre (mk_prefix (Some v) post child)
end else begin
let pre = String.sub prefix 0 !i in
let post = sub_after prefix (!i + 1) in
let post_key = sub_after key (offset + !i + 1) in
let c0 = String.unsafe_get prefix !i in
let child0 = mk_prefix None post child in
let c1 = String.unsafe_get key (offset + !i) in
let child1 = mk_prefix None post_key (Leaf { value = Some v; }) in
let c0, child0, c1, child1 =
if c0 <= c1 then c0, child0, c1, child1 else c1, child1, c0, child0
in
let keys = Bytes.make 2 c1 in
Bytes.unsafe_set keys 0 c0;
let keys = Bytes.unsafe_to_string keys in
let children = [| child0; child1; |] in
let node = Dichotomy { value = None; keys; children; } in
mk_prefix value pre node
end
| Dichotomy { keys; children; value; } ->
let c = key.[offset] in
let n = Array.length children in
let l = ref 0 in
let r = ref n in
while !l < !r do
let m = (!l + !r) / 2 in
if keys.[m] < c
then l := m + 1
else r := m
done;
let m = !l in
if m < n && keys.[m] = c then begin
let child = add_aux key v len (offset + 1) children.(m) in
let children = Array.copy children in
children.(m) <- child;
Dichotomy { keys; children; value; }
end else begin
let key_post = sub_after key (offset + 1) in
let child = mk_prefix None key_post (Leaf { value = Some v; }) in
if n < dichotomy_threshold then begin
let children = array_insert children m child in
let keys = string_insert keys m c in
Dichotomy { keys; children; value; }
end else begin
let children = Array.append children [| child |] in
let b = Bytes.make 256 (Char.unsafe_chr 255) in
Bytes.unsafe_set b (Char.code c) (Char.unsafe_chr n);
for i = 0 to n - 1 do
let c = String.unsafe_get keys i in
Bytes.unsafe_set b (Char.code c) (Char.unsafe_chr i)
done;
let keys = Bytes.unsafe_to_string b in
Indirect { keys; children; value; }
end
end
| Indirect { keys; children; value; } ->
let i = Char.code (String.unsafe_get key offset) in
let j = Char.code (String.unsafe_get keys i) in
if j < 255 then begin
let child = add_aux key v len (offset + 1) (Array.unsafe_get children j) in
let children = Array.copy children in
children.(j) <- child;
let b = Bytes.of_string keys in
Bytes.unsafe_set b i (Char.unsafe_chr j);
let keys = Bytes.unsafe_to_string b in
Indirect { keys; children; value; }
end else begin
let key_post = sub_after key (offset + 1) in
let child = mk_prefix None key_post (Leaf { value = Some v; }) in
let n = Array.length children in
if n < indirect_threshold then begin
let children = Array.append children [| child |] in
let b = Bytes.of_string keys in
Bytes.unsafe_set b i (Char.unsafe_chr n);
let keys = Bytes.unsafe_to_string b in
Indirect { keys; children; value; }
end else begin
let new_children = Array.make 256 empty in
Array.unsafe_set new_children i child;
for c = 0 to 255 do
let k = Char.code (String.unsafe_get keys c) in
if k < 255 then
Array.unsafe_set new_children c (Array.unsafe_get children k)
done;
Direct { value; children = new_children; }
end
end
| Direct { children; value; } ->
let i = Char.code (String.unsafe_get key offset) in
let child = add_aux key v len (offset + 1) (Array.unsafe_get children i) in
let children = Array.copy children in
Array.unsafe_set children i child;
Direct { children; value; }
end
let rec find_add_aux f key len offset t =
if offset = len then
match t with
| Leaf { value; _ }
| Prefix { value; _ }
| Dichotomy { value; _ }
| Indirect { value; _ }
| Direct { value; _ } ->
with_value (Some (f value)) t
else begin
assert (offset < len);
match t with
| Leaf { value; } ->
let v = f None in
let prefix = String.sub key offset (len - offset) in
let child = Leaf { value = Some v; } in
mk_prefix value prefix child
| Prefix { prefix; child; value; } ->
let i = ref 0 in
let n = String.length prefix in
while !i < n && offset + !i < len &&
prefix.[!i] = key.[offset + !i] do
i := !i + 1
done;
if !i = n then begin
let child = find_add_aux f key len (offset + !i) child in
mk_prefix value prefix child
end else if offset + !i = len then begin
let v = f None in
let pre = String.sub prefix 0 !i in
let post = String.sub prefix !i (n - !i) in
mk_prefix value pre (mk_prefix (Some v) post child)
end else begin
let v = f None in
let pre = String.sub prefix 0 !i in
let post = sub_after prefix (!i + 1) in
let post_key = sub_after key (offset + !i + 1) in
let c0 = String.unsafe_get prefix !i in
let child0 = mk_prefix None post child in
let c1 = String.unsafe_get key (offset + !i) in
let child1 = mk_prefix None post_key (Leaf { value = Some v; }) in
let c0, child0, c1, child1 =
if c0 <= c1 then c0, child0, c1, child1 else c1, child1, c0, child0
in
let keys = Bytes.make 2 c1 in
Bytes.unsafe_set keys 0 c0;
let keys = Bytes.unsafe_to_string keys in
let children = [| child0; child1; |] in
let node = Dichotomy { value = None; keys; children; } in
mk_prefix value pre node
end
| Dichotomy { keys; children; value; } ->
let c = key.[offset] in
let n = Array.length children in
let l = ref 0 in
let r = ref n in
while !l < !r do
let m = (!l + !r) / 2 in
if keys.[m] < c
then l := m + 1
else r := m
done;
let m = !l in
if m < n && keys.[m] = c then begin
let child = find_add_aux f key len (offset + 1) children.(m) in
let children = Array.copy children in
children.(m) <- child;
Dichotomy { keys; children; value; }
end else begin
let v = f None in
let key_post = sub_after key (offset + 1) in
let child = mk_prefix None key_post (Leaf { value = Some v; }) in
if n < dichotomy_threshold then begin
let children = array_insert children m child in
let keys = string_insert keys m c in
Dichotomy { keys; children; value; }
end else begin
let children = Array.append children [| child |] in
let b = Bytes.make 256 (Char.unsafe_chr 255) in
Bytes.unsafe_set b (Char.code c) (Char.unsafe_chr n);
for i = 0 to n - 1 do
let c = String.unsafe_get keys i in
Bytes.unsafe_set b (Char.code c) (Char.unsafe_chr i)
done;
let keys = Bytes.unsafe_to_string b in
Indirect { keys; children; value; }
end
end
| Indirect { keys; children; value; } ->
let i = Char.code (String.unsafe_get key offset) in
let j = Char.code (String.unsafe_get keys i) in
if j < 255 then begin
let child = find_add_aux f key len (offset + 1) (Array.unsafe_get children j) in
let children = Array.copy children in
children.(j) <- child;
let b = Bytes.of_string keys in
Bytes.unsafe_set b i (Char.unsafe_chr j);
let keys = Bytes.unsafe_to_string b in
Indirect { keys; children; value; }
end else begin
let v = f None in
let key_post = sub_after key (offset + 1) in
let child = mk_prefix None key_post (Leaf { value = Some v; }) in
let n = Array.length children in
if n < indirect_threshold then begin
let children = Array.append children [| child |] in
let b = Bytes.of_string keys in
Bytes.unsafe_set b i (Char.unsafe_chr n);
let keys = Bytes.unsafe_to_string b in
Indirect { keys; children; value; }
end else begin
let new_children = Array.make 256 empty in
Array.unsafe_set new_children i child;
for c = 0 to 255 do
let k = Char.code (String.unsafe_get keys c) in
if k < 255 then
Array.unsafe_set new_children c (Array.unsafe_get children k)
done;
Direct { value; children = new_children; }
end
end
| Direct { children; value; } ->
let i = Char.code (String.unsafe_get key offset) in
let child = find_add_aux f key len (offset + 1) (Array.unsafe_get children i) in
let children = Array.copy children in
Array.unsafe_set children i child;
Direct { children; value; }
end
let rec sum_lengths acc = function
| [] -> acc
| s :: r -> sum_lengths (String.length s + acc) r
let rec rev_unsafe_blits b pos = function
| [] -> ()
| s :: r ->
let n = String.length s in
Bytes.blit_string s 0 b (pos - n) n;
rev_unsafe_blits b (pos - n) r
let rev_concat = function
| [] -> ""
| l ->
let n = sum_lengths 0 l in
let b = Bytes.create n in
rev_unsafe_blits b n l;
Bytes.unsafe_to_string b
let iter_apply f acc = function
| None -> ()
| Some v ->
let s = rev_concat acc in
f s v
let rec iter_aux f acc t =
match t with
| Leaf { value; } ->
iter_apply f acc value
| Prefix { value; prefix; child; } ->
iter_apply f acc value;
iter_aux f (prefix :: acc) child
| Dichotomy { value; keys; children; } ->
iter_apply f acc value;
String.iteri (fun i c ->
let s = String.make 1 c in
let child = children.(i) in
iter_aux f (s :: acc) child
) keys
| Indirect { value; keys; children; } ->
iter_apply f acc value;
String.iter (fun c ->
let i = Char.code c in
if i < 255 then begin
let s = String.make 1 c in
let child = children.(i) in
iter_aux f (s :: acc) child
end) keys
| Direct { value; children; } ->
iter_apply f acc value;
Array.iteri (fun i child ->
let c = Char.chr i in
let s = String.make 1 c in
iter_aux f (s :: acc) child
) children
let empty = empty
let find_opt k t =
find_aux k (String.length k) 0 t
let find_exn k t =
match find_opt k t with
| None -> raise Not_found
| Some res -> res
let add k v t =
add_aux k v (String.length k) 0 t
let find_add k f t =
find_add_aux f k (String.length k) 0 t
let iter f t = iter_aux f [] t
let fold f t acc =
let r = ref acc in
iter_aux (fun s v -> r := f s v !r) [] t;
!r