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