Debian Codegraph @

clib/diff2.ml

raw symbols SCIP
1 (* copied from https://github.com/leque/ocaml-diff.git and renamed from "diff.ml" *)
2
3 (*
4 * Copyright (C) 2016 OOHASHI Daichi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *)
23
24 type 'a common =
25 [ `Common of int * int * 'a ]
26
27 type 'a edit =
28 [ `Added of int * 'a
29 | `Removed of int * 'a
30 | 'a common
31 ]
32
33 module type SeqType = sig
34 type t
35 type elem
36 val get : t -> int -> elem
37 val length : t -> int
38 end
39
40 module type S = sig
41 type t
42 type elem
43
44 val lcs :
45 ?equal:(elem -> elem -> bool) ->
46 t -> t -> elem common list
47
48 val diff :
49 ?equal:(elem -> elem -> bool) ->
50 t -> t -> elem edit list
51
52 val fold_left :
53 ?equal:(elem -> elem -> bool) ->
54 f:('a -> elem edit -> 'a) ->
55 init:'a ->
56 t -> t -> 'a
57
58 val iter :
59 ?equal:(elem -> elem -> bool) ->
60 f:(elem edit -> unit) ->
61 t -> t -> unit
62 end
63
64 module Make(M : SeqType) : (S with type t = M.t and type elem = M.elem) = struct
65 type t = M.t
66 type elem = M.elem
67
68 let lcs ?(equal = (=)) a b =
69 let n = M.length a in
70 let m = M.length b in
71 let mn = m + n in
72 let sz = 2 * mn + 1 in
73 let vd = Array.make sz 0 in
74 let vl = Array.make sz 0 in
75 let vr = Array.make sz [] in
76 let get v i = Array.get v (i + mn) in
77 let set v i x = Array.set v (i + mn) x in
78 let finish () =
79 let rec loop i maxl r =
80 if i > mn then
81 List.rev r
82 else if get vl i > maxl then
83 loop (i + 1) (get vl i) (get vr i)
84 else
85 loop (i + 1) maxl r
86 in loop (- mn) 0 []
87 in
88 if mn = 0 then
89 []
90 else
91 (* For d <- 0 to mn Do *)
92 let rec dloop d =
93 assert (d <= mn);
94 (* For k <- -d to d in steps of 2 Do *)
95 let rec kloop k =
96 if k > d then
97 dloop @@ d + 1
98 else
99 let x, l, r =
100 if k = -d || (k <> d && get vd (k - 1) < get vd (k + 1)) then
101 get vd (k + 1), get vl (k + 1), get vr (k + 1)
102 else
103 get vd (k - 1) + 1, get vl (k - 1), get vr (k - 1)
104 in
105 let x, y, l, r =
106 let rec xyloop x y l r =
107 if x < n && y < m && equal (M.get a x) (M.get b y) then
108 xyloop (x + 1) (y + 1) (l + 1) (`Common(x, y, M.get a x) :: r)
109 else
110 x, y, l, r
111 in xyloop x (x - k) l r
112 in
113 set vd k x;
114 set vl k l;
115 set vr k r;
116 if x >= n && y >= m then
117 (* Stop *)
118 finish ()
119 else
120 kloop @@ k + 2
121 in kloop @@ -d
122 in dloop 0
123
124 let fold_left ?(equal = (=)) ~f ~init a b =
125 let ff x y = f y x in
126 let fold_map f g x from to_ init =
127 let rec loop i init =
128 if i >= to_ then
129 init
130 else
131 loop (i + 1) (f (g i @@ M.get x i) init)
132 in loop from init
133 in
134 let added i x = `Added (i, x) in
135 let removed i x = `Removed (i, x) in
136 let rec loop cs apos bpos init =
137 match cs with
138 | [] ->
139 init
140 |> fold_map ff removed a apos (M.length a)
141 |> fold_map ff added b bpos (M.length b)
142 | `Common (aoff, boff, _) as e :: rest ->
143 init
144 |> fold_map ff removed a apos aoff
145 |> fold_map ff added b bpos boff
146 |> ff e
147 |> loop rest (aoff + 1) (boff + 1)
148 in loop (lcs ~equal a b) 0 0 init
149
150 let diff ?(equal = (=)) a b =
151 fold_left ~equal ~f:(fun xs x -> x::xs) ~init:[] a b
152
153 let iter ?(equal = (=)) ~f a b =
154 fold_left a b
155 ~equal
156 ~f:(fun () x -> f x)
157 ~init:()
158 end