20 using count_type = std::size_t;
23 range() : _lower_bound(_default_bound), _upper_bound(_default_bound) {}
29 explicit range(
const count_type n) : _lower_bound(n), _upper_bound(n) {}
36 range(
const count_type lower_bound,
const count_type upper_bound)
37 : _lower_bound(lower_bound), _upper_bound(upper_bound) {}
49 return this->_lower_bound.has_value() or this->_upper_bound.has_value();
65 [[nodiscard]] std::weak_ordering
ordering(
const range::count_type n)
const noexcept {
66 if (not (this->_lower_bound.has_value() or this->_upper_bound.has_value()))
67 return std::weak_ordering::equivalent;
69 if (this->_lower_bound.has_value() and this->_upper_bound.has_value()) {
70 if (n < this->_lower_bound.value())
71 return std::weak_ordering::less;
73 if (n > this->_upper_bound.value())
74 return std::weak_ordering::greater;
76 return std::weak_ordering::equivalent;
79 if (this->_lower_bound.has_value())
80 return (n < this->_lower_bound.value())
81 ? std::weak_ordering::less
82 : std::weak_ordering::equivalent;
84 return (n > this->_upper_bound.value())
85 ? std::weak_ordering::greater
86 : std::weak_ordering::equivalent;
89 friend std::ostream& operator<<(std::ostream& os,
const range& r)
noexcept {
90 if (r._lower_bound.has_value() and r._lower_bound == r._upper_bound) {
91 os << r._lower_bound.value();
95 if (not r._lower_bound.has_value() and not r._upper_bound.has_value()) {
100 os <<
"[" << r._lower_bound.value_or(0ull) <<
", ";
101 if (r._upper_bound.has_value())
102 os << r._upper_bound.value() <<
"]";
112 friend range up_to(
const count_type)
noexcept;
121 range(const std::optional<count_type> lower_bound, const std::optional<count_type> upper_bound)
122 : _lower_bound(lower_bound), _upper_bound(upper_bound) {}
124 std::optional<count_type> _lower_bound;
125 std::optional<count_type> _upper_bound;
127 static constexpr count_type _default_bound = 1ull;
136 return range(n, std::nullopt);
145 return range(n + 1, std::nullopt);
154 return range(std::nullopt, n - 1);
162[[nodiscard]]
inline range up_to(
const range::count_type n)
noexcept {
163 return range(std::nullopt, n);
171 return range(std::nullopt, std::nullopt);
Argument's number of values managing class.
range()
Default constructor: creates range [1, 1].
range(const count_type n)
Exact count constructor: creates range [n, n].
friend range at_least(const count_type) noexcept
range class builder function. Creates a range [n, inf].
friend range any() noexcept
range class builder function. Creates a range [0, inf].
std::weak_ordering ordering(const range::count_type n) const noexcept
Determines the ordering of the count against a range instance.
friend range less_than(const count_type) noexcept
range class builder function. Creates a range [0, n - 1].
friend range more_than(const count_type) noexcept
range class builder function. Creates a range [n + 1, inf].
range(const count_type lower_bound, const count_type upper_bound)
Concrete range constructor: creates range [lower_bound, upper_bound].
friend range up_to(const count_type) noexcept
range class builder function. Creates a range [0, n].
bool is_bound() const noexcept
Returns true if at least one bound (lower, upper) is set. Otherwise returns false
range up_to(const range::count_type n) noexcept
range class builder function. Creates a range [0, n].
range more_than(const range::count_type n) noexcept
range class builder function. Creates a range [n + 1, inf].
range any() noexcept
range class builder function. Creates a range [0, inf].
range less_than(const range::count_type n) noexcept
range class builder function. Creates a range [0, n - 1].
range at_least(const range::count_type n) noexcept
range class builder function. Creates a range [n, inf].