Source file segment.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
open Utils.Open
let max_short_segment_length = 215
let max_length = 1815
type side = Left | Right
let string_of_side = function
| Left -> "L"
| Right -> "R"
type segment =
| List of side list
| Cons of side * segment
| Append of segment * segment
| Encoding of int * string
type t = segment
let empty = List []
let of_sides l = List l
let of_sides = of_sides
let rec is_empty = function
| List [] -> true
| List _ -> false
| Cons _ -> false
| Append (seg1, seg2) -> is_empty seg1 && is_empty seg2
| Encoding (0, _) -> true
| Encoding _ -> false
let rec length = function
| List ss -> List.length ss
| Cons (_, seg) -> length seg + 1
| Append (seg1, seg2) -> length seg1 + length seg2
| Encoding (len, _) -> len
let cons x xs = Cons (x, xs)
let append seg1 seg2 = match seg1, seg2 with
| List xs1, List xs2 -> List (xs1 @ xs2)
| _ ->
if is_empty seg1 then seg2
else if is_empty seg2 then seg1
else Append (seg1, seg2)
let rec concat = function
| [] -> List []
| seg::segs -> append seg @@ concat segs
let decode_ s =
let sides_of_char c =
let c = Char.code c in
let f x = if c land x = 0 then Left else Right in
[f 128; f 64; f 32; f 16; f 8; f 4; f 2; f 1]
in
let sides =
List.concat @@ List.of_seq @@ Seq.map (fun c -> sides_of_char c) @@ String.to_seq s
in
let rec n = function
| Left :: xs ->
let n = n + 1 in
remove_header n xs
| Right :: xs -> xs
| [] -> assert false
in
remove_header 0 sides
let rec to_sides seg =
match seg with
| List ss -> ss
| Cons (s, seg) -> s :: to_sides seg
| Append (seg1, seg2) -> to_sides seg1 @ to_sides seg2
| Encoding (_, s) -> decode_ s
let rec equal seg1 seg2 =
seg1 == seg2 ||
match seg1, seg2 with
| List ss1, List ss2 -> ss1 = ss2
| Cons (s1, seg1), Cons (s2, seg2) ->
s1 = s2 && equal seg1 seg2
| Encoding (_, s), Encoding (_, s') -> s = s'
| _ -> to_sides seg1 = to_sides seg2
let compare seg1 seg2 = compare (to_sides seg1) (to_sides seg2)
let rec cut = function
| List [] -> None
| List (x::xs) -> Some (x, List xs)
| Cons (x,seg) -> Some (x, seg)
| Append (seg1, seg2) when is_empty seg1 -> cut seg2
| Append (seg1, seg2) ->
begin match cut seg1 with
| None -> assert false
| Some (side, seg1) when is_empty seg1 -> Some (side, seg2)
| Some (side, seg1) -> Some (side, Append (seg1, seg2))
end
| Encoding (0, _) -> None
| Encoding (_, s) ->
begin match decode_ s with
| [] -> assert false
| s::seg -> Some (s, List seg)
end
let decode s =
let len = String.length s in
let rec skip_zeros i =
if i >= len then assert false
else
let c = String.unsafe_get s i in
if c = '\x00' then skip_zeros (i+1)
else i
in
let nz = skip_zeros 0 in
assert (len - nz > 0);
let s = String.sub s nz (len - nz) in
let c = Char.code @@ String.unsafe_get s 0 in
let first_one =
if c land 128 <> 0 then 0
else if c land 64 <> 0 then 1
else if c land 32 <> 0 then 2
else if c land 16 <> 0 then 3
else if c land 8 <> 0 then 4
else if c land 4 <> 0 then 5
else if c land 2 <> 0 then 6
else 7
in
let seglen = (len - nz) * 8 - first_one - 1 in
Encoding (seglen, s)
let encode_ sides =
let len = List.length sides in
let bytes = len / 8 + 1 in
let head_zero_bits = bytes * 8 - len - 1 in
assert (0 <= head_zero_bits && head_zero_bits <= 7);
let bytes = Bytes.create bytes in
let make_byte = function
| x1 :: x2 :: x3 :: x4 :: x5 :: x6 :: x7 :: x8 :: seg ->
let bit = function
| Left -> 0
| Right -> 1
in
let byte =
(bit x1) lsl 7
+ (bit x2) lsl 6
+ (bit x3) lsl 5
+ (bit x4) lsl 4
+ (bit x5) lsl 3
+ (bit x6) lsl 2
+ (bit x7) lsl 1
+ (bit x8) * 1
in
(Char.chr byte, seg)
| seg -> Log.fatal "make_byte: %d" (List.length seg); assert false
in
let rec fill_bytes byte_pos = function
| [] ->
Bytes.to_string bytes
| seg ->
let (c, seg) = make_byte seg in
Bytes.unsafe_set bytes byte_pos c;
let byte_pos' = byte_pos + 1 in
fill_bytes byte_pos' seg
in
let sides' =
List.init head_zero_bits (fun _ -> Left)
@ Right :: sides
in
fill_bytes 0 sides'
let encode = function
| Encoding (_, s) -> s
| x -> encode_ @@ to_sides x
let to_string s = String.concat "" (List.map string_of_side @@ to_sides s)
let string_of_sides ss = String.concat "" (List.map string_of_side ss)
let string_of_sides = string_of_sides
let of_string s =
let rec aux st = function
| -1 -> Some (of_sides st)
| n ->
match String.unsafe_get s n with
| 'L' -> aux (Left :: st) (n-1)
| 'R' -> aux (Right :: st) (n-1)
| _ -> None
in
aux [] @@ String.length s - 1
let rec common_prefix seg1 seg2 = match (cut seg1, cut seg2) with
| (Some (h1, t1), Some (h2, t2)) ->
if h1 = h2 then
let (prefix, r1, r2) = common_prefix t1 t2 in
(h1 :: prefix, r1, r2)
else
([], seg1, seg2)
| (None, _) -> ([], empty, seg2)
| (_, None) -> ([], seg1, empty)
let common_prefix seg1 seg2 =
let sides, seg2, seg3 = common_prefix seg1 seg2 in
of_sides sides, seg2, seg3
let encoding =
let open Data_encoding in
string
|> conv to_string (fun s -> from_Some (of_string s))
let segments_to_string segs = String.concat "/" @@ List.map to_string segs
let string_of_segments = segments_to_string
let of_char c =
let c = Char.code c in
let bit n = if c land n = 0 then Left else Right in
[ bit 128 ; bit 64 ; bit 32 ; bit 16 ; bit 8 ; bit 4 ; bit 2 ; bit 1]
let encode_string s =
let open Data_encoding in
match Binary.to_bytes Data_encoding.Encoding.string s with
| Error _ -> assert false
| Ok b ->
let of_binary_string s =
let rec f st = function
| -1 -> st
| i ->
let c = String.unsafe_get s i in
f (of_char c @ st) (i-1)
in
f [] (String.length s - 1)
in
of_sides @@ of_binary_string (Bytes.to_string b)
let decode_string seg =
let sides = to_sides seg in
let buf = Buffer.create 10 in
let bit n = function
| Left -> 0
| Right -> n
in
let rec f = function
| [] -> Some (Buffer.contents buf)
| b7::b6::b5::b4::b3::b2::b1::b0::sides ->
Buffer.add_char buf
@@ Char.chr @@ bit 128 b7
+ bit 64 b6
+ bit 32 b5
+ bit 16 b4
+ bit 8 b3
+ bit 4 b2
+ bit 2 b1
+ bit 1 b0;
f sides
| _ -> None
in
match f sides with
| None -> None
| Some s ->
match Data_encoding.Binary.of_bytes Data_encoding.Encoding.string (Bytes.of_string s) with
| Error _ -> None
| Ok x -> Some x
module Segs = struct
type t =
{ rev_segs : segment list
; bottom : side list
}
let empty = { rev_segs = []; bottom = [] }
let add_side t side = { t with bottom = side :: t.bottom }
let append_seg t seg = { t with bottom = List.rev_append (to_sides seg) t.bottom }
let append_sides t sides = { t with bottom = List.rev_append sides t.bottom }
let append_rev_sides t rev_sides = { t with bottom = rev_sides @ t.bottom }
let push_bud t =
if t.bottom = [] && t.rev_segs = [] then t
else begin
assert ( t.bottom <> [] );
{ rev_segs = of_sides (List.rev t.bottom) :: t.rev_segs; bottom= [] }
end
let finalize t = List.rev (of_sides (List.rev t.bottom) :: t.rev_segs)
let to_string t =
let segs = finalize t in
String.concat "/" (List.map to_string segs)
let of_segments segs = match List.rev segs with
| [] -> empty
| bottom :: rev_segs -> { rev_segs; bottom= to_sides bottom }
let last t = List.rev t.bottom
end