Source file CCTrie.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
(** {1 Prefix Tree} *)
type 'a sequence = ('a -> unit) -> unit
type 'a ktree = unit -> [`Nil | `Node of 'a * 'a ktree list]
(** {2 Signatures} *)
(** {6 A Composite Word}
Words are made of characters, who belong to a total order *)
module type WORD = sig
type t
type char_
val compare : char_ -> char_ -> int
val to_seq : t -> char_ sequence
val of_list : char_ list -> t
end
module type S = sig
type char_
type key
type 'a t
val empty : 'a t
val is_empty : _ t -> bool
val add : key -> 'a -> 'a t -> 'a t
(** Add a binding to the trie (possibly erasing the previous one) *)
val remove : key -> 'a t -> 'a t
(** Remove the key, if present *)
val find : key -> 'a t -> 'a option
(** Find the value associated with the key, if any *)
val find_exn : key -> 'a t -> 'a
(** Same as {!find} but can fail.
@raise Not_found if the key is not present *)
val longest_prefix : key -> 'a t -> key
(** [longest_prefix k m] finds the longest prefix of [k] that leads to
at least one path in [m] (it does not mean that the prefix is bound to
a value.
Example: if [m] has keys "abc0" and "abcd", then [longest_prefix "abc2" m]
will return "abc"
@since 0.17 *)
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
(** Update the binding for the given key. The function is given
[None] if the key is absent, or [Some v] if [key] is bound to [v];
if it returns [None] the key is removed, otherwise it
returns [Some y] and [key] becomes bound to [y] *)
val fold : ('b -> key -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** Fold on key/value bindings. Will use {!WORD.of_list} to rebuild keys. *)
val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
(** Map values, giving both key and value. Will use {!WORD.of_list} to rebuild keys.
@since 0.17 *)
val map : ('a -> 'b) -> 'a t -> 'b t
(** Map values, giving only the value.
@since 0.17 *)
val iter : (key -> 'a -> unit) -> 'a t -> unit
(** Same as {!fold}, but for effectful functions *)
val fold_values : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** More efficient version of {!fold}, that doesn't keep keys *)
val iter_values : ('a -> unit) -> 'a t -> unit
val merge : ('a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
(** Merge two tries together. The function is used in
case of conflicts, when a key belongs to both tries *)
val size : _ t -> int
(** Number of bindings *)
(** {6 Conversions} *)
val to_list : 'a t -> (key * 'a) list
val of_list : (key * 'a) list -> 'a t
val to_seq : 'a t -> (key * 'a) sequence
val of_seq : (key * 'a) sequence -> 'a t
val to_seq_values : 'a t -> 'a sequence
val to_tree : 'a t -> [`Char of char_ | `Val of 'a | `Switch] ktree
(** {6 Ranges} *)
val above : key -> 'a t -> (key * 'a) sequence
(** All bindings whose key is bigger or equal to the given key, in
ascending order *)
val below : key -> 'a t -> (key * 'a) sequence
(** All bindings whose key is smaller or equal to the given key,
in decreasing order *)
(**/**)
val check_invariants: _ t -> bool
(**/**)
end
module Make(W : WORD)
: S with type char_ = W.char_ and type key = W.t
= struct
type char_ = W.char_
type key = W.t
module M = Map.Make(struct
type t = char_
let compare = W.compare
end)
type 'a t =
| Empty
| Cons of char_ * 'a t
| Node of 'a option * 'a t M.t
let empty = Empty
let _invariant = function
| Node (None, map) when M.is_empty map -> false
| _ -> true
let rec check_invariants = function
| Empty -> true
| Cons (_, t) -> check_invariants t
| Node (None, map) when M.is_empty map -> false
| Node (_, map) ->
M.for_all (fun _ v -> check_invariants v) map
let is_empty = function
| Empty -> true
| _ -> false
let _id x = x
let _fold_seq_and_then f ~finish acc seq =
let acc = ref acc in
seq (fun x -> acc := f !acc x);
finish !acc
let _filter_map_seq f seq k =
seq (fun x -> match f x with
| None -> ()
| Some y -> k y)
let _seq_map f seq k = seq (fun x -> k (f x))
let _seq_append_list_rev l seq =
let l = ref l in
seq (fun x -> l := x :: !l);
!l
let _seq_append_list l seq =
List.rev_append (_seq_append_list_rev [] seq) l
let seq_of_map map k =
M.iter (fun key v -> k (key,v)) map
let rec _merge_lists l1 l2 = match l1, l2 with
| [], _
| _, [] -> [], l1, l2
| c1::l1', c2::l2' ->
if W.compare c1 c2 = 0
then
let pre, rest1, rest2 = _merge_lists l1' l2' in
c1::pre, rest1, rest2
else
[], l1, l2
let _cons c t = Cons (c, t)
let _mk_node value map = match value with
| Some _ -> Node (value, map)
| None ->
if M.is_empty map then Empty
else
if M.cardinal map = 1
then
let c, sub = M.min_binding map in
_cons c sub
else Node (value,map)
let _remove c t = match t with
| Empty -> t
| Cons (c', _) ->
if W.compare c c' = 0
then Empty
else t
| Node (value, map) ->
if M.mem c map
then
let map' = M.remove c map in
_mk_node value map'
else t
let update key f t =
let goto (t, rebuild) c =
match t with
| Empty -> empty, fun t -> rebuild (_cons c t)
| Cons (c', t') ->
if W.compare c c' = 0
then t', (fun t -> rebuild (_cons c t))
else
let rebuild' new_child =
rebuild (
if is_empty new_child then t
else
let map = M.singleton c new_child in
let map = M.add c' t' map in
_mk_node None map
) in
empty, rebuild'
| Node (value, map) ->
try
let t' = M.find c map in
let rebuild' new_child =
rebuild (
if is_empty new_child
then _mk_node value (M.remove c map)
else _mk_node value (M.add c new_child map)
)
in
t', rebuild'
with Not_found ->
let rebuild' new_child =
if is_empty new_child
then rebuild t
else
let map' = M.add c new_child map in
rebuild (_mk_node value map')
in
empty, rebuild'
in
let finish (t,rebuild) = match t with
| Empty -> rebuild (_mk_node (f None) M.empty)
| Cons (c, t') ->
rebuild
(match f None with
| None -> t
| Some _ as v -> _mk_node v (M.singleton c t')
)
| Node (value, map) ->
let value' = f value in
rebuild (_mk_node value' map)
in
let word = W.to_seq key in
_fold_seq_and_then goto ~finish (t, _id) word
let add k v t = update k (fun _ -> Some v) t
let remove k t = update k (fun _ -> None) t
let find_exn k t =
let goto t c = match t with
| Empty -> raise Not_found
| Cons (c', t') ->
if W.compare c c' = 0
then t'
else raise Not_found
| Node (_, map) -> M.find c map
and finish t = match t with
| Node (Some v, _) -> v
| _ -> raise Not_found
in
let word = W.to_seq k in
_fold_seq_and_then goto ~finish t word
let find k t =
try Some (find_exn k t)
with Not_found -> None
type 'a difflist = 'a list -> 'a list
let _difflist_add
: 'a difflist -> 'a -> 'a difflist
= fun f x -> fun l' -> f (x :: l')
let longest_prefix k t =
let goto (t,prefix) c = match t with
| Empty -> Empty, prefix
| Cons (c', t') ->
if W.compare c c' = 0
then t', _difflist_add prefix c
else Empty, prefix
| Node (_, map) ->
try
let t' = M.find c map in
t', _difflist_add prefix c
with Not_found -> Empty, prefix
and finish (_,prefix) =
W.of_list (prefix [])
in
let word = W.to_seq k in
_fold_seq_and_then goto ~finish (t,_id) word
let rec _fold f path t acc = match t with
| Empty -> acc
| Cons (c, t') -> _fold f (_difflist_add path c) t' acc
| Node (v, map) ->
let acc = match v with
| None -> acc
| Some v -> f acc path v
in
M.fold
(fun c t' acc -> _fold f (_difflist_add path c) t' acc)
map acc
let fold f acc t =
_fold
(fun acc path v ->
let key = W.of_list (path []) in
f acc key v)
_id t acc
let mapi f t =
let rec map_ prefix t = match t with
| Empty -> Empty
| Cons (c, t') -> Cons (c, map_ (_difflist_add prefix c) t')
| Node (v, map) ->
let v' = match v with
| None -> None
| Some v -> Some (f (W.of_list (prefix [])) v)
in let map' =
M.mapi (fun c t' ->
let prefix' = _difflist_add prefix c in
map_ prefix' t')
map
in Node (v', map')
in map_ _id t
let map f t =
let rec map_ = function
| Empty -> Empty
| Cons (c, t') -> Cons (c, map_ t')
| Node (v, map) ->
let v' = match v with
| None -> None
| Some v -> Some (f v)
in let map' = M.map map_ map
in Node (v', map')
in map_ t
let iter f t =
_fold
(fun () path y -> f (W.of_list (path [])) y)
_id t ()
let _iter_prefix ~prefix f t =
_fold
(fun () path y ->
let key = W.of_list (prefix (path [])) in
f key y)
_id t ()
let rec fold_values f acc t = match t with
| Empty -> acc
| Cons (_, t') -> fold_values f acc t'
| Node (v, map) ->
let acc = match v with
| None -> acc
| Some v -> f acc v
in
M.fold
(fun _c t' acc -> fold_values f acc t')
map acc
let iter_values f t = fold_values (fun () x -> f x) () t
let rec merge f t1 t2 = match t1, t2 with
| Empty, _ -> t2
| _, Empty -> t1
| Cons (c1,t1'), Cons (c2,t2') ->
if W.compare c1 c2 = 0
then _cons c1 (merge f t1' t2')
else
let map = M.add c1 t1' M.empty in
let map = M.add c2 t2' map in
_mk_node None map
| Cons (c1, t1'), Node (value, map) ->
begin try
let t2' = M.find c1 map in
let new_t = merge f t1' t2' in
let map' = if is_empty new_t
then M.remove c1 map
else M.add c1 new_t map
in
_mk_node value map'
with Not_found ->
assert (not(is_empty t1'));
Node (value, M.add c1 t1' map)
end
| Node _, Cons _ -> merge f t2 t1
| Node(v1, map1), Node (v2, map2) ->
let v = match v1, v2 with
| None, _ -> v2
| _, None -> v1
| Some v1, Some v2 -> f v1 v2
in
let map' = M.merge
(fun _c t1 t2 -> match t1, t2 with
| None, None -> assert false
| Some t, None
| None, Some t -> Some t
| Some t1, Some t2 ->
let new_t = merge f t1 t2 in
if is_empty new_t then None else Some new_t
) map1 map2
in
_mk_node v map'
let rec size t = match t with
| Empty -> 0
| Cons (_, t') -> size t'
| Node (v, map) ->
let s = match v with None -> 0 | Some _ -> 1 in
M.fold
(fun _ t' acc -> size t' + acc)
map s
let to_list t = fold (fun acc k v -> (k,v)::acc) [] t
let of_list l =
List.fold_left (fun acc (k,v) -> add k v acc) empty l
let to_seq t k = iter (fun key v -> k (key,v)) t
let to_seq_values t k = iter_values k t
let of_seq seq =
_fold_seq_and_then (fun acc (k,v) -> add k v acc) ~finish:_id empty seq
let rec to_tree t () =
let _tree_node x l () = `Node (x,l) in
match t with
| Empty -> `Nil
| Cons (c, t') -> `Node (`Char c, [to_tree t'])
| Node (v, map) ->
let x = match v with
| None -> `Switch
| Some v -> `Val v
in
let l = M.bindings map in
`Node(x, List.map (fun (c,t') -> _tree_node (`Char c) [to_tree t']) l)
(** {6 Ranges} *)
type 'a alternative =
| Yield of 'a * char_ difflist
| Explore of 'a t * char_ difflist
type direction =
| Above
| Below
let rec explore ~dir k alt = match alt with
| Yield (v,prefix) -> k (W.of_list (prefix[]), v)
| Explore (Empty, _) -> ()
| Explore (Cons (c,t), prefix) ->
explore ~dir k (Explore (t, _difflist_add prefix c))
| Explore (Node (o,map), prefix) ->
begin match o, dir with
| Some v, Above -> k (W.of_list (prefix[]), v)
| _ -> ()
end;
let seq = seq_of_map map in
let seq = _seq_map (fun (c,t') -> Explore (t', _difflist_add prefix c)) seq in
let l' = match o, dir with
| _, Above -> _seq_append_list [] seq
| None, Below -> _seq_append_list_rev [] seq
| Some v, Below ->
_seq_append_list_rev [Yield (v, prefix)] seq
in
List.iter (explore ~dir k) l'
let _list_eq l1 l2 =
try List.for_all2 (fun x y -> W.compare x y = 0) l1 l2
with Invalid_argument _ -> false
let _key_to_list key =
List.rev (_seq_append_list_rev [] (W.to_seq key))
let _half_range ~dir ~p key t k =
let on_char (cur, alternatives) c =
match cur with
| None -> (None, alternatives)
| Some (Empty,_) -> (None, alternatives)
| Some (Cons (c', t'), trail) ->
if W.compare c c' = 0
then Some (t', _difflist_add trail c), alternatives
else None, alternatives
| Some (Node (o, map), trail) ->
let alternatives = match o, dir with
| Some v, Below -> Yield (v, trail) :: alternatives
| _ -> alternatives
in
let alternatives =
let seq = seq_of_map map in
let seq = _filter_map_seq
(fun (c', t') ->
if p ~cur:c ~other:c'
then Some (Explore (t', _difflist_add trail c'))
else None)
seq
in
match dir with
| Above -> _seq_append_list alternatives seq
| Below -> _seq_append_list_rev alternatives seq
in
begin
try
let t' = M.find c map in
Some (t', _difflist_add trail c), alternatives
with Not_found ->
None, alternatives
end
and finish (cur,alternatives) =
begin match cur, dir with
| Some (t, prefix), Above ->
_iter_prefix ~prefix (fun key' v -> k (key', v)) t
| Some (Node (Some v, _), prefix), Below ->
assert (_list_eq (prefix []) (_key_to_list key));
k (key, v)
| Some _, _
| None, _ -> ()
end;
List.iter (explore ~dir k) alternatives
in
let word = W.to_seq key in
_fold_seq_and_then on_char ~finish (Some(t,_id), []) word
let above key t =
_half_range ~dir:Above ~p:(fun ~cur ~other -> W.compare cur other < 0) key t
let below key t =
_half_range ~dir:Below ~p:(fun ~cur ~other -> W.compare cur other > 0) key t
end
module type ORDERED = sig
type t
val compare : t -> t -> int
end
module MakeArray(X : ORDERED) = Make(struct
type t = X.t array
type char_ = X.t
let compare = X.compare
let to_seq a k = Array.iter k a
let of_list = Array.of_list
end)
module MakeList(X : ORDERED) = Make(struct
type t = X.t list
type char_ = X.t
let compare = X.compare
let to_seq a k = List.iter k a
let of_list l = l
end)
module String = Make(struct
type t = string
type char_ = char
let compare = Char.compare
let to_seq s k = String.iter k s
let of_list l =
let buf = Buffer.create (List.length l) in
List.iter (fun c -> Buffer.add_char buf c) l;
Buffer.contents buf
end)