4747#include " base/basic_types.h"
4848#include " base/string/cstring.h"
4949#include < cassert>
50+ #include < cmath>
5051
5152#define MPTIME_MAXVAL 1.0e+30
5253#define MPTIME_MIN_INF_VAL -1.0e+20
@@ -55,16 +56,26 @@ namespace MaxPlus {
5556
5657using MPThroughput = CDouble;
5758
59+ class MPTime ;
60+ CString timeToString (MPTime val);
61+
5862class MPTime {
5963public:
6064 explicit MPTime (CDouble val = MPTIME_MAXVAL) : myVal(val) {}
6165
6266 explicit operator CDouble () const { return myVal; }
67+ explicit operator CString () const { return timeToString (*this ); }
6368 MPTime &operator -();
6469 MPTime &operator +=(MPTime a);
6570 MPTime &operator -=(MPTime a);
6671 bool operator ==(MPTime a) const ;
67- bool operator <(MPTime b) const ;
72+ bool operator !=(MPTime a) const ;
73+ bool operator <(MPTime a) const ;
74+ bool operator >(MPTime a) const ;
75+ bool operator <=(MPTime a) const ;
76+ bool operator >=(MPTime a) const ;
77+ [[nodiscard]] bool isMinusInfinity () const ;
78+ [[nodiscard]] MPTime fabs () const ;
6879
6980private:
7081 CDouble myVal;
@@ -103,12 +114,23 @@ inline CDouble MP_MIN(CDouble a, CDouble b) { return CDouble(MP_MIN(MPTime(a), M
103114// the quick and dirty way of representing -infinity
104115const MPTime MP_MINUSINFINITY = MPTime(-1.0e+30 );
105116
106- inline bool MP_ISMINUSINFINITY (MPTime a) { return ((CDouble)a) < MPTIME_MIN_INF_VAL; }
107- inline bool MP_ISMINUSINFINITY_VAL (CDouble a) { return a < MPTIME_MIN_INF_VAL; }
117+ inline bool MP_ISMINUSINFINITY (CDouble a) { return a < MPTIME_MIN_INF_VAL; }
108118
109- inline MPTime MP_PLUS (MPTime a, MPTime b) {
119+ inline MPTime MP_PLUS (CDouble a, CDouble b) {
110120 return (MP_ISMINUSINFINITY (a) || MP_ISMINUSINFINITY (b)) ? MP_MINUSINFINITY
111- : MPTime (a + b);
121+ : MPTime (static_cast <CDouble>(a) + static_cast <CDouble>(b));
122+ }
123+
124+ inline MPTime MP_PLUS (MPTime a, CDouble b) {
125+ return MP_PLUS (static_cast <CDouble>(a), b);
126+ }
127+
128+ inline MPTime MP_PLUS (CDouble a, MPTime b) {
129+ return MP_PLUS (a, static_cast <CDouble>(b));
130+ }
131+
132+ inline MPTime MP_PLUS (MPTime a, MPTime b) {
133+ return MP_PLUS (static_cast <CDouble>(a), static_cast <CDouble>(b));
112134}
113135
114136// MaxPlus epsilon (used to compare floating point numbers for equality)
@@ -117,10 +139,10 @@ const MPTime MP_EPSILON = MPTime(1e-10);
117139// ==============================
118140// MPTime operators
119141// ==============================
120- inline MPTime operator +(MPTime a, MPTime b) { return MP_PLUS ((CDouble) a, (CDouble) b); }
142+ inline MPTime operator +(MPTime a, MPTime b) { return MP_PLUS (a, b); }
121143
122144inline MPTime operator -(MPTime a, MPTime b) {
123- assert (!MP_ISMINUSINFINITY (b ));
145+ assert (!b. isMinusInfinity ( ));
124146 return a + MPTime (-b);
125147}
126148
@@ -129,17 +151,17 @@ inline MPTime operator-(MPTime a, CDouble b) { return MPTime(a) - MPTime(b); }
129151inline MPTime operator -(CDouble a, MPTime b) { return MPTime (a) - MPTime (b); }
130152
131153inline MPTime &MPTime::operator -() {
132- assert (!MP_ISMINUSINFINITY (* this ));
154+ assert (!this -> isMinusInfinity ( ));
133155 myVal = -myVal;
134156 return *this ;
135157}
136158
137159inline MPTime operator *(MPTime a, MPTime b) {
138- if (MP_ISMINUSINFINITY (a )) {
160+ if (a. isMinusInfinity ( )) {
139161 assert (((CDouble)b) > 0.0 );
140162 return MP_MINUSINFINITY;
141163 }
142- if (MP_ISMINUSINFINITY (b )) {
164+ if (b. isMinusInfinity ( )) {
143165 assert (((CDouble)a) > 0.0 );
144166 return MP_MINUSINFINITY;
145167 }
@@ -156,7 +178,7 @@ inline MPTime &MPTime::operator+=(MPTime a) {
156178}
157179
158180inline MPTime &MPTime::operator -=(MPTime a) {
159- assert (!MP_ISMINUSINFINITY (a ));
181+ assert (!a. isMinusInfinity ( ));
160182 *this = *this + MPTime (CDouble (-a));
161183 return *this ;
162184}
@@ -165,31 +187,55 @@ inline bool MPTime::operator==(MPTime a) const {
165187 return this ->myVal == a.myVal ;
166188}
167189
190+ inline bool MPTime::operator !=(MPTime a) const {
191+ return this ->myVal != a.myVal ;
192+ }
193+
168194inline bool MPTime::operator <(MPTime a) const {
169195 return this ->myVal < a.myVal ;
170196}
171197
198+ inline bool MPTime::operator >(MPTime a) const {
199+ return this ->myVal > a.myVal ;
200+ }
201+
202+ inline bool MPTime::operator <=(MPTime a) const {
203+ return this ->myVal <= a.myVal ;
204+ }
205+
206+ inline bool MPTime::operator >=(MPTime a) const {
207+ return this ->myVal >= a.myVal ;
208+ }
209+
210+ inline bool MPTime::isMinusInfinity () const {
211+ return MP_ISMINUSINFINITY (this ->myVal );
212+ }
213+
214+ inline MPTime MPTime::fabs () const {
215+ return MPTime (std::fabs (this ->myVal ));
216+ }
217+
172218// ==============================
173219// toString
174220// ==============================
175221
176222inline CString timeToString (MPTime val) {
177- // We intentionally dont use MP_ISMINUSINFINITY(val ) here,
223+ // We intentionally dont use isMinusInfinity( ) here,
178224 // so that we can expose the unwanted "impure" infinities here.
179225 //
180- if (MP_ISMINUSINFINITY (val)) {
226+ if (static_cast <CDouble> (val)==MPTIME_MIN_INF_VAL ) {
181227 return {" -mp_inf" };
182228 }
183229 return {static_cast <CDouble>(val)};
184230}
185231inline CString timeToMatlabString (MPTime val) {
186- if (MP_ISMINUSINFINITY ( val)) {
232+ if (val. isMinusInfinity ( )) {
187233 return {" -Inf" };
188234 }
189235 return {static_cast <CDouble>(val)};
190236}
191237inline CString timeToLaTeXString (MPTime val) {
192- if (MP_ISMINUSINFINITY ( val)) {
238+ if (val. isMinusInfinity ( )) {
193239 return {" -\\ infty{}" };
194240 }
195241 return {static_cast <CDouble>(val)};
0 commit comments