| 1 | (* copied from https://github.com/leque/ocaml-diff.git and renamed from "diff.mli" *) |
| 2 | (** |
| 3 | An implementation of Eugene Myers' O(ND) Difference Algorithm\[1\]. |
| 4 | This implementation is a port of util.lcs module of |
| 5 | {{:http://practical-scheme.net/gauche} Gauche Scheme interpreter}. |
| 6 | |
| 7 | - \[1\] Eugene Myers, An O(ND) Difference Algorithm and Its Variations, Algorithmica Vol. 1 No. 2, pp. 251-266, 1986. |
| 8 | *) |
| 9 | |
| 10 | type 'a common = [ |
| 11 | `Common of int * int * 'a |
| 12 | ] |
| 13 | (** an element of lcs of seq1 and seq2 *) |
| 14 | |
| 15 | type 'a edit = |
| 16 | [ `Removed of int * 'a |
| 17 | | `Added of int * 'a |
| 18 | | 'a common |
| 19 | ] |
| 20 | (** an element of diff of seq1 and seq2. *) |
| 21 | |
| 22 | module type SeqType = sig |
| 23 | type t |
| 24 | (** The type of the sequence. *) |
| 25 | |
| 26 | type elem |
| 27 | (** The type of the elements of the sequence. *) |
| 28 | |
| 29 | val get : t -> int -> elem |
| 30 | (** [get t n] returns [n]-th element of the sequence [t]. *) |
| 31 | |
| 32 | val length : t -> int |
| 33 | (** [length t] returns the length of the sequence [t]. *) |
| 34 | end |
| 35 | (** Input signature of {!Diff.Make}. *) |
| 36 | |
| 37 | module type S = sig |
| 38 | type t |
| 39 | (** The type of input sequence. *) |
| 40 | |
| 41 | type elem |
| 42 | (** The type of the elements of result / input sequence. *) |
| 43 | |
| 44 | val lcs : |
| 45 | ?equal:(elem -> elem -> bool) -> |
| 46 | t -> t -> elem common list |
| 47 | (** |
| 48 | [lcs ~equal seq1 seq2] computes the LCS (longest common sequence) of |
| 49 | [seq1] and [seq2]. |
| 50 | Elements of [seq1] and [seq2] are compared with [equal]. |
| 51 | [equal] defaults to [Pervasives.(=)]. |
| 52 | |
| 53 | Elements of lcs are [`Common (pos1, pos2, e)] |
| 54 | where [e] is an element, [pos1] is a position in [seq1], |
| 55 | and [pos2] is a position in [seq2]. |
| 56 | *) |
| 57 | |
| 58 | val diff : |
| 59 | ?equal:(elem -> elem -> bool) -> |
| 60 | t -> t -> elem edit list |
| 61 | (** |
| 62 | [diff ~equal seq1 seq2] computes the diff of [seq1] and [seq2]. |
| 63 | Elements of [seq1] and [seq2] are compared with [equal]. |
| 64 | |
| 65 | Elements only in [seq1] are represented as [`Removed (pos, e)] |
| 66 | where [e] is an element, and [pos] is a position in [seq1]; |
| 67 | those only in [seq2] are represented as [`Added (pos, e)] |
| 68 | where [e] is an element, and [pos] is a position in [seq2]; |
| 69 | those common in [seq1] and [seq2] are represented as |
| 70 | [`Common (pos1, pos2, e)] |
| 71 | where [e] is an element, [pos1] is a position in [seq1], |
| 72 | and [pos2] is a position in [seq2]. |
| 73 | *) |
| 74 | |
| 75 | val fold_left : |
| 76 | ?equal:(elem -> elem -> bool) -> |
| 77 | f:('a -> elem edit -> 'a) -> |
| 78 | init:'a -> |
| 79 | t -> t -> 'a |
| 80 | (** |
| 81 | [fold_left ~equal ~f ~init seq1 seq2] is same as |
| 82 | [diff ~equal seq1 seq2 |> ListLabels.fold_left ~f ~init], |
| 83 | but does not create an intermediate list. |
| 84 | *) |
| 85 | |
| 86 | val iter : |
| 87 | ?equal:(elem -> elem -> bool) -> |
| 88 | f:(elem edit -> unit) -> |
| 89 | t -> t -> unit |
| 90 | (** |
| 91 | [iter ~equal ~f seq1 seq2] is same as |
| 92 | [diff ~equal seq1 seq2 |> ListLabels.iter ~f], |
| 93 | but does not create an intermediate list. |
| 94 | *) |
| 95 | end |
| 96 | (** Output signature of {!Diff.Make}. *) |
| 97 | |
| 98 | module Make : |
| 99 | functor (M : SeqType) -> (S with type t = M.t and type elem = M.elem) |
| 100 | (** Functor building an implementation of the diff structure |
| 101 | given a sequence type. *) |