1
(**************************************************************************)
5
(* Fran�ois Pottier and Yann R�gis-Gianas, INRIA Rocquencourt *)
7
(* Copyright 2005 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* under the terms of the Q Public License version 1.0, with the *)
10
(* change described in file LICENSE. *)
12
(**************************************************************************)
14
(* This is an implementation of Patricia trees, following Chris Okasaki's paper at the 1998 ML Workshop in Baltimore.
15
Both big-endian and little-endian trees are provided. Both sets and maps are implemented on top of Patricia
18
(*i --------------------------------------------------------------------------------------------------------------- i*)
19
(*s \mysection{Little-endian vs big-endian trees} *)
21
(* A tree is little-endian if it expects the key's least significant bits to be tested first during a search. It is
22
big-endian if it expects the key's most significant bits to be tested first.
24
Most of the code is independent of this design choice, so it is written as a functor, parameterized by a small
25
structure which defines endianness. Here is the interface which must be adhered to by such a structure. *)
27
module Endianness = struct
31
(* A mask is an integer with a single one bit (i.e. a power of 2). *)
35
(* [branching_bit] accepts two distinct integers and returns a mask which identifies the first bit where they
36
differ. The meaning of ``first'' varies according to the endianness being implemented. *)
38
val branching_bit: int -> int -> mask
40
(* [mask i m] returns an integer [i'], where all bits which [m] says are relevant are identical to those in [i],
41
and all others are set to some unspecified, but fixed value. Which bits are ``relevant'' according to a given
42
mask varies according to the endianness being implemented. *)
44
val mask: int -> mask -> int
46
(* [shorter m1 m2] returns [true] if and only if [m1] describes a shorter prefix than [m2], i.e. if it makes fewer
47
bits relevant. Which bits are ``relevant'' according to a given mask varies according to the endianness being
50
val shorter: mask -> mask -> bool
54
(* Now, let us define [Little] and [Big], two possible [Endiannness] choices. *)
56
module Little = struct
63
(* Performing a logical ``xor'' of [i0] and [i1] yields a bit field where all differences between [i0] and [i1]
64
show up as one bits. (There must be at least one, since [i0] and [i1] are distinct.) The ``first'' one is
65
the lowest bit in this bit field, since we are checking least significant bits first. *)
67
let branching_bit i0 i1 =
68
lowest_bit (i0 lxor i1)
70
(* The ``relevant'' bits in an integer [i] are those which are found (strictly) to the right of the single one bit
71
in the mask [m]. We keep these bits, and set all others to 0. *)
76
(* The smaller [m] is, the fewer bits are relevant. *)
90
let rec highest_bit x =
91
let m = lowest_bit x in
97
(* Performing a logical ``xor'' of [i0] and [i1] yields a bit field where all differences between [i0] and [i1]
98
show up as one bits. (There must be at least one, since [i0] and [i1] are distinct.) The ``first'' one is
99
the highest bit in this bit field, since we are checking most significant bits first.
101
In Okasaki's paper, this loop is sped up by computing a conservative initial guess. Indeed, the bit at which
102
the two prefixes disagree must be somewhere within the shorter prefix, so we can begin searching at the
103
least-significant valid bit in the shorter prefix. Unfortunately, to allow computing the initial guess, the
104
main code has to pass in additional parameters, e.g. a mask which describes the length of each prefix. This
105
``pollutes'' the endianness-independent code. For this reason, this optimization isn't implemented here. *)
107
let branching_bit i0 i1 =
108
highest_bit (i0 lxor i1)
110
(* The ``relevant'' bits in an integer [i] are those which are found (strictly) to the left of the single one bit
111
in the mask [m]. We keep these bits, and set all others to 0. Okasaki uses a different convention, which allows
112
big-endian Patricia trees to masquerade as binary search trees. This feature does not seem to be useful here. *)
115
i land (lnot (2*m-1))
117
(* The smaller [m] is, the more bits are relevant. *)
126
(*i --------------------------------------------------------------------------------------------------------------- i*)
127
(*s \mysection{Patricia-tree-based maps} *)
129
module Make (X : Endianness.S) = struct
131
(* Patricia trees are maps whose keys are integers. *)
135
(* A tree is either empty, or a leaf node, containing both the integer key and a piece of data, or a binary node.
136
Each binary node carries two integers. The first one is the longest common prefix of all keys in this
137
sub-tree. The second integer is the branching bit. It is an integer with a single one bit (i.e. a power of 2),
138
which describes the bit being tested at this node. *)
143
| Branch of int * X.mask * 'a t * 'a t
150
(* [lookup k m] looks up the value associated to the key [k] in the map [m], and raises [Not_found] if no value is
153
This implementation takes branches \emph{without} checking whether the key matches the prefix found at the
154
current node. This means that a query for a non-existent key shall be detected only when finally reaching
155
a leaf, rather than higher up in the tree. This strategy is better when (most) queries are expected to be
158
let rec lookup key = function
161
| Leaf (key', data) ->
166
| Branch (_, mask, tree0, tree1) ->
167
lookup key (if (key land mask) = 0 then tree0 else tree1)
169
(* The auxiliary function [join] merges two trees in the simple case where their prefixes disagree.
171
Assume $t_0$ and $t_1$ are non-empty trees, with longest common prefixes $p_0$ and $p_1$, respectively. Further,
172
suppose that $p_0$ and $p_1$ disagree, that is, neither prefix is contained in the other. Then, no matter how
173
large $t_0$ and $t_1$ are, we can merge them simply by creating a new [Branch] node that has $t_0$ and $t_1$
176
let join p0 t0 p1 t1 =
177
let m = X.branching_bit p0 p1 in
178
let p = X.mask p0 (* for instance *) m in
179
if (p0 land m) = 0 then
184
(* The auxiliary function [match_prefix] tells whether a given key has a given prefix. More specifically,
185
[match_prefix k p m] returns [true] if and only if the key [k] has prefix [p] up to bit [m].
187
Throughout our implementation of Patricia trees, prefixes are assumed to be in normal form, i.e. their
188
irrelevant bits are set to some predictable value. Formally, we assume [X.mask p m] equals [p] whenever [p]
189
is a prefix with [m] relevant bits. This allows implementing [match_prefix] using only one call to
190
[X.mask]. On the other hand, this requires normalizing prefixes, as done e.g. in [join] above, where
191
[X.mask p0 m] has to be used instead of [p0]. *)
193
let match_prefix k p m =
196
(* [fine_add decide k d m] returns a map whose bindings are all bindings in [m], plus a binding of the key [k] to
197
the datum [d]. If a binding from [k] to [d0] already exists, then the resulting map contains a binding from
198
[k] to [decide d0 d]. *)
200
type 'a decision = 'a -> 'a -> 'a
204
let basic_add decide k d m =
212
let d' = decide d0 d in
218
join k (Leaf (k, d)) k0 t
219
| Branch (p, m, t0, t1) ->
220
if match_prefix k p m then
221
if (k land m) = 0 then Branch (p, m, add t0, t1)
222
else Branch (p, m, t0, add t1)
224
join k (Leaf (k, d)) p t in
228
let strict_add k d m =
229
basic_add (fun _ _ -> raise Unchanged) k d m
231
let fine_add decide k d m =
233
basic_add decide k d m
237
(* [add k d m] returns a map whose bindings are all bindings in [m], plus a binding of the key [k] to the datum
238
[d]. If a binding already exists for [k], it is overridden. *)
241
fine_add (fun old_binding new_binding -> new_binding) k d m
243
(* [singleton k d] returns a map whose only binding is from [k] to [d]. *)
248
(* [is_singleton m] returns [Some (k, d)] if [m] is a singleton map
249
that maps [k] to [d]. Otherwise, it returns [None]. *)
251
let is_singleton = function
258
(* [is_empty m] returns [true] if and only if the map [m] defines no bindings at all. *)
260
let is_empty = function
267
(* [cardinal m] returns [m]'s cardinal, that is, the number of keys it binds, or, in other words, its domain's
270
let rec cardinal = function
275
| Branch (_, _, t0, t1) ->
276
cardinal t0 + cardinal t1
278
(* [remove k m] returns the map [m] deprived from any binding involving [k]. *)
282
let rec remove = function
290
| Branch (prefix, mask, tree0, tree1) ->
291
if (key land mask) = 0 then
292
match remove tree0 with
296
Branch (prefix, mask, tree0, tree1)
298
match remove tree1 with
302
Branch (prefix, mask, tree0, tree1) in
309
(* [lookup_and_remove k m] looks up the value [v] associated to the key [k] in the map [m], and raises [Not_found]
310
if no value is bound to [k]. The call returns the value [v], together with the map [m] deprived from the binding
313
let rec lookup_and_remove key = function
316
| Leaf (key', data) ->
321
| Branch (prefix, mask, tree0, tree1) ->
322
if (key land mask) = 0 then
323
match lookup_and_remove key tree0 with
327
data, Branch (prefix, mask, tree0, tree1)
329
match lookup_and_remove key tree1 with
333
data, Branch (prefix, mask, tree0, tree1)
335
(* [fine_union decide m1 m2] returns the union of the maps [m1] and
336
[m2]. If a key [k] is bound to [x1] (resp. [x2]) within [m1]
337
(resp. [m2]), then [decide] is called. It is passed [x1] and
338
[x2], and must return the value which shall be bound to [k] in
339
the final map. The operation returns [m2] itself (as opposed to a
340
copy of it) when its result is equal to [m2]. *)
342
let reverse decision elem1 elem2 =
345
let fine_union decide m1 m2 =
352
| (Leaf _ | Branch _), Empty ->
355
| Leaf(key, value), _ ->
356
fine_add (reverse decide) key value t
357
| Branch _, Leaf(key, value) ->
358
fine_add decide key value s
360
| Branch(p, m, s0, s1), Branch(q, n, t0, t1) ->
361
if (p = q) & (m = n) then
363
(* The trees have the same prefix. Merge their sub-trees. *)
366
and u1 = union s1 t1 in
367
if t0 == u0 && t1 == u1 then t
368
else Branch(p, m, u0, u1)
370
else if (X.shorter m n) & (match_prefix q p m) then
372
(* [q] contains [p]. Merge [t] with a sub-tree of [s]. *)
374
if (q land m) = 0 then
375
Branch(p, m, union s0 t, s1)
377
Branch(p, m, s0, union s1 t)
379
else if (X.shorter n m) & (match_prefix p q n) then
381
(* [p] contains [q]. Merge [s] with a sub-tree of [t]. *)
383
if (p land n) = 0 then
384
let u0 = union s t0 in
386
else Branch(q, n, u0, t1)
388
let u1 = union s t1 in
390
else Branch(q, n, t0, u1)
394
(* The prefixes disagree. *)
400
(* [union m1 m2] returns the union of the maps [m1] and
401
[m2]. Bindings in [m2] take precedence over those in [m1]. The
402
operation returns [m2] itself (as opposed to a copy of it) when
403
its result is equal to [m2]. *)
406
fine_union (fun d d' -> d') m1 m2
408
(* [iter f m] invokes [f k x], in turn, for each binding from key [k] to element [x] in the map [m]. Keys are
409
presented to [f] according to some unspecified, but fixed, order. *)
411
let rec iter f = function
414
| Leaf (key, data) ->
416
| Branch (_, _, tree0, tree1) ->
420
(* [fold f m seed] invokes [f k d accu], in turn, for each binding from key [k] to datum [d] in the map
421
[m]. Keys are presented to [f] in increasing order according to the map's ordering. The initial value of
422
[accu] is [seed]; then, at each new call, its value is the value returned by the previous invocation of [f]. The
423
value returned by [fold] is the final value of [accu]. *)
425
let rec fold f m accu =
429
| Leaf (key, data) ->
431
| Branch (_, _, tree0, tree1) ->
432
fold f tree1 (fold f tree0 accu)
434
(* [fold_rev] performs exactly the same job as [fold], but presents keys to [f] in the opposite order. *)
436
let rec fold_rev f m accu =
440
| Leaf (key, data) ->
442
| Branch (_, _, tree0, tree1) ->
443
fold_rev f tree0 (fold_rev f tree1 accu)
445
(* It is valid to evaluate [iter2 f m1 m2] if and only if [m1] and [m2] have the same domain. Doing so invokes
446
[f k x1 x2], in turn, for each key [k] bound to [x1] in [m1] and to [x2] in [m2]. Bindings are presented to [f]
447
according to some unspecified, but fixed, order. *)
449
let rec iter2 f t1 t2 =
453
| Leaf (key1, data1), Leaf (key2, data2) ->
454
assert (key1 = key2);
455
f key1 (* for instance *) data1 data2
456
| Branch (p1, m1, left1, right1), Branch (p2, m2, left2, right2) ->
460
iter2 f right1 right2
464
(* [map f m] returns the map obtained by composing the map [m] with the function [f]; that is, the map
465
$k\mapsto f(m(k))$. *)
467
let rec map f = function
470
| Leaf (key, data) ->
472
| Branch (p, m, tree0, tree1) ->
473
Branch (p, m, map f tree0, map f tree1)
475
(* [endo_map] is similar to [map], but attempts to physically share its result with its input. This saves
476
memory when [f] is the identity function. *)
478
let rec endo_map f tree =
482
| Leaf (key, data) ->
483
let data' = f data in
484
if data == data' then
488
| Branch (p, m, tree0, tree1) ->
489
let tree0' = endo_map f tree0 in
490
let tree1' = endo_map f tree1 in
491
if (tree0' == tree0) & (tree1' == tree1) then
494
Branch (p, m, tree0', tree1')
496
(*i --------------------------------------------------------------------------------------------------------------- i*)
497
(*s \mysection{Patricia-tree-based sets} *)
499
(* To enhance code sharing, it would be possible to implement maps as sets of pairs, or (vice-versa) to implement
500
sets as maps to the unit element. However, both possibilities introduce some space and time inefficiency. To
501
avoid it, we define each structure separately. *)
503
module Domain = struct
510
| Branch of int * X.mask * t * t
517
(* [is_empty s] returns [true] if and only if the set [s] is empty. *)
519
let is_empty = function
526
(* [singleton x] returns a set whose only element is [x]. *)
531
(* [is_singleton s] returns [Some x] if [s] is a singleton
532
containing [x] as its only element; otherwise, it returns
535
let is_singleton = function
542
(* [choose s] returns an element of [s] if [s] is nonempty and
543
raises [Not_found] otherwise. *)
545
let rec choose = function
550
| Branch (_, _, tree0, _) ->
553
(* [cardinal s] returns [s]'s cardinal. *)
555
let rec cardinal = function
560
| Branch (_, _, t0, t1) ->
561
cardinal t0 + cardinal t1
563
(* [mem x s] returns [true] if and only if [x] appears in the set [s]. *)
565
let rec mem x = function
570
| Branch (_, mask, tree0, tree1) ->
571
mem x (if (x land mask) = 0 then tree0 else tree1)
573
(* The auxiliary function [join] merges two trees in the simple case where their prefixes disagree. *)
575
let join p0 t0 p1 t1 =
576
let m = X.branching_bit p0 p1 in
577
let p = X.mask p0 (* for instance *) m in
578
if (p0 land m) = 0 then
583
(* [add x s] returns a set whose elements are all elements of [s], plus [x]. *)
587
let rec strict_add x t =
596
| Branch (p, m, t0, t1) ->
597
if match_prefix x p m then
598
if (x land m) = 0 then Branch (p, m, strict_add x t0, t1)
599
else Branch (p, m, t0, strict_add x t1)
609
(* [make2 x y] creates a set whose elements are [x] and [y]. [x] and [y] need not be distinct. *)
614
(* [fine_add] does not make much sense for sets of integers. Better warn the user. *)
616
type decision = int -> int -> int
618
let fine_add decision x s =
621
(* [remove x s] returns a set whose elements are all elements of [s], except [x]. *)
625
let rec strict_remove = function
633
| Branch (prefix, mask, tree0, tree1) ->
634
if (x land mask) = 0 then
635
match strict_remove tree0 with
639
Branch (prefix, mask, tree0, tree1)
641
match strict_remove tree1 with
645
Branch (prefix, mask, tree0, tree1) in
652
(* [union s1 s2] returns the union of the sets [s1] and [s2]. *)
659
| (Leaf _ | Branch _), Empty ->
664
| Branch _, Leaf x ->
667
| Branch(p, m, s0, s1), Branch(q, n, t0, t1) ->
668
if (p = q) & (m = n) then
670
(* The trees have the same prefix. Merge their sub-trees. *)
673
and u1 = union s1 t1 in
674
if t0 == u0 && t1 == u1 then t
675
else Branch(p, m, u0, u1)
677
else if (X.shorter m n) & (match_prefix q p m) then
679
(* [q] contains [p]. Merge [t] with a sub-tree of [s]. *)
681
if (q land m) = 0 then
682
Branch(p, m, union s0 t, s1)
684
Branch(p, m, s0, union s1 t)
686
else if (X.shorter n m) & (match_prefix p q n) then
688
(* [p] contains [q]. Merge [s] with a sub-tree of [t]. *)
690
if (p land n) = 0 then
691
let u0 = union s t0 in
693
else Branch(q, n, u0, t1)
695
let u1 = union s t1 in
697
else Branch(q, n, t0, u1)
701
(* The prefixes disagree. *)
705
(* [fine_union] does not make much sense for sets of integers. Better warn the user. *)
707
let fine_union decision s1 s2 =
710
(* [build] is a ``smart constructor''. It builds a [Branch] node with the specified arguments, but ensures
711
that the newly created node does not have an [Empty] child. *)
713
let build p m t0 t1 =
724
(* [diff s t] returns the set difference of [s] and [t], that is, $s\setminus t$. *)
734
if mem x t then Empty else s
738
| Branch(p, m, s0, s1), Branch(q, n, t0, t1) ->
739
if (p = q) & (m = n) then
741
(* The trees have the same prefix. Compute the differences of their sub-trees. *)
743
build p m (diff s0 t0) (diff s1 t1)
745
else if (X.shorter m n) & (match_prefix q p m) then
747
(* [q] contains [p]. Subtract [t] off a sub-tree of [s]. *)
749
if (q land m) = 0 then
750
build p m (diff s0 t) s1
752
build p m s0 (diff s1 t)
754
else if (X.shorter n m) & (match_prefix p q n) then
756
(* [p] contains [q]. Subtract a sub-tree of [t] off [s]. *)
758
diff s (if (p land n) = 0 then t0 else t1)
762
(* The prefixes disagree. *)
766
(* [inter s t] returns the set intersection of [s] and [t], that is, $s\cap t$. *)
776
if mem x t then s else Empty
778
if mem x s then t else Empty
780
| Branch(p, m, s0, s1), Branch(q, n, t0, t1) ->
781
if (p = q) & (m = n) then
783
(* The trees have the same prefix. Compute the intersections of their sub-trees. *)
785
build p m (inter s0 t0) (inter s1 t1)
787
else if (X.shorter m n) & (match_prefix q p m) then
789
(* [q] contains [p]. Intersect [t] with a sub-tree of [s]. *)
791
inter (if (q land m) = 0 then s0 else s1) t
793
else if (X.shorter n m) & (match_prefix p q n) then
795
(* [p] contains [q]. Intersect [s] with a sub-tree of [t]. *)
797
inter s (if (p land n) = 0 then t0 else t1)
801
(* The prefixes disagree. *)
805
(* [disjoint s1 s2] returns [true] if and only if the sets [s1] and [s2] are disjoint, i.e. iff their intersection
806
is empty. It is a specialized version of [inter], which uses less space. *)
808
exception NotDisjoint
826
| Branch(p, m, s0, s1), Branch(q, n, t0, t1) ->
827
if (p = q) & (m = n) then begin
831
else if (X.shorter m n) & (match_prefix q p m) then
832
inter (if (q land m) = 0 then s0 else s1) t
833
else if (X.shorter n m) & (match_prefix p q n) then
834
inter s (if (p land n) = 0 then t0 else t1)
844
(* [iter f s] invokes [f x], in turn, for each element [x] of the set [s]. Elements are presented to [f] according
845
to some unspecified, but fixed, order. *)
847
let rec iter f = function
852
| Branch (_, _, tree0, tree1) ->
856
(* [fold f s seed] invokes [f x accu], in turn, for each element [x] of the set [s]. Elements are presented to [f]
857
according to some unspecified, but fixed, order. The initial value of [accu] is [seed]; then, at each new call,
858
its value is the value returned by the previous invocation of [f]. The value returned by [fold] is the final
859
value of [accu]. In other words, if $s = \{ x_1, x_2, \ldots, x_n \}$, where $x_1 < x_2 < \ldots < x_n$, then
860
[fold f s seed] computes $([f]\,x_n\,\ldots\,([f]\,x_2\,([f]\,x_1\,[seed]))\ldots)$. *)
862
let rec fold f s accu =
868
| Branch (_, _, s0, s1) ->
869
fold f s1 (fold f s0 accu)
871
(* [fold_rev] performs exactly the same job as [fold], but presents elements to [f] in the opposite order. *)
873
let rec fold_rev f s accu =
879
| Branch (_, _, s0, s1) ->
880
fold_rev f s0 (fold_rev f s1 accu)
882
(* [iter2] does not make much sense for sets of integers. Better warn the user. *)
884
let rec iter2 f t1 t2 =
887
(* [iterator s] returns a stateful iterator over the set [s]. That is, if $s = \{ x_1, x_2, \ldots, x_n \}$, where
888
$x_1 < x_2 < \ldots < x_n$, then [iterator s] is a function which, when invoked for the $k^{\text{th}}$ time,
889
returns [Some]$x_k$, if $k\leq n$, and [None] otherwise. Such a function can be useful when one wishes to
890
iterate over a set's elements, without being restricted by the call stack's discipline.
892
For more comments about this algorithm, please see module [Baltree], which defines a similar one. *)
896
let remainder = ref [ s ] in
899
match !remainder with
905
| (Leaf x) :: parent ->
908
| (Branch(_, _, s0, s1)) :: parent ->
909
remainder := s0 :: s1 :: parent;
914
(* [exists p s] returns [true] if and only if some element of [s] matches the predicate [p]. *)
928
(* [compare] is an ordering over sets. *)
933
let iterator2 = iterator s2 in
936
match iterator2() with
944
match iterator2() with
952
(* [equal] implements equality over sets. *)
957
(* [subset] implements the subset predicate over sets. In other words, [subset s t] returns [true] if and only if
958
$s\subseteq t$. It is a specialized version of [diff]. *)
971
| Branch _, Leaf _ ->
974
if not (mem x t) then
977
| Branch(p, m, s0, s1), Branch(q, n, t0, t1) ->
979
if (p = q) & (m = n) then begin
985
else if (X.shorter n m) & (match_prefix p q n) then
987
diff s (if (p land n) = 0 then t0 else t1)
991
(* Either [q] contains [p], which means at least one of [s]'s sub-trees is not contained within [t],
992
or the prefixes disagree. In either case, the subset relationship cannot possibly hold. *)
1002
(* [filter p s] returns the subset of [s] formed by all elements which satisfy the predicate [p]. *)
1004
let filter predicate s =
1005
let modified = ref false in
1007
let subset = fold (fun element subset ->
1008
if predicate element then
1021
(* [map f s] computes the image of [s] through [f]. *)
1024
fold (fun element accu ->
1025
add (f element) accu
1028
(* [monotone_map] and [endo_map] do not make much sense for sets of integers. Better warn the user. *)
1030
let monotone_map f s =
1038
(*i --------------------------------------------------------------------------------------------------------------- i*)
1039
(*s \mysection{Relating sets and maps} *)
1041
(* Back to the world of maps. Let us now describe the relationship which exists between maps and their domains. *)
1043
(* [domain m] returns [m]'s domain. *)
1045
let rec domain = function
1050
| Branch (p, m, t0, t1) ->
1051
Domain.Branch (p, m, domain t0, domain t1)
1053
(* [lift f s] returns the map $k\mapsto f(k)$, where $k$ ranges over a set of keys [s]. *)
1055
let rec lift f = function
1060
| Domain.Branch (p, m, t0, t1) ->
1061
Branch(p, m, lift f t0, lift f t1)
1063
(* [build] is a ``smart constructor''. It builds a [Branch] node with the specified arguments, but ensures
1064
that the newly created node does not have an [Empty] child. *)
1066
let build p m t0 t1 =
1075
Branch(p, m, t0, t1)
1077
(* [corestrict m d] performs a co-restriction of the map [m] to the domain [d]. That is, it returns the map
1078
$k\mapsto m(k)$, where $k$ ranges over all keys bound in [m] but \emph{not} present in [d]. Its code resembles
1081
let rec corestrict s t =
1085
| _, Domain.Empty ->
1089
if Domain.mem k t then Empty else s
1090
| _, Domain.Leaf k ->
1093
| Branch(p, m, s0, s1), Domain.Branch(q, n, t0, t1) ->
1094
if (p = q) & (m = n) then
1096
build p m (corestrict s0 t0) (corestrict s1 t1)
1098
else if (X.shorter m n) & (match_prefix q p m) then
1100
if (q land m) = 0 then
1101
build p m (corestrict s0 t) s1
1103
build p m s0 (corestrict s1 t)
1105
else if (X.shorter n m) & (match_prefix p q n) then
1107
corestrict s (if (p land n) = 0 then t0 else t1)
1115
(*i --------------------------------------------------------------------------------------------------------------- i*)
1116
(*s \mysection{Instantiating the functor} *)
1118
module Little = Make(Endianness.Little)
1120
module Big = Make(Endianness.Big)