blob: 10c31f700accf1435a5c414249ed98fb46aa6c04 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2class intervals {
3 public static function wellFormed($i) {
4 return (isset($i[0]) && isset($i[1]) && $i[0] <= $i[1]);
5 }
6
7 public static function measure($i) {
8 return ($i[1] - $i[0]);
9 }
10
11 // Does A overlap B? (with "$open = true" meaning [0, 100] does not overlap [100, 200])
12 public static function overlaps($a, $b, $open = true) {
13 return ($open ? ($a[0] < $b[1] && $a[1] > $b[0]) : ($a[0] <= $b[1] && $a[1] >= $b[0]));
14 }
15
16 // Is A inside of B?
17 public static function isSubset($a, $b) {
18 return ($a[0] >= $b[0] && $a[1] <= $b[1]);
19 }
20
21 // Intersect A and B and return the corresponding interval
22 public static function intersect($a, $b) {
23 $int = [max($a[0], $b[0]), min($a[1], $b[1])];
24
25 return (self::wellFormed($int) ? $int : false);
26 }
27
28 // Return measure of the intersection
29 public static function measureIntersection($a, $b) {
30 return self::measure(self::intersect($a, $b));
31 }
32}