1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
module type OrderedType =
|
|
|
|
sig
|
|
|
|
type t
|
|
|
|
val compare: t -> t -> int
|
|
|
|
end
|
|
|
|
|
|
|
|
module type S =
|
|
|
|
sig
|
|
|
|
type key
|
2001-02-12 15:46:18 -08:00
|
|
|
type +'a t
|
1995-05-30 06:33:57 -07:00
|
|
|
val empty: 'a t
|
2004-04-23 03:01:34 -07:00
|
|
|
val is_empty: 'a t -> bool
|
2010-05-25 06:29:43 -07:00
|
|
|
val mem: key -> 'a t -> bool
|
1995-05-30 06:33:57 -07:00
|
|
|
val add: key -> 'a -> 'a t -> 'a t
|
2010-05-25 06:29:43 -07:00
|
|
|
val singleton: key -> 'a -> 'a t
|
1996-04-04 07:57:00 -08:00
|
|
|
val remove: key -> 'a t -> 'a t
|
2013-03-09 14:38:52 -08:00
|
|
|
val merge:
|
|
|
|
(key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
|
2010-05-25 06:29:43 -07:00
|
|
|
val compare: ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
|
|
|
val equal: ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
1997-10-31 04:59:29 -08:00
|
|
|
val iter: (key -> 'a -> unit) -> 'a t -> unit
|
2010-05-25 06:29:43 -07:00
|
|
|
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
|
|
|
val for_all: (key -> 'a -> bool) -> 'a t -> bool
|
|
|
|
val exists: (key -> 'a -> bool) -> 'a t -> bool
|
|
|
|
val filter: (key -> 'a -> bool) -> 'a t -> 'a t
|
|
|
|
val partition: (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
|
|
|
|
val cardinal: 'a t -> int
|
|
|
|
val bindings: 'a t -> (key * 'a) list
|
|
|
|
val min_binding: 'a t -> (key * 'a)
|
|
|
|
val max_binding: 'a t -> (key * 'a)
|
|
|
|
val choose: 'a t -> (key * 'a)
|
|
|
|
val split: key -> 'a t -> 'a t * 'a option * 'a t
|
|
|
|
val find: key -> 'a t -> 'a
|
1997-05-10 08:38:44 -07:00
|
|
|
val map: ('a -> 'b) -> 'a t -> 'b t
|
2000-02-21 10:12:16 -08:00
|
|
|
val mapi: (key -> 'a -> 'b) -> 'a t -> 'b t
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
module Make(Ord: OrderedType) = struct
|
|
|
|
|
|
|
|
type key = Ord.t
|
|
|
|
|
|
|
|
type 'a t =
|
|
|
|
Empty
|
|
|
|
| Node of 'a t * key * 'a * 'a t * int
|
|
|
|
|
|
|
|
let height = function
|
|
|
|
Empty -> 0
|
|
|
|
| Node(_,_,_,_,h) -> h
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let create l x d r =
|
1995-05-30 06:33:57 -07:00
|
|
|
let hl = height l and hr = height r in
|
|
|
|
Node(l, x, d, r, (if hl >= hr then hl + 1 else hr + 1))
|
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
let singleton x d = Node(Empty, x, d, Empty, 1)
|
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
let bal l x d r =
|
|
|
|
let hl = match l with Empty -> 0 | Node(_,_,_,_,h) -> h in
|
|
|
|
let hr = match r with Empty -> 0 | Node(_,_,_,_,h) -> h in
|
|
|
|
if hl > hr + 2 then begin
|
|
|
|
match l with
|
1997-10-15 02:21:03 -07:00
|
|
|
Empty -> invalid_arg "Map.bal"
|
1995-05-30 06:33:57 -07:00
|
|
|
| Node(ll, lv, ld, lr, _) ->
|
|
|
|
if height ll >= height lr then
|
1996-04-22 04:15:41 -07:00
|
|
|
create ll lv ld (create lr x d r)
|
1995-05-30 06:33:57 -07:00
|
|
|
else begin
|
|
|
|
match lr with
|
1997-10-15 02:21:03 -07:00
|
|
|
Empty -> invalid_arg "Map.bal"
|
1995-05-30 06:33:57 -07:00
|
|
|
| Node(lrl, lrv, lrd, lrr, _)->
|
1996-04-22 04:15:41 -07:00
|
|
|
create (create ll lv ld lrl) lrv lrd (create lrr x d r)
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
|
|
|
end else if hr > hl + 2 then begin
|
|
|
|
match r with
|
1997-10-15 02:21:03 -07:00
|
|
|
Empty -> invalid_arg "Map.bal"
|
1995-05-30 06:33:57 -07:00
|
|
|
| Node(rl, rv, rd, rr, _) ->
|
|
|
|
if height rr >= height rl then
|
1996-04-22 04:15:41 -07:00
|
|
|
create (create l x d rl) rv rd rr
|
1995-05-30 06:33:57 -07:00
|
|
|
else begin
|
|
|
|
match rl with
|
1997-10-15 02:21:03 -07:00
|
|
|
Empty -> invalid_arg "Map.bal"
|
1995-05-30 06:33:57 -07:00
|
|
|
| Node(rll, rlv, rld, rlr, _) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
create (create l x d rll) rlv rld (create rlr rv rd rr)
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
|
|
|
end else
|
|
|
|
Node(l, x, d, r, (if hl >= hr then hl + 1 else hr + 1))
|
|
|
|
|
2004-04-23 03:01:34 -07:00
|
|
|
let empty = Empty
|
|
|
|
|
|
|
|
let is_empty = function Empty -> true | _ -> false
|
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
let rec add x data = function
|
|
|
|
Empty ->
|
|
|
|
Node(Empty, x, data, Empty, 1)
|
2004-11-25 05:28:27 -08:00
|
|
|
| Node(l, v, d, r, h) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then
|
|
|
|
Node(l, x, data, r, h)
|
|
|
|
else if c < 0 then
|
|
|
|
bal (add x data l) v d r
|
|
|
|
else
|
|
|
|
bal l v d (add x data r)
|
|
|
|
|
|
|
|
let rec find x = function
|
|
|
|
Empty ->
|
|
|
|
raise Not_found
|
|
|
|
| Node(l, v, d, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then d
|
|
|
|
else find x (if c < 0 then l else r)
|
|
|
|
|
1999-02-25 02:26:38 -08:00
|
|
|
let rec mem x = function
|
|
|
|
Empty ->
|
|
|
|
false
|
|
|
|
| Node(l, v, d, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
|
|
|
c = 0 || mem x (if c < 0 then l else r)
|
|
|
|
|
2003-06-23 00:28:54 -07:00
|
|
|
let rec min_binding = function
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Node(Empty, x, d, r, _) -> (x, d)
|
|
|
|
| Node(l, x, d, r, _) -> min_binding l
|
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
let rec max_binding = function
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Node(l, x, d, Empty, _) -> (x, d)
|
|
|
|
| Node(l, x, d, r, _) -> max_binding r
|
|
|
|
|
2003-06-23 00:28:54 -07:00
|
|
|
let rec remove_min_binding = function
|
|
|
|
Empty -> invalid_arg "Map.remove_min_elt"
|
|
|
|
| Node(Empty, x, d, r, _) -> r
|
|
|
|
| Node(l, x, d, r, _) -> bal (remove_min_binding l) x d r
|
|
|
|
|
|
|
|
let merge t1 t2 =
|
1996-04-04 07:57:00 -08:00
|
|
|
match (t1, t2) with
|
|
|
|
(Empty, t) -> t
|
|
|
|
| (t, Empty) -> t
|
2003-06-23 00:28:54 -07:00
|
|
|
| (_, _) ->
|
|
|
|
let (x, d) = min_binding t2 in
|
|
|
|
bal t1 x d (remove_min_binding t2)
|
1996-04-04 07:57:00 -08:00
|
|
|
|
|
|
|
let rec remove x = function
|
|
|
|
Empty ->
|
|
|
|
Empty
|
2004-11-25 05:28:27 -08:00
|
|
|
| Node(l, v, d, r, h) ->
|
1996-04-04 07:57:00 -08:00
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then
|
|
|
|
merge l r
|
|
|
|
else if c < 0 then
|
|
|
|
bal (remove x l) v d r
|
|
|
|
else
|
|
|
|
bal l v d (remove x r)
|
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
let rec iter f = function
|
|
|
|
Empty -> ()
|
|
|
|
| Node(l, v, d, r, _) ->
|
|
|
|
iter f l; f v d; iter f r
|
|
|
|
|
1997-05-10 08:38:44 -07:00
|
|
|
let rec map f = function
|
2010-05-25 04:22:14 -07:00
|
|
|
Empty ->
|
|
|
|
Empty
|
|
|
|
| Node(l, v, d, r, h) ->
|
|
|
|
let l' = map f l in
|
|
|
|
let d' = f d in
|
|
|
|
let r' = map f r in
|
|
|
|
Node(l', v, d', r', h)
|
1997-05-10 08:38:44 -07:00
|
|
|
|
2000-02-21 10:12:16 -08:00
|
|
|
let rec mapi f = function
|
2010-05-25 04:22:14 -07:00
|
|
|
Empty ->
|
|
|
|
Empty
|
|
|
|
| Node(l, v, d, r, h) ->
|
|
|
|
let l' = mapi f l in
|
|
|
|
let d' = f v d in
|
|
|
|
let r' = mapi f r in
|
|
|
|
Node(l', v, d', r', h)
|
2000-02-21 10:12:16 -08:00
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
let rec fold f m accu =
|
|
|
|
match m with
|
|
|
|
Empty -> accu
|
|
|
|
| Node(l, v, d, r, _) ->
|
2005-08-13 13:59:37 -07:00
|
|
|
fold f r (f v d (fold f l accu))
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
let rec for_all p = function
|
|
|
|
Empty -> true
|
|
|
|
| Node(l, v, d, r, _) -> p v d && for_all p l && for_all p r
|
|
|
|
|
|
|
|
let rec exists p = function
|
|
|
|
Empty -> false
|
|
|
|
| Node(l, v, d, r, _) -> p v d || exists p l || exists p r
|
|
|
|
|
2011-12-23 12:54:57 -08:00
|
|
|
(* Beware: those two functions assume that the added k is *strictly*
|
|
|
|
smaller (or bigger) than all the present keys in the tree; it
|
|
|
|
does not test for equality with the current min (or max) key.
|
|
|
|
|
|
|
|
Indeed, they are only used during the "join" operation which
|
|
|
|
respects this precondition.
|
|
|
|
*)
|
|
|
|
|
|
|
|
let rec add_min_binding k v = function
|
|
|
|
| Empty -> singleton k v
|
|
|
|
| Node (l, x, d, r, h) ->
|
|
|
|
bal (add_min_binding k v l) x d r
|
|
|
|
|
|
|
|
let rec add_max_binding k v = function
|
|
|
|
| Empty -> singleton k v
|
|
|
|
| Node (l, x, d, r, h) ->
|
|
|
|
bal l x d (add_max_binding k v r)
|
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
(* Same as create and bal, but no assumptions are made on the
|
|
|
|
relative heights of l and r. *)
|
|
|
|
|
|
|
|
let rec join l v d r =
|
|
|
|
match (l, r) with
|
2011-12-23 12:54:57 -08:00
|
|
|
(Empty, _) -> add_min_binding v d r
|
|
|
|
| (_, Empty) -> add_max_binding v d l
|
2010-05-25 06:29:43 -07:00
|
|
|
| (Node(ll, lv, ld, lr, lh), Node(rl, rv, rd, rr, rh)) ->
|
|
|
|
if lh > rh + 2 then bal ll lv ld (join lr v d r) else
|
|
|
|
if rh > lh + 2 then bal (join l v d rl) rv rd rr else
|
|
|
|
create l v d r
|
|
|
|
|
|
|
|
(* Merge two trees l and r into one.
|
|
|
|
All elements of l must precede the elements of r.
|
|
|
|
No assumption on the heights of l and r. *)
|
|
|
|
|
|
|
|
let concat t1 t2 =
|
|
|
|
match (t1, t2) with
|
|
|
|
(Empty, t) -> t
|
|
|
|
| (t, Empty) -> t
|
|
|
|
| (_, _) ->
|
|
|
|
let (x, d) = min_binding t2 in
|
|
|
|
join t1 x d (remove_min_binding t2)
|
|
|
|
|
|
|
|
let concat_or_join t1 v d t2 =
|
|
|
|
match d with
|
|
|
|
| Some d -> join t1 v d t2
|
|
|
|
| None -> concat t1 t2
|
|
|
|
|
|
|
|
let rec split x = function
|
|
|
|
Empty ->
|
|
|
|
(Empty, None, Empty)
|
|
|
|
| Node(l, v, d, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then (l, Some d, r)
|
|
|
|
else if c < 0 then
|
|
|
|
let (ll, pres, rl) = split x l in (ll, pres, join rl v d r)
|
|
|
|
else
|
|
|
|
let (lr, pres, rr) = split x r in (join l v d lr, pres, rr)
|
|
|
|
|
|
|
|
let rec merge f s1 s2 =
|
|
|
|
match (s1, s2) with
|
|
|
|
(Empty, Empty) -> Empty
|
|
|
|
| (Node (l1, v1, d1, r1, h1), _) when h1 >= height s2 ->
|
|
|
|
let (l2, d2, r2) = split v1 s2 in
|
|
|
|
concat_or_join (merge f l1 l2) v1 (f v1 (Some d1) d2) (merge f r1 r2)
|
|
|
|
| (_, Node (l2, v2, d2, r2, h2)) ->
|
|
|
|
let (l1, d1, r1) = split v2 s1 in
|
|
|
|
concat_or_join (merge f l1 l2) v2 (f v2 d1 (Some d2)) (merge f r1 r2)
|
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
|
2012-01-16 01:03:16 -08:00
|
|
|
let rec filter p = function
|
|
|
|
Empty -> Empty
|
|
|
|
| Node(l, v, d, r, _) ->
|
2012-05-30 04:28:28 -07:00
|
|
|
(* call [p] in the expected left-to-right order *)
|
|
|
|
let l' = filter p l in
|
|
|
|
let pvd = p v d in
|
|
|
|
let r' = filter p r in
|
|
|
|
if pvd then join l' v d r' else concat l' r'
|
2012-01-16 01:03:16 -08:00
|
|
|
|
|
|
|
let rec partition p = function
|
|
|
|
Empty -> (Empty, Empty)
|
|
|
|
| Node(l, v, d, r, _) ->
|
2012-05-30 04:28:28 -07:00
|
|
|
(* call [p] in the expected left-to-right order *)
|
|
|
|
let (lt, lf) = partition p l in
|
|
|
|
let pvd = p v d in
|
|
|
|
let (rt, rf) = partition p r in
|
|
|
|
if pvd
|
2012-01-16 01:03:16 -08:00
|
|
|
then (join lt v d rt, concat lf rf)
|
|
|
|
else (concat lt rt, join lf v d rf)
|
|
|
|
|
2004-04-23 03:01:34 -07:00
|
|
|
type 'a enumeration = End | More of key * 'a * 'a t * 'a enumeration
|
|
|
|
|
|
|
|
let rec cons_enum m e =
|
|
|
|
match m with
|
|
|
|
Empty -> e
|
|
|
|
| Node(l, v, d, r, _) -> cons_enum l (More(v, d, r, e))
|
|
|
|
|
|
|
|
let compare cmp m1 m2 =
|
|
|
|
let rec compare_aux e1 e2 =
|
|
|
|
match (e1, e2) with
|
|
|
|
(End, End) -> 0
|
|
|
|
| (End, _) -> -1
|
|
|
|
| (_, End) -> 1
|
|
|
|
| (More(v1, d1, r1, e1), More(v2, d2, r2, e2)) ->
|
|
|
|
let c = Ord.compare v1 v2 in
|
|
|
|
if c <> 0 then c else
|
|
|
|
let c = cmp d1 d2 in
|
|
|
|
if c <> 0 then c else
|
|
|
|
compare_aux (cons_enum r1 e1) (cons_enum r2 e2)
|
|
|
|
in compare_aux (cons_enum m1 End) (cons_enum m2 End)
|
|
|
|
|
|
|
|
let equal cmp m1 m2 =
|
|
|
|
let rec equal_aux e1 e2 =
|
|
|
|
match (e1, e2) with
|
|
|
|
(End, End) -> true
|
|
|
|
| (End, _) -> false
|
|
|
|
| (_, End) -> false
|
|
|
|
| (More(v1, d1, r1, e1), More(v2, d2, r2, e2)) ->
|
|
|
|
Ord.compare v1 v2 = 0 && cmp d1 d2 &&
|
|
|
|
equal_aux (cons_enum r1 e1) (cons_enum r2 e2)
|
|
|
|
in equal_aux (cons_enum m1 End) (cons_enum m2 End)
|
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
let rec cardinal = function
|
|
|
|
Empty -> 0
|
|
|
|
| Node(l, _, _, r, _) -> cardinal l + 1 + cardinal r
|
|
|
|
|
|
|
|
let rec bindings_aux accu = function
|
|
|
|
Empty -> accu
|
|
|
|
| Node(l, v, d, r, _) -> bindings_aux ((v, d) :: bindings_aux accu r) l
|
|
|
|
|
|
|
|
let bindings s =
|
|
|
|
bindings_aux [] s
|
|
|
|
|
|
|
|
let choose = min_binding
|
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|