-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.ml
More file actions
242 lines (208 loc) · 7.73 KB
/
patch.ml
File metadata and controls
242 lines (208 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
open Str;; (* for first/last_chars *)
(* behold, the data structure *)
type segment = KeepChars of int
| DelChars of int
| InsChars of string
;;
(* shortcuts *)
let keep n = KeepChars n;;
let del n = DelChars n;;
let ins s = InsChars s;;
(* for debug output *)
let str_of_patchSegment segment =
match segment with
| KeepChars n -> "K" ^ string_of_int n
| DelChars n -> "D" ^ string_of_int n
| InsChars s -> "I\"" ^ s ^ "\"" ;;
let str_of_patch p =
("[" ^ (List.fold_left
(fun prev seg -> (prev ^ (str_of_patchSegment seg) ^ " "))
""
p) ^ "]")
;;
(* how many chars this segment will read *)
let readDim seg =
match seg with
| KeepChars n -> n
| DelChars n -> n
| InsChars _ -> 0
;;
let writeDim seg =
match seg with
| KeepChars n -> n
| DelChars n -> 0
| InsChars s -> String.length s
;;
let advance consumed patch =
if consumed > 0 then
match patch with
| [] -> failwith "tried to advance empty patch"
| (KeepChars n)::pxs -> begin
if n > consumed then
(keep (n-consumed))::pxs
else if n = consumed then
pxs
else failwith "advanced keep segment too far"
end
| (DelChars n)::pxs -> begin
if n > consumed then
(del (n-consumed))::pxs
else if n = consumed then
pxs
else failwith "advanced del segment too far"
end
| (InsChars s)::pxs ->
let slen = String.length s in begin
if slen > consumed then
(ins (last_chars s (slen-consumed)))::pxs
else if slen = consumed then
pxs
else failwith "advanced ins segment too far"
end
else
patch
;;
(* cons, but check for runs of same-typed segments first *)
let defrag newhead applied =
match newhead, applied with
| (InsChars s, (InsChars t)::rest) ->
(InsChars (s^t))::rest (* this could become a performance problem *)
| (DelChars n, (DelChars m)::rest) ->
(DelChars (n+m))::rest
| (KeepChars n, (KeepChars m)::rest) ->
(KeepChars (n+m))::rest
| (_, _) ->
newhead::applied
;;
(* defrag, but make sure del is in front of ins where possible. Basically, put
InsChars behind DelChars *)
let normal_defrag newhead applied =
match newhead, applied with
| (InsChars s), (DelChars n)::(InsChars z)::tail ->
(DelChars n)::(InsChars (s ^ z))::tail
| (InsChars s), (DelChars n)::tail ->
(DelChars n)::(InsChars s)::tail
| _ ->
defrag newhead applied
;;
(* cons if first argument is not None. makes it easier to express algorithms
which may or may not actually produce a new segment *)
let maybe_defrag seg tail =
match seg with
| None -> tail
| Some s -> normal_defrag s tail
;;
(* returns a function you can use as yield (segment option) patchAction patchAction *)
let yield_with make_rest patch base =
fun seg patch_consumed base_consumed -> begin
let newTail = make_rest (advance patch_consumed patch) (advance base_consumed base) in
maybe_defrag seg newTail
end
;;
(* behold, the function. patch is the patch, base the source *)
let rec apply patch base =
let yield = yield_with apply patch base in
match patch, base with
(* base case *)
| ([], []) -> []
(* copy up base del *)
| _, (DelChars n)::rxs ->
yield (Some (DelChars n)) 0 n
(* copy down patch ins *)
| (InsChars s)::lxs, _ ->
yield (Some (InsChars s)) (String.length s) 0
(* patch del *)
| (DelChars m)::lxs, (KeepChars n)::rxs ->
let consumed = (min m n) in
yield (Some (DelChars consumed)) consumed consumed
| (DelChars m)::lxs, (InsChars s)::rxs ->
let slen = (String.length s) in
let consumed = (min m slen) in
yield None consumed consumed
(* patch keep *)
| (KeepChars m)::lxs, (KeepChars n)::rxs ->
let consumed = (min m n) in
(* this defrag only matters for patches that start off with repeated segments *)
yield (Some (KeepChars consumed)) consumed consumed
| (KeepChars m)::lxs, (InsChars s)::rxs ->
let slen = (String.length s) in
let consumed = (min m slen) in
yield (Some (InsChars (Str.first_chars s consumed))) consumed consumed
(* starved reader error *)
| (KeepChars _)::_, [] -> failwith "Starved keeper"
| (DelChars _)::_, [] -> failwith "Starved deleter"
(* dangling writer error *)
| [], (InsChars _)::_ -> failwith "Dangling insert in source"
| [], (KeepChars _)::_ -> failwith "Dangling Keeper in source"
;;
(* patch -> base -> (commuted base, commuted patch) *)
let rec commute patch base =
let yield (new_comm_base_seg, new_patch_base_seg) (patch_consumed, base_consumed) = begin
let (comm_base_tail, comm_patch_tail) = commute (advance patch_consumed patch) (advance base_consumed base) in
( maybe_defrag new_comm_base_seg comm_base_tail, maybe_defrag new_patch_base_seg comm_patch_tail )
end in
match patch, base with
| [], [] -> [], []
(* rhs del *)
| _, (DelChars n)::bxs ->
yield (Some (DelChars n), Some (KeepChars n))
(0, n)
(* lhs ins *)
| (InsChars s)::pxs, _ ->
let slen = String.length s in
yield (Some (KeepChars slen), Some (InsChars s))
(slen, 0)
(* K*K *)
| (KeepChars n)::pxs, (KeepChars m)::bxs ->
let consumed = min n m in
yield (Some (KeepChars consumed), Some(KeepChars consumed))
(consumed, consumed)
(* K*I *)
| (KeepChars n)::pxs, (InsChars s)::bxs ->
let consumed = min n (String.length s) in
yield (Some (InsChars (Str.first_chars s consumed)), None)
(consumed, consumed)
(* D * K *)
| (DelChars n)::pxs, (KeepChars m)::bxs ->
let consumed = min n m in
yield (None, Some (DelChars consumed))
(consumed, consumed)
(* D*I *)
| (DelChars n)::_, (InsChars s)::_ ->
let consumed = min n (String.length s) in
yield (None, None)
(consumed, consumed)
(* starved reader error *)
| (KeepChars _)::_, [] -> failwith "Starved keeper"
| (DelChars _)::_, [] -> failwith "Starved deleter"
(* dangling writer error *)
| [], (InsChars _)::_ -> failwith "Dangling insert in source"
| [], (KeepChars _)::_ -> failwith "Dangling Keeper in source"
;;
(* invert patch with respect to base. That is, the inverse of D will
insert what was in the base there, if it was chars. This will fail for D*K columns,
because we don't (and can't!) know how to invert those *)
let rec invert patch base =
let yield = yield_with invert patch base in
match patch, base with
| [], [] -> []
(* base del is wholly irrelevant *)
| _, (DelChars n)::_ ->
yield None 0 n
| (InsChars s)::_, _ ->
let len = String.length s in
yield (Some (DelChars len)) len 0
| (DelChars n)::_, (KeepChars m)::_ ->
failwith "can't invert D*K"
| (DelChars n)::_, (InsChars s)::_ ->
let len = String.length s in
let m = min n len in
yield (Some (InsChars (Str.first_chars s m))) m m
| (KeepChars n)::_, _::_ -> (* base must have at least one element for keep to be valid *)
yield (Some (KeepChars n)) n n
(* starvation/dangling errors *)
| (KeepChars _)::_, [] -> failwith "Starved keeper while inverting"
| (DelChars _)::_, [] -> failwith "Starved deleter while inverting"
| [], (InsChars _)::_ -> failwith "Dangling insert in base while inverting"
| [], (KeepChars _)::_ -> failwith "Dangling Keeper in base while inverting"
;;