Source file cursor_tools.ml
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
243
244
open Cursor
open Node_type
open Result_lwt.Infix
let traverse (acc, dests, segs, c) f =
let c, v = view c in
let rec next (acc, dests, segs, c) = match dests with
| `Exit -> Ok (acc, `Exit, segs, c)
| `Up (segs, dests) ->
go_up c >>? fun c ->
next (acc, dests, segs, c)
| `Right dests ->
go_side Right c >|? fun c ->
(acc, `Up (segs, dests), Segment.Segs.add_side segs Right, c)
in
let rec exit (acc, dests, segs, c) = match dests with
| `Exit -> Ok (acc, `Exit, segs, c)
| `Up (segs, dests) ->
go_up c >>? fun c -> exit (acc, dests, segs, c)
| `Right dests -> exit (acc, dests, segs, c)
in
match f acc segs c with
| Error e -> Error e
| Ok (`Exit, acc) -> exit (acc, dests, segs, c)
| Ok (`Up, acc) -> next (acc, dests, segs, c)
| Ok (`Continue, acc) ->
match v with
| Leaf _ | Bud (None, _, _) -> next (acc, dests, segs, c)
| Bud (Some _, _, _) ->
begin
go_below_bud c >>? function
| None -> assert false
| Some c -> Ok (acc, `Up (segs, dests), Segment.Segs.push_bud segs, c)
end
| Internal (_, _, _, _) ->
go_side Left c >>? fun c ->
Ok (acc, `Up (segs, `Right dests), Segment.Segs.add_side segs Left, c)
| Extender (seg, _, _, _) ->
go_down_extender c >>? fun c ->
Ok (acc, `Up (segs, dests), Segment.Segs.append_seg segs seg, c)
let fold ~init segs c f =
let rec aux (acc, dests, segs, c) =
traverse (acc, dests, segs, c) f >>? function
| (acc, `Exit, _, c) -> Ok (acc, c)
| x -> aux x
in
aux (init, `Exit, segs, c) >|? fun (acc, c) ->
(acc, c)
let ls c =
let get_seg segs =
match Segment.Segs.to_segments segs with
| [seg] -> seg
| _ -> assert false
in
let f acc segs c =
let c, v = Cursor.view c in
match v with
| Leaf _ -> Ok (`Continue, (get_seg segs, v, c)::acc)
| Bud _ -> Ok (`Up, (get_seg segs, v, c)::acc)
| _ -> Ok (`Continue, acc)
in
fold ~init:[] Segment.Segs.empty' c f
let ls_rec c =
let f acc segs c =
let c, v = Cursor.view c in
match v with
| Leaf _ -> Ok (`Continue, (Segment.Segs.to_segments segs, View v, c)::acc)
| _ -> Ok (`Continue, acc)
in
fold ~init:[] Segment.Segs.empty' c f
module GenTraverse : sig
type ('acc, 'data) visitor
(** Traversal handle *)
type dir = [`Bud | `Extender | `Side of Segment.side]
val prepare
: 'acc
-> 'data
-> Cursor.t
-> ('acc
-> 'data
-> Cursor.t
-> (Cursor.t
* ('acc
* ('data
* dir ) list option),
Error.t) Result.t)
-> ('acc, 'data) visitor
val step : ('acc, 'data) visitor
-> ([`Finished of Cursor.t * 'acc | `Continue of ('acc, 'data) visitor], Error.t) Result.t
(** Run one step of traversal. *)
val fold
: 'acc -> 'data -> Cursor.t
-> ('acc -> 'data -> Cursor.t -> (Cursor.t * ('acc * ('data * dir) list option), Error.t) Result.t)
-> (Cursor.t * 'acc, Error.t) Result.t
(** Traversal in one call. Not interleavable. *)
val ls : Cursor.t -> (Cursor.t * (Segment.t * node * Cursor.t) list, Error.t) Result.t
val ls_rec : Cursor.t -> (Cursor.t * (Segment.t list * node * Cursor.t) list, Error.t) Result.t
end = struct
type dir = [`Bud | `Extender | `Side of Segment.side]
type 'data jobs =
| Up of 'data jobs
| Next of ('data * dir) list * 'data jobs
| End
type ('acc, 'data) visitor =
{ acc : 'acc
; f : ('acc -> 'data -> Cursor.t -> (Cursor.t * ('acc * ('data * dir) list option), Error.t) Result.t)
; data : 'data
; cursor : Cursor.t
; jobs : 'data jobs
}
let step { acc; f; data; cursor=c; jobs } =
let rec exit jobs c =
match jobs with
| End -> Result.return c
| Up jobs -> go_up c >>? fun c -> exit jobs c
| Next (_, jobs) -> exit jobs c
in
let rec next acc jobs c =
match jobs with
| End -> Result.return (`Finished (c, acc))
| Up jobs ->
let c = match Cursor.may_forget c with Some c -> c | None -> c in
go_up c >>? fun c -> next acc jobs c
| Next ([], jobs) -> next acc jobs c
| Next ((data,`Side s)::ds, jobs) ->
go_side s c >|? fun c ->
`Continue { acc; f; data; cursor=c; jobs= Up (Next (ds, jobs)) }
| Next ((data,`Extender)::ds, jobs) ->
go_down_extender c >|? fun c ->
`Continue { acc; f; data; cursor=c; jobs= Up (Next (ds, jobs)) }
| Next ((data,`Bud)::ds, jobs) ->
go_below_bud c >>? function
| None -> next acc (Next (ds, jobs)) c
| Some c ->
Result.return (`Continue { acc; f; data; cursor=c; jobs= Up (Next (ds, jobs)) })
in
f acc data c >>? fun (c, (acc, todo)) ->
match todo with
| None ->
exit jobs c >>? fun c -> Result.return (`Finished (c, acc))
| Some ds ->
let jobs = Next (ds, jobs) in
next acc jobs c
let prepare acc data cursor f = { acc; f; jobs= End; data; cursor }
let fold init data cursor f =
let rec loop tr = step tr >>? function
| `Finished a -> Result.return a
| `Continue tr -> loop tr
in
loop @@ prepare init data cursor f
let ls c =
let f acc rev_seg c =
let c, v = Cursor.view c in
match v with
| Leaf _ -> Ok (c, ( (Segment.of_sides @@ List.rev rev_seg, View v, c)::acc, Some [] ))
| Bud _ -> Ok (c, ( (Segment.of_sides @@ List.rev rev_seg, View v, c)::acc, Some []))
| Internal (_, _, _, _) ->
Ok (c, ( acc, Some [ Segment.Left :: rev_seg, `Side Segment.Left
; Segment.Right :: rev_seg, `Side Segment.Right]))
| Extender (seg, _, _, _) ->
Ok (c, ( acc, Some [ List.rev_append (Segment.to_sides seg) rev_seg, `Extender]))
in
fold [] [] c f
let ls_rec c =
let f acc segs c =
let c, v = Cursor.view c in
match v with
| Leaf _ ->
Ok (c, (( Segment.Segs.to_segments segs, View v, c)::acc, Some []))
| Bud _ ->
Ok (c, ( acc, Some [ Segment.Segs.push_bud segs, `Bud ]))
| Internal (_, _, _, _) ->
Ok (c, (acc, Some [ Segment.Segs.add_side segs Segment.Left, `Side Segment.Left
; Segment.Segs.add_side segs Segment.Right, `Side Segment.Right
]))
| Extender (seg, _, _, _) ->
Ok (c, (acc, Some [ Segment.Segs.append_seg segs seg, `Extender ]))
in
fold [] Segment.Segs.empty' c f
end