-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathRelations.swift
143 lines (123 loc) · 4.18 KB
/
Relations.swift
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
import Foundation
/// The relationship between two calendar values
///
/// There are 13 possible ways in which two values may be related to each other.
/// This set of possibilities is entirely encapsulated by the `Relation` enum.
///
/// - SeeAlso: [Allen's Interval Algebra](https://en.wikipedia.org/wiki/Allen%27s_interval_algebra)
public enum Relation: Hashable, CaseIterable, Sendable {
internal static let meetings: Set<Relation> = [.meets, .isMetBy, .starts, .isStartedBy, .finishes, .isFinishedBy]
internal static let overlappings: Set<Relation> = [.overlaps, .isOverlappedBy, .during, .contains, .equal]
/// The first range occurs entirely before the second range
///
/// - Example: Range `A` is before range `B`:
/// ````
/// ●--A--○
/// ●--B--○
/// ````
case before
/// The first range occurs entirely after the second rage
///
/// - Example: Range `A` is after range `B`:
/// ````
/// ●--B--○
/// ●--A--○
/// ````
case after
/// The first range ends where the second range starts
///
/// - Example: Range `A` meets range `B`:
/// ````
/// ●--A--○
/// ●--B--○
/// ````
case meets
/// The first range starts where the second range ends
///
/// - Example: Range `A` is met by range `B`:
/// ````
/// ●--B--○
/// ●--A--○
/// ````
case isMetBy
/// The first range starts before the second range starts, and ends before the second range ends
///
/// - Example: Range `A` overlaps range `B`:
/// ````
/// ●--A--○
/// ●--B--○
/// ````
case overlaps
/// The first range starts after the second range starts, and ends after the second range ends
///
/// - Example: Range `A` is overlapped by range `B`:
/// ````
/// ●--B--○
/// ●--A--○
/// ````
case isOverlappedBy
/// The first range starts where the second range starts, and ends before the second range ends
///
/// - Example: Range `A` starts range `B`:
/// ````
/// ●--A--○
/// ●----B----○
/// ````
case starts
/// The first range starts where the second range starts, and ends after the second range ends
///
/// - Example: Range `A` is started by range `B`:
/// ````
/// ●--B--○
/// ●----A----○
/// ````
case isStartedBy
/// The first range starts after the second range starts, and ends before the second range ends
///
/// - Example: Range `A` is during range `B`:
/// ````
/// ●--A--○
/// ●----B----○
/// ````
case during
/// The first range starts before the second range starts, and ends after the second range ends
///
/// - Example: Range `A` contains range `B`:
/// ````
/// ●--B--○
/// ●----A----○
/// ````
case contains
/// The first range starts after the second range starts, and ends with the second range
///
/// - Example: Range `A` finishes range `B`:
/// ````
/// ●--A--○
/// ●----B----○
/// ````
case finishes
/// The first range starts before the second range starts, and ends with the second range
///
/// - Example: Range `A` is finished by range `B`:
/// ````
/// ●--B--○
/// ●----A----○
/// ````
case isFinishedBy
/// The first and second ranges start and end together
///
/// - Example: Range `A` equals range `B`:
/// ````
/// ●----A----○
/// ●----B----○
/// ````
case equal
/// Returns `true` if the relation describes two ranges that meet at any extreme
public var isMeeting: Bool { Relation.meetings.contains(self) }
/// Returns `true` if the relation describes any kind of overlapping
public var isOverlapping: Bool { Relation.overlappings.contains(self) }
/// Returns `true` if the relation describes disjointedness
public var isDisjoint: Bool { self == .before || self == .after }
/// Returns `true` if the relation describes equality
public var isEqual: Bool { self == .equal }
}