diff -r 95dbf5f94ca2 src/core/model/nstime.h --- a/src/core/model/nstime.h Sun Apr 07 14:50:51 2013 +0200 +++ b/src/core/model/nstime.h Sun Apr 07 20:44:34 2013 +0200 @@ -93,13 +93,18 @@ */ enum Unit { - S = 0, //!< second - MS = 1, //!< millisecond - US = 2, //!< microsecond - NS = 3, //!< nanosecond - PS = 4, //!< picosecond - FS = 5, //!< femtosecond - LAST = 6 + A = 0, //!< year + WK = 1, //!< week + D = 2, //!< day + H = 3, //!< hour + MIN = 4, //!< minute + S = 5, //!< second + MS = 6, //!< millisecond + US = 7, //!< microsecond + NS = 8, //!< nanosecond + PS = 9, //!< picosecond + FS = 10, //!< femtosecond + LAST = 11 }; inline Time &operator = (const Time &o) @@ -194,6 +199,11 @@ * - `ns` (nanoseconds) * - `ps` (picoseconds) * - `fs` (femtoseconds) + * - `min` (minutes) + * - `h` (hours) + * - `d` (days) + * - `wk` (weeks) + * - `a` (years) * * There can be no white space between the numerical portion * and the units. Any otherwise malformed string causes a fatal error to @@ -305,6 +315,48 @@ { return ToInteger (Time::FS); } + + /** + * \returns an approximation in minutes of the time stored in this + * instance. + */ + inline double GetMinutes (void) const + { + return ToDouble (Time::MIN); + } + /** + * \returns an approximation in hours of the time stored in this + * instance. + */ + inline double GetHours (void) const + { + return ToDouble (Time::H); + } + /** + * \returns an approximation in days of the time stored in this + * instance. + */ + inline double GetDays (void) const + { + return ToDouble (Time::D); + } + /** + * \returns an approximation in weeks of the time stored in this + * instance. + */ + inline double GetWeeks (void) const + { + return ToDouble (Time::WK); + } + /** + * \returns an approximation in years of the time stored in this + * instance. + */ + inline double GetYears (void) const + { + return ToDouble (Time::A); + } + /** * \returns the raw time value, in the current units */ @@ -678,6 +730,31 @@ return Time::FromDouble (seconds, Time::S); } +inline Time Seconds (float seconds) +{ + return Time::FromDouble (seconds, Time::S); +} + +inline Time Seconds (uint64_t seconds) +{ + return Time::FromInteger (seconds, Time::S); +} + +inline Time Seconds (int64_t seconds) +{ + return Time::FromInteger (seconds, Time::S); +} + +inline Time Seconds (uint32_t seconds) +{ + return Time::FromInteger (seconds, Time::S); +} + +inline Time Seconds (int32_t seconds) +{ + return Time::FromInteger (seconds, Time::S); +} + /** * \brief create ns3::Time instances in units of milliseconds. * @@ -689,10 +766,36 @@ * \param ms milliseconds value * \relates ns3::Time */ +inline Time MilliSeconds (double ms) +{ + return Time::FromDouble (ms, Time::MS); +} + +inline Time MilliSeconds (float ms) +{ + return Time::FromDouble (ms, Time::MS); +} + inline Time MilliSeconds (uint64_t ms) { return Time::FromInteger (ms, Time::MS); } + +inline Time MilliSeconds (int64_t ms) +{ + return Time::FromInteger (ms, Time::MS); +} + +inline Time MilliSeconds (uint32_t ms) +{ + return Time::FromInteger (ms, Time::MS); +} + +inline Time MilliSeconds (int32_t ms) +{ + return Time::FromInteger (ms, Time::MS); +} + /** * \brief create ns3::Time instances in units of microseconds. * @@ -704,10 +807,36 @@ * \param us microseconds value * \relates ns3::Time */ +inline Time MicroSeconds (double us) +{ + return Time::FromDouble (us, Time::US); +} + +inline Time MicroSeconds (float us) +{ + return Time::FromDouble (us, Time::US); +} + inline Time MicroSeconds (uint64_t us) { return Time::FromInteger (us, Time::US); } + +inline Time MicroSeconds (int64_t us) +{ + return Time::FromInteger (us, Time::US); +} + +inline Time MicroSeconds (uint32_t us) +{ + return Time::FromInteger (us, Time::US); +} + +inline Time MicroSeconds (int32_t us) +{ + return Time::FromInteger (us, Time::US); +} + /** * \brief create ns3::Time instances in units of nanoseconds. * @@ -719,10 +848,36 @@ * \param ns nanoseconds value * \relates ns3::Time */ +inline Time NanoSeconds (double ns) +{ + return Time::FromDouble (ns, Time::NS); +} + +inline Time NanoSeconds (float ns) +{ + return Time::FromDouble (ns, Time::NS); +} + inline Time NanoSeconds (uint64_t ns) { return Time::FromInteger (ns, Time::NS); } + +inline Time NanoSeconds (int64_t ns) +{ + return Time::FromInteger (ns, Time::NS); +} + +inline Time NanoSeconds (uint32_t ns) +{ + return Time::FromInteger (ns, Time::NS); +} + +inline Time NanoSeconds (int32_t ns) +{ + return Time::FromInteger (ns, Time::NS); +} + /** * \brief create ns3::Time instances in units of picoseconds. * @@ -734,10 +889,36 @@ * \param ps picoseconds value * \relates ns3::Time */ +inline Time PicoSeconds (double ps) +{ + return Time::FromDouble (ps, Time::PS); +} + +inline Time PicoSeconds (float ps) +{ + return Time::FromDouble (ps, Time::PS); +} + inline Time PicoSeconds (uint64_t ps) { return Time::FromInteger (ps, Time::PS); } + +inline Time PicoSeconds (int64_t ps) +{ + return Time::FromInteger (ps, Time::PS); +} + +inline Time PicoSeconds (uint32_t ps) +{ + return Time::FromInteger (ps, Time::PS); +} + +inline Time PicoSeconds (int32_t ps) +{ + return Time::FromInteger (ps, Time::PS); +} + /** * \brief create ns3::Time instances in units of femtoseconds. * @@ -749,11 +930,240 @@ * \param fs femtoseconds value * \relates ns3::Time */ +inline Time FemtoSeconds (double fs) +{ + return Time::FromDouble (fs, Time::FS); +} + +inline Time FemtoSeconds (float fs) +{ + return Time::FromDouble (fs, Time::FS); +} + inline Time FemtoSeconds (uint64_t fs) { return Time::FromInteger (fs, Time::FS); } +inline Time FemtoSeconds (int64_t fs) +{ + return Time::FromInteger (fs, Time::FS); +} + +inline Time FemtoSeconds (uint32_t fs) +{ + return Time::FromInteger (fs, Time::FS); +} + +inline Time FemtoSeconds (int32_t fs) +{ + return Time::FromInteger (fs, Time::FS); +} + +/** + * \brief create ns3::Time instances in units of minutes. + * + * For example: + * \code + * Time t = Minutes (2.0); + * Simulator::Schedule (Minutes (5.0), ...); + * \endcode + * \param minutes mintues value + * \relates ns3::Time + */ +inline Time Minutes (double minutes) +{ + return Time::FromDouble (minutes, Time::MIN); +} + +inline Time Minutes (float minutes) +{ + return Time::FromDouble (minutes, Time::MIN); +} + +inline Time Minutes (uint64_t minutes) +{ + return Time::FromInteger (minutes, Time::MIN); +} + +inline Time Minutes (int64_t minutes) +{ + return Time::FromInteger (minutes, Time::MIN); +} + +inline Time Minutes (uint32_t minutes) +{ + return Time::FromInteger (minutes, Time::MIN); +} + +inline Time Minutes (int32_t minutes) +{ + return Time::FromInteger (minutes, Time::MIN); +} + +/** + * \brief create ns3::Time instances in units of hours. + * + * For example: + * \code + * Time t = Hours (2.0); + * Simulator::Schedule (Hours (5.0), ...); + * \endcode + * \param hours hours value + * \relates ns3::Time + */ +inline Time Hours (double hours) +{ + return Time::FromDouble (hours, Time::H); +} + +inline Time Hours (float hours) +{ + return Time::FromDouble (hours, Time::H); +} + +inline Time Hours (uint64_t hours) +{ + return Time::FromInteger (hours, Time::H); +} + +inline Time Hours (int64_t hours) +{ + return Time::FromInteger (hours, Time::H); +} + +inline Time Hours (uint32_t hours) +{ + return Time::FromInteger (hours, Time::H); +} + +inline Time Hours (int32_t hours) +{ + return Time::FromInteger (hours, Time::H); +} + +/** + * \brief create ns3::Time instances in units of days. + * + * For example: + * \code + * Time t = Days (2.0); + * Simulator::Schedule (Days (5.0), ...); + * \endcode + * \param days days value + * \relates ns3::Time + */ +inline Time Days (double days) +{ + return Time::FromDouble (days, Time::D); +} + +inline Time Days (float days) +{ + return Time::FromDouble (days, Time::D); +} + +inline Time Days (uint64_t days) +{ + return Time::FromInteger (days, Time::D); +} + +inline Time Days (int64_t days) +{ + return Time::FromInteger (days, Time::D); +} + +inline Time Days (uint32_t days) +{ + return Time::FromInteger (days, Time::D); +} + +inline Time Days (int32_t days) +{ + return Time::FromInteger (days, Time::D); +} + +/** + * \brief create ns3::Time instances in units of weeks. + * + * For example: + * \code + * Time t = Weeks (2.0); + * Simulator::Schedule (Weeks (5.0), ...); + * \endcode + * \param weeks weeks value + * \relates ns3::Time + */ +inline Time Weeks (double weeks) +{ + return Time::FromDouble (weeks, Time::WK); +} + +inline Time Weeks (float weeks) +{ + return Time::FromDouble (weeks, Time::WK); +} + +inline Time Weeks (uint64_t weeks) +{ + return Time::FromInteger (weeks, Time::WK); +} + +inline Time Weeks (int64_t weeks) +{ + return Time::FromInteger (weeks, Time::WK); +} + +inline Time Weeks (uint32_t weeks) +{ + return Time::FromInteger (weeks, Time::WK); +} + +inline Time Weeks (int32_t weeks) +{ + return Time::FromInteger (weeks, Time::WK); +} + +/** + * \brief create ns3::Time instances in units of years. + * + * For example: + * \code + * Time t = Years (2.0); + * Simulator::Schedule (Years (5.0), ...); + * \endcode + * \param years years value + * \relates ns3::Time + */ +inline Time Years (double years) +{ + return Time::FromDouble (years, Time::A); +} + +inline Time Years (float years) +{ + return Time::FromDouble (years, Time::A); +} + +inline Time Years (uint64_t years) +{ + return Time::FromInteger (years, Time::A); +} + +inline Time Years (int64_t years) +{ + return Time::FromInteger (years, Time::A); +} + +inline Time Years (uint32_t years) +{ + return Time::FromInteger (years, Time::A); +} + +inline Time Years (int32_t years) +{ + return Time::FromInteger (years, Time::A); +} /** * \see Seconds(double) @@ -803,6 +1213,46 @@ { return Time::From (fs, Time::FS); } +/** + * \see Minutes(uint64_t) + * \relates ns3::Time + */ +inline Time Minutes (int64x64_t minutes) +{ + return Time::From (minutes, Time::MIN); +} +/** + * \see Minutes(uint64_t) + * \relates ns3::Time + */ +inline Time Hours (int64x64_t hours) +{ + return Time::From (hours, Time::H); +} +/** + * \see Minutes(uint64_t) + * \relates ns3::Time + */ +inline Time Days (int64x64_t days) +{ + return Time::From (days, Time::D); +} +/** + * \see Minutes(uint64_t) + * \relates ns3::Time + */ +inline Time Weeks (int64x64_t weeks) +{ + return Time::From (weeks, Time::WK); +} +/** + * \see Minutes(uint64_t) + * \relates ns3::Time + */ +inline Time Years (int64x64_t years) +{ + return Time::From (years, Time::A); +} // internal function not publicly documented inline Time TimeStep (uint64_t ts) diff -r 95dbf5f94ca2 src/core/model/time.cc --- a/src/core/model/time.cc Sun Apr 07 14:50:51 2013 +0200 +++ b/src/core/model/time.cc Sun Apr 07 20:44:34 2013 +0200 @@ -102,6 +102,30 @@ { *this = Time::FromDouble (r, Time::FS); } + else if (trailer == std::string ("fs")) + { + *this = Time::FromDouble (r, Time::FS); + } + else if (trailer == std::string ("min")) + { + *this = Time::FromDouble (r, Time::MIN); + } + else if (trailer == std::string ("h")) + { + *this = Time::FromDouble (r, Time::H); + } + else if (trailer == std::string ("d")) + { + *this = Time::FromDouble (r, Time::D); + } + else if (trailer == std::string ("wk")) + { + *this = Time::FromDouble (r, Time::WK); + } + else if (trailer == std::string ("a")) + { + *this = Time::FromDouble (r, Time::A); + } else { NS_ABORT_MSG ("Can't Parse Time " << s); @@ -153,21 +177,39 @@ ConvertTimes (unit); } - int8_t power [LAST] = { 15, 12, 9, 6, 3, 0}; + // A, WK, D, H, MIN, S, MS, US, NS, PS, FS + const int8_t power [LAST] = { 17, 17, 17, 17, 16, 15, 12, 9, 6, 3, 0 }; + const int32_t coefficient [LAST] = { 315576, 6048, 864, 36, 6, 1, 1, 1, 1, 1, 1 }; for (int i = 0; i < Time::LAST; i++) { int shift = power[i] - power[(int)unit]; - uint64_t factor = (uint64_t) std::pow (10, std::fabs (shift)); + int quotient = 1; + if (coefficient[i] > coefficient[(int) unit]) + { + quotient = coefficient[i] / coefficient[(int) unit]; + } + else if (coefficient[i] < coefficient[(int) unit]) + { + quotient = -coefficient[(int) unit] / coefficient[i]; + } + NS_LOG_DEBUG ("SetResolution for unit " << (int) unit << " loop iteration " << i + << " has shift " << shift << " has quotient " << quotient); + uint64_t factor = (uint64_t) std::pow (10, std::fabs (shift)) * std::fabs (quotient); + double realFactor = std::pow (10, shift) + * static_cast (coefficient[i]) / coefficient[(int)unit]; + NS_LOG_DEBUG ("SetResolution factor " << factor << " real factor " << realFactor); struct Information *info = &resolution->info[i]; info->factor = factor; - if (shift == 0) + // here we could equivalently check for realFactor == 1.0 but it's better + // to avoid checking equality of doubles + if (shift == 0 && quotient == 1) { info->timeFrom = int64x64_t (1); info->timeTo = int64x64_t (1); info->toMul = true; info->fromMul = true; } - else if (shift > 0) + else if (realFactor > 1) { info->timeFrom = int64x64_t (factor); info->timeTo = int64x64_t::Invert (factor); @@ -176,7 +218,7 @@ } else { - NS_ASSERT (shift < 0); + NS_ASSERT (realFactor < 1); info->timeFrom = int64x64_t::Invert (factor); info->timeTo = int64x64_t (factor); info->toMul = true; @@ -308,6 +350,21 @@ case Time::FS: unit = "fs"; break; + case Time::MIN: + unit = "min"; + break; + case Time::H: + unit = "h"; + break; + case Time::D: + unit = "d"; + break; + case Time::WK: + unit = "wk"; + break; + case Time::A: + unit = "a"; + break; case Time::LAST: NS_ABORT_MSG ("can't be reached"); unit = "unreachable";