1[/
2    Copyright (c) 2008-2010 Joachim Faulhaber
3
4    Distributed under the Boost Software License, Version 1.0.
5    (See accompanying file LICENSE_1_0.txt or copy at
6    http://www.boost.org/LICENSE_1_0.txt)
7]
8
9[section Introduction]
10
11[/ note [* The Interval Container Library is accepted into Boost]
12
13The [*Interval Container Library] (formerly /Interval Template Library/) underwent
14a formal review on the boost developer's list
15from February 18, 2010 to March 08, 2010 and has been accepted
16by declaration of review manager Hartmut Kaiser
17into the boost library collection on April 18, 2010.
18
19
20The library has been refactored for the suggestions and requests
21that came up during the review. The current version is now
22ready for inclusion into the next boost release.
23The name ['*Interval Template Library (ITL)*]
24has been changed to ['*Interval Container Library (ICL)*].
25
26If you find bugs in the library or typos or other shortcomings in
27the documentation please send reports to the boost developers list
28boost@lists.boost.org
29the boost users list or
30boost-users@lists.boost.org
31to `[afojgo<AT>gmail dot com]`.
32
33]
34
35["A bug crawls across the boost docs on my laptop screen.
36Let him be! We need all the readers we can get.] --
37Freely adapted from [@http://en.wikipedia.org/wiki/Jack_Kornfield Jack Kornfield]
38
39Intervals are almost ubiquitous in software development. Yet they are
40very easily coded into user defined classes by a pair of numbers
41so they are only /implicitly/ used most of the time. The meaning of
42an interval is simple. They represent all the elements between their
43lower and upper bound and thus a set. But unlike sets, intervals
44usually can not be added to a single new interval.
45If you want to add intervals to a collection of
46intervals that does still represent a /set/,
47you arrive at the idea of /interval_sets/ provided by this library.
48
49Interval containers of the *ICL* have been developed initially at
50[@http://www.cortex-software.de/desktopdefault.aspx Cortex Software GmbH]
51to solve problems related to date and time interval
52computations in the context of a Hospital Information System.
53Time intervals with associated values like ['amount of invoice]
54or ['set of therapies] had to be manipulated in statistics,
55billing programs and therapy scheduling programs.
56So the *ICL* emerged out of those industrial use cases.
57It extracts generic code that helps to solve common
58problems from the date and time problem domain and can be
59beneficial in other fields as well.
60
61One of the most advantageous aspects of interval containers is
62their very compact representation of sets and maps. Working with
63sets and maps /of elements/ can be very inefficient, if in a given
64problem domain, elements are typically occurring in contiguous
65chunks.
66Besides a compact representation of associative containers, that
67can reduce the cost of space and time drastically, the ICL
68comes with a universal mechanism of aggregation, that allows
69to combine associated values in meaningful ways when intervals
70overlap on insertion.
71
72For a condensed introduction and overview you may want to look at the
73[@http://www.herold-faulhaber.de/boost_icl/doc/libs/icl/doc/boostcon09/intro_to_itl.pdf
74presentation material on the *ICL* from ['*BoostCon2009*]].
75
76
77[section Definition and Basic Example]
78
79The [*Interval Container Library (ICL)] provides __itvs__ and two kinds of
80interval containers: __itv_sets__ and __itv_maps__.
81
82* An __itv_bset__ is a *set* that is implemented as a set of intervals.
83* An __itv_bmap__ is a *map* that is implemented as a map of interval value pairs.
84
85[h4 Two Aspects]
86
87__Itv_bsets__ and __itv_bmaps__ expose two different aspects in
88their interfaces: (1) The functionality of a set or a map, which is the more
89['*abstract aspect*]. And (2) the functionality of a container of intervals which
90is the more specific and ['*implementation related aspect*]. In practice both
91aspects are useful and are therefore supported.
92
93The first aspect, that will be called __bi_conceptual__ ['*aspect*], is the more important one.
94It means that we can use an __itv_set__ or __itv_map__ like a
95set or map ['*of elements*]. It exposes the same functions.
96``
97interval_set<int> mySet;
98mySet.insert(42);
99bool has_answer = contains(mySet, 42);
100``
101
102
103The second aspect, that will be called __bi_iterative__ ['*aspect*], allows to exploit the
104fact, that the elements of __itv_sets__ and __itv_maps__ are clustered in
105['*intervals*] or ['*segments*] that we can iterate over.
106
107``
108// Switch on my favorite telecasts using an interval_set
109interval<seconds>::type news(make_seconds("20:00:00"), make_seconds("20:15:00"));
110interval<seconds>::type talk_show(make_seconds("22:45:30"), make_seconds("23:30:50"));
111interval_set<seconds> myTvProgram;
112myTvProgram.add(news).add(talk_show);
113
114// Iterating over elements (seconds) would be silly ...
115for(interval_set<seconds>::iterator telecast = myTvProgram.begin();
116    telecast != myTvProgram.end(); ++telecast)
117    //...so this iterates over intervals
118   TV.switch_on(*telecast);
119``
120
121Working with __itv_bsets__ and __itv_bmaps__ can be
122beneficial whenever the elements of
123sets appear in contiguous chunks: __itvs__. This is obviously the
124case in many problem domains, particularly in fields that deal with
125computations related to date and time.
126
127[h4 Addabitlity and Subtractability]
128
129Unlike `std::sets` and `maps`, __itv_bsets__ and __itv_bmaps__ implement
130concept `Addable` and `Subtractable`. So __itv_bsets__ define an
131`operator +=` that is naturally implemented as ['*set union*] and an
132`operator -=` that is consequently implemented as ['*set difference*].
133In the *Icl* __itv_bmaps__ are addable and subtractable as well.
134It turned out to be a very fruitful concept to propagate the
135addition or subtraction to the __itv_bmap_s__ associated values
136in cases where the insertion of an interval value pair into an
137__itv_map__ resulted in a collision of the inserted interval
138value pair with interval value pairs, that are already in the
139__itv_map__. This operation propagation is called ['*aggregate on overlap*].
140
141
142[h4 Aggregate on Overlap]
143
144This is a first motivating example of a very small party, demonstrating the
145['*aggregate on overlap*] principle on __itv_maps__:
146
147In the example Mary enters the party first. She attends during the
148time interval `[20:00,22:00)`. Harry enters later. He stays within `[21:00,23:00)`.
149``
150typedef std::set<string> guests;
151interval_map<time, guests> party;
152party += make_pair(interval<time>::right_open(time("20:00"), time("22:00")), guests("Mary"));
153party += make_pair(interval<time>::right_open(time("21:00"), time("23:00")), guests("Harry"));
154// party now contains
155[20:00, 21:00)->{"Mary"}
156[21:00, 22:00)->{"Harry","Mary"} //guest sets aggregated on overlap
157[22:00, 23:00)->{"Harry"}
158``
159['*On overlap of intervals*], the corresponding name sets are ['*accumulated*]. At
160the ['*points of overlap*] the intervals are ['*split*]. The accumulation of content on
161overlap of intervals is done via an `operator +=` that has to be implemented
162for the associated values of the __itv_map__. In the example
163the associated values are `guest sets`. Thus a `guest set` has to implement
164`operator +=` as set union.
165
166As can be seen from the example an __itv_map__ has both
167a ['*decompositional behavior*] (on the time dimension) as well as
168an ['*accumulative one*] (on the associated values).
169
170Addability and aggregate on overlap are useful features on
171__itv_maps__ implemented via function `add` and `operator +=`.
172But you can also use them with the ['traditional]
173[link boost_icl.function_reference.insertion insert semantics]
174that behaves like `std::map::insert` generalized for
175interval insertion.
176
177[endsect]
178
179[section Icl's class templates]
180
181In addition to interval containers we can work with
182containers of elements that are ['*behavioral equal*]
183to the interval containers: On the fundamental aspect
184they have exactly the same functionality.
185An __std_set__ of the STL is such an equivalent set implementation.
186Due to the aggregation facilities of the icl's interval maps
187__std_map__ is fundamentally not completely equivalent to an __itv_map__.
188Therefore there is an extra __icl_map__ class template for maps of
189elements in the icl.
190
191
192* The __std_set__ is behavioral equal to __itv_bsets__ on the __bi_conceptual__ aspect.
193
194* An __icl_map__ is behavioral equal to __itv_bmaps__ on the __bi_conceptual__  aspect.
195  Specifically an __icl_map__
196  implements ['*aggregate on overlap*], which is
197  named ['*aggregate on collision*] for an element container.
198
199The following tables give an overview over the main
200class templates provided by the *icl*.
201
202[table Interval class templates
203[[group]              [form]      [template]          ]
204[[statically bounded] [asymmetric][__ro_itv__]        ]
205[[ ]                  []          [__lo_itv__]        ]
206[[ ]                  [symmetric] [__cl_itv__]        ]
207[[ ]                  []          [__op_itv__]        ]
208[[dynamically bounded][]          [__dc_itv__]        ]
209[[ ]                  []          [__ct_itv__]        ]
210]
211
212Statically bounded intervals always have the same kind of interval borders,
213e.g. right open borders`[a..b)` for __ro_itv__. Dynamically bounded intervals
214can have different borders. Refer to the chapter about
215[link boost_icl.interface.class_templates.intervals ['*intervals*]]
216for details.
217
218[table Container class templates
219[[granularity][style]     [sets]           [maps]           ]
220[[interval]   [joining]   [__itv_set__]    [__itv_map__]    ]
221[[]           [separating][__sep_itv_set__][]               ]
222[[]           [splitting] [__spl_itv_set__][__spl_itv_map__]]
223[[element]    []          [(__ele_set__)]  [__ele_map__]    ]
224]
225
226__Std_set__ is placed in paretheses, because it is not a class template
227of the *ICL*. It can be used as element container though that is
228behavioral equal to the ICL's interval sets on their fundamental aspect.
229Column ['*style*] refers to
230the different ways in which interval containers combine added
231intervals. These ['*combining styles*] are described in the next
232section.
233
234
235[endsect]
236
237[section Interval Combining Styles]
238
239When we add intervals or interval value pairs to interval containers,
240the intervals can be added in different ways: Intervals can be
241joined or split or kept separate. The different interval combining
242styles are shown by example in the tables below.
243
244[table Interval container's ways to combine intervals
245    [[]         [joining]       [separating]            [splitting]]
246    [[set]      [[classref boost::icl::interval_set          interval_set]]
247                [[classref boost::icl::separate_interval_set separate_interval_set]]
248                [[classref boost::icl::split_interval_set    split_interval_set]]]
249    [[map]      [[classref boost::icl::interval_map          interval_map]]
250                []
251                [[classref boost::icl::split_interval_map    split_interval_map]]]
252    [[]         [Intervals are joined on overlap or touch\n(if associated values are equal).]
253                [Intervals are joined on overlap, not on touch.]
254                [Intervals are split on overlap.\nAll interval borders are preserved.]]
255]
256
257[table Interval combining styles by example
258    [[]         [joining]       [separating]            [splitting]]
259    [[set]      [[classref boost::icl::interval_set          interval_set] /A/]
260                [[classref boost::icl::separate_interval_set separate_interval_set] /B/]
261                [[classref boost::icl::split_interval_set    split_interval_set] /C/]]
262[[]
263[``
264  {[1      3)          }
265+       [2      4)
266+                 [4 5)
267= {[1                5)}``]
268[``
269  {[1      3)}         }
270+       [2      4)
271+                 [4 5)
272= {[1           4)[4 5)}``]
273[``
274  {[1      3)          }
275+       [2      4)
276+                 [4 5)
277= {[1 2)[2 3)[3 4)[4 5)}``]
278]
279
280    [[map]      [[classref boost::icl::interval_map          interval_map] /D/]
281                []
282                [[classref boost::icl::split_interval_map    split_interval_map] /E/]]
283
284[[]
285[``
286  {[1      3)->1          }
287+       [2      4)->1
288+                 [4 5)->1
289= {[1 2)[2 3)[3      5)   }
290  | ->1  ->2     ->1      |``]
291[]
292[``
293  {[1      3)->1          }
294+       [2      4)->1
295+                 [4 5)->1
296= {[1 2)[2 3)[3 4)[4 5)   }
297  | ->1  ->2  ->1  ->1    |``]
298]
299]
300
301Note that =interval_sets= /A/, /B/ and /C/ represent the same set of elements ={1,2,3,4}=
302and =interval_maps= /D/ and /E/ represent the same map of elements [^{1->1, 2->2, 3->1, 4->1}].
303See example program [link boost_icl.examples.interval_container Interval container]
304for an additional demo.
305
306[h4 Joining interval containers]
307
308__Itv_set__ and __itv_map__ are always
309in a ['*minimal representation*]. This is useful in many cases, where the
310points of insertion or intersection of intervals are not relevant. So in most
311instances __itv_set__ and
312__itv_map__ will be the first choice
313for an interval container.
314
315[h4 Splitting interval containers]
316
317__Spl_itv_set__ and __spl_itv_map__ on the contrary
318have an ['*insertion memory*]. They do accumulate interval borders both
319from additions and intersections. This is specifically useful, if we want
320to enrich an interval container with certain time grids, like e.g. months
321or calendar weeks or both. See example [link boost_icl.examples.time_grids time grids for months and weeks].
322
323[h4 Separating interval containers]
324
325__Sep_itv_set__ implements the separating style.
326This style preserves borders, that are never passed by an overlapping
327interval. So if all intervals that are inserted into a __sep_itv_set__
328are generated form a certain grid that never pass say month borders, then
329these borders are preserved in the __sep_itv_set__.
330
331[endsect]
332
333[endsect][/ Introduction]
334
335
336