Project: ArangoDB (App Definition and Development / Database)
File: intro_to_itl.pdf

2009-05-08Lecture
held at the Boost Library Conference 2009Joachim Faulhaber
Slide Design by Chih-Hao Tsaihttp://www.chtsai.orgCopyright © Joachim Faulhaber 2009Distributed under Boost Software Licence 1.0Updated version 3.2.0   2009-12-02An Introduction to the 
Interval Template Library2
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Lecture Outline
Background and Motivation
Design
Examples
Semantics
Implementation
Future Works
Availability3
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Background and Motivation
Interval containers simplified the implementation of 
date and time related tasks
Decomposing “histories”  of attributed events into 
segments with constant attributes.
Working with time grids, e.g. a grid of months.
Aggregations of values associated to date or time 
intervals.
… that occurred frequently in programs like
Billing modules
Therapy scheduling programs
Hospital and controlling statistics4
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Background is the date time problem domain ...
… but the scope of the Itl as a generic library is more 
general: 
an interval_set  is a set
       that is implemented as a set of intervals  
an interval_map  is a map
       that is implemented as a map of interval value pairs5
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Aspects
There are two aspects in the design of interval 
containers
Fundamental aspect
interval_set <int> mySet;
mySet.insert(42);
bool has_answer = mySet.contains(42);interval_set <int> mySet;
mySet.insert(42);
bool has_answer = mySet.contains(42);
On the fundamental aspect an interval_set can be used 
just as a set of elements
Set theoretic operations are supported
Abstracts from sequential and segmental information
Segmental aspect
Allows to access and iterate over the segments  of 
interval containers6
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Addability and Subtractability
All of itl's (interval) containers are Addable and 
Subtractable  
They implement operators += , +, -= and -
+= -=
 sets set union set difference
 maps ? ?
A possible implementation for maps
Propagate addition/subtraction to the associated values 
. . . or aggregate on overlap
. . . or aggregate on collision7
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Aggregate on overlap
→ a
→ b
+→ a
→ (a + b)
→ b
Decompositional 
effect on Intervals
Accumulative effect 
on associated values
I
JJ-II-J
I∩J
I, J: intervals,  a,b: associated values8
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Aggregate on overlap, a minimal example
typedef itl::set<string> guests;
interval_map <time, guests> party;
 
party += make_pair(
  interval< time>::rightopen (20:00, 22:00), guests( "Mary"));
party += make_pair(
  interval< time>::rightopen (21:00, 23:00), guests( "Harry")); 
// party now contains
[20:00, 21:00)->{ "Mary"} 
[21:00, 22:00)->{ "Harry","Mary"} //guest sets aggregated 
[22:00, 23:00)->{ "Harry"}9
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Granu-
larityStyle Sets Maps
interval interval
joining interval_set interval_map
separating separate_interval_set
splitting split_interval_set split_interval_map
element set mapDesign
The Itl's class templates10
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Interval Combining Styles: Joining
Intervals are joined on overlap or on touch
. . . for maps , if associated values are equal
Keeps interval_maps and sets in a minimal form
 interval_set
 
   {[1      3)          }
 +       [2      4)
 +                 [4 5)
 = {[1           4)     }
 
 = {[1                5)}  interval_map
 
   {[1      3) ->1         } 
 +      [2      4) ->1
 +                [4 5) ->1
 ={[1 2)[2 3)[3 4)        }
    ->1  ->2  ->1       
 ={[1 2)[2 3)[3      5)   }
    ->1  ->2     ->1       11
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Interval Combining Styles: Splitting
Intervals are split on overlap and kept separate on touch
All interval borders are preserved (insertion memory)
 split_interval_set
 
   {[1      3)          }
 +       [2      4)
 +                 [4 5)
 = {[1 2)[2 3)[3 4)     }
 
 = {[1 2)[2 3)[3 4)[4 5)}  split_interval_map
 
   {[1      3) ->1         } 
 +      [2      4) ->1
 +                [4 5) ->1
 ={[1 2)[2 3)[3 4)        }
    ->1  ->2  ->1       
 ={[1 2)[2 3)[3 4)[4 5)   }
    ->1  ->2  ->1   ->1    12
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Interval Combining Styles: Separating
Intervals are joined on overlap but kept separate on 
touch
Preserves borders that are never crossed (preserves a 
hidden grid).
 separate_interval_set
 
   {[1      3)          }
 +       [2      4)
 +                 [4 5)
 = {[1           4)     }
 
 = {[1           4)[4 5)} 13
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
A few instances of intervals (interval.cpp)
interval< int> int_interval = interval< int>::closed(3,7);
interval< double> sqrt_interval
  = interval< double>::rightopen (1/sqrt(2.0), sqrt(2.0));
