/
interval.ex
1514 lines (1191 loc) · 44.1 KB
/
interval.ex
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Interval do
@moduledoc """
An interval represents the points between two endpoints.
The interval can be empty.
The empty interval is never contained in any other interval,
and itself contains no points.
It can be left and/or right unbounded, in which case
it contains all points in the unbounded direction.
A fully unbounded interval contains all other intervals, except
the empty interval.
## Features
The key features of this library are
- Common interval operations built in are
- `intersection/2`
- `union/2`
- `overlaps?/2`
- `contains?/2`
- `partition/2`
- adjacent?
- empty?
- unbounded?
- Built in support for intervals containing
- `Integer`
- `Float`
- `Date`
- `DateTime`
- `NaiveDateTime`
- `Decimal`
- Also implements
- `Ecto.Type`
- `Jason.Encoder`
## Interval Notation
Throughout the documentation and comments, you'll see a notation for
writing about intervals.
As this library is inspired by the functionality in PostgreSQL's range types,
we align ourselves with it's notation choice and borrow it
(https://www.postgresql.org/docs/current/rangetypes.html)
This notation is also described in ISO 31-11.
[left-inclusive, right-inclusive]
(left-exclusive, right-exclusive)
[left-inclusive, right-exclusive)
(left-exclusive, right-inclusive]
:empty
An unbounded interval is written by omitting the bound type and point:
,right-exclusive)
[left-inclusive,
When specifying bound types we sometimes leave the point out and just write
the left and right bounds:
[]
()
(]
[)
(
)
[
]
## Types of Interval
This library ships with a few different types of intervals.
The built-in intervals are:
- `Interval.Date` containing points of type `Date`
- `Interval.DateTime` containing points of type `DateTime`
- `Interval.NaiveDateTime` containing points of type `NaiveDateTime`
- `Interval.Float` containing points of type `Float`
- `Interval.Integer` containing points of type `Integer`
- `Interval.Decimal` containing points of type `Decimal` (See https://hexdocs.pm/decimal/2.0.0)
However, you can quite easily implement an interval by implementing
the `Interval.Behaviour`.
The easiest way to do so, is by using the `Interval.__using__` macro:
defmodule MyInterval do
use Interval, type: MyType, discrete: false
end
You must implement a few functions defined in `Interval.Behaviour`.
Once that's done, all operations available in the `Interval` module (like
interesection, union, overlap etc) will work on your interval struct.
An obvious usecase for this would be to implement an interval that works
with the https://hexdocs.pm/decimal library.
## Discrete vs Continuous intervals
Depending on the behaviour you want from your interval, it is either said to be
discrete or continuous.
A discrete interval represents a set of finite points (like integers).
A continuous interval can be said to represent the infinite number of points between
two endpoints (like an interval between two floats).
With discrete points, it is possible to define what the next and previous
point is, and we normalise these intervals to the bound type `[)`.
The distinction between a discrete and continuous interval is important
because the two behave slightly different in some of the library functions.
A discrete interval is adjacent to another discrete interval, if there
is no points between the two interval.
Contrast this to continuous intervals of real numbers where there is always
an infinite number of real numbers between two distinct real numbers,
and so continuous intervals are only said to be adjacent to each other
if they include the same point, and one point is inclusive where the other
is exclusive.
Where relevant, the function documentation will mention the differences
between discrete and continuous intervals.
## Create an Interval
See `new/1`.
## Normalization
When creating an interval through `new/1`, it will get normalized
so that intervals that represents the same points,
are also represented in the same way in the struct.
This allows you to compare two intervals for equality by using `==`
(and using pattern matching).
It is therefore not recommended to modify an interval struct directly,
but instead do so by using one of the functions that modify the interval.
An interval is said to be empty if it spans zero points.
The normalized form of an empty interval is the special interval struct
where left and right is set to `:empty`,
however a non-normalized empty struct will still correctly report
empty via the `empty?/1` function.
"""
@typedoc """
An interval struct, representing all points between
two endpoints.
The struct has two fields: `left` and `right`,
representing the left (lower) and right (upper) points
in the interval.
If either left or right is set to `:empty`, the both must be
set to `:empty`.
The specific struct type depends on the interval implementation,
but the `left` and `right` field is always present, all will
be manipulated by the `Interval` module regardless of the interval
implementation.
"""
@type t(point) :: %{
__struct__: atom(),
# Left endpoint
left: :empty | :unbounded | {bound(), point},
# Right endpoint
right: :empty | :unbounded | {bound(), point}
}
@typedoc """
Shorthand for `t(any())`
"""
@type t() :: t(any())
@typedoc """
A point in an interval.
"""
@type point() :: any()
@typedoc """
The bound type of an endpoint.
"""
@type bound() :: :inclusive | :exclusive
@doc """
Create a new interval.
## Options
- `module` The interval implementation to use.
When calling `new/1` from a `Interval.Behaviour` this is inferred.
- `left` The left (or lower) endpoint of the interval
- `right` The right (or upper) endpoint of the interval
- `bounds` The bound mode to use. Defaults to `"[)"`
A `nil` (`left` or `right`) endpoint is considered unbounded.
The endpoint will also be considered unbounded if the `bounds` is explicitly
set as unbounded.
A special value `:empty` can be given to `left` and `right` to
construct an empty interval.
## Bounds
The `bounds` options contains the left and right bound mode to use.
The bound can be inclusive, exclusive or unbounded.
The following valid bound values are supported:
- `"[)"` left-inclusive, right-exclusive (default)
- `"(]"` left-exclusive, right-inclusive
- `"[]"` left-inclusive, right-inclusive
- `"()"` left-exclusive, right-exclusive
- `")"` left-unbounded, right-exclusive
- `"]"` left-unbounded, right-inclusive
- `"("` left-exclusive, right-unbounded
- `"["` left-inclusive, right-unbounded
## Examples
iex> new(module: Interval.Integer)
iex> new(module: Interval.Integer, left: :empty, right: :empty)
iex> new(module: Interval.Integer, left: 1)
iex> new(module: Interval.Integer, left: 1, right: 1, bounds: "[]")
iex> new(module: Interval.Integer, left: 10, right: 20, bounds: "()")
"""
@spec new(Keyword.t()) :: t()
def new(opts) when is_list(opts) do
module = Keyword.get(opts, :module, nil)
left = Keyword.get(opts, :left, nil)
right = Keyword.get(opts, :right, nil)
bounds = Keyword.get(opts, :bounds, nil)
{left_bound, right_bound} = unpack_bounds(bounds)
module = module || infer_implementation([left, right])
if is_nil(module) do
raise ArgumentError,
message: "No implementation for interval available. Options given: #{inspect(opts)}"
end
left_endpoint = normalize_bound(left, left_bound)
right_endpoint = normalize_bound(right, right_bound)
module
|> struct!(left: left_endpoint, right: right_endpoint)
|> normalize()
end
defp normalize_bound(point, bound) do
case {point, bound} do
{:empty, _} -> :empty
{nil, _} -> :unbounded
{:unbound, _} -> :unbounded
{_, :unbounded} -> :unbounded
{_, :inclusive} -> {:inclusive, point}
{_, :exclusive} -> {:exclusive, point}
end
end
@doc """
Is the interval empty?
An empty interval is an interval that represents no points.
Any interval containing no points is considered empty.
An unbounded interval is never empty.
For continuous points, the interval is empty when the left and
right points are identical, and the point is not included in the interval.
For discrete points, the interval is empty when the left and right point
isn't inclusive, and there are no points between the left and right point.
## Examples
iex> empty?(new(module: Interval.Integer, left: 0, right: 0))
true
iex> empty?(new(module: Interval.Float, left: 1.0))
false
iex> empty?(new(module: Interval.Integer, left: 1, right: 2))
false
"""
@spec empty?(t()) :: boolean()
def empty?(a)
def empty?(%{left: :unbounded}), do: false
def empty?(%{right: :unbounded}), do: false
def empty?(%{left: :empty, right: :empty}), do: true
# If the interval is not properly normalized, we don't want to give an
# incorrect answer, so we do the math to check if the interval is indeed empty:
def empty?(%{
left: {:exclusive, p},
right: {:exclusive, p}
}) do
true
end
def empty?(%module{
left: {left_bound, left_point},
right: {right_bound, right_point}
}) do
compare = module.point_compare(left_point, right_point)
cond do
# left and right is equal, then the interval is empty
# if the point is not included in the interval.
# We don't want to rely on normalized intervals in empty?/1
# in this function body, because if the interval was already normalized,
# we'd only have to check for the `(zero,zero)` interval.
# Therefore we must assume that the bounds could be incorrectly set to e.g. [p,p)
compare == :eq ->
left_bound == :exclusive or right_bound == :exclusive
# if the point type is discrete and both bounds are exclusive,
# then the interval could _also_ be empty if next(left) == right,
# because the interval would represent 0 points.
module.discrete?() and
left_bound == :exclusive and right_bound == :exclusive ->
:eq ==
left_point
|> module.point_step(+1)
|> module.point_compare(right_point)
# If none of the above, then the interval is not empty
true ->
false
end
end
@doc """
Return the left point.
This function always returns nil when no point exist.
Use the functions `empty?/1`, `inclusive_left?/1` and `unbounded_left?/1`
to check for the meaning of the point.
## Example
iex> left(new(module: Interval.Integer, left: 1, right: 2))
1
"""
@compile {:inline, left: 1}
@spec left(t()) :: point()
def left(%{left: {_, value}}), do: value
def left(%{left: _}), do: nil
@doc """
Return the right point.
This function always returns nil when no point exist.
Use the functions `empty?/1`, `inclusive_right?/1` and `unbounded_right?/1`
to check for the meaning of the point.
## Example
iex> right(new(module: Interval.Integer, left: 1, right: 2))
2
"""
@compile {:inline, right: 1}
@spec right(t()) :: point()
def right(%{right: {_, value}}), do: value
def right(%{right: _}), do: nil
@doc """
Check if the interval is left-unbounded.
The interval is left-unbounded if all points
left of the right bound is included in this interval.
## Examples
iex> unbounded_left?(new(module: Interval.Integer))
true
iex> unbounded_left?(new(module: Interval.Integer, right: 2))
true
iex> unbounded_left?(new(module: Interval.Integer, left: 1, right: 2))
false
"""
@spec unbounded_left?(t()) :: boolean()
def unbounded_left?(%{left: :unbounded}), do: true
def unbounded_left?(%{}), do: false
@doc """
Check if the interval is right-unbounded.
The interval is right-unbounded if all points
right of the left bound is included in this interval.
## Examples
iex> unbounded_right?(new(module: Interval.Integer, right: 1))
false
iex> unbounded_right?(new(module: Interval.Integer))
true
iex> unbounded_right?(new(module: Interval.Integer, left: 1))
true
"""
@spec unbounded_right?(t()) :: boolean()
def unbounded_right?(%{right: :unbounded}), do: true
def unbounded_right?(%{}), do: false
@doc """
Is the interval left-inclusive?
The interval is left-inclusive if the left endpoint
value is included in the interval.
> #### Note {: .info}
> Discrete intervals (like `Interval.Integer` and `Interval.Date`) are always normalized
> to be left-inclusive right-exclusive (`[)`).
iex> inclusive_left?(new(module: Interval.Float, left: 1.0, right: 2.0, bounds: "[]"))
true
iex> inclusive_left?(new(module: Interval.Float, left: 1.0, right: 2.0, bounds: "[)"))
true
iex> inclusive_left?(new(module: Interval.Float, left: 1.0, right: 2.0, bounds: "()"))
false
"""
@spec inclusive_left?(t()) :: boolean()
def inclusive_left?(%{left: {:inclusive, _}}), do: true
def inclusive_left?(%{}), do: false
@doc """
Is the interval right-inclusive?
The interval is right-inclusive if the right endpoint
value is included in the interval.
> #### Note {: .info}
> Discrete intervals (like `Interval.Integer` and `Interval.Date`) are always normalized
> to be left-inclusive right-exclusive (`[)`).
iex> inclusive_right?(new(module: Interval.Float, left: 1.0, right: 2.0, bounds: "[]"))
true
iex> inclusive_right?(new(module: Interval.Float, left: 1.0, right: 2.0, bounds: "[)"))
false
iex> inclusive_right?(new(module: Interval.Float, left: 1.0, right: 2.0, bounds: "()"))
false
"""
@spec inclusive_right?(t()) :: boolean()
def inclusive_right?(%{right: {:inclusive, _}}), do: true
def inclusive_right?(%{}), do: false
@doc since: "0.1.3"
@spec size(t()) :: any()
def size(%module{} = a), do: module.size(a)
@doc """
Is `a` strictly left of `b`.
`a` is strictly left of `b` if no point in `a` is in `b`,
and all points in `a` is left (<) of all points in `b`.
## Examples
# a: [---)
# b: [---)
# r: true
# a: [---)
# b: [---)
# r: true
# a: [---)
# b: [---)
# r: false (overlaps)
iex> strictly_left_of?(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 3, right: 4))
true
iex> strictly_left_of?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
false
iex> strictly_left_of?(new(module: Interval.Integer, left: 3, right: 4), new(module: Interval.Integer, left: 1, right: 2))
false
"""
@spec strictly_left_of?(t(), t()) :: boolean()
def strictly_left_of?(%module{} = a, %module{} = b) do
not unbounded_right?(a) and
not unbounded_left?(b) and
not empty?(a) and
not empty?(b) and
case module.point_compare(right(a), left(b)) do
:lt -> true
:eq -> not inclusive_right?(a) or not inclusive_left?(b)
:gt -> false
end
end
@doc """
Is `a` strictly right of `b`.
`a` is strictly right of `b` if no point in `a` is in `b`,
and all points in `a` is right (>) of all points in `b`.
## Examples
# a: [---)
# b: [---)
# r: true
# a: [---)
# b: [---)
# r: true
# a: [---)
# b: [---)
# r: false (overlaps)
iex> strictly_right_of?(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 3, right: 4))
false
iex> strictly_right_of?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
false
iex> strictly_right_of?(new(module: Interval.Integer, left: 3, right: 4), new(module: Interval.Integer, left: 1, right: 2))
true
"""
@spec strictly_right_of?(t(), t()) :: boolean()
def strictly_right_of?(%module{} = a, %module{} = b) do
not unbounded_left?(a) and
not unbounded_right?(b) and
not empty?(a) and
not empty?(b) and
case module.point_compare(left(a), right(b)) do
:lt -> false
:eq -> not inclusive_left?(a) or not inclusive_right?(b)
:gt -> true
end
end
@doc """
Is the interval `a` adjacent to `b`, to the left of `b`.
`a` is adjacent to `b` left of `b`, if `a` and `b` do _not_ overlap,
and there are no points between `a.right` and `b.left`.
# a: [---)
# b: [---)
# r: true
# a: [---]
# b: [---]
# r: false (overlaps)
# a: (---)
# b: (---)
# r: false (points exist between a.right and b.left)
## Examples
iex> adjacent_left_of?(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 2, right: 3))
true
iex> adjacent_left_of?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
false
iex> adjacent_left_of?(new(module: Interval.Integer, left: 3, right: 4), new(module: Interval.Integer, left: 1, right: 2))
false
iex> adjacent_left_of?(new(module: Interval.Integer, right: 2, bounds: "[]"), new(module: Interval.Integer, left: 3))
true
"""
@spec adjacent_left_of?(t(), t()) :: boolean()
def adjacent_left_of?(%module{} = a, %module{} = b) do
prerequisite =
not unbounded_right?(a) and
not unbounded_left?(b) and
not empty?(a) and
not empty?(b)
with true <- prerequisite do
# Assuming we've normalized both a and b,
# if the point types are discrete, and and normalized to `[)`
# then continuous and discrete intervals are checked in the same way.
# To ensure we don't give the wrong answer though,
# we have an assertion that that a discrete point type must be
# bounded as `[)`:
assert_normalized_bounds(a)
assert_normalized_bounds(b)
inclusive_right?(a) != inclusive_left?(b) and
module.point_compare(right(a), left(b)) == :eq
end
end
@doc """
Is the interval `a` adjacent to `b`, to the right of `b`.
`a` is adjacent to `b` right of `b`, if `a` and `b` do _not_ overlap,
and there are no points between `a.left` and `b.right`.
# a: [---)
# b: [---)
# r: true
# a: [---)
# b: [---]
# r: false (overlaps)
# a: (---)
# b: (---)
# r: false (points exist between a.left and b.right)
## Examples
iex> adjacent_right_of?(new(module: Interval.Integer, left: 2, right: 3), new(module: Interval.Integer, left: 1, right: 2))
true
iex> adjacent_right_of?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
false
iex> adjacent_right_of?(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 3, right: 4))
false
iex> adjacent_right_of?(new(module: Interval.Integer, left: 3), new(module: Interval.Integer, right: 2, bounds: "]"))
true
"""
@spec adjacent_right_of?(t(), t()) :: boolean()
def adjacent_right_of?(%module{} = a, %module{} = b) do
prerequisite =
not unbounded_left?(a) and
not unbounded_right?(b) and
not empty?(a) and
not empty?(b)
with true <- prerequisite do
# Assuming we've normalized both a and b,
# if the point types are discrete, and and normalized to `[)`
# then continuous and discrete intervals are checked in the same way.
# To ensure we don't give the wrong answer though,
# we have an assertion that that a discrete point type must be
# bounded as `[)`:
assert_normalized_bounds(a)
assert_normalized_bounds(b)
module.point_compare(left(a), right(b)) == :eq and
inclusive_left?(a) != inclusive_right?(b)
end
end
@doc """
Does `a` overlap with `b`?
`a` overlaps with `b` if any point in `a` is also in `b`.
# a: [---)
# b: [---)
# r: true
# a: [---)
# b: [---)
# r: false
# a: [---]
# b: [---]
# r: true
# a: (---)
# b: (---)
# r: false
# a: [---)
# b: [---)
# r: false
## Examples
# [--a--)
# [--b--)
iex> overlaps?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
true
# [--a--)
# [--b--)
iex> overlaps?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 3, right: 5))
false
# [--a--]
# [--b--]
iex> overlaps?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
true
# (--a--)
# (--b--)
iex> overlaps?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 3, right: 5))
false
# [--a--)
# [--b--)
iex> overlaps?(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 3, right: 4))
false
"""
@spec overlaps?(t(), t()) :: boolean()
def overlaps?(%module{} = a, %module{} = b) do
not empty?(a) and
not empty?(b) and
not strictly_left_of?(a, b) and
not strictly_right_of?(a, b)
end
@doc """
Does `a` contain `b`?
`a` contains `b` of all points in `b` is also in `a`.
For an interval `a` to contain an interval `b`, all points
in `b` must be contained in `a`:
# a: [-------]
# b: [---]
# r: true
# a: [---]
# b: [---]
# r: true
# a: [---]
# b: (---)
# r: true
# a: (---)
# b: [---]
# r: false
# a: [---]
# b: [-------]
# r: false
This means that `a.left` is less than `b.left` (or unbounded), and `a.right` is greater than
`b.right` (or unbounded)
If `a` and `b`'s point match, then `b` is "in" `a` if `a` and `b` share bound types.
E.g. if `a.left` and `b.left` matches, then `a` contains `b` if both `a` and `b`'s
`left` is inclusive or exclusive.
If either of `b` endpoints are unbounded, then `a` only contains `b`
if the corresponding endpoint in `a` is also unbounded.
## Examples
iex> contains?(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 1, right: 2))
true
iex> contains?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 3))
true
iex> contains?(new(module: Interval.Integer, left: 2, right: 3), new(module: Interval.Integer, left: 1, right: 4))
false
iex> contains?(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 1, right: 2))
true
iex> contains?(new(module: Interval.Integer, left: 1, right: 2, bounds: "()"), new(module: Interval.Integer, left: 1, right: 3))
false
iex> contains?(new(module: Interval.Integer, right: 1), new(module: Interval.Integer, left: 0, right: 1))
true
"""
@spec contains?(t(), t()) :: boolean()
def contains?(%module{} = a, %module{} = b) do
# Neither A or B must be empty, so that's a prerequisite for
# even checking anything.
prerequisite = not (empty?(a) or empty?(b))
with true <- prerequisite do
# check that left(a) is less than or equal to (if inclusive) left(b):
contains_left =
unbounded_left?(a) or
(not unbounded_left?(b) and
case module.point_compare(left(a), left(b)) do
:gt -> false
:eq -> inclusive_left?(a) == inclusive_left?(b)
:lt -> true
end)
# check that right(a) is greater than or equal to (if inclusive) right(b):
contains_right =
unbounded_right?(a) or
(not unbounded_right?(b) and
case module.point_compare(right(a), right(b)) do
:gt -> true
:eq -> inclusive_right?(a) == inclusive_right?(b)
:lt -> false
end)
# a contains b if both the left check and right check passes:
contains_left and contains_right
end
end
@doc """
Does `a` contain the point `x`?
## Examples
iex> contains_point?(new(module: Interval.Integer, left: 1, right: 2), 0)
false
iex> contains_point?(new(module: Interval.Integer, left: 1, right: 2), 1)
true
"""
@doc since: "0.1.4"
@spec contains_point?(t(), point()) :: boolean()
def contains_point?(%module{} = a, x) do
with true <- not empty?(a) do
contains_left =
unbounded_left?(a) or
case module.point_compare(left(a), x) do
:gt -> false
:eq -> inclusive_left?(a)
:lt -> true
end
contains_right =
unbounded_right?(a) or
case module.point_compare(right(a), x) do
:gt -> true
:eq -> inclusive_right?(a)
:lt -> false
end
contains_left and contains_right
end
end
@doc """
Computes the union of `a` and `b`.
The union contains all of the points that are either in `a` or `b`.
If either `a` or `b` are empty, the returned interval will be empty.
# a: [---)
# b: [---)
# r: [-----)
## Examples
# [--A--)
# [--B--)
# [----C----)
iex> union(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
new(module: Interval.Integer, left: 1, right: 4)
# [-A-)
# [-B-)
# [---C---)
iex> union(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 2, right: 3))
new(module: Interval.Integer, left: 1, right: 3)
iex> union(new(module: Interval.Integer, left: 1, right: 2), new(module: Interval.Integer, left: 3, right: 4))
new(module: Interval.Integer, left: 0, right: 0)
"""
@spec union(t(), t()) :: t()
def union(%module{} = a, %module{} = b) do
cond do
# if either is empty, return the other
empty?(a) ->
b
empty?(b) ->
a
# if a and b overlap or are adjacent, we can union the intervals
overlaps?(a, b) or adjacent_left_of?(a, b) or adjacent_right_of?(a, b) ->
left = pick_union_left(module, a.left, b.left)
right = pick_union_right(module, a.right, b.right)
from_endpoints(module, left, right)
# fall-through, if neither A or B is empty,
# but there is also no overlap or adjacency,
# then the two intervals are either strictly left or strictly right,
# we return empty (A and B share an empty amount of points)
true ->
# This assertion _must_ be true, since overlap?/2 returned false
# so there is no point in running it.
# true == strictly_left_of?(a, b) or strictly_right_of?(a, b)
new_empty(module)
end
end
@doc """
Compute the intersection between `a` and `b`.
The intersection contains all of the points that are both in `a` and `b`.
If either `a` or `b` are empty, the returned interval will be empty.
# a: [----]
# b: [----]
# r: [-]
# a: (----)
# b: (----)
# r: (-)
# a: [----)
# b: [----)
# r: [-)
## Examples:
Discrete:
# a: [----)
# b: [----)
# c: [-)
iex> intersection(new(module: Interval.Integer, left: 1, right: 3), new(module: Interval.Integer, left: 2, right: 4))
new(module: Interval.Integer, left: 2, right: 3)
Continuous:
# a: [----)
# b: [----)
# c: [-)
iex> intersection(new(module: Interval.Float, left: 1.0, right: 3.0), new(module: Interval.Float, left: 2.0, right: 4.0))
new(module: Interval.Float, left: 2.0, right: 3.0)
# a: (----)
# b: (----)
# c: (-)
iex> intersection(
...> new(module: Interval.Float, left: 1.0, right: 3.0, bounds: "()"),
...> new(module: Interval.Float, left: 2.0, right: 4.0, bounds: "()")
...> )
new(module: Interval.Float, left: 2.0, right: 3.0, bounds: "()")
# a: [-----)
# b: [-------
# c: [---)
iex> intersection(new(module: Interval.Float, left: 1.0, right: 3.0), new(module: Interval.Float, left: 2.0))
new(module: Interval.Float, left: 2.0, right: 3.0)
"""
@spec intersection(t(), t()) :: t()
def intersection(%module{} = a, %module{} = b) do
cond do
# if A is empty, we return A
empty?(a) ->
a
# if B is empty, we return B
empty?(b) ->
b
# if A and B doesn't overlap,
# then there can be no intersection
not overlaps?(a, b) ->
new_empty(module)
# otherwise, we can compute the intersection
true ->
# The intersection between `a` and `b` is the points that exist in
# both `a` and `b`.
left = pick_intersection_left(module, a.left, b.left)
right = pick_intersection_right(module, a.right, b.right)
from_endpoints(module, left, right)
end
end