Skip to content

Commit 74fb8f2

Browse files
committed
sync
1 parent ea9c54c commit 74fb8f2

File tree

13 files changed

+268
-220
lines changed

13 files changed

+268
-220
lines changed

include/maxplus/algebra/mpmatrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Vector {
101101
}
102102

103103
Vector &operator-=(MPTime decrease) {
104-
assert(!MP_ISMINUSINFINITY(decrease));
104+
assert(!decrease.isMinusInfinity());
105105
this->add(-decrease, this);
106106
return *this;
107107
}
@@ -217,7 +217,7 @@ class Matrix {
217217
}
218218

219219
Matrix &operator-=(MPTime decrease) {
220-
assert(!MP_ISMINUSINFINITY(decrease));
220+
assert(!decrease.isMinusInfinity());
221221
this->add(-decrease, this);
222222
return *this;
223223
}

include/maxplus/algebra/mptype.h

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
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

5657
using MPThroughput = CDouble;
5758

59+
class MPTime;
60+
CString timeToString(MPTime val);
61+
5862
class MPTime {
5963
public:
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

6980
private:
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
104115
const 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

122144
inline 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); }
129151
inline MPTime operator-(CDouble a, MPTime b) { return MPTime(a) - MPTime(b); }
130152

131153
inline MPTime &MPTime::operator-() {
132-
assert(!MP_ISMINUSINFINITY(*this));
154+
assert(!this->isMinusInfinity());
133155
myVal = -myVal;
134156
return *this;
135157
}
136158

137159
inline 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

158180
inline 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+
168194
inline 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

176222
inline 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
}
185231
inline CString timeToMatlabString(MPTime val) {
186-
if (MP_ISMINUSINFINITY(val)) {
232+
if (val.isMinusInfinity()) {
187233
return {"-Inf"};
188234
}
189235
return {static_cast<CDouble>(val)};
190236
}
191237
inline CString timeToLaTeXString(MPTime val) {
192-
if (MP_ISMINUSINFINITY(val)) {
238+
if (val.isMinusInfinity()) {
193239
return {"-\\infty{}"};
194240
}
195241
return {static_cast<CDouble>(val)};

include/maxplus/game/mpgameautomaton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class MaxPlusGameAutomatonWithRewards : public MaxPlusAutomatonWithRewards,
7272

7373
void addV1(MPARState *s) { this->setV1->insert(s); }
7474

75-
MPTime getWeight1(const MPAREdge *e) const { return e->label.reward; }
75+
MPTime getWeight1(const MPAREdge *e) const { return MPTime(e->label.reward); }
7676

7777
MPTime getWeight2(const MPAREdge *e) const { return e->label.delay; }
7878

0 commit comments

Comments
 (0)