14
14
(* *************************************************************************)
15
15
16
16
17
- type 'a with_pos = int * 'a
18
- let with_pos l = List. mapi (fun n x -> n+ 1 ,x) l
19
- let pos (x ,_ ) = x
20
- let data (_ ,x ) = x
21
- let mk_pos pos data = pos, data
17
+ type 'a with_pos = {pos :int ; data :'a}
18
+ let with_pos l = List. mapi (fun n data -> {pos= n+ 1 ; data}) l
22
19
23
20
(* * Composite change and mismatches *)
24
21
type ('l,'r,'diff) mismatch =
@@ -114,12 +111,12 @@ module Define(D:Diffing.Defs with type eq := unit) = struct
114
111
115
112
(* * Compute the partial cycle and edge associated to an edge *)
116
113
let edge state (x :left ) (y :right ) =
117
- let kx, ky = key_left (data x) , key_right (data y) in
114
+ let kx, ky = key_left x.data , key_right y.data in
118
115
let edge =
119
116
if kx < = ky then
120
- Left (pos x , state, (x,y))
117
+ Left (x.pos , state, (x,y))
121
118
else
122
- Right (pos x ,state, (x,y))
119
+ Right (x.pos ,state, (x,y))
123
120
in
124
121
Two_cycle. create kx ky, edge
125
122
@@ -140,40 +137,40 @@ module Define(D:Diffing.Defs with type eq := unit) = struct
140
137
let k, edge = edge state x y in
141
138
Swap. update k (merge_edge edge) swaps, moves
142
139
| Insert nx ->
143
- let k = key_right (data nx) in
144
- let edge = Right (pos nx , state,nx) in
140
+ let k = key_right nx.data in
141
+ let edge = Right (nx.pos , state,nx) in
145
142
swaps, Move. update k (merge_edge edge) moves
146
143
| Delete nx ->
147
- let k, edge = key_left (data nx) , Left (pos nx , state, nx) in
144
+ let k, edge = key_left nx.data , Left (nx.pos , state, nx) in
148
145
swaps, Move. update k (merge_edge edge) moves
149
146
| _ -> swaps, moves
150
147
in
151
148
List. fold_left add (state,(Swap. empty,Move. empty)) changes
152
149
153
150
(* * Check if an edge belongs to a known 2-cycle *)
154
151
let swap swaps x y =
155
- let kx, ky = key_left (data x) , key_right (data y) in
152
+ let kx, ky = key_left x.data , key_right y.data in
156
153
let key = Two_cycle. create kx ky in
157
154
match Swap. find_opt key swaps with
158
155
| None | Some (Left _ | Right _ )-> None
159
156
| Some Both (state , (ll ,lr ),(rl ,rr )) ->
160
157
match test state ll rr, test state rl lr with
161
158
| Ok _ , Ok _ ->
162
- Some (mk_pos ( pos ll) kx, mk_pos ( pos rl) ky )
159
+ Some ({ pos= ll.pos; data = kx}, { pos= rl.pos; data = ky} )
163
160
| Error _ , _ | _ , Error _ -> None
164
161
165
162
let move moves x =
166
163
let name =
167
164
match x with
168
- | Either. Left x -> key_left (data x)
169
- | Either. Right x -> key_right (data x)
165
+ | Either. Left x -> key_left x.data
166
+ | Either. Right x -> key_right x.data
170
167
in
171
168
match Move. find_opt name moves with
172
169
| None | Some (Left _ | Right _ )-> None
173
170
| Some Both (state ,got ,expected ) ->
174
171
match test state got expected with
175
172
| Ok _ ->
176
- Some (Move {name; got= pos got; expected= pos expected})
173
+ Some (Move {name; got= got.pos ; expected= expected.pos })
177
174
| Error _ -> None
178
175
179
176
let refine state patch =
@@ -183,17 +180,17 @@ module Define(D:Diffing.Defs with type eq := unit) = struct
183
180
| Insert x ->
184
181
begin match move moves (Either. Right x) with
185
182
| Some _ as move -> move
186
- | None -> Some (Insert {pos= pos x ;insert= data x })
183
+ | None -> Some (Insert {pos= x.pos ;insert= x.data })
187
184
end
188
185
| Delete x ->
189
186
begin match move moves (Either. Left x) with
190
187
| Some _ -> None
191
- | None -> Some (Delete {pos= pos x; delete= data x })
188
+ | None -> Some (Delete {pos= x.pos; delete= x.data })
192
189
end
193
190
| Change (x ,y , reason ) ->
194
191
match swap swaps x y with
195
- | Some (( pos1 , first ),( pos2 , last ) ) ->
196
- if pos x = pos1 then
192
+ | Some ({ pos = pos1 ; data = first } , { pos = pos2 ; data = last } ) ->
193
+ if x.pos = pos1 then
197
194
Some (Swap { pos = pos1, pos2; first; last})
198
195
else None
199
196
| None -> Some (Change reason)
0 commit comments