tempoch-cpp 0.5.3
Header-only C++ wrapper for tempoch
Loading...
Searching...
No Matches
period.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "qtty/qtty.hpp"
9#include "time.hpp"
10#include <ostream>
11#include <vector>
12
13namespace tempoch {
14
15template <typename S> struct TimeTraits<Time<S>> {
16 static double to_mjd_value(const Time<S> &time) {
17 return time.template to<format::MJD>().value();
18 }
19
23};
24
25template <typename S, typename F> struct TimeTraits<EncodedTime<S, F>> {
26 static double to_mjd_value(const EncodedTime<S, F> &time) {
27 if constexpr (std::is_same_v<F, format::MJD>) {
28 return time.value();
29 } else {
30 return Time<S>::from_encoded(time).template to<format::MJD>().value();
31 }
32 }
33
35 if constexpr (std::is_same_v<F, format::MJD>) {
36 return EncodedTime<S, F>(mjd);
37 } else {
38 return ModifiedJulianDate<S>(mjd).template to<F>();
39 }
40 }
41};
42
43template <> struct TimeTraits<CivilTime> {
44 static double to_mjd_value(const CivilTime &time) {
45 return Time<scale::UTC>::from_civil(time).template to<format::MJD>().value();
46 }
47
51};
52
53template <typename T = ModifiedJulianDate<scale::TT>> class Period {
55
56 explicit Period(const tempoch_period_mjd_t &inner) : m_inner(inner) {}
57
58public:
63 Period(const T &start, const T &end) {
66 "Period::Period");
67 }
68
69 static Period from_c(const tempoch_period_mjd_t &c) { return Period(c); }
70
71 T start() const { return TimeTraits<T>::from_mjd_value(m_inner.start_mjd); }
72 T end() const { return TimeTraits<T>::from_mjd_value(m_inner.end_mjd); }
73
74 template <typename TargetType = qtty::DayTag>
75 qtty::Quantity<typename qtty::ExtractTag<TargetType>::type> duration() const {
77 return qtty::Quantity<qtty::DayTag>(qty.value).template to<TargetType>();
78 }
79
81 template <typename TargetType = qtty::DayTag>
82 qtty::Quantity<typename qtty::ExtractTag<TargetType>::type> length() const {
83 return duration<TargetType>();
84 }
85
89 "Period::intersection");
90 return from_c(out);
91 }
92
93 bool contains(const T &point) const noexcept {
95 }
96
97 std::vector<Period<T>> union_with(const Period<T> &other) const {
99 std::size_t count = 0;
101 "Period::union_with");
102 std::vector<Period<T>> result;
103 result.reserve(count);
104 for (std::size_t i = 0; i < count; ++i)
105 result.push_back(from_c(buf[i]));
106 return result;
107 }
108
109 std::vector<Period<T>> complement_of(const std::vector<Period<T>> &others) const {
110 std::vector<tempoch_period_mjd_t> raw;
111 raw.reserve(others.size());
112 for (const auto &p : others)
113 raw.push_back(p.c_inner());
114 tempoch_period_mjd_t *out = nullptr;
115 std::size_t n = 0;
116 check_status(tempoch_period_list_complement(m_inner, raw.data(), raw.size(), &out, &n),
117 "Period::complement_of");
118 std::vector<Period<T>> result;
119 result.reserve(n);
120 for (std::size_t i = 0; i < n; ++i)
121 result.push_back(from_c(out[i]));
123 return result;
124 }
125
126 const tempoch_period_mjd_t &c_inner() const noexcept { return m_inner; }
127};
128
129template <typename T> Period(T, T) -> Period<T>;
130
133
134namespace detail {
135
136template <typename T>
137inline std::vector<tempoch_period_mjd_t> to_raw(const std::vector<Period<T>> &periods) {
138 std::vector<tempoch_period_mjd_t> raw;
139 raw.reserve(periods.size());
140 for (const auto &p : periods)
141 raw.push_back(p.c_inner());
142 return raw;
143}
144
145template <typename T>
146inline std::vector<Period<T>> from_alloc(tempoch_period_mjd_t *ptr, std::size_t count) {
147 std::vector<Period<T>> result;
148 result.reserve(count);
149 for (std::size_t i = 0; i < count; ++i)
150 result.push_back(Period<T>::from_c(ptr[i]));
152 return result;
153}
154
155} // namespace detail
156
157template <typename T> inline void validate_periods(const std::vector<Period<T>> &periods) {
158 auto raw = detail::to_raw(periods);
159 check_status(tempoch_period_list_validate(raw.data(), raw.size()), "validate_periods");
160}
161
162template <typename T>
163inline std::vector<Period<T>> intersect_periods(const std::vector<Period<T>> &a,
164 const std::vector<Period<T>> &b) {
165 auto ra = detail::to_raw(a), rb = detail::to_raw(b);
166 tempoch_period_mjd_t *out = nullptr;
167 std::size_t n = 0;
168 check_status(tempoch_period_list_intersect(ra.data(), ra.size(), rb.data(), rb.size(), &out, &n),
169 "intersect_periods");
170 return detail::from_alloc<T>(out, n);
171}
172
173template <typename T>
174inline std::vector<Period<T>> union_periods(const std::vector<Period<T>> &a,
175 const std::vector<Period<T>> &b) {
176 auto ra = detail::to_raw(a), rb = detail::to_raw(b);
177 tempoch_period_mjd_t *out = nullptr;
178 std::size_t n = 0;
179 check_status(tempoch_period_list_union(ra.data(), ra.size(), rb.data(), rb.size(), &out, &n),
180 "union_periods");
181 return detail::from_alloc<T>(out, n);
182}
183
184template <typename T>
185inline std::vector<Period<T>> normalize_periods(const std::vector<Period<T>> &periods) {
186 auto raw = detail::to_raw(periods);
187 tempoch_period_mjd_t *out = nullptr;
188 std::size_t n = 0;
189 check_status(tempoch_period_list_normalize(raw.data(), raw.size(), &out, &n),
190 "normalize_periods");
191 return detail::from_alloc<T>(out, n);
192}
193
194template <typename T> inline std::ostream &operator<<(std::ostream &os, const Period<T> &period) {
195 return os << '[' << period.start() << ", " << period.end() << ')';
196}
197
198} // namespace tempoch
A typed external encoding of a time instant on scale S.
qtty::Quantity< typename qtty::ExtractTag< TargetType >::type > duration() const
Definition period.hpp:75
const tempoch_period_mjd_t & c_inner() const noexcept
Definition period.hpp:126
Period intersection(const Period &other) const
Definition period.hpp:86
bool contains(const T &point) const noexcept
Definition period.hpp:93
T start() const
Definition period.hpp:71
Period(const T &start, const T &end)
Definition period.hpp:63
std::vector< Period< T > > union_with(const Period< T > &other) const
Definition period.hpp:97
static Period from_c(const tempoch_period_mjd_t &c)
Definition period.hpp:69
std::vector< Period< T > > complement_of(const std::vector< Period< T > > &others) const
Definition period.hpp:109
T end() const
Definition period.hpp:72
qtty::Quantity< typename qtty::ExtractTag< TargetType >::type > length() const
Returns the length of the period (primary name; duration() is a backward-compat alias).
Definition period.hpp:82
A point in time on scale S, stored as a split J2000-second pair.
static Time from_civil(const CivilTime &civil)
static Time from_encoded(const EncodedTime< S, Fmt > &encoded)
Decode a scalar encoding Fmt into canonical split storage on scale S (default context).
CivilTime to_civil() const
std::vector< tempoch_period_mjd_t > to_raw(const std::vector< Period< T > > &periods)
Definition period.hpp:137
std::vector< Period< T > > from_alloc(tempoch_period_mjd_t *ptr, std::size_t count)
Definition period.hpp:146
std::ostream & operator<<(std::ostream &os, const CivilTime &u)
Stream CivilTime as YYYY-MM-DD HH:MM:SS[.nnnnnnnnn].
std::vector< Period< T > > intersect_periods(const std::vector< Period< T > > &a, const std::vector< Period< T > > &b)
Definition period.hpp:163
std::vector< Period< T > > normalize_periods(const std::vector< Period< T > > &periods)
Definition period.hpp:185
void validate_periods(const std::vector< Period< T > > &periods)
Definition period.hpp:157
void check_status(tempoch_status_t status, const char *operation)
Check a tempoch_status_t and throw the appropriate exception on error.
Definition ffi_core.hpp:139
std::vector< Period< T > > union_periods(const std::vector< Period< T > > &a, const std::vector< Period< T > > &b)
Definition period.hpp:174
constexpr tempoch_scale_tag_t scale_tag_v
UTC date-time breakdown.
static CivilTime from_mjd_value(double mjd)
Definition period.hpp:48
static double to_mjd_value(const CivilTime &time)
Definition period.hpp:44
static double to_mjd_value(const EncodedTime< S, F > &time)
Definition period.hpp:26
static EncodedTime< S, F > from_mjd_value(double mjd)
Definition period.hpp:34
static Time< S > from_mjd_value(double mjd)
Definition period.hpp:20
static double to_mjd_value(const Time< S > &time)
Definition period.hpp:16
Public aliases for tempoch time encodings.