interval< std::string > city_interval
  = interval<std::string>:: leftopen("Barcelona" , "Boston");
interval< boost::ptime> time_interval
  = interval< boost::ptime>::open(
                       time_from_string( "2008-05-20 19:30" ),
                       time_from_string( "2008-05-20 23:00" )
    );14
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
A way to iterate over months and weeks 
(month_and_week_grid.cpp )
#include <boost/itl/gregorian.hpp> //boost::gregorian plus adapter code  
#include <boost/itl/split_interval_set.hpp>
// A split_interval_set of gregorian dates as date_grid.
typedef split_interval_set<boost::gregorian::date> date_grid;
// Compute a date_grid of months using boost::gregorian.
date_grid month_grid( const interval<date>& scope)
{
    date_grid month_grid;
    // Compute a date_grid of months using boost::gregorian.
    . . .
    return month_grid;
}
// Compute a date_grid of weeks using boost::gregorian.
date_grid week_grid( const interval<date>& scope)
{
    date_grid week_grid;
    // Compute a date_grid of weeks using boost::gregorian.
    . . .
    return week_grid;
}15
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
A way to iterate over months and weeks
void month_and_time_grid()
{
    date someday = day_clock::local_day();
    date thenday = someday + months(2);
    interval<date> scope = interval<date>::rightopen(someday, thenday);
    // An intersection of the month and week grids ...
    date_grid month_and_week_grid 
        = month_grid(scope) & week_grid(scope);
    // ... allows to iterate months and weeks. Whenever a month
    // or a week changes there is a new interval.
    for(date_grid::iterator it = month_and_week_grid.begin(); 
        it != month_and_week_grid.end(); it++)
    {   . . .   }
    // We can also intersect the grid into an interval_map to make
    // shure that all intervals are within months and week bounds.
    interval_map< boost::gregorian::date, some_type> accrual;
    compute_some_result(accrual, scope);
    accrual &= month_and_week_grid;
}16
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Aggregating with interval_maps
Computing averages via implementing operator  +=
(partys_guest_average.cpp)
class counted_sum
{
public:
counted_sum() :_sum(0),_count(0){}
counted_sum( int sum):_sum(sum),_count(1){}
int sum()const  {return _sum;}
int count()const{return _count;}
double average() const
    { return _count==0 ? 0.0 : _sum/ static_cast <double>(_count); }
counted_sum& operator +=  (const counted_sum& right)
{ _sum += right.sum(); _count += right.count(); return *this; }
private:
int _sum;
int _count;
};
bool operator ==  (const counted_sum& left, const counted_sum& right)
{ return left.sum()==right.sum() && left.count()==right.count(); } 17
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Aggregating with interval_maps
Computing averages via implementing operator  +=
void partys_height_average()
{
    interval_map<ptime, counted_sum > height_sums;
    height_sums += (
      make_pair(
        interval<ptime>::rightopen(
          time_from_string( "2008-05-20 19:30" ), 
          time_from_string( "2008-05-20 23:00" )), 
          counted_sum(165) ) // Mary is 1,65 m tall.
    );
    // Add height of more pary guests . . .  
    interval_map<ptime, counted_sum>::iterator height_sum_ =
        height_sums.begin();
    while(height_sum_ != height_sums.end())
    {
        interval<ptime> when = height_sum_->first;
        double height_average = (*height_sum_++).second. average();
        cout << "[" << when.first() << " - " << when.upper() << ")"
             << ": " << height_average << " cm" << endl;
    }
}18
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Interval containers allow to express a variety of date 
and time operations in an easy way.
Example man_power.cpp  ...
Subtract weekends and holidays from an interval_set
worktime -= weekends(scope)
worktime -= german_reunification_day
Intersect an interval_map with an interval_set
claudias_working_hours &= worktime
Subtract and interval_set from an interval map
claudias_working_hours -= claudias_absense_times
Adding interval_maps
interval_map<date, int> manpower;
manpower += claudias_working_hours;
manpower += bodos_working_hours;19
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Interval_maps can also be intersected
Example user_groups.cpp
typedef boost::itl::set<string> MemberSetT;
typedef interval_map<date, MemberSetT> MembershipT;
void user_groups()
{
    . . .
    MembershipT med_users;
    // Compute membership of medical staff
    med_users += make_pair( member_interval_1, MemberSetT( "Dr.Jekyll" ));
    med_users += . . . 
    MembershipT admin_users;
    // Compute membership of administation staff
    med_users += make_pair( member_interval_2, MemberSetT( "Mr.Hyde"));
    . . .
    MembershipT all_users   = med_users + admin_users;
    MembershipT super_users = med_users & admin_users;
    . . .
}20
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
The semantics of itl sets is based on a concept itl::Set
itl::set , interval_set , split_interval_set  
and separate_interval_set  are models of concept 
itl::Set
// Abstract part
empty set:        Set::Set()
subset relation:  bool Set::contained_in (const Set& s2)const
equality:         bool is_element_equal (const Set& s1, const Set& s2)
set union:        Set& operator += (Set& s1, const Set& s2)
                  Set  operator +  (const Set& s1, const Set& s2)
