introduction.qbk 14 KB

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