31 #ifndef __adevs_time_h_ 32 #define __adevs_time_h_ 39 template <
class T>
inline T adevs_inf();
41 template <
class T>
inline T adevs_zero();
43 template <
class T>
inline T adevs_sentinel();
45 template <
class T>
inline T adevs_epsilon();
55 template<
class T =
double>
struct Time 62 Time(T t = adevs_zero<T>(),
unsigned int c = 0):t(t),c(c){}
87 if (t2.t == 0)
return Time(t,t2.c+c);
88 else return Time(t+t2.t,0);
104 return (t == t2.t && c == t2.c);
109 return !(*
this == t2);
114 return (t < t2.t || (t == t2.t && c < t2.c));
116 bool operator<=(
const Time& t2)
const 118 return (*
this == t2 || *
this < t2);
120 bool operator>(
const Time& t2)
const 122 return !(*
this <= t2);
124 bool operator>=(
const Time& t2)
const 126 return !(*
this < t2);
139 sd_time(
double t,
int k):t(t),k(k){}
141 double real()
const {
return t; }
142 double integer()
const {
return k; }
152 return (t == t2.t && k == t2.k);
157 return !(*
this == t2);
162 return (t < t2.t || (t == t2.t && k < t2.k));
164 bool operator<=(
const sd_time& t2)
const 166 return (*
this == t2 || *
this < t2);
168 bool operator>(
const sd_time& t2)
const 170 return !(*
this <= t2);
172 bool operator>=(
const sd_time& t2)
const 174 return !(*
this < t2);
185 if (t2.t == 0.0) k += t2.k;
186 else { t += t2.t; k = t2.k; }
197 if (t == t2.t) { t = 0.0; k -= t2.k; }
201 friend std::ostream& operator<<(std::ostream& out,
const sd_time& t)
203 out <<
"(" << t.t <<
"," << t.k <<
")";
224 inline int fcmp(
double x1,
double x2,
double epsilon)
243 frexp(fabs(x1) > fabs(x2) ? x1 : x2, &exponent);
254 delta = ldexp(epsilon, exponent);
256 difference = x1 - x2;
258 if (difference > delta)
260 else if (difference < -delta)
304 return (
fcmp(d, rhs, epsilon) < 0);
308 return (
fcmp(d, rhs.d, epsilon) < 0);
312 return (
fcmp(d, rhs.d, epsilon) <= 0);
316 return (
fcmp(d, rhs.d, epsilon) > 0);
320 return (
fcmp(d, rhs.d, epsilon) >= 0);
324 return (
fcmp(d, rhs, epsilon) == 0);
328 return (
fcmp(d, rhs.d, epsilon) == 0);
334 template <>
inline long double adevs_inf() {
335 return std::numeric_limits<long double>::max(); }
336 template <>
inline double adevs_inf() {
337 return std::numeric_limits<double>::max(); }
338 template <>
inline int adevs_inf() {
339 return std::numeric_limits<int>::max(); }
340 template <>
inline long adevs_inf() {
341 return std::numeric_limits<long>::max(); }
343 return std::numeric_limits<double>::max(); }
345 return adevs::sd_time(std::numeric_limits<float>::max(),std::numeric_limits<int>::max()); }
347 template <>
inline long double adevs_zero() {
return 0.0L; }
348 template <>
inline double adevs_zero() {
return 0.0; }
349 template <>
inline int adevs_zero() {
return 0; }
350 template <>
inline long adevs_zero() {
return 0; }
354 template <>
inline long double adevs_sentinel() {
return -1.0L; }
355 template <>
inline double adevs_sentinel() {
return -1.0; }
356 template <>
inline int adevs_sentinel() {
return -1; }
357 template <>
inline long adevs_sentinel() {
return -1; }
361 template <>
inline long double adevs_epsilon() {
return 0.0L; }
362 template <>
inline double adevs_epsilon() {
return 0.0; }
363 template <>
inline int adevs_epsilon() {
return 0; }
364 template <>
inline long adevs_epsilon() {
return 0; }
369 std::ostream& operator<<(std::ostream& strm, const adevs::Time<T>& t);
bool operator==(const sd_time &t2) const
Equivalence.
Definition: adevs_time.h:150
static adevs::Time< T > Inf()
Value for infinity.
Definition: adevs_time.h:60
const Time & operator=(T t2)
Definition: adevs_time.h:78
bool operator<(const sd_time &t2) const
Order by t then by c.
Definition: adevs_time.h:160
Time(const Time &t2)
Copy constructor.
Definition: adevs_time.h:64
const Time & operator=(const Time &t2)
Assignment operator.
Definition: adevs_time.h:66
bool operator==(const Time &t2) const
Equivalence.
Definition: adevs_time.h:102
Definition: adevs_time.h:273
Definition: adevs_time.h:55
Time operator+(const Time &t2) const
Advance operator (this is not commutative or associative!)
Definition: adevs_time.h:85
T operator-(T t2) const
Subtract a real number (used to get the elapsed time)
Definition: adevs_time.h:97
bool operator<(T t2) const
Comparing with a T compares the real field.
Definition: adevs_time.h:73
Definition: adevs_fmi.h:56
bool operator<(const Time &t2) const
Order by t then by c.
Definition: adevs_time.h:112
static double epsilon
Definition: adevs_time.h:283
const Time & operator+=(const Time &t2)
Advance and assign.
Definition: adevs_time.h:91
int fcmp(double x1, double x2, double epsilon)
Definition: adevs_time.h:224
bool operator!=(const sd_time &t2) const
Not equal.
Definition: adevs_time.h:155
bool operator!=(const Time &t2) const
Not equal.
Definition: adevs_time.h:107
Time(T t=adevs_zero< T >(), unsigned int c=0)
Constructor. Default time is (0,0).
Definition: adevs_time.h:62
sd_time operator+(const sd_time &t2) const
Add and subtract.
Definition: adevs_time.h:177
Definition: adevs_time.h:133