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
|
module Mixed (A: Map) (B: Map with type key = A.key)
:
sig
(* folds *)
type ('a,'b) intersection =
(A.key -> 'a -> 'a -> 'b -> 'b) -> 'b -> 'a A.t -> 'a B.t -> 'b
type ('a,'b) union =
(B.key -> 'a -> 'b -> 'b) -> 'b -> 'a A.t -> 'a B.t -> 'b
type ('a,'b,'c) product =
(A.key -> B.key -> 'a -> 'b -> 'c -> 'c) -> 'c -> 'a A.t -> 'b B.t -> 'c
(* predicates *)
type ('a,'b) intersection_cond =
A.key -> 'a -> 'a -> bool
type ('a,'b) product_cond =
A.key -> B.key -> 'a -> 'b -> bool
(* operations *)
val union: ('a,'b) union
val intersection: ('a,'b) intersection
val product: ('a,'b,'c) product
val filter_union: ('a) A.cond -> ('a,'b) union
val filter_product: ('a,'b) product_cond -> ('a,'b,'c) product
val filter_intersection: ('a,'b) intersection_cond -> ('a,'b) intersection
val make_union: ('a,'b) A.fold -> ('a,'b) B.fold -> ('a,'b) union
val make_intersection: ('a,'b) A.fold -> ('a,'b) B.fold -> ('a,'b) intersection
val make_product:
((A.key -> 'a -> 'b -> 'b) -> 'c -> 'd A.t -> 'c) ->
((B.key -> 'd -> 'c -> 'c) -> 'b -> 'a B.t -> 'b) ->
(A.key -> B.key -> 'a -> 'd -> 'c -> 'c) -> 'c -> 'd A.t -> 'a B.t -> 'c
(* ('a,'b) A.fold -> ('c,'d) B.fold -> ('e,'f,'g) product *)
end
=
struct
(* folds *)
type ('a,'b) intersection =
(A.key -> 'a -> 'a -> 'b -> 'b) -> 'b -> 'a A.t -> 'a B.t -> 'b
type ('a,'b) union =
(B.key -> 'a -> 'b -> 'b) -> 'b -> 'a A.t -> 'a B.t -> 'b
type ('a,'b,'c) product =
(A.key -> B.key -> 'a -> 'b -> 'c -> 'c) -> 'c -> 'a A.t -> 'b B.t -> 'c
(* predicates *)
type ('a,'b) intersection_cond =
A.key -> 'a -> 'a -> bool
type ('a,'b) product_cond =
A.key -> B.key -> 'a -> 'b -> bool
(* operations *)
let make_intersection fa fb f init ta tb =
if A.count ta < B.count tb then
fa
( fun key a b -> match B.lookup key tb with
| None -> b
| Some x -> f key a x b )
init ta
else
fb
( fun key a b -> match A.lookup key ta with
| None -> b
| Some x -> f key a x b )
init tb
let make_union fa fb f init ta tb =
fb
f
( fa
(fun key a b -> if B.has key tb then b else f key a b)
init ta )
tb
let make_product fa fb f init ta tb =
fa
(fun key a b -> fb (fun k -> f key k a) b tb)
init ta
let filter_intersection cond f init ta tb =
if A.count ta < B.count tb then
A.fold
( fun key a b -> match B.lookup key tb with
| None -> b
| Some x -> if cond key a x then f key a x b else b)
init ta
else
B.fold
( fun key a b -> match A.lookup key ta with
| None -> b
| Some x -> if cond key a x then f key a x b else b)
init tb
let filter_union cond f init ta tb =
B.filter
cond
f
( A.filter
cond
(fun key a b -> if B.has key tb then b else f key a b)
init ta )
tb
let filter_product cond f init ta tb =
A.fold
(fun key a b -> B.filter (fun k -> cond key k a) (fun k -> f key k a) b tb)
init ta
let union f =
make_union A.fold B.fold f
let product f =
make_product A.fold B.fold f
let intersection f =
make_intersection A.fold B.fold f
end |
Partager