Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

SimpleTimeDuration.cpp

Go to the documentation of this file.
00001 #include "SimpleTimeDuration.h"
00002 #include "SimpleTimeInterval.h"
00003 #include <cmath>
00004 #include <limits.h>
00005 
00006 
00007 // DinoFM: Win32 does not support properly unsigned 64 bit integers
00008 #ifdef _WIN32
00009 
00010 const DurationT SIMPLEDURATION_MAX = 9223372036854775807;  
00011 #else
00012 
00013 //ULL  const DurationT SIMPLEDURATION_MAX = 18446744073709551615LL;    // 2^64-1
00015 const DurationT SIMPLEDURATION_MAX = 9223372036854775807LL;     
00016 #endif
00017 
00018 // Construction
00020 
00021 
00022 SimpleTimeDuration::SimpleTimeDuration(const SimpleTime & start,
00023                                        const SimpleTime & end) {
00024   setDuration(start, end);
00025 }
00026 
00027 SimpleTimeDuration::SimpleTimeDuration(const SimpleTimeInterval & i) {
00028   setDuration(i);
00029 }
00030 
00031 void SimpleTimeDuration::setDuration(const SimpleTimeDuration & d) {
00032   durationval=d.duration();
00033 }
00034 
00035 void SimpleTimeDuration::setDuration(const SimpleTimeInterval & i) {
00036   durationval=i.end().toTimeT()-i.start().toTimeT();
00037 }
00038 
00039 void SimpleTimeDuration::setDuration(const SimpleTime & start,
00040                                      const SimpleTime & end) {
00041   // set Duration from two points in time:
00042   // Take the difference between them. Always nonnegative.
00043 
00044   // Reverse the interval (and call the fcn again) if end is before start
00045   if (end.isBefore(start)) {
00046     setDuration(end, start);
00047     return;
00048   } 
00049 
00050   // If either of the points is inf and they don't coincide,
00051   // set the duration to inf.
00052   if (end.isPlusInf()) {
00053     if (start.isPlusInf()) {
00054       durationval=0;
00055     } else {
00056       setPlusInf();
00057     }
00058     return;
00059   }
00060   if (start.isMinusInf()) {
00061     if (end.isMinusInf()) {
00062       durationval=0;
00063     } else {
00064       setPlusInf();
00065     }
00066     return;
00067   }
00068 
00069   // The standard case: Finite points, start before end.
00070   durationval=end.toTimeT()-start.toTimeT();
00071 }
00072 
00074 // Aux. functions
00076 
00077 DurationT SimpleTimeDuration::duration() const {
00078   return durationval;
00079 }
00080 
00081 void SimpleTimeDuration::setPlusInf() {
00082   durationval = SIMPLEDURATION_MAX;
00083 }
00084 
00085 bool SimpleTimeDuration::isPlusInf() const {
00086   return (durationval == SIMPLEDURATION_MAX);
00087 }
00088 
00090 // Comparison
00092 
00093 bool 
00094 SimpleTimeDuration::isShorterThan(const SimpleTimeDuration & comparison) const {
00095   return (durationval < comparison.duration());
00096 }
00097 
00099 // Operators
00101 
00102 void SimpleTimeDuration::operator = (const SimpleTimeDuration & d) {
00103   setDuration(d);
00104 }
00105 
00106 void SimpleTimeDuration::operator += (const SimpleTimeDuration & d) {
00107   if (isPlusInf()) { // Leave unchanged if infinity
00108     return;
00109   } else if (d.isPlusInf()) { // Go to inf if inf is added
00110     setPlusInf();
00111   } else if(durationval/2 + d.duration()/2 > SIMPLEDURATION_MAX/2) { // Overflow
00112     setPlusInf();
00113   } else { // Normal case
00114     durationval+=d.duration();
00115   }
00116 }
00117 
00118 void SimpleTimeDuration::operator -= (const SimpleTimeDuration & d) {
00119   if (isPlusInf()) { // Leave unchanged if inf, even if inf is subtracted
00120     return;
00121   } else if (d.isPlusInf()) { // Infinity subtracted from finite value
00122     durationval=0; // This is not really the best solution!
00123     // TODO: Throw an exception
00124   } else if (isShorterThan(d)) { // The subtractor is larger than the operand
00125     durationval=0; // TODO: Throw an exception
00126   } else {
00127     durationval-=d.duration(); // Normal case
00128   }
00129 }
00130 
00131 void SimpleTimeDuration::operator *= (const double & factor) {
00132   if (factor < 0) {
00133     durationval=0;
00134     return; // No negative values allowed! TODO: Throw an exception
00135   }
00136   if (isPlusInf()) { // Leave unchanged if infinity
00137     return;
00138   } else if (SIMPLEDURATION_MAX/factor < (double)durationval) { // Overflow!
00139     setPlusInf();
00140   } else { // Normal case
00141     durationval = (DurationT) std::ceil(durationval*factor);
00142   }
00143 }
00144 
00145 void SimpleTimeDuration::operator /= (const double & denom) {
00146   if (denom < 0) {
00147     durationval=0;
00148     return; // No negative values allowed! TODO: Throw an exception
00149   }
00150   if (isPlusInf()) { // Leave unchanged if infinity
00151     return;
00152   } else if ((denom < 1) && (durationval > denom*SIMPLEDURATION_MAX)) {
00153     setPlusInf(); // Overflow! TODO: Throw an exception
00154   } else {
00155     durationval = (DurationT) std::ceil(durationval/denom);
00156   }
00157 }
00158 
00160 // Global operators
00162 
00163 SimpleTimeDuration operator * (const SimpleTimeDuration & d,
00164                                const double & factor) {
00165   SimpleTimeDuration retval(d);
00166   retval *= factor;
00167   return retval;
00168 }
00169 
00170 SimpleTimeDuration operator / (const SimpleTimeDuration & d,
00171                                const double & denom) {
00172   SimpleTimeDuration retval(d);
00173   retval /= denom;
00174   return retval;
00175 }
00176 
00177 double operator / (const SimpleTimeDuration & d,
00178                    const SimpleTimeDuration & denom) {
00179   if (d.isPlusInf()) {
00180     return SIMPLEDURATION_MAX;
00181   } else if (denom.isPlusInf()) {
00182     return .0;
00183   } else {
00184     return (double)d.duration()/(double)denom.duration();
00185   }
00186 }
00187 
00188 SimpleTimeDuration operator + (const SimpleTimeDuration & arg1,
00189                                const SimpleTimeDuration & arg2) {
00190   SimpleTimeDuration retval(arg1);
00191   retval += arg2;
00192   return retval;
00193 }
00194 
00195 SimpleTimeDuration operator - (const SimpleTimeDuration & arg1,
00196                                const SimpleTimeDuration & arg2) {
00197   SimpleTimeDuration retval(arg1);
00198   retval -= arg2;
00199   return retval;
00200 }
00201 
00202 bool operator < (const SimpleTimeDuration & left,
00203                  const SimpleTimeDuration & right) {
00204   return (left.duration() < right.duration());
00205 }
00206 
00207 bool operator > (const SimpleTimeDuration & left,
00208                  const SimpleTimeDuration & right) {
00209   return (left.duration() > right.duration());
00210 }
00211 
00212 #ifndef _WIN32
00213 AIDA_STD::ostream & operator << (AIDA_STD::ostream & out, const SimpleTimeDuration & d) {
00214   out << d.duration();
00215   return out;
00216 }
00217 #endif

Generated on Tue May 20 14:50:25 2003 for HepUtilities by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002