3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_MATH_INTERVALS_H
7#define ELEMENTA_MATH_INTERVALS_H
60template <
typename ELEMTYPE>
71 {
return(
Interval{mi,
static_cast<ELEMTYPE
>(mi +
width),cl}); }
91 "Invalid interval (max " +
92 std::to_string(ma) +
" is < min " +
93 std::to_string(mi) +
")");
94 if ((closedl_ != closedr_) && (ma == mi))
104 ELEMTYPE
minimum(
void)
const noexcept {
return(min_); }
107 ELEMTYPE
maximum(
void)
const noexcept {
return(max_); }
110 ELEMTYPE
width(
void)
const noexcept {
return(max_ - min_); }
115 {
return(closednessFromCloses(closedl_,closedr_)); }
117 bool closedMin(
void)
const noexcept {
return(closedl_); }
120 bool closedMax(
void)
const noexcept {
return(closedr_); }
123 bool empty(
void)
const noexcept {
return((!allclosed_) && (min_ == max_)); }
131 if ( ((oth.min_ > max_) || (oth.max_ < min_)) ||
132 ((oth.closedl_ != closedr_) && (oth.min_ == max_)) ||
133 ((oth.closedr_ != closedl_) && (oth.max_ == min_)) )
137 if ((oth.min_ > min_) || (oth.min_ == min_))
141 b = std::min(oth.max_,max_);
148 b = std::min(oth.max_,max_);
151 return(
Interval{a,b,closednessFromCloses(cl,cr)});
156 {
return( ((e > min_) && (e < max_)) ||
157 ((e == min_) && (closedl_)) ||
158 ((e == max_) && (closedr_)) ); }
162 {
return(std::string(1, closedl_ ?
'[' :
'(') +
163 std::to_string(min_) +
".." + std::to_string(max_) +
164 std::string(1, closedl_ ?
']' :
')')); }
169 static IntervalCl closednessFromCloses(
bool l,
bool r)
noexcept
171 (l ? IntervalCl::CLOSED : IntervalCl::OPENED) :
172 (l ? IntervalCl::LEFT_CLOSED : IntervalCl::RIGHT_CLOSED)); }
174 bool closedl_,closedr_,allclosed_;
179 { closedl_ = o.closedl_; closedr_ = o.closedr_; allclosed_ = o.allclosed_;
180 min_ = o.min_; max_ = o.max_; }
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
A numeric interval, either closed, opened or semi-closed.
bool closedMin(void) const noexcept
Return the closedness of the minimum bound.
static Interval fromWidth(ELEMTYPE mi, ELEMTYPE width, IntervalCl cl=IntervalCl::CLOSED)
Factory constructor from the minimum and width of the interval.
bool closedMax(void) const noexcept
Return the closedness of the maximum bound.
IntervalCl closedness(void) const noexcept
< Return the closedness of the interval.
Interval(ELEMTYPE mi, ELEMTYPE ma, IntervalCl cl=IntervalCl::CLOSED)
Constructor from the minimum and maximum of the interval.
std::string to_string(void) const
< Return a string describing the interval.
bool containPoint(ELEMTYPE e) const noexcept
< Return true if E is inside the interval.
ELEMTYPE minimum(void) const noexcept
Return the minimum value.
bool empty(void) const noexcept
Return true if the interval is empty.
IntervalCl
Kinds of closedness for numeric intervals.
Interval intersect(const Interval &oth) const noexcept
< Return the interval that is the intersection of both, or empty if none.
ELEMTYPE width(void) const noexcept
Return the distance from the minimum to the maximum.
ELEMTYPE maximum(void) const noexcept
Return the maximum value.
Interval(void)
Default constructor: empty interval.