set difference:   Set& operator -= (Set& s1, const Set& s2)
                  Set  operator -  (const Set& s1, const Set& s2)
set intersection: Set& operator &= (Set& s1, const Set& s2)
                  Set  operator &  (const Set& s1, const Set& s2)  
// Part related to sequential ordering
sorting order:    bool operator <  (const Set& s1, const Set& s2)
lexicographical equality:
                  bool operator == (const Set& s1, const Set& s2)
 21
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
The semantics of itl maps is based on a concept itl::Map
itl::map , interval_map  and split_interval_map  
are models of concept 
itl::Map
// Abstract part
empty map:        Map::Map()
submap relation:  bool Map::contained_in (const Map& m2)const
equality:         bool is_element_equal (const Map& m1, const Map& m2)
map union:        Map& operator += (Map& m1, const Map& m2)
                  Map  operator +  (const Map& m1, const Map& m2)
map difference:   Map& operator -= (Map& m1, const Map& m2)
                  Map  operator -  (const Map& m1, const Map& m2)
map intersection: Map& operator &= (Map& m1, const Map& m2)
                  Map  operator &  (const Map& m1, const Map& m2)  
// Part related to sequential ordering
sorting order:    bool operator <  (const Map& m1, const Map& m2)
lexicographical equality:
                  bool operator == (const Map& m1, const Map& m2)
 22
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Defining semantics of itl concepts via sets of laws
aka c++0x axioms
Checking law sets via automatic testing:
A Law Based Test Automaton LaBatea
Generate
law instance
apply law to instance
collect violations
Commutativity<T a, U b, +>:
              a + b = b + a;23
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Lexicographical Ordering and Equality
For all itl containers operator <  implements a strict 
weak ordering . 
The induced equivalence  of this ordering is 
lexicographical equality  which is implemented as 
operator ==
This is in line with the semantics of 
SortedAssociativeContainers24
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Subset Ordering and Element Equality
For all itl containers function contained_in  
implements a partial ordering .
The induced equivalence  of this ordering is 
equality of elements  which is implemented as 
function is_element_equal .25
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
itl::Sets
All itl sets implement a Set Algebra , which is to say 
satisfy a “ classical”  set of laws . . .
. . . using is_element_equal  as equality
Associativity, Neutrality, Commutativity (for + and &)
Distributivity, DeMorgan, Symmetric Difference
Most of the itl sets satisfy the classical set of laws 
even if . . .
. . .  lexicographical equality: operator ==  is used
The differences reflect proper inequalities in sequence 
that occur for separate_interval_set  and 
split_interval_set . 26
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Concept Induction / Concept Transition
The semantics of itl::Maps appears to be determined  by the 
codomain type  of the map
                     is model of  if   example                    
 Map<D,Monoid>       Monoid            interval_map<int, string>   
 Map<D,Set>          Set          C1   interval_map<int, set<int>>
                                 
 Map<D,CommutMonoid > CommutMonoid       interval_map<int, unsigned>
 Map<D,AbelianGroup>  AbelianGroup  C2   interval_map<int, int,total>
