1#ifndef STORAGE_DATA_RANGE_H
2#define STORAGE_DATA_RANGE_H
4#include <vkdb/concepts.h>
5#include <vkdb/time_series_key.h>
14template <RegularNoCVRefQuals TData>
15 requires std::totally_ordered<TData>
18 using data_type = TData;
33 DataRange(
const data_type& start,
const data_type& end) noexcept
34 : is_set_{
true}, range_{start, end} {}
48 auto ampersand_pos{
str.find(
'&')};
49 if (ampersand_pos == std::string::npos) {
50 throw std::invalid_argument{
51 "DataRange::DataRange(): Invalid range string '" +
str +
"'."
55 if constexpr (std::is_same_v<data_type, TimeSeriesKey>) {
59 range_.first = std::stod(
str.substr(0, ampersand_pos));
60 range_.second = std::stod(
str.substr(ampersand_pos + 1));
96 [[nodiscard]] auto operator<=>(const
DataRange& other) const noexcept {
98 return std::strong_ordering::less;
100 if (!other.is_set_) {
101 return std::strong_ordering::greater;
103 return range_ <=> other.range_;
116 range_.second = data;
121 range_.first = std::min(range_.first, data);
122 range_.second = std::max(range_.second, data);
132 [[nodiscard]]
bool inRange(
const data_type& data)
const noexcept {
133 return is_set_ && data >= range_.first && data <= range_.second;
145 const data_type& start,
148 return is_set_ && range_.first <= end && range_.second >= start;
158 [[nodiscard]] data_type
lower()
const {
160 throw std::logic_error{
"DataRange::lower(): Range is not set."};
172 [[nodiscard]] data_type
upper()
const {
174 throw std::logic_error{
"DataRange::upper(): Range is not set."};
176 return range_.second;
192 [[nodiscard]] std::string
str() const noexcept {
197 if constexpr (std::is_same_v<data_type, TimeSeriesKey>) {
198 return range_.first.str() +
"&" + range_.second.str();
200 return std::to_string(range_.first)
201 +
"&" + std::to_string(range_.second);
216 std::pair<data_type, data_type> range_;
223using TimeRange = DataRange<Timestamp>;
229using KeyRange = DataRange<TimeSeriesKey>;
DataRange(std::string &&str)
Construct a new DataRange object from the given string.
data_type lower() const
Get the lower bound of the range.
std::string str() const noexcept
Convert the range to a string.
data_type upper() const
Get the upper bound of the range.
bool overlapsWith(const data_type &start, const data_type &end) const noexcept
Check if the range overlaps with the given range.
DataRange(const data_type &start, const data_type &end) noexcept
Construct a new DataRange object from the given start and end.
void clear() noexcept
Clear the range.
DataRange(DataRange &&) noexcept=default
Move-construct a DataRange object.
bool inRange(const data_type &data) const noexcept
Check if the data is in the range.
DataRange() noexcept
Construct a new DataRange object.
void updateRange(const data_type &data) noexcept
Update the range with the given data.
Represents a key in vkdb.