Boost logo

Boost :

From: Joachim Faulhaber (afojgo_at_[hidden])
Date: 2008-05-22 16:56:33


Hello,

as suggested by Paul Bristow in his response to my proposal on
interval containers I started to integrate boost::date_time into my
library examples.

To my surprise I discovered that boost::date_time does not provide
operator ++ (--) for it's date and time template classes. So the boost
date and time classes are not Incrementable (Decrementable). One could
say: they lack a tic-tac ;-) Which is for *time* kind of a major
omission.

Now, Incrementability seems most fundamental to me. Therefore, in the
design of interval containers in my library, I avoided to reqire
Addability for interval template parameters. Requiring only
In(De)crementability I enable a broader set of instances for intervals
and interval containers.

Iterators for instance provide incrementation and decrementation but
no addability in general (some do).

Incrementability seems also more fundamental to me than Addability.
Which is to say that a concept or algebra that offers Addability
almost certainly also has a Incrementability, but not the other way
round. Take for instance the Peano Axioms of natural numbers, where
Incrementability is given in the form of the fundamental successor
function.

Therefore I would strongly suggest, that a class template that
implements Addability/Subtractability (operators +, +=, -, -=) also
implements In(De)crementability (operators ++ and --).

This should apply even more to classes that resemble in their basic
characteristics an integral numeric type like date and time do.

For boost::date_time I tried to completed the code in this respect. I
added 6 lines of code:

boost\date_time\time_system_counted.hpp(114):
counted_time_rep& operator ++(){++time_count_; return *this;}
counted_time_rep& operator --(){--time_count_; return *this;}

boost\date_time\int_adapter.hpp(191):
int_adapter& operator ++() {++value_; return *this;}
int_adapter& operator --() {--value_; return *this;}

boost\date_time\time.hpp(179):
time_type operator ++() { ++time_; return time_type(time_); }
time_type operator --() { --time_; return time_type(time_); }

With these minor additions to the code the 'party' example from my
interval template library works fine with boost::date_time:

-----------------------------------------------------------------------
#include <stdio.h>
#include <iostream>
#include <itl/itl_value.hpp>
#include <itl/string_set.hpp>
#include <itl/split_interval_map.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace std;
using namespace boost::posix_time;
using namespace itl;

typedef itl::set<string> GuestSetT;
typedef split_interval_map<ptime, GuestSetT>
  PartyAttendenceHistoryT;

void boost_party()
{
  GuestSetT mary_harry;
  mary_harry.insert("Mary");
  mary_harry.insert("Harry");

  GuestSetT diana_susan;
  diana_susan.insert("Diana");
  diana_susan.insert("Susan");

  GuestSetT peter;
  peter.insert("Peter");

  PartyAttendenceHistoryT party;

  party.insert(
    make_pair(
      rightopen_interval<ptime>(
        time_from_string("2008-05-20 19:30:00.000"),
        time_from_string("2008-05-20 23:00:00.000")),
        mary_harry) );
  party.insert(
    make_pair(
      rightopen_interval<ptime>(
        time_from_string("2008-05-20 20:10:00.000"),
        time_from_string("2008-05-21 00:00:00.000")),
        diana_susan) );
    party.insert(
      make_pair(
        rightopen_interval<ptime>(
        time_from_string("2008-05-20 22:15:00.000"),
        time_from_string("2008-05-21 00:30:00.000")),
        peter) );

  PartyAttendenceHistoryT::iterator it = party.begin();
  while(it != party.end())
  {
      interval<ptime> when = (*it).first;
      // Who is at the party within the time interval 'when' ?
      GuestSetT who = (*it++).second;
      cout << "[" << to_simple_string(when.first())
        << " - " << to_simple_string(when.last()) << "]"
        << ": " << who.as_string() << endl;
  }
}

int main()
{
  cout << ">> Interval Template Library: Sample boost_party.cpp <<\n";
  cout << "-------------------------------------------------------\n";
  boost_party();
  return 0;
}

Program output:

>> Interval Template Library: Sample boost_party.cpp <<
-------------------------------------------------------
[2008-May-20 19:30:00 - 2008-May-20 20:09:59.999999]: Harry Mary
[2008-May-20 20:10:00 - 2008-May-20 22:14:59.999999]: Diana Harry Mary Susan
[2008-May-20 22:15:00 - 2008-May-20 22:59:59.999999]: Diana Harry Mary Peter

            Susan
[2008-May-20 23:00:00 - 2008-May-20 23:59:59.999999]: Diana Peter Susan
[2008-May-21 00:00:00 - 2008-May-21 00:29:59.999999]: Peter

cheers
Joachim

-----
Interval Template Library (ITL)
download from http://www.sourceforge.net/projects/itl
documentation http://www.herold-faulhaber.de/itl


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk