package core_unix
Unix-specific portions of Core
Install
Dune Dependency
Authors
Maintainers
Sources
v0.17.1.tar.gz
md5=9370dca36f518fcea046d2752e3de22b
sha512=c4e8ce9d5885ac8fa8d554a97e1857f3a1c933e0eb5dfd4fe874412b9d09e6d0a2973b644733855553f33f5c859719228f0e6aaf3a2b7eb5befb46fc513750de
doc/src/core_unix.interval_lib/interval.ml.html
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 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
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, sexp_grammar, stable_witness] type 'a interval = 'a t [@@deriving bin_io, of_sexp, compare, hash, sexp_grammar, stable_witness] let interval_of_sexp a_of_sexp sexp = try interval_of_sexp a_of_sexp sexp (* for backwards compatibility *) 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 ] ;; let interval_sexp_grammar a_sexp_grammar = Sexplib0.Sexp_grammar.coerce { untyped = Union [ (interval_sexp_grammar a_sexp_grammar).untyped ; List Empty ; List (Cons (a_sexp_grammar.untyped, Cons (a_sexp_grammar.untyped, Empty))) ] } ;; end open T type 'a t = 'a interval [@@deriving sexp, bin_io, compare, hash, sexp_grammar, stable_witness] module Float = struct module T = struct type t = float interval [@@deriving sexp, bin_io, compare, hash, sexp_grammar, 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, sexp_grammar, 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, sexp_grammar, 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, sexp_grammar, 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 _ = ( <> ) (* Prevent unused value warning for "<>" *) 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 = (* if x > y, then this is just the Empty interval. *) 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 (* requires sorted list of intervals *) 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 -> (* Compute the convex hull of two intervals *) 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 (* The intervals are sorted by their lower bound *) let drop_empty_intervals_and_sort intervals = List.filter intervals ~f:(fun i -> not (Interval.is_empty i)) |> List.sort ~compare:(Comparable.lift T.compare ~f:Interval.lbound_exn) ;; let create_from_intervals_exn intervals = let intervals = drop_empty_intervals_and_sort intervals in if not (Interval.are_disjoint intervals) then failwith "Interval_set.create: intervals were not disjoint" else intervals ;; let create_merging_intervals intervals = (* We only need to check for overlapping intervals that are adjacent in the sorted order. That's because, if you have intervals [abc] that are sorted by their lower-bound, if a intersects with c, then b must intersect with c as well. As a result we can just iteratively merge together adjacent intervals that intersect, and that will capture all necessary merges. *) drop_empty_intervals_and_sort intervals |> List.fold ~init:[] ~f:(fun acc interval -> match acc with | [] -> [ interval ] | prev_interval :: tl -> if Interval.are_disjoint [ prev_interval; interval ] then interval :: acc else Interval.convex_hull [ prev_interval; interval ] :: tl) |> List.rev ;; let create_exn pair_list = let intervals = List.map pair_list ~f:(fun (lbound, ubound) -> Interval.create lbound ubound) in create_from_intervals_exn 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) ;; let union_list ts = List.concat_no_order ts |> create_merging_intervals let union t1 t2 = union_list [ t1; t2 ] let inter t1 t2 = Interval.list_intersect t1 t2 |> create_from_intervals_exn 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 [hi] and [lo] are far enough apart (e.g. if [lo <= 0] and [hi = Int.max_value]), [len] will overlow. *) 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 : _ -> _ -> _) = if not (phys_equal compare Int.compare) then For_container.min_elt t ~compare else lbound t ;; let max_elt t ~(compare : _ -> _ -> _) = 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 ;; (* Note that we use zero-based indexing here, because that's what Binary_searchable requires, even though at the end we want to export functions that use the natural bounds of the interval. *) 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
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>