Conditions C1 and C2 restrict the Concept Induction  to specific 
map traits
C1: Value pairs that carry a neutral element  as associated 
value are always deleted  (Trait: absorbs_neutrons ).
C2: The map is total: Non existing keys are implicitly mapped to 
neutral elements  (Trait: is_total ). 27
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Implementation
Itl containers are implemented based on
std::set and std::map
Basic operations like adding and subtracting  intervals or 
interval value pairs perform with a time complexity 
between * amortized O(log n)  and O(n), where n is the 
number of intervals of a container.
Operations like addition and subtraction  of whole 
containers are having a worst case complexity of
O(m log(n+m)) , where n and m are the numbers of 
intervals of the containers to combine.
* : Consult the library documentation for more detailed 
information.28
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Future Works
Implementing interval_maps of sets more efficiently
Revision of features of the extended itl (itl_plus.zip)
Decomposition of histories : k histories hk with attribute 
types A1, ..., Ak are “decomposed ” to a product history 
of tuples of attribute sets:
(h1<T,A1>,..., h<T,Ak>) → h<T, (set<A1>,…, set<Ak>)>
Cubes (generalized crosstables): Applying aggregate 
on collision  to maps of tuple value pairs  in order to 
organize hierachical data and their aggregates.29
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Availability
Itl project on sourceforge  (version 2.0.1)
http://sourceforge.net/projects/itl
Latest version on boost vault/Containers  (3.2.0)
http://www.boostpro.com/vault/  → containers
itl_3_2_0.zip : Core itl in preparation for boost
itl_plus_3_2_0.zip : Extended itl including histories, cubes 
and automatic validation (LaBatea).
Online documentation  at
http://www.herold-faulhaber.de/
Doxygen generated docs for (version 2.0.1)
http://www.herold-faulhaber.de/itl/
Latest boost style documentation (version 3.2.0)
http://www.herold-faulhaber.de/boost_itl/doc/libs/itl/doc/html/30
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Availability
Boost sandbox
https://svn.boost.org/svn/boost/sandbox/itl/
Core itl: Interval containers proposed for boost
https://svn.boost.org/svn/boost/sandbox/itl/boost/itl/
https://svn.boost.org/svn/boost/sandbox/itl/libs/itl/
Extended itl_xt: interval_bitset, “histories”, cubes
https://svn.boost.org/svn/boost/sandbox/itl/boost/itl_xt/
https://svn.boost.org/svn/boost/sandbox/itl/libs/itl_xt/
Validater LaBatea: 
Compiles with msvc-8.0 or newer, gcc-4.3.2 or newer
https://svn.boost.org/svn/boost/sandbox/itl/boost/validate/
https://svn.boost.org/svn/boost/sandbox/itl/libs/validate/2009-05-08Lectureheld at the Boost Library Conference 2009Joachim Faulhaber
Slide Design by Chih-Hao Tsaihttp://www.chtsai.orgCopyright © Joachim Faulhaber 2009Distributed under Boost Software Licence 1.0Updated version 3.2.0   2009-12-02An Introduction to the 
Interval Template Library2
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Lecture Outline
Background and Motivation
Design
Examples
Semantics
Implementation
Future Works
Availability3
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Background and Motivation
Interval containers simplified the implementation of 
date and time related tasks
Decomposing “histories” of attributed events into 
segments with constant attributes.
Working with time grids, e.g. a grid of months.
Aggregations of values associated to date or time 
intervals.
… that occurred frequently in programs like
Billing modules
Therapy scheduling programs
Hospital and controlling statistics4
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Background is the date time problem domain ...
… but the scope of the Itl as a generic library is more 
general: 
an interval_set is a set
       that is implemented as a set of intervals  
