functions_addition.qbk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. [/
  2. Copyright (c) 2008-2009 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. [/ //= Addition ===============================================================]
  8. [section Addition]
  9. [section Synopsis]
  10. [table
  11. [[Addition] [__ch_itv_sets__][__ch_itv_maps__][__ch_ele_sets__][__ch_ele_maps__] ]
  12. [[`T& T::add(const P&)`] [__ei] [__bp] [ ] [__b] ]
  13. [[`T& add(T&, const P&)`] [__ei] [__bp] [__e] [__b] ]
  14. [[`J T::add(J pos, const P&)`] [__i] [__p] [ ] [__b] ]
  15. [[`J add(T&, J pos, const P&)`] [__i] [__p] [__e] [__b] ]
  16. [[`T& operator +=(T&, const P&)`] [__eiS] [__bpM] [__es] [__bm] ]
  17. [[`T operator + (T, const P&)`\n
  18. `T operator + (const P&, T)`]
  19. [__eiS] [__bpM] [__es] [__bm] ]
  20. [[`T& operator |=( T&, const P&)`] [__eiS] [__bpM] [__es] [__bm] ]
  21. [[`T operator | (T, const P&)`\n
  22. `T operator | (const P&, T)`]
  23. [__eiS] [__bpM] [__es] [__bm] ]
  24. ]
  25. Functions and operators that implement ['*Addition*] on *icl* objects
  26. are given in the table above.
  27. `operator |=` and `operator |` are behavioral identical to
  28. `operator +=` and `operator +`.
  29. This is a redundancy that has been introduced deliberately, because
  30. a /set union/ semantics is often attached `operators |=` and `|`.
  31. [table
  32. [[] [Description of Addition]]
  33. [[`Sets`][Addition on Sets implements ['*set union*]]]
  34. [[`Maps`][Addition on Maps implements a ['*map union*] function similar to /set union/.
  35. If, on insertion of an element value pair `(k,v)` it's key `k` is in the map
  36. already, the addition function is propagated to the associated value.
  37. This functionality has been introduced as /aggregate on collision/
  38. for element maps and /aggregate on overlap/ for interval maps.
  39. Find more on
  40. [link boost_icl.concepts.aggrovering ['addability of maps]]
  41. and related
  42. [link boost_icl.semantics.maps ['semantic issues]]
  43. following the links.
  44. Examples, demonstrating Addition on interval containers are
  45. [link boost_icl.examples.overlap_counter ['overlap counter]],
  46. [link boost_icl.examples.party ['party]] and
  47. [link boost_icl.examples.partys_height_average ['party's height average.]]
  48. ]]
  49. ]
  50. For `Sets` ['*addition*] and ['*insertion*] are implemented identically.
  51. Functions `add` and `insert` collapse to the same function.
  52. For `Maps` ['*addition*] and ['*insertion*] work differently.
  53. Function `add` performs aggregations on collision or overlap,
  54. while function `insert` only inserts values that do not yet have key values.
  55. [endsect][/ Synopsis]
  56. [section Functions][/ Addition]
  57. The admissible combinations of types for member function
  58. `T& T::add(const P&)` can be summarized in the
  59. ['*overload table*] below:
  60. ``
  61. // overload table for T\P| e i b p
  62. T& T::add(const P&) ---+--------
  63. T& add(T&, const P&) s | s
  64. m | m
  65. S | S S
  66. M | M M
  67. ``
  68. The next table contains complexity characteristics for `add`.
  69. [table Time Complexity for member function add on icl containers
  70. [[`T& T::add(const P&)`\n
  71. `T& add(T&, const P&)`] [__ch_dom_t__][__ch_itv_t__][__ch_dom_mp_t__][__ch_itv_mp_t__]]
  72. [[__icl_set__] [__Olgn__] [] [] [] ]
  73. [[__icl_map__] [] [] [__Olgn__][] ]
  74. [[__itv_set__\n__sep_itv_set__][__Olgn__] [__a_Olgn__][] [] ]
  75. [[__spl_itv_set__] [__Olgn__] [__On__] [] [] ]
  76. [[__itv_map__\n__spl_itv_map__][] [] [__Olgn__][__On__] ]
  77. ]
  78. [h5 Hinted addition]
  79. Function `J T::add(J prior, const P& addend)` allows
  80. for an addition in ['*constant time*], if `addend` can be inserted
  81. right after iterator `prior` without collision. If this is not possible
  82. the complexity characteristics are as stated for the non hinted addition
  83. above. Hinted addition is available for these combinations of types:
  84. ``
  85. // overload table for addition with hint T\P| e i b p
  86. J T::add(J prior, const P&) ---+--------
  87. J add(T&, J prior, const P&) s | s
  88. m | m
  89. S | S
  90. M | M
  91. ``
  92. [endsect][/ Functions Addition]
  93. [section Inplace operators]
  94. The possible overloads of inplace `T& operator += (T&, const P&)`
  95. are given by two tables, that show admissible combinations of types.
  96. Row types show instantiations of argument type `T`. Columns types
  97. show show instantiations of argument type `P`. If a combination
  98. of argument types is possible, the related table cell contains
  99. the result type of the operation.
  100. __eiS_Phs__ __eibpsSmM__ will be used to denote
  101. /elements, intervals,
  102. element value pairs, interval value pairs,
  103. element sets, interval sets,
  104. element maps/ and /interval maps/.
  105. The first table shows the
  106. overloads of `+=` for /element containers/ the second table
  107. refers to /interval containers/.
  108. ``
  109. // overload tables for element containers: interval containers:
  110. T& operator += (T&, const P&) += | e b s m += | e i b p S M
  111. ---+-------- ---+------------
  112. s | s s S | S S S
  113. m | m m M | M M M
  114. ``
  115. For the definition of admissible overloads
  116. we separate /element containers/ from /interval containers/.
  117. Within each group all combinations of types are supported
  118. for an operation, that are in line with the *icl's*
  119. design and the sets of laws, that establish the *icl's*
  120. [link boost_icl.semantics semantics].
  121. Overloads between /element containers/ and /interval containers/
  122. could also be defined. But this has not been done for
  123. pragmatical reasons: Each additional combination of types
  124. for an operation enlarges the space of possible overloads.
  125. This makes the overload resolution by compilers more complex,
  126. error prone and slows down compilation speed. Error messages
  127. for unresolvable or ambiguous overloads are difficult
  128. to read and understand. Therefore overloading of namespace
  129. global functions in the *icl* are limited to a reasonable
  130. field of combinations, that are described here.
  131. [endsect][/ Inplace operators]
  132. [h4 Complexity]
  133. For different combinations of argument types `T` and `P`
  134. different implementations of the `operator +=` are
  135. selected. These implementations show different complexity
  136. characteristics.
  137. If `T` is a container type,
  138. the combination of
  139. domain elements (__e) or element value pairs (__b)
  140. is faster than a combination of intervals (__i) or
  141. interval value pairs (__p) which in turn is faster than
  142. the combination of element or interval containers.
  143. The next table shows /time complexities/ of addition for
  144. *icl's* element containers.
  145. Sizes `n` and `m` are in the complexity statements are sizes
  146. of objects `T y` and `P x`:
  147. ``
  148. n = iterative_size(y);
  149. m = iterative_size(x); //if P is a container type
  150. ``
  151. Note, that for an interval container the number of elements `T::size` is
  152. different from the number of intervals that you can iterate over.
  153. Therefore a function `T::iterative_size()` is used that provides the
  154. desired kind of size.
  155. [table Time Complexity for inplace Addition on element containers
  156. [[`T& operator += (T& y, const P& x)`][__ch_dom_t__][__ch_dom_mp_t__][__ch_icl_sets__][__ch_icl_maps__]]
  157. [[__icl_set__] [__Olgn__] [] [__Om__] [] ]
  158. [[__icl_map__] [] [__Olgn__] [] [__Om__] ]
  159. ]
  160. Time complexity characteristics of inplace addition for interval containers
  161. is given by this table.
  162. [table Time Complexity for inplace Addition on interval containers
  163. [[`T& operator += (T& y, const P& x)`][][__ch_dom_t__][__ch_itv_t__][__ch_dom_mp_t__][__ch_itv_mp_t__][__ch_itv_sets__][__ch_itv_maps__]]
  164. [[interval_sets][__itv_set__\n__sep_itv_set__][__Olgn__] [__a_Olgn__][] [] [__Omlgnpm__] [] ]
  165. [[] [__spl_itv_set__] [__Olgn__] [__On__] [] [] [__Omlgnpm__] [] ]
  166. [[interval_maps][] [] [] [__Olgn__][__On__] [] [__Omlgnpm__] ]
  167. ]
  168. Since the implementation of
  169. element and interval containers is based on the
  170. [link boost_icl.implementation link red-black tree implementation]
  171. of std::AssociativeContainers, we have a logarithmic complexity for
  172. addition of elements.
  173. Addition of intervals or interval value pairs is amortized logarithmic
  174. for __itv_sets__ and __sep_itv_sets__ and linear for __spl_itv_sets__
  175. and __itv_maps__.
  176. Addition is linear for element containers and
  177. loglinear for interval containers.
  178. [section Infix operators]
  179. The admissible type combinations for infix `operator +`
  180. are defined by the overload tables below.
  181. ``
  182. // overload tables for element containers: interval containers:
  183. T operator + (T, const P&) + | e b s m + | e i b p S1 S2 S3 M1 M3
  184. T operator + (const P&, T) ---+-------- ---+---------------------------
  185. e | s e | S1 S2 S3
  186. b | m i | S1 S2 S3
  187. s | s s b | M1 M3
  188. m | m m p | M1 M3
  189. S1 | S1 S1 S1 S2 S3
  190. S2 | S2 S2 S2 S2 S3
  191. S3 | S3 S3 S3 S3 S3
  192. M1 | M1 M1 M1 M3
  193. M3 | M3 M3 M3 M3
  194. ``
  195. [endsect][/ Infix operators]
  196. ['*See also . . .*]
  197. [table
  198. []
  199. [[[link boost_icl.function_reference.subtraction ['*Subtraction*]] ]]
  200. [[[link boost_icl.function_reference.insertion ['*Insertion*]] ]]
  201. ]
  202. ['*Back to section . . .*]
  203. [table
  204. []
  205. [[[link function_synopsis_table ['*Function Synopsis*]] ]]
  206. [[[link boost_icl.interface ['*Interface*]] ]]
  207. ]
  208. [endsect][/ Addition]