implement total order on time intervals

This commit is contained in:
Fischlurch 2011-04-04 00:41:38 +02:00
parent 0e4ed856d7
commit 9364d717b0
2 changed files with 26 additions and 12 deletions

View file

@ -312,7 +312,7 @@ namespace time {
{ }
explicit
Duration (Time const& timeSpec)
Duration (TimeValue const& timeSpec)
: Offset(Offset(timeSpec).abs())
{ }
@ -347,16 +347,27 @@ namespace time {
*/
class TimeSpan
: public Time
, boost::totally_ordered<TimeSpan>
{
Duration dur_;
public:
TimeSpan(TimeValue start, Duration length)
TimeSpan(TimeValue const& start, Duration const& length)
: Time(start)
, dur_(length)
{ }
TimeSpan(TimeValue start, FSecs(duration_in_secs))
TimeSpan(TimeValue const& start, Offset const& reference_distance)
: Time(start)
, dur_(reference_distance)
{ }
TimeSpan(TimeValue const& start, TimeValue const& end)
: Time(start)
, dur_(Offset(start,end))
{ }
TimeSpan(TimeValue const& start, FSecs(duration_in_secs))
: Time(start)
, dur_(duration_in_secs)
{ }
@ -380,6 +391,11 @@ namespace time {
TimeVar startPoint (*this);
return (startPoint + dur_);
}
/// Supporting extended total order, based on start and interval length
friend bool operator== (TimeSpan const& t1, TimeSpan const& t2) { return t1.t_==t2.t_ && t1.dur_==t2.dur_; }
friend bool operator< (TimeSpan const& t1, TimeSpan const& t2) { return t1.t_< t2.t_ ||
(t1.t_==t2.t_ && t1.dur_< t2.dur_);}
};

View file

@ -29,12 +29,10 @@
#include <boost/lexical_cast.hpp>
#include <iostream>
//#include <cstdlib>
#include <string>
using boost::lexical_cast;
using util::isnil;
//using std::rand;
using std::cout;
using std::endl;
using std::string;
@ -312,10 +310,10 @@ namespace test{
void
compareTimeSpan (TimeValue org)
{
TimeSpan span1 (Time(org), Duration(Time(org)));
TimeSpan span2 (Time(org), Offset(org, TimeValue(0)));
TimeSpan span3 (Time(org), FSecs(5,2));
TimeSpan span4 (Time(org), FSecs(5,-2));
TimeSpan span1 (Time(org), Time(org)+Time(org)); // using the distance between start and end point
TimeSpan span2 (Time(org), Offset(org, TimeValue(0))); // note: the offset is taken absolute, as Duration
TimeSpan span3 (Time(org), FSecs(5,2)); // Duration given explicitly, in seconds
TimeSpan span4 (Time(org), FSecs(5,-2)); // again: the Duration is taken absolute
CHECK (span1 == span2);
CHECK (span2 == span1);
@ -368,19 +366,19 @@ namespace test{
CHECK (span3 > span3y);
CHECK (span3.duration() > span3y.duration());
CHECK (span3.start() == span3y.start());
CHECK (span3.end() == span3y.end());
CHECK (span3.end() > span3y.end());
CHECK (Time(span3) == Time(span3y));
CHECK (span3 < span3z);
CHECK (span3.duration() > span3z.duration());
CHECK (span3.start() < span3z.start());
CHECK (span3.end() < span3z.end());
CHECK (span3.end() != span3z.end()); // it's shorter, and org can be random, so that's all we know
CHECK (Time(span3) < Time(span3z));
CHECK (span3y < span3z);
CHECK (span3y.duration() == span3z.duration());
CHECK (span3y.start() < span3z.start());
CHECK (span3.end() < span3z.end());
CHECK (span3y.end() < span3z.end());
CHECK (Time(span3) < Time(span3z));
}
};