Source file time_slots.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
open Int64_utils
exception Time_slots_are_not_sorted
exception Time_slots_are_not_disjoint
module Check = struct
let check_if_valid (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq.map Time_slot.Check.check_if_valid time_slots
let check_if_not_empty (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq.map Time_slot.Check.check_if_not_empty time_slots
let check_if_sorted (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq_utils.check_if_f_holds_for_immediate_neighbors ~f:Time_slot.le
~f_exn:(fun _ _ -> Time_slots_are_not_sorted)
time_slots
let check_if_sorted_rev (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq_utils.check_if_f_holds_for_immediate_neighbors ~f:Time_slot.ge
~f_exn:(fun _ _ -> Time_slots_are_not_sorted)
time_slots
let check_if_disjoint (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq_utils.check_if_f_holds_for_immediate_neighbors
~f:(fun x y ->
match Time_slot.overlap_of_a_over_b ~a:y ~b:x with
| None, None, None | Some _, None, None | None, None, Some _ -> true
| _ -> false)
~f_exn:(fun _ _ -> Time_slots_are_not_disjoint)
time_slots
let check_if_normalized (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
time_slots
|> check_if_valid
|> check_if_not_empty
|> check_if_sorted
|> check_if_disjoint
end
module Filter = struct
let filter_invalid (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq.filter Time_slot.Check.is_valid time_slots
let filter_invalid_list (time_slots : Time_slot.t list) : Time_slot.t list =
List.filter Time_slot.Check.is_valid time_slots
let filter_empty (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
Seq.filter Time_slot.Check.is_not_empty time_slots
let filter_empty_list (time_slots : Time_slot.t list) : Time_slot.t list =
List.filter Time_slot.Check.is_not_empty time_slots
end
module Sort = struct
let sort_time_slots_list ?(skip_check = false) (time_slots : Time_slot.t list)
: Time_slot.t list =
time_slots
|> (fun l ->
if skip_check then l
else l |> List.to_seq |> Check.check_if_valid |> List.of_seq)
|> List.sort Time_slot.compare
let sort_uniq_time_slots_list ?(skip_check = false)
(time_slots : Time_slot.t list) : Time_slot.t list =
time_slots
|> (fun l ->
if skip_check then l
else l |> List.to_seq |> Check.check_if_valid |> List.of_seq)
|> List.sort_uniq Time_slot.compare
let sort_uniq_time_slots ?(skip_check = false)
(time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
time_slots
|> (fun s -> if skip_check then s else Check.check_if_valid s)
|> List.of_seq
|> List.sort_uniq Time_slot.compare
|> List.to_seq
let sort_time_slots ?(skip_check = false) (time_slots : Time_slot.t Seq.t) :
Time_slot.t Seq.t =
time_slots
|> (fun s -> if skip_check then s else Check.check_if_valid s)
|> List.of_seq
|> List.sort Time_slot.compare
|> List.to_seq
end
module Join_internal = struct
let join (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux cur time_slots =
match time_slots () with
| Seq.Nil -> (
match cur with None -> Seq.empty | Some x -> Seq.return x )
| Seq.Cons ((start, end_exc), rest) -> (
match cur with
| None -> aux (Some (start, end_exc)) rest
| Some cur -> (
match Time_slot.join cur (start, end_exc) with
| Some x -> aux (Some x) rest
| None ->
fun () -> Seq.Cons (cur, aux (Some (start, end_exc)) rest) ) )
in
aux None time_slots
end
let join ?(skip_check = false) time_slots =
time_slots
|> (fun s ->
if skip_check then s
else s |> Check.check_if_valid |> Check.check_if_sorted)
|> Join_internal.join
module Normalize = struct
let normalize ?(skip_filter_invalid = false) ?(skip_filter_empty = false)
?(skip_sort = false) time_slots =
time_slots
|> (fun s -> if skip_filter_invalid then s else Filter.filter_invalid s)
|> (fun s -> if skip_filter_empty then s else Filter.filter_empty s)
|> (fun s -> if skip_sort then s else Sort.sort_uniq_time_slots s)
|> Join_internal.join
let normalize_list_in_seq_out ?(skip_filter_invalid = false)
?(skip_filter_empty = false) ?(skip_sort = false) time_slots =
time_slots
|> List.to_seq
|> normalize ~skip_filter_invalid ~skip_filter_empty ~skip_sort
end
module Slice_internal = struct
let slice_start ~start (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux start time_slots =
match time_slots () with
| Seq.Nil -> Seq.empty
| Seq.Cons ((ts_start, ts_end_exc), rest) ->
if start <= ts_start then
time_slots
else if ts_start < start && start < ts_end_exc then
fun () -> Seq.Cons ((start, ts_end_exc), rest)
else
aux start rest
in
aux start time_slots
let slice_end_exc ~end_exc (time_slots : Time_slot.t Seq.t) :
Time_slot.t Seq.t =
let rec aux end_exc time_slots =
match time_slots () with
| Seq.Nil -> Seq.empty
| Seq.Cons ((ts_start, ts_end_exc), rest) ->
if end_exc <= ts_start then
aux end_exc Seq.empty
else if ts_start < end_exc && end_exc < ts_end_exc then
fun () -> Seq.Cons ((ts_start, end_exc), aux end_exc Seq.empty)
else
fun () -> Seq.Cons ((ts_start, ts_end_exc), aux end_exc rest)
in
aux end_exc time_slots
end
module Slice_rev_internal = struct
let slice_start ~start (time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux acc start time_slots =
match time_slots () with
| Seq.Nil -> List.rev acc |> List.to_seq
| Seq.Cons ((ts_start, ts_end_exc), slots) ->
if start <= ts_start then
aux ((ts_start, ts_end_exc) :: acc) start slots
else if ts_start < start && start < ts_end_exc then
aux ((start, ts_end_exc) :: acc) start slots
else
aux acc start Seq.empty
in
aux [] start time_slots
let slice_end_exc ~end_exc (time_slots : Time_slot.t Seq.t) :
Time_slot.t Seq.t =
let rec aux end_exc time_slots =
match time_slots () with
| Seq.Nil -> Seq.empty
| Seq.Cons ((ts_start, ts_end_exc), slots) ->
if ts_end_exc <= end_exc then
time_slots
else if ts_start < end_exc && end_exc < ts_end_exc then
OSeq.cons (ts_start, end_exc) slots
else
aux end_exc slots
in
aux end_exc time_slots
end
module Slice = struct
let slice ?(skip_check = false) ?start ?end_exc time_slots =
time_slots
|> (fun s ->
if skip_check then s
else
s
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted)
|> (fun s ->
match start with
| None -> s
| Some start -> Slice_internal.slice_start ~start s)
|> fun s ->
match end_exc with
| None -> s
| Some end_exc -> Slice_internal.slice_end_exc ~end_exc s
let slice_rev ?(skip_check = false) ?start ?end_exc time_slots =
time_slots
|> (fun s ->
if skip_check then s
else
s
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted_rev)
|> (fun s ->
match start with
| None -> s
| Some start -> Slice_rev_internal.slice_start ~start s)
|> fun s ->
match end_exc with
| None -> s
| Some end_exc -> Slice_rev_internal.slice_end_exc ~end_exc s
end
let relative_complement ?(skip_check = false) ~(not_mem_of : Time_slot.t Seq.t)
(mem_of : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux mem_of not_mem_of =
match (mem_of (), not_mem_of ()) with
| Seq.Nil, _ -> Seq.empty
| _, Seq.Nil -> mem_of
| ( Seq.Cons (mem_of_ts, mem_of_rest),
Seq.Cons (not_mem_of_ts, not_mem_of_rest) ) -> (
let mem_of () = Seq.Cons (mem_of_ts, mem_of_rest) in
let not_mem_of () = Seq.Cons (not_mem_of_ts, not_mem_of_rest) in
match Time_slot.overlap_of_a_over_b ~a:mem_of_ts ~b:not_mem_of_ts with
| None, None, None ->
aux mem_of_rest not_mem_of
| Some _, None, None ->
fun () -> Seq.Cons (mem_of_ts, aux mem_of_rest not_mem_of)
| None, None, Some _ ->
aux mem_of not_mem_of_rest
| Some (start, end_exc), Some _, None ->
fun () -> Seq.Cons ((start, end_exc), aux mem_of_rest not_mem_of)
| None, Some _, None -> aux mem_of_rest not_mem_of
| None, Some _, Some (start, end_exc) ->
let mem_of () = Seq.Cons ((start, end_exc), mem_of_rest) in
aux mem_of not_mem_of_rest
| Some (start1, end_exc1), _, Some (start2, end_exc2) ->
let mem_of () = Seq.Cons ((start2, end_exc2), mem_of_rest) in
fun () -> Seq.Cons ((start1, end_exc1), aux mem_of not_mem_of_rest)
)
in
let mem_of =
if skip_check then mem_of
else
mem_of
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted
in
let not_mem_of =
if skip_check then not_mem_of
else
not_mem_of
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted
in
aux mem_of not_mem_of
let invert ?(skip_check = false) ~start ~end_exc
(time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
relative_complement ~skip_check ~not_mem_of:time_slots
(Seq.return (start, end_exc))
let inter ?(skip_check = false) (time_slots1 : Time_slot.t Seq.t)
(time_slots2 : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux time_slots1 time_slots2 : Time_slot.t Seq.t =
match (time_slots1 (), time_slots2 ()) with
| Seq.Nil, _ -> Seq.empty
| _, Seq.Nil -> Seq.empty
| Seq.Cons ((start1, end_exc1), rest1), Seq.Cons ((start2, end_exc2), rest2)
->
if end_exc1 < start2 then
aux rest1 time_slots2
else if end_exc2 < start1 then
aux time_slots1 rest2
else
let overlap_start = max start1 start2 in
let overlap_end_exc = min end_exc1 end_exc2 in
let s1 = if end_exc1 <= overlap_end_exc then rest1 else time_slots1 in
let s2 = if end_exc2 <= overlap_end_exc then rest2 else time_slots2 in
if overlap_start < overlap_end_exc then
fun () -> Seq.Cons ((overlap_start, overlap_end_exc), aux s1 s2)
else aux s1 s2
in
let time_slots1 =
if skip_check then time_slots1
else
time_slots1
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted
in
let time_slots2 =
if skip_check then time_slots2
else
time_slots2
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted
in
aux time_slots1 time_slots2
module Merge = struct
let merge ?(skip_check = false) (time_slots1 : Time_slot.t Seq.t)
(time_slots2 : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux time_slots1 time_slots2 =
match (time_slots1 (), time_slots2 ()) with
| Seq.Nil, s | s, Seq.Nil -> fun () -> s
| Seq.Cons (x1, rest1), Seq.Cons (x2, rest2) ->
let ts1 () = Seq.Cons (x1, rest1) in
let ts2 () = Seq.Cons (x2, rest2) in
if Time_slot.le x1 x2 then fun () -> Seq.Cons (x1, aux rest1 ts2)
else fun () -> Seq.Cons (x2, aux rest2 ts1)
in
let time_slots1 =
if skip_check then time_slots1
else time_slots1 |> Check.check_if_valid |> Check.check_if_sorted
in
let time_slots2 =
if skip_check then time_slots2
else time_slots2 |> Check.check_if_valid |> Check.check_if_sorted
in
aux time_slots1 time_slots2
let merge_multi_seq ?(skip_check = false)
(time_slot_batches : Time_slot.t Seq.t Seq.t) : Time_slot.t Seq.t =
Seq.fold_left
(fun acc time_slots -> merge ~skip_check acc time_slots)
Seq.empty time_slot_batches
let merge_multi_list ?(skip_check = false)
(time_slot_batches : Time_slot.t Seq.t list) : Time_slot.t Seq.t =
List.to_seq time_slot_batches |> merge_multi_seq ~skip_check
end
module Round_robin = struct
let collect_round_robin_non_decreasing ?(skip_check = false)
(batches : Time_slot.t Seq.t list) : Time_slot.t option list Seq.t =
batches
|> List.map (fun s ->
if skip_check then s
else s |> Check.check_if_valid |> Check.check_if_sorted)
|> Seq_utils.collect_round_robin Time_slot.le
let merge_multi_list_round_robin_non_decreasing ?(skip_check = false)
(batches : Time_slot.t Seq.t list) : Time_slot.t Seq.t =
collect_round_robin_non_decreasing ~skip_check batches
|> Seq.flat_map (fun l -> List.to_seq l |> Seq.filter_map (fun x -> x))
let merge_multi_seq_round_robin_non_decreasing ?(skip_check = false)
(batches : Time_slot.t Seq.t Seq.t) : Time_slot.t Seq.t =
batches
|> List.of_seq
|> merge_multi_list_round_robin_non_decreasing ~skip_check
end
module Union = struct
let union ?(skip_check = false) time_slots1 time_slots2 =
let time_slots1 =
if skip_check then time_slots1
else
time_slots1
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted
in
let time_slots2 =
if skip_check then time_slots2
else
time_slots2
|> Check.check_if_valid
|> Check.check_if_disjoint
|> Check.check_if_sorted
in
Merge.merge time_slots1 time_slots2
|> Normalize.normalize ~skip_filter_invalid:true ~skip_filter_empty:true
~skip_sort:true
let union_multi_seq ?(skip_check = false)
(time_slot_batches : Time_slot.t Seq.t Seq.t) : Time_slot.t Seq.t =
Seq.fold_left
(fun acc time_slots -> union ~skip_check acc time_slots)
Seq.empty time_slot_batches
let union_multi_list ?(skip_check = false)
(time_slot_batches : Time_slot.t Seq.t list) : Time_slot.t Seq.t =
List.to_seq time_slot_batches |> union_multi_seq ~skip_check
end
let chunk ?(skip_check = false) ?(drop_partial = false) ~chunk_size
(time_slots : Time_slot.t Seq.t) : Time_slot.t Seq.t =
let rec aux time_slots =
match time_slots () with
| Seq.Nil -> Seq.empty
| Seq.Cons ((start, end_exc), rest) ->
let chunk_end_exc = min end_exc (start +^ chunk_size) in
let size = chunk_end_exc -^ start in
if size = 0L || (size < chunk_size && drop_partial) then aux rest
else
let rest () = Seq.Cons ((chunk_end_exc, end_exc), rest) in
fun () -> Seq.Cons ((start, chunk_end_exc), aux rest)
in
time_slots
|> (fun s -> if skip_check then s else s |> Check.check_if_valid)
|> aux
module Sum = struct
let sum_length ?(skip_check = false) (time_slots : Time_slot.t Seq.t) : int64
=
time_slots
|> (fun s -> if skip_check then s else Check.check_if_valid s)
|> Seq.fold_left (fun acc (start, end_exc) -> acc +^ (end_exc -^ start)) 0L
let sum_length_list ?(skip_check = false) (time_slots : Time_slot.t list) :
int64 =
time_slots |> List.to_seq |> sum_length ~skip_check
end
module Bound = struct
let min_start_and_max_end_exc ?(skip_check = false)
(time_slots : Time_slot.t Seq.t) : (int64 * int64) option =
time_slots
|> (fun s -> if skip_check then s else Check.check_if_valid s)
|> Seq.fold_left
(fun acc (start, end_exc) ->
match acc with
| None -> Some (start, end_exc)
| Some (min_start, max_end_exc) ->
Some (min min_start start, max max_end_exc end_exc))
None
let min_start_and_max_end_exc_list ?(skip_check = false)
(time_slots : Time_slot.t list) : (int64 * int64) option =
time_slots |> List.to_seq |> min_start_and_max_end_exc ~skip_check
end
let shift_list ~offset (time_slots : Time_slot.t list) : Time_slot.t list =
List.map
(fun (start, end_exc) -> (start +^ offset, end_exc +^ offset))
time_slots
let equal (time_slots1 : Time_slot.t list) (time_slots2 : Time_slot.t list) :
bool =
let time_slots1 =
time_slots1 |> List.to_seq |> Normalize.normalize |> List.of_seq
in
let time_slots2 =
time_slots2 |> List.to_seq |> Normalize.normalize |> List.of_seq
in
time_slots1 = time_slots2
let a_is_subset_of_b ~(a : Time_slot.t Seq.t) ~(b : Time_slot.t Seq.t) : bool =
let inter = inter a b |> List.of_seq in
let a = List.of_seq a in
a = inter
let count_overlap ?(skip_check = false) (time_slots : Time_slot.t Seq.t) :
(Time_slot.t * int) Seq.t =
let flatten_buffer buffer =
buffer
|> List.sort (fun (x, _count) (y, _count) -> Time_slot.compare x y)
|> List.to_seq
|> Seq.flat_map (fun (x, count) ->
OSeq.(0 --^ count) |> Seq.map (fun _ -> x))
in
let flush_buffer_to_input buffer time_slots =
Merge.merge (flatten_buffer buffer) time_slots
in
let rec aux (cur : ((int64 * int64) * int) option)
(buffer : ((int64 * int64) * int) list) (time_slots : Time_slot.t Seq.t) :
(Time_slot.t * int) Seq.t =
match time_slots () with
| Seq.Nil -> (
match buffer with
| [] -> (
match cur with None -> Seq.empty | Some cur -> Seq.return cur )
| buffer -> aux cur [] (flatten_buffer buffer) )
| Seq.Cons (x, rest) -> (
let s () = Seq.Cons (x, rest) in
match cur with
| None -> (
match buffer with
| [] -> aux (Some (x, 1)) [] rest
| buffer -> aux None [] (flush_buffer_to_input buffer s) )
| Some ((cur_start, cur_end_exc), cur_count) -> (
match
Time_slot.overlap_of_a_over_b ~a:x ~b:(cur_start, cur_end_exc)
with
| None, None, None -> aux cur buffer rest
| Some _, _, _ ->
failwith "Unexpected case, time slots are not sorted"
| None, Some (start, end_exc), None
| None, Some (start, _), Some (_, end_exc) ->
if start = cur_start then
if end_exc < cur_end_exc then
failwith "Unexpected case, time slots are not sorted"
else if end_exc = cur_end_exc then
aux
(Some ((cur_start, cur_end_exc), succ cur_count))
buffer rest
else
aux
(Some ((cur_start, cur_end_exc), succ cur_count))
(((cur_end_exc, end_exc), 1) :: buffer)
rest
else fun () ->
Seq.Cons
( ((cur_start, start), cur_count),
let buffer =
if end_exc < cur_end_exc then
((start, end_exc), succ cur_count)
:: ((end_exc, cur_end_exc), cur_count)
:: buffer
else if end_exc = cur_end_exc then
((start, cur_end_exc), succ cur_count) :: buffer
else
((start, cur_end_exc), succ cur_count)
:: ((cur_end_exc, end_exc), 1)
:: buffer
in
aux None buffer rest )
| None, None, Some _ ->
fun () ->
Seq.Cons
(((cur_start, cur_end_exc), cur_count), aux None buffer s) )
)
in
time_slots
|> (fun s ->
if skip_check then s
else s |> Check.check_if_valid |> Check.check_if_sorted)
|> aux None []
module Serialize = struct
let pack_time_slots time_slots =
List.map Time_slot.Serialize.pack_time_slot time_slots
end
module Deserialize = struct
let unpack_time_slots time_slots =
List.map Time_slot.Deserialize.unpack_time_slot time_slots
end