examples.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 Examples]
  8. [section Overview]
  9. [table Overview over Icl Examples
  10. [[level] [example] [classes] [features]]
  11. [[intro][[link boost_icl.examples.party Party]]
  12. [__itv_map__][Generates an attendance history of a party
  13. by inserting into an __itv_map__.
  14. Demonstrating
  15. ['*aggregate on overlap*].]]
  16. [[basic] [[link boost_icl.examples.interval Interval]]
  17. [__dc_itv__, __ct_itv__ ] [Intervals for discrete and continuous instance types.
  18. Closed and open interval borders.]]
  19. [[basic] [[link boost_icl.examples.dynamic_interval Dynamic intervals]]
  20. [__dc_itv__, __ct_itv__, __itv__ ] [Intervals with dynamic interval bounds as library default.]]
  21. [[basic] [[link boost_icl.examples.static_interval Static intervals]]
  22. [__ro_itv__, __itv__ ] [Intervals with static interval bounds and changing the library default.]]
  23. [[basic] [[link boost_icl.examples.interval_container Interval container]]
  24. [__itv_set__,\n__sep_itv_set__,\n__spl_itv_set__,\n__spl_itv_map__,\n__itv_map__]
  25. [Basic characteristics of interval containers.]]
  26. [[basic] [[link boost_icl.examples.overlap_counter Overlap counter]]
  27. [__itv_map__][The most simple application of an interval map:
  28. Counting the overlaps of added intervals.]]
  29. [[advanced][[link boost_icl.examples.partys_height_average Party's height average]]
  30. [__itv_map__][Using /aggregate on overlap/ a history of height averages of party guests is computed.
  31. Associated values are user defined class objects, that implement
  32. an appropriate `operator +=` for the aggregation.]]
  33. [[advanced][[link boost_icl.examples.partys_tallest_guests Party's tallest guests]]
  34. [__itv_map__,\n__spl_itv_map__]
  35. [Using /aggregate on overlap/ the heights of the party's tallest guests are computed.
  36. Associated values are aggregated via a maximum functor, that can
  37. be chosen as template parameter of an interval_map class template.]]
  38. [[advanced][[link boost_icl.examples.time_grids Time grids for months and weeks]]
  39. [__spl_itv_set__]
  40. [Shows how the ['*border preserving*]
  41. __spl_itv_set__ can be used to create time partitions where different
  42. periodic time intervals overlay each other.]]
  43. [[advanced][[link boost_icl.examples.man_power Man power]]
  44. [__itv_set__,\n__itv_map__]
  45. [Set style operations on __itv_sets__ and __itv_maps__ like union, difference
  46. and intersection can be used to obtain calculations in a flexible way. Example
  47. [*man_power] demonstrates such operations in the process of calculating the
  48. available man-power of a company in a given time interval.]]
  49. [[advanced][[link boost_icl.examples.user_groups User groups]][__itv_map__]
  50. [Example [*user_groups] shows how interval_maps can be unified or
  51. intersected to calculate desired information.]]
  52. [[and std] [[link boost_icl.examples.std_copy Std copy]]
  53. [__itv_map__][Fill interval containers using `std::copy`.]]
  54. [[and std] [[link boost_icl.examples.std_transform Std transform]]
  55. [__itv_map__,\n__sep_itv_set__][Fill interval containers from user defined objects using `std::transform`.]]
  56. [[customize] [[link boost_icl.examples.custom_interval Custom interval]]
  57. [__itv_tr__][Use interval containers with your own interval class types.]]
  58. ]
  59. [endsect]
  60. [section Party]
  61. [import ../example/boost_party_/boost_party.cpp]
  62. Example *party* demonstrates the possibilities of an interval map
  63. (__itv_map__ or __spl_itv_map__).
  64. An __itv_map__ maps intervals to a given content. In this case the
  65. content is a set of party guests represented by their name strings.
  66. As time goes by, groups of people join the party and leave later in the evening.
  67. So we add a time interval and a name set to the __itv_map__ for the attendance
  68. of each group of people, that come together and leave together.
  69. On every overlap of intervals, the corresponding name sets are accumulated. At
  70. the points of overlap the intervals are split. The accumulation of content
  71. is done via an operator += that has to be implemented
  72. for the content parameter of the __itv_map__.
  73. Finally the interval_map contains the history of attendance and all points in
  74. time, where the group of party guests changed.
  75. Party demonstrates a principle that we call
  76. ['*aggregate on overlap*]:
  77. On insertion a value associated to the interval is aggregated with those
  78. values in the interval_map that overlap with the inserted value.
  79. There are two behavioral aspects to ['*aggregate on overlap*]: a ['*decompositional
  80. behavior*] and an ['*accumulative behavior*].
  81. * The ['*decompositional behavior*] splits up intervals on the /time/ /dimension/ of the
  82. interval_map so that the intervals are split whenever associated values
  83. change.
  84. * The ['*accumulative behavior*] accumulates associated values on every overlap of
  85. an insertion for the associated values.
  86. The aggregation function is += by default. Different aggregations can
  87. be used, if desired.
  88. [example_boost_party]
  89. [caution
  90. We are introducing __itv_maps__ using an
  91. ['interval map ['*of sets of strings*]],
  92. because of it's didactic advantages. The party example is
  93. used to give an immediate access to the basic ideas of
  94. interval maps and /aggregate on overlap/.
  95. For real world applications, an interval_map of sets is
  96. not necessarily recommended.
  97. It has the same efficiency problems as
  98. a `std::map` of `std::sets`.
  99. There is a big realm though of using
  100. interval_maps with numerical and other
  101. efficient data types for the associated values.
  102. ]
  103. [endsect] [/ Party]
  104. [section Interval]
  105. Example *interval* shows some basic functionality of __itvs__.
  106. * Different instances of __itvs__ for integral ([^int, Time]) and
  107. continuous types ([^double, std::string]) are used.
  108. * The examples uses open and closed intervals bounds.
  109. * Some borderline functions calls on open interval borders are tested e.g.:
  110. `interval<double>::rightopen(1/sqrt(2.0), sqrt(2.0)).contains(sqrt(2.0));`
  111. [import ../example/interval_/interval.cpp]
  112. [example_interval]
  113. [endsect]
  114. [section Dynamic interval]
  115. [import ../example/dynamic_interval_/dynamic_interval.cpp]
  116. [example_dynamic_interval]
  117. [endsect]
  118. [section Static interval]
  119. [import ../example/static_interval_/static_interval.cpp]
  120. [example_static_interval]
  121. [endsect]
  122. [section Interval container]
  123. Example [*interval container] demonstrates the characteristic behaviors
  124. of different interval containers that are also summarized
  125. in the introductory [link boost_icl.introduction.interval_combining_styles Interval Combining Styles].
  126. [import ../example/interval_container_/interval_container.cpp]
  127. [example_interval_container]
  128. [endsect]
  129. [section Overlap counter]
  130. Example [*overlap counter] provides the simplest application
  131. of an interval_map that maps intervals to integers.
  132. An interval_map<int,int> serves as an overlap counter
  133. if we only add interval value pairs that carry 1 as
  134. associated value.
  135. Doing so, the associated values that are accumulated in
  136. the interval_map are just the number of overlaps of all added
  137. intervals.
  138. [import ../example/overlap_counter_/overlap_counter.cpp]
  139. [example_overlap_counter]
  140. [endsect]
  141. [section:partys_height_average Party's height average]
  142. In the example `partys_height_average.cpp` we compute yet another aggregation:
  143. The average height of guests. This is done by defining a `class counted_sum`
  144. that sums up heights and counts the number of guests
  145. via an `operator +=`.
  146. Based on the `operator +=` we can aggregate counted sums on addition
  147. of interval value pairs into an interval_map.
  148. [import ../example/partys_height_average_/partys_height_average.cpp]
  149. [example_partys_height_average]
  150. Required for `class counted_sum` is a default constructor
  151. `counted_sum()`and
  152. an `operator ==` to test equality.
  153. To enable additive aggregation on overlap also
  154. an `operator +=` is needed.
  155. Note that no `operator -=` for a subtraction of `counted_sum` values
  156. is defined. So you can only add to the
  157. `interval_map<ptime, counted_sum>`
  158. but not subtract from it.
  159. In many real world applications only addition is needed and user
  160. defined classes will work fine, if they only implement
  161. `operator +=`. Only if any of the `operator`s `-=` or `-`
  162. is called on the interval_map, the user defined class has to
  163. implement it's own `operator -=` to provide the subtractive
  164. aggregation on overlap.
  165. [endsect]
  166. [section:partys_tallest_guests Party's tallest guests]
  167. Defining `operator +=` (and `-=`) is probably the most important
  168. method to implement arbitrary kinds of user defined aggregations.
  169. An alternative way to choose a desired aggregation is to
  170. instantiate an interval_map class template with an
  171. appropriate ['*aggregation functor*]. For the most common
  172. kinds of aggregation the [*icl]
  173. provides such functors as shown in the example.
  174. Example `partys_tallest_guests.cpp` also demonstrates
  175. the difference between an __itv_map__
  176. that joins intervals for equal associated values and a
  177. __spl_itv_map__ that preserves all borders of inserted
  178. intervals.
  179. [import ../example/partys_tallest_guests_/partys_tallest_guests.cpp]
  180. [example_partys_tallest_guests]
  181. [endsect]
  182. [section:time_grids Time grids for months and weeks]
  183. A __spl_itv_set__ preserves all interval borders on insertion
  184. and intersection operations. So given a __spl_itv_set__ and an addition of an interval
  185. ``
  186. x = {[1, 3)}
  187. x.add( [2, 4))
  188. ``
  189. then the intervals are split at their borders
  190. ``
  191. x == {[1,2)[2,3)[3,4)}
  192. ``
  193. Using this property we can intersect __spl_itv_maps__ in
  194. order to iterate over intervals accounting for all occurring
  195. changes of interval borders.
  196. In this example we provide an intersection of two __spl_itv_sets__
  197. representing a month and week time grid.
  198. [import ../example/month_and_week_grid_/month_and_week_grid.cpp]
  199. [example_month_and_week_grid]
  200. [endsect]
  201. [section Man power]
  202. __Itv_sets__ and __itv_maps__ can be filled and manipulated using
  203. set style operations such as union `+=`, difference `-=` and
  204. intersection `&=`.
  205. In this example [*man power] a number of those operations are
  206. demonstrated in the process of calculation the available working
  207. times (man-power) of a company's employees accounting for weekends,
  208. holidays, sickness times and vacations.
  209. [import ../example/man_power_/man_power.cpp]
  210. [example_man_power]
  211. [endsect]
  212. [section User groups]
  213. Example [*user groups] shows the availability of set operations
  214. on __itv_maps__.
  215. In the example there is a user group `med_users` of a hospital staff
  216. that has the authorisation to handle medical data of patients.
  217. User group `admin_users` has access to administrative data like
  218. health insurance and financial data.
  219. The membership for each user in one of the user groups has a time
  220. interval of validity. The group membership begins and ends.
  221. * Using a union operation `+` we can have an overview over the unified
  222. user groups and the membership dates of employees.
  223. * Computing an intersection `&` shows who is member of both med_users
  224. and admin_users at what times.
  225. [import ../example/user_groups_/user_groups.cpp]
  226. [example_user_groups]
  227. [endsect]
  228. [section Std copy]
  229. The standard algorithm
  230. [@http://www.cplusplus.com/reference/algorithm/copy/ `std::copy`]
  231. can be used to fill interval containers
  232. from standard containers of intervals or
  233. interval value pairs (segments). Because intervals
  234. do not represent ['*elements*] but ['*sets*], that
  235. can be empty or contain more than one element,
  236. the usage of `std::copy` differs from what we
  237. are familiar with using ['containers of elements].
  238. * Use `icl::inserter` from `#include <boost/icl/iterator.hpp>`
  239. instead of `std::inserter` to call insertions on the target
  240. interval container.
  241. * As shown in the examples above and below this point, most of
  242. the time we will not be interested to `insert` segments
  243. into __itv_maps__ but to __biLadd__
  244. them, in order to generate the desired aggregation results.
  245. You can use `std::copy` with an `icl::adder` instead of an
  246. `icl::inserter` to achieve this.
  247. [import ../example/std_copy_/std_copy.cpp]
  248. [example_std_copy]
  249. [endsect][/ Std copy]
  250. [section Std transform]
  251. Instead of writing loops, the standard algorithm
  252. [@http://www.cplusplus.com/reference/algorithm/transform/ `std::transform`]
  253. can be used to fill interval containers
  254. from std containers of user defined objects.
  255. We need a function, that
  256. maps the ['user defined object] into the
  257. ['segement type] of an interval map or the
  258. ['interval type] of an interval set.
  259. Based on that we can use `std::transform`
  260. with an `icl::inserter` or `icl::adder`
  261. to transform the user objects into interval
  262. containers.
  263. [import ../example/std_transform_/std_transform.cpp]
  264. [example_std_transform]
  265. To get clear about the different behaviors of interval containers
  266. in the example, you may want to refer to the section about
  267. [link boost_icl.introduction.interval_combining_styles interval combining styles]
  268. that uses the same data.
  269. [/
  270. Instead of `icl::inserter` we could also use an `std::inserter`
  271. with algorithms `std::copy` and `std::transform`.
  272. This is ['*depreciated*], because `std::inserter` is designed for
  273. containers of elements, where ['*exacty one*] element is inserted
  274. via `std::inserter` if it is not already in the container.
  275. In contrast to that, an interval or segemnt can represent zero, one,
  276. or many elements of an interval container.
  277. You can use `std::inserter` *only*, if you actively take care of
  278. two preconditions:
  279. # None of the intervals or segements of the source containers
  280. must be empty.
  281. # A segment never carries a neutral element when your target
  282. interval map absorbs identities.
  283. Violating those preconditions leads to ['*undefined behavior*].
  284. ]
  285. [endsect][/ std::transform]
  286. [section Custom interval]
  287. Example *custom interval* demonstrates how to use interval
  288. containers with an own interval class type.
  289. [import ../example/custom_interval_/custom_interval.cpp]
  290. [example_custom_interval]
  291. [endsect][/ Custom interval]
  292. [endsect][/ examples]