an interval_map is a map
       that is implemented as a map of interval value pairs5
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Aspects
There are two aspects in the design of interval 
containers
Fundamental aspect
interval_set<int> mySet;
mySet.insert(42);
bool has_answer = mySet.contains(42);interval_set<int> mySet;
mySet.insert(42);
bool has_answer = mySet.contains(42);
On the fundamental aspect an interval_set can be used 
just as a set of elements
Set theoretic operations are supported
Abstracts from sequential and segmental information
Segmental aspect
Allows to access and iterate over the segments of 
interval containers6
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Addability and Subtractability
All of itl's (interval) containers are Addable and 
Subtractable 
They implement operators +=, +, -= and -
+=-=
 sets set unionset difference
 maps ??
A possible implementation for maps
Propagate addition/subtraction to the associated values 
. . . or aggregate on overlap
. . . or aggregate on collision7
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Aggregate on overlap
→ a
→ b
+→ a
→ (a + b)
→ b
Decompositional 
effect on Intervals
Accumulative effect 
on associated values
I
JJ-II-J
I∩J
I, J: intervals,  a,b: associated values8
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Aggregate on overlap, a minimal example
typedef itl::set<string> guests;
interval_map<time, guests> party;
 
party += make_pair(
  interval<time>::rightopen(20:00, 22:00), guests( "Mary"));
party += make_pair(
  interval<time>::rightopen(21:00, 23:00), guests( "Harry")); 
// party now contains[20:00, 21:00)->{"Mary"} [21:00, 22:00)->{"Harry","Mary"} //guest sets aggregated [22:00, 23:00)->{"Harry"}9
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Granu-
larityStyleSets Maps
intervalinterval
joininginterval_set interval_map
separatingseparate_interval_set
splittingsplit_interval_set split_interval_map
elementset mapDesign
The Itl's class templates10
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Interval Combining Styles: Joining
Intervals are joined on overlap or on touch
. . . for maps, if associated values are equal
Keeps interval_maps and sets in a minimal form
 interval_set
 
   {[1      3)          }
 +       [2      4)
 +                 [4 5)
 = {[1           4)     }
 
 = {[1                5)}  interval_map
 
   {[1      3)->1         } 
 +      [2      4) ->1
 +                [4 5) ->1
 ={[1 2)[2 3)[3 4)        }
    ->1  ->2  ->1      
 ={[1 2)[2 3)[3      5)   }
    ->1  ->2     ->1       11
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Interval Combining Styles: Splitting
Intervals are split on overlap and kept separate on touch
All interval borders are preserved (insertion memory)
 split_interval_set
 
   {[1      3)          }
 +       [2      4)
 +                 [4 5)
 = {[1 2)[2 3)[3 4)     }
 
 = {[1 2)[2 3)[3 4)[4 5)}  split_interval_map
 
   {[1      3)->1         } 
 +      [2      4) ->1
 +                [4 5) ->1
 ={[1 2)[2 3)[3 4)        }
    ->1  ->2  ->1      
 ={[1 2)[2 3)[3 4)[4 5)   }
    ->1  ->2  ->1  ->1    12
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Design
Interval Combining Styles: Separating
Intervals are joined on overlap but kept separate on 
touch
Preserves borders that are never crossed (preserves a 
hidden grid).
 separate_interval_set
 
   {[1      3)          }
 +       [2      4)
 +                 [4 5)
 = {[1           4)     }
 
 = {[1           4)[4 5)} 13
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
A few instances of intervals (interval.cpp)
interval<int> int_interval = interval< int>::closed(3,7);
interval<double> sqrt_interval
  = interval<double>::rightopen(1/sqrt(2.0), sqrt(2.0));
