Total Complexity | 12 |
Total Lines | 69 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | 1 | from typing import Optional |
|
2 | |||
3 | |||
4 | class Allen: |
||
5 | """ |
||
6 | 1 | Utility class for Allen's interval algebra, https://en.wikipedia.org/wiki/Allen%27s_interval_algebra. |
|
7 | 1 | """ |
|
8 | 1 | # ------------------------------------------------------------------------------------------------------------------ |
|
9 | 1 | X_BEFORE_Y = 1 |
|
10 | 1 | X_MEETS_Y = 2 |
|
11 | 1 | X_OVERLAPS_WITH_Y = 3 |
|
12 | 1 | X_STARTS_Y = 4 |
|
13 | 1 | X_DURING_Y = 5 |
|
14 | 1 | X_FINISHES_Y = 6 |
|
15 | 1 | X_EQUAL_Y = 0 |
|
16 | 1 | X_BEFORE_Y_INVERSE = -1 |
|
17 | 1 | X_MEETS_Y_INVERSE = -2 |
|
18 | 1 | X_OVERLAPS_WITH_Y_INVERSE = -3 |
|
19 | X_STARTS_Y_INVERSE = -4 |
||
20 | X_DURING_Y_INVERSE = -5 |
||
21 | 1 | X_FINISHES_Y_INVERSE = -6 |
|
22 | 1 | ||
23 | # ------------------------------------------------------------------------------------------------------------------ |
||
24 | @staticmethod |
||
25 | def relation(x_start: int, x_end: int, y_start: int, y_end: int) -> Optional[int]: |
||
26 | """ |
||
27 | Returns the relation between two intervals. |
||
28 | |||
29 | :param x_start: The start point of the first interval. |
||
30 | :param x_end: The end point of the first interval. |
||
31 | :param y_start: The start point of the second interval. |
||
32 | :param y_end: The end point of the second interval. |
||
33 | """ |
||
34 | 1 | ||
35 | 1 | if (x_end - x_start) < 0 or (y_end - y_start) < 0: |
|
36 | return None |
||
37 | 1 | ||
38 | diff_end = y_end - x_end |
||
39 | 1 | ||
40 | 1 | if diff_end < 0: |
|
41 | return -Allen.relation(y_start, y_end, x_start, x_end) |
||
42 | 1 | ||
43 | 1 | diff_start = y_start - x_start |
|
44 | gab = y_start - x_end |
||
45 | 1 | ||
46 | 1 | if diff_end == 0: |
|
47 | 1 | if diff_start == 0: |
|
48 | return Allen.X_EQUAL_Y |
||
49 | 1 | ||
50 | 1 | if diff_start < 0: |
|
51 | return Allen.X_FINISHES_Y |
||
52 | 1 | ||
53 | return Allen.X_FINISHES_Y_INVERSE |
||
54 | 1 | ||
55 | 1 | if gab > 1: |
|
56 | return Allen.X_BEFORE_Y |
||
57 | 1 | ||
58 | 1 | if gab == 1: |
|
59 | return Allen.X_MEETS_Y |
||
60 | 1 | ||
61 | 1 | if diff_start > 0: |
|
62 | return Allen.X_OVERLAPS_WITH_Y |
||
63 | 1 | ||
64 | 1 | if diff_start == 0: |
|
65 | return Allen.X_STARTS_Y |
||
66 | 1 | ||
67 | 1 | if diff_start < 0: |
|
68 | return Allen.X_DURING_Y |
||
69 | |||
71 |