Source file interval.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
open! Core
open! Int.Replace_polymorphic_compare
module Stable = struct
open Stable_witness.Export
module V1 = struct
module T = struct
type 'a t =
| Interval of 'a * 'a
| Empty
[@@deriving bin_io, of_sexp, variants, compare, hash, stable_witness]
type 'a interval = 'a t [@@deriving bin_io, of_sexp, compare, hash, stable_witness]
let interval_of_sexp a_of_sexp sexp =
try interval_of_sexp a_of_sexp sexp with
| _exn ->
(match sexp with
| Sexp.List [] -> Empty
| Sexp.List [ lb; ub ] ->
Interval (a_of_sexp lb, a_of_sexp ub)
| Sexp.Atom _ | Sexp.List _ ->
of_sexp_error "Interval.t_of_sexp: expected pair or empty list" sexp)
;;
let sexp_of_interval sexp_of_a t =
match t with
| Empty -> Sexp.List []
| Interval (lb, ub) -> Sexp.List [ sexp_of_a lb; sexp_of_a ub ]
;;
end
open T
type 'a t = 'a interval [@@deriving sexp, bin_io, compare, hash, stable_witness]
module Float = struct
module T = struct
type t = float interval [@@deriving sexp, bin_io, compare, hash, stable_witness]
end
include T
include Comparator.Stable.V1.Make (T)
end
module Int = struct
module T = struct
type t = int interval [@@deriving sexp, bin_io, compare, hash, stable_witness]
end
include T
include Comparator.Stable.V1.Make (T)
end
module Time = struct end
module Time_ns = struct end
module Ofday = struct
module T = struct
type t = Core.Time_float.Stable.Ofday.V1.t interval
[@@deriving sexp, bin_io, compare, hash, stable_witness]
end
include T
include Comparator.Stable.V1.Make (T)
end
module Ofday_ns = struct
module T = struct
type t = Core.Time_ns.Stable.Ofday.V1.t interval
[@@deriving sexp, bin_io, compare, stable_witness]
end
include T
include Comparator.Stable.V1.Make (T)
end
module Private = struct
include T
let to_float t = t
let to_int t = t
let to_ofday t = t
let to_time t = t
end
end
end
open Stable.V1.T
module type Bound = sig
type 'a bound
val compare : 'a bound -> 'a bound -> int
val ( >= ) : 'a bound -> 'a bound -> bool
val ( <= ) : 'a bound -> 'a bound -> bool
val ( = ) : 'a bound -> 'a bound -> bool
val ( > ) : 'a bound -> 'a bound -> bool
val ( < ) : 'a bound -> 'a bound -> bool
val ( <> ) : 'a bound -> 'a bound -> bool
end
module Raw_make (T : Bound) = struct
module T = struct
include T
let _ = ( <> )
let max x y = if T.( >= ) x y then x else y
let min x y = if T.( <= ) x y then x else y
end
module Interval = struct
let empty = Empty
let is_malformed = function
| Empty -> false
| Interval (x, y) -> T.( > ) x y
;;
let empty_cvt = function
| Empty -> Empty
| Interval (x, y) as i -> if T.( > ) x y then Empty else i
;;
let create x y =
empty_cvt (Interval (x, y))
;;
let intersect i1 i2 =
match i1, i2 with
| Empty, _ | _, Empty -> Empty
| Interval (l1, u1), Interval (l2, u2) ->
empty_cvt (Interval (T.max l1 l2, T.min u1 u2))
;;
let is_empty = function
| Empty -> true
| _ -> false
;;
let is_empty_or_singleton = function
| Empty -> true
| Interval (x, y) -> T.( = ) x y
;;
let bounds = function
| Empty -> None
| Interval (l, u) -> Some (l, u)
;;
let lbound = function
| Empty -> None
| Interval (l, _) -> Some l
;;
let ubound = function
| Empty -> None
| Interval (_, u) -> Some u
;;
let bounds_exn = function
| Empty -> invalid_arg "Interval.bounds_exn: empty interval"
| Interval (l, u) -> l, u
;;
let lbound_exn = function
| Empty -> invalid_arg "Interval.lbound_exn: empty interval"
| Interval (l, _) -> l
;;
let ubound_exn = function
| Empty -> invalid_arg "Interval.ubound_exn: empty interval"
| Interval (_, u) -> u
;;
let compare_value i x =
match i with
| Empty -> `Interval_is_empty
| Interval (l, u) ->
if T.( < ) x l then `Below else if T.( > ) x u then `Above else `Within
;;
let contains i x = Poly.( = ) (compare_value i x) `Within
let bound i x =
match i with
| Empty -> None
| Interval (l, u) ->
let bounded_value = if T.( < ) x l then l else if T.( < ) u x then u else x in
Some bounded_value
;;
let is_superset i1 ~of_:i2 =
match i1, i2 with
| Interval (l1, u1), Interval (l2, u2) -> T.( <= ) l1 l2 && T.( >= ) u1 u2
| _, Empty -> true
| Empty, Interval (_, _) -> false
;;
let is_subset i1 ~of_:i2 = is_superset i2 ~of_:i1
let map t ~f =
match t with
| Empty -> Empty
| Interval (l, u) -> empty_cvt (Interval (f l, f u))
;;
let interval_compare t1 t2 =
match t1, t2 with
| Empty, Empty -> 0
| Empty, Interval _ -> -1
| Interval _, Empty -> 1
| Interval (l1, u1), Interval (l2, u2) ->
let c = T.compare l1 l2 in
if Int.( <> ) c 0 then c else T.compare u1 u2
;;
let are_disjoint_gen ~are_disjoint intervals =
let intervals = Array.of_list intervals in
try
for i = 0 to Array.length intervals - 1 do
for j = i + 1 to Array.length intervals - 1 do
if not (are_disjoint intervals.(i) intervals.(j)) then raise Exit
done
done;
true
with
| Exit -> false
;;
let are_disjoint intervals =
are_disjoint_gen intervals ~are_disjoint:(fun i1 i2 -> is_empty (intersect i1 i2))
;;
let are_disjoint_as_open_intervals intervals =
are_disjoint_gen intervals ~are_disjoint:(fun i1 i2 ->
is_empty_or_singleton (intersect i1 i2))
;;
let list_intersect ilist1 ilist2 =
if (not (are_disjoint ilist1)) || not (are_disjoint ilist2)
then invalid_arg "Interval.list_intersect: non-disjoint input list";
let pairs = List.cartesian_product ilist1 ilist2 in
List.filter_map pairs ~f:(fun (i1, i2) ->
let i = intersect i1 i2 in
if is_empty i then None else Some i)
;;
let half_open_intervals_are_a_partition intervals =
let intervals = List.filter ~f:(fun x -> not (is_empty x)) intervals in
let intervals = List.sort ~compare:interval_compare intervals in
let rec is_partition a = function
| [] -> true
| b :: tl -> T.( = ) (ubound_exn a) (lbound_exn b) && is_partition b tl
in
match intervals with
| [] -> true
| x :: xs -> is_partition x xs
;;
let convex_hull intervals =
List.fold intervals ~init:empty ~f:(fun i1 i2 ->
match bounds i1, bounds i2 with
| None, _ -> i2
| _, None -> i1
| Some (l1, u1), Some (l2, u2) -> create (T.min l1 l2) (T.max u1 u2))
;;
end
module Set = struct
let create_from_intervals intervals =
let intervals = List.filter intervals ~f:(fun i -> not (Interval.is_empty i)) in
let intervals =
let lb i = Interval.lbound_exn i in
List.sort intervals ~compare:(fun i i' -> T.compare (lb i) (lb i'))
in
if not (Interval.are_disjoint intervals)
then failwith "Interval_set.create: intervals were not disjoint"
else intervals
;;
let create pair_list =
let intervals =
List.map pair_list ~f:(fun (lbound, ubound) -> Interval.create lbound ubound)
in
create_from_intervals intervals
;;
let contains_set ~container ~contained =
List.for_all contained ~f:(fun contained_interval ->
List.exists container ~f:(fun container_interval ->
Interval.is_superset container_interval ~of_:contained_interval))
;;
let contains t x = List.exists t ~f:(fun interval -> Interval.contains interval x)
let ubound_exn t =
match t with
| [] -> invalid_arg "Interval_set.ubound called on empty set"
| _ -> Interval.ubound_exn (List.last_exn t)
;;
let lbound_exn t =
match t with
| [] -> invalid_arg "Interval_set.lbound called on empty set"
| _ -> Interval.lbound_exn (List.hd_exn t)
;;
let ubound t =
match List.last t with
| None -> None
| Some i ->
(match Interval.ubound i with
| None -> assert false
| Some x -> Some x)
;;
let lbound t =
match List.hd t with
| None -> None
| Some i ->
(match Interval.lbound i with
| None -> assert false
| Some x -> Some x)
;;
end
end
type 'a t = 'a interval [@@deriving bin_io, sexp, compare, hash]
module C = Raw_make (struct
type 'a bound = 'a
include Poly
end)
include C.Interval
let t_of_sexp a_of_sexp s =
let t = t_of_sexp a_of_sexp s in
if is_malformed t then of_sexp_error "Interval.t_of_sexp error: malformed input" s;
t
;;
module Set = struct
type 'a t = 'a interval list [@@deriving bin_io, sexp, compare, hash]
include C.Set
end
module Make (Bound : sig
type t [@@deriving bin_io, sexp, hash]
include Comparable.S with type t := t
end) =
struct
type t = Bound.t interval [@@deriving bin_io, sexp, compare, hash]
type interval = t [@@deriving bin_io, sexp]
type bound = Bound.t
module C = Raw_make (struct
type 'a bound = Bound.t
let compare = Bound.compare
include (Bound : Comparable.Infix with type t := Bound.t)
end)
include C.Interval
let to_poly (t : t) = t
let t_of_sexp s =
let t = t_of_sexp s in
if is_malformed t
then
failwithf "Interval.Make.t_of_sexp error: malformed input %s" (Sexp.to_string s) ()
else t
;;
module Set = struct
type t = interval list [@@deriving sexp, bin_io]
include C.Set
let to_poly (t : t) = t
let to_list (t : t) : interval list = t
end
end
module type S1 = Interval_intf.S1
module type S =
Interval_intf.S with type 'a poly_t := 'a t with type 'a poly_set := 'a Set.t
module type S_time = sig end
module Float = Make (Float)
module Ofday = Make (Core.Time_float.Ofday)
module Ofday_ns = Make (Core.Time_ns.Ofday)
module Int = struct
include Make (Int)
let length t =
match t with
| Empty -> 0
| Interval (lo, hi) ->
let len = 1 + hi - lo in
if len < 0
then failwiths ~here:[%here] "interval length not representable" t [%sexp_of: t];
len
;;
let get t i =
let fail () =
failwiths ~here:[%here] "index out of bounds" (i, t) [%sexp_of: int * t]
in
match t with
| Empty -> fail ()
| Interval (lo, hi) ->
if i < 0 then fail ();
let x = lo + i in
if x < lo || x > hi then fail ();
x
;;
let iter t ~f =
match t with
| Empty -> ()
| Interval (lo, hi) ->
for x = lo to hi do
f x
done
;;
let fold =
let rec fold_interval ~lo ~hi ~acc ~f =
if lo = hi then f acc hi else fold_interval ~lo:(lo + 1) ~hi ~acc:(f acc lo) ~f
in
fun t ~init ~f ->
match t with
| Empty -> init
| Interval (lo, hi) -> fold_interval ~lo ~hi ~acc:init ~f
;;
module For_container = Container.Make0 (struct
type nonrec t = t
module Elt = Int
let iter = `Custom iter
let fold = fold
let length = `Custom length
end)
let exists = For_container.exists
let for_all = For_container.for_all
let sum = For_container.sum
let count = For_container.count
let find = For_container.find
let find_map = For_container.find_map
let to_list = For_container.to_list
let to_array = For_container.to_array
let fold_result = For_container.fold_result
let fold_until = For_container.fold_until
let min_elt t ~compare:((compare : _ -> _ -> _) [@local]) =
if not (phys_equal compare Int.compare)
then For_container.min_elt t ~compare
else lbound t
;;
let max_elt t ~compare:((compare : _ -> _ -> _) [@local]) =
if not (phys_equal compare Int.compare)
then For_container.max_elt t ~compare
else ubound t
;;
let mem t x =
if not (phys_equal equal Int.equal) then For_container.mem t x else contains t x
;;
module For_binary_search = Binary_searchable.Make (struct
type nonrec t = t
type nonrec elt = bound
let length = length
let get = get
end)
let binary_search ?pos ?len t ~compare which elt =
let zero_based_pos = Option.map pos ~f:(fun x -> x - lbound_exn t) in
let zero_based_result =
For_binary_search.binary_search ?pos:zero_based_pos ?len t ~compare which elt
in
Option.map zero_based_result ~f:(fun x -> x + lbound_exn t)
;;
let binary_search_segmented ?pos ?len t ~segment_of which =
let zero_based_pos = Option.map pos ~f:(fun x -> x - lbound_exn t) in
let zero_based_result =
For_binary_search.binary_search_segmented
?pos:zero_based_pos
?len
t
~segment_of
which
in
Option.map zero_based_result ~f:(fun x -> x + lbound_exn t)
;;
module Private = struct
let get = get
end
end
module Private = struct
module Make = Make
end
module Time = struct end
module Time_ns = struct end