interval<std::string> city_interval
  = interval<std::string>:: leftopen("Barcelona", "Boston");
interval<boost::ptime> time_interval
  = interval<boost::ptime>::open(
                       time_from_string("2008-05-20 19:30" ),                       time_from_string("2008-05-20 23:00" )    );14
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
A way to iterate over months and weeks 
(month_and_week_grid.cpp )
#include <boost/itl/gregorian.hpp> //boost::gregorian plus adapter code  #include <boost/itl/split_interval_set.hpp>
// A split_interval_set of gregorian dates as date_grid.typedef split_interval_set<boost::gregorian::date> date_grid;
// Compute a date_grid of months using boost::gregorian.date_grid month_grid( const interval<date>& scope){    date_grid month_grid;    // Compute a date_grid of months using boost::gregorian.    . . .    return month_grid;}
// Compute a date_grid of weeks using boost::gregorian.date_grid week_grid( const interval<date>& scope){    date_grid week_grid;    // Compute a date_grid of weeks using boost::gregorian.    . . .    return week_grid;}15
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
A way to iterate over months and weeks
void month_and_time_grid(){    date someday = day_clock::local_day();    date thenday = someday + months(2);    interval<date> scope = interval<date>::rightopen(someday, thenday);
    // An intersection of the month and week grids ...    date_grid month_and_week_grid         = month_grid(scope) & week_grid(scope);
    // ... allows to iterate months and weeks. Whenever a month    // or a week changes there is a new interval.    for(date_grid::iterator it = month_and_week_grid.begin();         it != month_and_week_grid.end(); it++)    {   . . .   }
    // We can also intersect the grid into an interval_map to make    // shure that all intervals are within months and week bounds.    interval_map<boost::gregorian::date, some_type> accrual;    compute_some_result(accrual, scope);    accrual &= month_and_week_grid;
}16
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Aggregating with interval_maps
Computing averages via implementing operator +=
(partys_guest_average.cpp)
class counted_sum{public:counted_sum():_sum(0),_count(0){}counted_sum(int sum):_sum(sum),_count(1){}
int sum()const  {return _sum;}int count()const{return _count;}double average()const    { return _count==0 ? 0.0 : _sum/ static_cast<double>(_count); }
counted_sum& operator += (const counted_sum& right){ _sum += right.sum(); _count += right.count(); return *this; }
private:int _sum;int _count;};
bool operator == (const counted_sum& left, const counted_sum& right){ return left.sum()==right.sum() && left.count()==right.count(); } 17
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Aggregating with interval_maps
Computing averages via implementing operator +=
void partys_height_average(){    interval_map<ptime, counted_sum> height_sums;
    height_sums += (      make_pair(        interval<ptime>::rightopen(          time_from_string( "2008-05-20 19:30"),           time_from_string( "2008-05-20 23:00")),           counted_sum(165)) // Mary is 1,65 m tall.    );
    // Add height of more pary guests . . .  
    interval_map<ptime, counted_sum>::iterator height_sum_ =        height_sums.begin();    while(height_sum_ != height_sums.end())    {        interval<ptime> when = height_sum_->first;        double height_average = (*height_sum_++).second. average();
        cout << "[" << when.first() << " - " << when.upper() << ")"             << ": " << height_average << " cm" << endl;    }}18
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Interval containers allow to express a variety of date 
and time operations in an easy way.
Example man_power.cpp  ...
Subtract weekends and holidays from an interval_set
worktime -= weekends(scope)
worktime -= german_reunification_day
Intersect an interval_map with an interval_set
claudias_working_hours &= worktime
Subtract and interval_set from an interval map
claudias_working_hours -= claudias_absense_times
Adding interval_maps
interval_map<date, int> manpower;
manpower += claudias_working_hours;
manpower += bodos_working_hours;19
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Examples
Interval_maps can also be intersected
Example user_groups.cpp
typedef boost::itl::set<string> MemberSetT;typedef interval_map<date, MemberSetT> MembershipT;
void user_groups(){    . . .
    MembershipT med_users;    // Compute membership of medical staff    med_users += make_pair(member_interval_1, MemberSetT("Dr.Jekyll"));    med_users += . . . 
    MembershipT admin_users;    // Compute membership of administation staff    med_users += make_pair(member_interval_2, MemberSetT("Mr.Hyde"));    . . .
    MembershipT all_users   = med_users + admin_users;
    MembershipT super_users = med_users & admin_users;    . . .
}20
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
The semantics of itl sets is based on a concept itl::Set
itl::set, interval_set, split_interval_set  
and separate_interval_set  are models of concept 
itl::Set
// Abstract partempty set:        Set::Set()subset relation:  bool Set::contained_in(const Set& s2)constequality:         bool is_element_equal(const Set& s1, const Set& s2)set union:        Set& operator += (Set& s1, const Set& s2)                  Set  operator +  (const Set& s1, const Set& s2)set difference:   Set& operator -= (Set& s1, const Set& s2)                  Set  operator -  (const Set& s1, const Set& s2)set intersection: Set& operator &= (Set& s1, const Set& s2)                  Set  operator &  (const Set& s1, const Set& s2)  
// Part related to sequential orderingsorting order:    bool operator <  (const Set& s1, const Set& s2)lexicographical equality:                  bool operator == (const Set& s1, const Set& s2) 21
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
The semantics of itl maps is based on a concept itl::Map
itl::map, interval_map and split_interval_map  
are models of concept 
itl::Map
// Abstract partempty map:        Map::Map()submap relation:  bool Map::contained_in(const Map& m2)constequality:         bool is_element_equal(const Map& m1, const Map& m2)map union:        Map& operator += (Map& m1, const Map& m2)                  Map  operator +  (const Map& m1, const Map& m2)map difference:   Map& operator -= (Map& m1, const Map& m2)                  Map  operator -  (const Map& m1, const Map& m2)map intersection: Map& operator &= (Map& m1, const Map& m2)                  Map  operator &  (const Map& m1, const Map& m2)  
// Part related to sequential orderingsorting order:    bool operator <  (const Map& m1, const Map& m2)lexicographical equality:                  bool operator == (const Map& m1, const Map& m2) 22
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Defining semantics of itl concepts via sets of laws
aka c++0x axioms
Checking law sets via automatic testing:
A Law Based Test Automaton LaBatea
Generate
law instance
apply law to instance
collect violations
Commutativity<T a, U b, +>:
              a + b = b + a;23
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Lexicographical Ordering and Equality
For all itl containers operator < implements a strict 
weak ordering. 
The induced equivalence  of this ordering is 
lexicographical equality  which is implemented as 
operator ==
This is in line with the semantics of 
SortedAssociativeContainers24
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Subset Ordering and Element Equality
For all itl containers function contained_in  
implements a partial ordering .
The induced equivalence  of this ordering is 
equality of elements  which is implemented as 
function is_element_equal .25
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
itl::Sets
All itl sets implement a Set Algebra, which is to say 
satisfy a “classical” set of laws . . .
. . . using is_element_equal  as equality
Associativity, Neutrality, Commutativity (for + and &)
Distributivity, DeMorgan, Symmetric Difference
Most of the itl sets satisfy the classical set of laws 
even if . . .
. . .  lexicographical equality: operator == is used
The differences reflect proper inequalities in sequence 
that occur for separate_interval_set  and 
split_interval_set . 26
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Semantics
Concept Induction / Concept Transition
The semantics of itl::Maps appears to be determined by the 
codomain type of the map
                     is model of  if   example                    
 Map<D,Monoid>       Monoid            interval_map<int, string>   
 Map<D,Set>          Set          C1   interval_map<int, set<int>>                                  Map<D,CommutMonoid> CommutMonoid      interval_map<int, unsigned>
 Map<D,AbelianGroup> AbelianGroup C2   interval_map<int,int,total>
Conditions C1 and C2 restrict the Concept Induction to specific 
map traits
C1: Value pairs that carry a neutral element as associated 
value are always deleted (Trait: absorbs_neutrons ).
C2: The map is total: Non existing keys are implicitly mapped to 
neutral elements (Trait: is_total). 27
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Implementation
Itl containers are implemented based on
std::set and std::map
Basic operations like adding and subtracting intervals or 
interval value pairs perform with a time complexity 
between* amortized O(log n)  and O(n), where n is the 
number of intervals of a container.
Operations like addition and subtraction of whole 
containers are having a worst case complexity of
O(m log(n+m)), where n and m are the numbers of 
intervals of the containers to combine.
* : Consult the library documentation for more detailed 
information.28
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Future Works
Implementing interval_maps of sets more efficiently
Revision of features of the extended itl (itl_plus.zip)
Decomposition of histories : k histories hk with attribute 
types A1, ..., Ak are “decomposed” to a product history 
of tuples of attribute sets:
(h1<T,A1>,..., h<T,Ak>) → h<T, (set<A1>,…, set<Ak>)>
Cubes (generalized crosstables): Applying aggregate 
on collision to maps of tuple value pairs  in order to 
organize hierachical data and their aggregates.29
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Availability
Itl project on sourceforge (version 2.0.1)
http://sourceforge.net/projects/itl
Latest version on boost vault/Containers  (3.2.0)
http://www.boostpro.com/vault/  → containers
itl_3_2_0.zip : Core itl in preparation for boost
itl_plus_3_2_0.zip : Extended itl including histories, cubes 
and automatic validation (LaBatea).
Online documentation  at
http://www.herold-faulhaber.de/
Doxygen generated docs for (version 2.0.1)
http://www.herold-faulhaber.de/itl/
Latest boost style documentation (version 3.2.0)
http://www.herold-faulhaber.de/boost_itl/doc/libs/itl/doc/html/30
Slide Design by Chih-Hao Tsai   http://www.chtsai.org
 Copyright © Joachim Faulhaber 2009Availability
Boost sandbox
https://svn.boost.org/svn/boost/sandbox/itl/
Core itl: Interval containers proposed for boost
https://svn.boost.org/svn/boost/sandbox/itl/boost/itl/
https://svn.boost.org/svn/boost/sandbox/itl/libs/itl/
Extended itl_xt: interval_bitset, “histories”, cubes
https://svn.boost.org/svn/boost/sandbox/itl/boost/itl_xt/
https://svn.boost.org/svn/boost/sandbox/itl/libs/itl_xt/
Validater LaBatea: 
Compiles with msvc-8.0 or newer, gcc-4.3.2 or newer
https://svn.boost.org/svn/boost/sandbox/itl/boost/validate/
https://svn.boost.org/svn/boost/sandbox/itl/libs/validate/