Skip to content

Commit

Permalink
[feature] libbase: Taking an element to show the difference between …
Browse files Browse the repository at this point in the history
…two set/map

    Usefull to explain why two sets are not equal.
  • Loading branch information
Arthur Milchior authored and Mathieu Barbin committed Jul 11, 2011
1 parent e74f6b5 commit 645f07a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libbase/baseMap.ml
Expand Up @@ -580,6 +580,16 @@ struct
| Empty -> None
| Node(_, k, v, _r, _) -> Some (k, v)

let example_diff s1 s2 =
let diff_ = diff s1 s2 in
match choose_opt diff_ with
| Some elt -> Some elt
| None ->
let diff = diff s2 s1 in
match choose_opt diff with
| Some elt -> Some elt
| None -> None

let from_sorted_array keys vals =
let len_keys = Array.length keys in
let len_vals = Array.length vals in
Expand Down
7 changes: 7 additions & 0 deletions libbase/baseMapSig.ml
Expand Up @@ -208,6 +208,13 @@ sig
*)
val diff2 : 'a t -> _ t -> _ t -> 'a t

(**
If the two set have different key, give back an element in the left set not
in the right one, or in the right one not in the left one, or None.
See also [BaseSetSig.S.example_diff]
*)
val example_diff : 'a t -> 'a t -> (key * 'a) option

(**
Optimized initialization for huge maps, to avoid temporary unused balancing.
The key array should be sorted in increasing order, using the same order than the set.
Expand Down
10 changes: 10 additions & 0 deletions libbase/baseSet.ml
Expand Up @@ -325,6 +325,16 @@ struct
| Empty -> None
| Node (_, v, _r, _) -> Some v

let example_diff s1 s2 =
let diff_ = diff s1 s2 in
match choose_opt diff_ with
| Some elt -> Some elt
| None ->
let diff = diff s2 s1 in
match choose_opt diff with
| Some elt -> Some elt
| None -> None

let complete_join big small =
fold add small big

Expand Down
9 changes: 9 additions & 0 deletions libbase/baseSetSig.ml
Expand Up @@ -67,6 +67,15 @@ sig
Returns an arbitrary element of the set if it is not empty
*)
val choose_opt : t -> elt option

(**
If the two set are different, give back an element in the left set not in
the right one, or in the right one not in the left one, or None
We cannot use there [elt Base.either option], because this would create
a circular build. If needed, we can return a variant indicating where
the element was found.
*)
val example_diff : t -> t -> elt option
val split : elt -> t -> t * bool * t
val draw : t -> elt * t
val size : t -> int
Expand Down
10 changes: 10 additions & 0 deletions libbase/intMap.ml
Expand Up @@ -463,4 +463,14 @@ let diff2 map1 map2 map3 =
add k v acc
) map1 empty

let example_diff s1 s2 =
let diff_ = diff s1 s2 in
match choose_opt diff_ with
| Some elt -> Some elt
| None ->
let diff = diff s2 s1 in
match choose_opt diff with
| Some elt -> Some elt
| None -> None

let from_sorted_array _ = assert false
10 changes: 10 additions & 0 deletions libbase/intSet.ml
Expand Up @@ -76,6 +76,16 @@ let choose_opt (set:t) =
| None -> None
| Some (a, _) -> Some a

let example_diff s1 s2 =
let diff_ = diff s1 s2 in
match choose_opt diff_ with
| Some elt -> Some elt
| None ->
let diff = diff s2 s1 in
match choose_opt diff with
| Some elt -> Some elt
| None -> None

let split int set =
fold
(fun i (inf, b, sup) ->
Expand Down

0 comments on commit 645f07a

Please sign in to comment.