summaryrefslogtreecommitdiffstats
path: root/server/src/time.cppm
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/time.cppm')
-rw-r--r--server/src/time.cppm237
1 files changed, 220 insertions, 17 deletions
diff --git a/server/src/time.cppm b/server/src/time.cppm
index 0007839..32df737 100644
--- a/server/src/time.cppm
+++ b/server/src/time.cppm
@@ -4,28 +4,33 @@ import std;
4 4
5export namespace routemon::time { 5export namespace routemon::time {
6 6
7using timestamp = std::chrono::time_point<std::chrono::utc_clock>; 7template <class Duration = std::chrono::utc_clock::duration>
8using timestamp = std::chrono::time_point<std::chrono::utc_clock, Duration>;
8 9
10auto iso8601_secs(timestamp<std::chrono::seconds> ts) -> std::string;
11
12template <class Duration>
9class period 13class period
10{ 14{
11 // Assuming [start, end). Unfortunately the DATEX II model is not 15 // Assuming [start, end). Unfortunately the DATEX II model is not
12 // clear about this. 16 // clear about this.
13 timestamp start_; 17 timestamp<Duration> start_;
14 timestamp end_; 18 timestamp<Duration> end_;
15 19
16public: 20public:
17 explicit period(timestamp start, timestamp end); 21 explicit period(timestamp<Duration> start, timestamp<Duration> end);
18 22
19 [[nodiscard]] auto intersect(period other) const -> std::optional<period>; 23 [[nodiscard]] auto intersect(period other) const -> std::optional<period>;
20 [[nodiscard]] auto except(period other) const 24 [[nodiscard]] auto except(period other) const
21 -> std::pair<std::optional<period>, std::optional<period>>; 25 -> std::pair<std::optional<period>, std::optional<period>>;
22 [[nodiscard]] auto start() const -> timestamp; 26 [[nodiscard]] auto start() const -> timestamp<Duration>;
23 [[nodiscard]] auto end() const -> timestamp; 27 [[nodiscard]] auto end() const -> timestamp<Duration>;
24}; 28};
25 29
30template <class Duration>
26class period_seq 31class period_seq
27{ 32{
28 std::vector<period> periods_; 33 std::vector<period<Duration>> periods_;
29 34
30 // The way lt and ge are ordered makes a difference for how the sorting 35 // The way lt and ge are ordered makes a difference for how the sorting
31 // (insertion based on lower_bound) works. Do not carelessly reorder this. 36 // (insertion based on lower_bound) works. Do not carelessly reorder this.
@@ -37,11 +42,11 @@ class period_seq
37 42
38 // O(n log n) 43 // O(n log n)
39 template <std::input_iterator I, std::sentinel_for<I> S> 44 template <std::input_iterator I, std::sentinel_for<I> S>
40 requires std::same_as<std::iter_value_t<I>, period> 45 requires std::same_as<std::iter_value_t<I>, period<Duration>>
41 static auto consolidate(I begin, S end) -> std::vector<period> 46 static auto consolidate(I begin, S end) -> std::vector<period<Duration>>
42 { 47 {
43 auto periods = std::vector<period>{}; 48 auto periods = std::vector<period<Duration>>{};
44 auto preds = std::vector<std::pair<timestamp, lt_ge>>{}; 49 auto preds = std::vector<std::pair<timestamp<Duration>, lt_ge>>{};
45 50
46 for (auto it = begin; it != end; it++) 51 for (auto it = begin; it != end; it++)
47 { 52 {
@@ -89,23 +94,221 @@ class period_seq
89 return periods; 94 return periods;
90 } 95 }
91 96
92 explicit period_seq(std::vector<period> periods); 97 explicit period_seq(std::vector<period<Duration>> periods);
93 98
94public: 99public:
95 template <std::input_iterator I, std::sentinel_for<I> S> 100 template <std::input_iterator I, std::sentinel_for<I> S>
96 requires std::same_as<std::iter_value_t<I>, period> 101 requires std::same_as<std::iter_value_t<I>, period<Duration>>
97 explicit period_seq(I begin, S end) : periods_{consolidate(begin, end)} 102 explicit period_seq(I begin, S end) : periods_{consolidate(begin, end)}
98 { 103 {
99 } 104 }
100 105
101 explicit period_seq(period singleton); 106 explicit period_seq(period<Duration> singleton);
102 107
103 [[nodiscard]] auto intersect(period_seq const& other) const -> period_seq; 108 [[nodiscard]] auto intersect(period_seq const& other) const -> period_seq;
104 [[nodiscard]] auto except(period_seq const& other) const -> period_seq; 109 [[nodiscard]] auto except(period_seq const& other) const -> period_seq;
105 [[nodiscard]] auto periods() const -> std::vector<period> const&; 110 [[nodiscard]] auto periods() const -> std::vector<period<Duration>> const&;
106}; 111};
107 112
108auto operator<<(std::ostream& os, period const& p) -> std::ostream&; 113template <class Duration>
109auto operator<<(std::ostream& os, period_seq const& ps) -> std::ostream&; 114auto operator<<(std::ostream& os, period<Duration> const& p) -> std::ostream&;
115
116template <class Duration>
117auto operator<<(std::ostream& os, period_seq<Duration> const& ps)
118 -> std::ostream&;
119
120} // namespace routemon::time
121
122namespace routemon::time {
123
124template <class Duration>
125period<Duration>::period(timestamp<Duration> start, timestamp<Duration> end)
126 : start_{start}, end_{end}
127{
128 if (start >= end)
129 {
130 throw std::invalid_argument{"period: start should be before end"};
131 }
132}
133
134template <class Duration>
135auto period<Duration>::intersect(period other) const -> std::optional<period>
136{
137 auto const new_start = start_ < other.start() ? other.start() : start_;
138 auto const new_end = other.end() < end_ ? other.end() : end_;
139 return new_start < new_end ? std::make_optional(period{new_start, new_end})
140 : std::nullopt;
141}
142
143template <class Duration>
144auto period<Duration>::except(period other) const
145 -> std::pair<std::optional<period>, std::optional<period>>
146{
147 auto const before_start = start_;
148 auto const before_end = other.start();
149 auto const after_start = end_;
150 auto const after_end = other.end();
151 std::optional<period> before, after;
152 if (before_start < before_end)
153 before = period{before_start, before_end};
154 if (after_start < after_end)
155 after = period{after_end, after_start};
156 return std::make_pair(before, after);
157}
158
159template <class Duration>
160auto period<Duration>::start() const -> timestamp<Duration>
161{
162 return start_;
163}
164
165template <class Duration>
166auto period<Duration>::end() const -> timestamp<Duration>
167{
168 return end_;
169}
170
171template <class Duration>
172period_seq<Duration>::period_seq(std::vector<period<Duration>> periods)
173 : periods_{std::move(periods)}
174{
175 for (auto i = 0uz; i < periods_.size(); i++)
176 {
177 if (i + 1 < periods_.size())
178 {
179 if (periods_[i].end() >= periods_[i + 1].start())
180 {
181 throw std::logic_error{"period_seq: vector provided to private "
182 "constructor not ordered properly"};
183 }
184 }
185 }
186}
187
188template <class Duration>
189period_seq<Duration>::period_seq(period<Duration> singleton)
190 : periods_{singleton}
191{
192}
193
194template <class Duration>
195auto period_seq<Duration>::intersect(period_seq const& other) const
196 -> period_seq
197{
198 auto it1 = periods_.begin();
199 auto end1 = periods_.end();
200 auto it2 = other.periods_.begin();
201 auto end2 = other.periods_.end();
202
203 auto res = std::vector<period<Duration>>{};
204 while (it1 != end1 && it2 != end2)
205 {
206 auto overlap = it1->intersect(*it2);
207 if (overlap)
208 {
209 res.push_back(*overlap);
210 if (it1->end() < it2->end())
211 {
212 it1++;
213 }
214 else
215 {
216 it2++;
217 }
218 }
219 else
220 {
221 if (it1->end() < it2->start())
222 {
223 it1++;
224 }
225 else
226 {
227 it2++;
228 }
229 }
230 }
231
232 return period_seq{res};
233}
234
235template <class Duration>
236auto period_seq<Duration>::except(period_seq const& other) const -> period_seq
237{
238 // This code was pretty tricky to write, I wouldn't be surprised if it
239 // has some bugs in it.
240
241 auto it1 = periods_.begin();
242 auto end1 = periods_.end();
243 auto it2 = other.periods_.begin();
244 auto end2 = other.periods_.end();
245
246 auto res = std::vector<period<Duration>>{};
247 if (it1 == end1)
248 return period_seq{res};
249 if (it2 == end2)
250 return period_seq{periods_};
251 auto period1 = period<Duration>{*it1++};
252
253 while (it1 != end1 && it2 != end2)
254 {
255 if (period1.end() <= it2->start())
256 {
257 res.push_back(period1);
258 period1 = *it1++;
259 }
260 else if (it2->end() <= period1.start())
261 {
262 it2++;
263 }
264 else /* period1.begin() < it2->end() && it2->begin() <
265 period1.end() */
266 {
267 auto const [mbefore, mafter] = period1.except(*it2);
268 if (mbefore)
269 res.push_back(*mbefore);
270 if (mafter)
271 {
272 period1 = *mafter;
273 }
274 else
275 {
276 period1 = *it1++;
277 }
278 }
279 }
280
281 return period_seq{res};
282}
283
284template <class Duration>
285auto period_seq<Duration>::periods() const
286 -> std::vector<period<Duration>> const&
287{
288 return periods_;
289}
290
291template <class Duration>
292auto operator<<(std::ostream& os, period<Duration> const& p) -> std::ostream&
293{
294 return os << "[" << p.start() << ", " << p.end() << ")";
295}
296
297template <class Duration>
298auto operator<<(std::ostream& os, period_seq<Duration> const& ps)
299 -> std::ostream&
300{
301 os << "{";
302 auto it = ps.periods().begin();
303 while (it != ps.periods().end())
304 {
305 os << " " << *it;
306 if (++it != ps.periods().end())
307 {
308 os << ",";
309 }
310 }
311 return os << " }";
312}
110 313
111} // namespace routemon::time 314} // namespace routemon::time