concepts.qbk 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. [/
  2. Copyright 2010 Neil Groves
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. /]
  6. [section:concepts Range Concepts]
  7. [section Overview]
  8. A Range is a [*/concept/] similar to the STL [@http://www.sgi.com/tech/stl/Container.html Container] concept. A Range provides iterators for accessing a half-open range `[first,one_past_last)` of elements and provides information about the number of elements in the Range. However, a Range has fewer requirements than a Container.
  9. The motivation for the Range concept is that there are many useful Container-like types that do not meet the full requirements of Container, and many algorithms that can be written with this reduced set of requirements. In particular, a Range does not necessarily
  10. * own the elements that can be accessed through it,
  11. * have copy semantics,
  12. Because of the second requirement, a Range object must be passed by (const or non-const) reference in generic code.
  13. The operations that can be performed on a Range is dependent on the [@boost:/libs/iterator/doc/new-iter-concepts.html#iterator-traversal-concepts-lib-iterator-traversal traversal category] of the underlying iterator type. Therefore the range concepts are named to reflect which traversal category its iterators support. See also terminology and style guidelines. for more information about naming of ranges.
  14. The concepts described below specifies associated types as [@boost:/libs/mpl/doc/refmanual/metafunction.html metafunctions] and all functions as free-standing functions to allow for a layer of indirection.
  15. [endsect]
  16. [section Single Pass Range]
  17. [heading Notation]
  18. [table
  19. []
  20. [[`X`] [A type that is a model of __single_pass_range__.]]
  21. [[`a`] [Object of type X.]]
  22. ]
  23. [heading Description]
  24. A range `X` where `boost::range_iterator<X>::type` is a model of __single_pass_iterator__.
  25. [heading Associated types]
  26. [table
  27. []
  28. [[Iterator type ] [`boost::range_iterator<X>::type` ] [The type of iterator used to iterate through a Range's elements. The iterator's value type is expected to be the Range's value type. A conversion from the iterator type to the `const` iterator type must exist.]]
  29. [[Const iterator type] [`boost::range_iterator<const X>::type`] [A type of iterator that may be used to examine, but not to modify, a Range's elements.]]
  30. ]
  31. [heading Valid expressions]
  32. The following expressions must be valid.
  33. [table
  34. [[Name ] [Expression ] [Return type ]]
  35. [[Beginning of range] [`boost::begin(a)`] [`boost::range_iterator<X>::type` if `a` is mutable, `boost::range_iterator<const X>::type` otherwise]]
  36. [[End of range ] [`boost::end(a)` ] [`boost::range_iterator<X>::type` if `a` is mutable, `boost::range_iterator<const X>::type` otherwise]]
  37. ]
  38. [heading Expression semantics]
  39. [table
  40. [[Expression ] [Semantics ] [Postcondition]]
  41. [[`boost::begin(a)`] [Returns an iterator pointing to the first element in the Range. ] [`boost::begin(a)` is either dereferenceable or past-the-end. It is past-the-end if and only if `boost::distance(a) == 0`.]]
  42. [[`boost::end(a)` ] [Returns an iterator pointing one past the last element in the Range. ] [`boost::end(a)` is past-the-end.]]
  43. ]
  44. [heading Complexity guarantees]
  45. `boost::end(a)` is at most amortized linear time, `boost::begin(a)` is amortized constant time. For most practical purposes, one can expect both to be amortized constant time.
  46. [heading Invariants]
  47. [table
  48. []
  49. [[Valid range ] [For any Range `a`, `[boost::begin(a),boost::end(a))` is a valid range, that is, `boost::end(a)` is reachable from `boost::begin(a)` in a finite number of increments.]]
  50. [[Completeness] [An algorithm that iterates through the range `[boost::begin(a),boost::end(a))` will pass through every element of `a`.]]
  51. ]
  52. [heading See also]
  53. __extending_for_udts__
  54. __implementation_of_metafunctions__
  55. __implementation_of_functions__
  56. __container__
  57. [endsect]
  58. [section Forward Range]
  59. [heading Notation]
  60. [table
  61. []
  62. [[`X`] [A type that is a model of __forward_range__.]]
  63. [[`a`] [Object of type X.]]
  64. ]
  65. [heading Description]
  66. A range `X` where `boost::range_iterator<X>::type` is a model of __forward_traversal_iterator__.
  67. [heading Refinement of]
  68. __single_pass_range__
  69. [heading Associated types]
  70. [table
  71. []
  72. [[Distance type] [`boost::range_difference<X>::type`] [A signed integral type used to represent the distance between two of the Range's iterators. This type must be the same as the iterator's distance type.]]
  73. [[Size type ] [`boost::range_size<X>::type` ] [An unsigned integral type that can represent any nonnegative value of the Range's distance type.]]
  74. ]
  75. [heading See also]
  76. __implementation_of_metafunctions__
  77. __implementation_of_functions__
  78. [endsect]
  79. [section Bidirectional Range]
  80. [heading Notation]
  81. [table
  82. []
  83. [[`X`] [A type that is a model of __bidirectional_range__.]]
  84. [[`a`] [Object of type X.]]
  85. ]
  86. [heading Description]
  87. This concept provides access to iterators that traverse in both directions (forward and reverse). The `boost::range_iterator<X>::type` iterator must meet all of the requirements of __bidirectional_traversal_iterator__.
  88. [heading Refinement of]
  89. __forward_range__
  90. [heading Associated types]
  91. [table
  92. []
  93. [[Reverse Iterator type ] [`boost::range_reverse_iterator<X>::type` ] [The type of iterator used to iterate through a Range's elements in reverse order. The iterator's value type is expected to be the Range's value type. A conversion from the reverse iterator type to the const reverse iterator type must exist.]]
  94. [[Const reverse iterator type] [`boost::range_reverse_iterator<const X>::type`] [A type of reverse iterator that may be used to examine, but not to modify, a Range's elements.]]
  95. ]
  96. [heading Valid expressions]
  97. [table
  98. [[Name ] [Expression ] [Return type] [Semantics]]
  99. [[Beginning of range] [`boost::rbegin(a)`] [`boost::range_reverse_iterator<X>::type` if `a` is mutable `boost::range_reverse_iterator<const X>::type` otherwise.] [Equivalent to `boost::range_reverse_iterator<X>::type(boost::end(a))`.]]
  100. [[End of range ] [`boost::rend(a)` ] [`boost::range_reverse_iterator<X>::type` if `a` is mutable, `boost::range_reverse_iterator<const X>::type` otherwise.] [Equivalent to `boost::range_reverse_iterator<X>::type(boost::begin(a))`.]]
  101. ]
  102. [heading Complexity guarantees]
  103. `boost::rbegin(a)` has the same complexity as `boost::end(a)` and `boost::rend(a)` has the same complexity as `boost::begin(a)` from __forward_range__.
  104. [heading Invariants]
  105. [table
  106. []
  107. [[Valid reverse range] [For any Bidirectional Range a, `[boost::rbegin(a),boost::rend(a))` is a valid range, that is, `boost::rend(a)` is reachable from `boost::rbegin(a)` in a finite number of increments.]]
  108. [[Completeness ] [An algorithm that iterates through the range `[boost::rbegin(a),boost::rend(a))` will pass through every element of `a`.]]
  109. ]
  110. [heading See also]
  111. __implementation_of_metafunctions__
  112. __implementation_of_functions__
  113. [endsect]
  114. [section Random Access Range]
  115. [heading Description]
  116. A range `X` where `boost::range_iterator<X>::type` is a model of __random_access_traversal_iterator__.
  117. [heading Refinement of]
  118. __bidirectional_range__
  119. [heading Valid expressions]
  120. [table
  121. [[Name ] [Expression ] [Return type ]]
  122. [[Size of range] [`boost::size(a)`] [`boost::range_size<X>::type`]]
  123. ]
  124. [heading Expression semantics]
  125. [table
  126. [[Expression ] [Semantics] [Postcondition]]
  127. [[`boost::size(a)`] [Returns the size of the Range, that is, its number of elements. Note `boost::size(a) == 0u` is equivalent to `boost::empty(a)`.] [`boost::size(a) >= 0`]]
  128. ]
  129. [heading Complexity guarantees]
  130. `boost::size(a)` completes in amortized constant time.
  131. [heading Invariants]
  132. [table
  133. []
  134. [[Range size] [`boost::size(a)` is equal to the `boost::end(a)` - `boost::begin(a)`.]]
  135. ]
  136. [endsect]
  137. [section Concept Checking]
  138. Each of the range concepts has a corresponding concept checking class in the file [@boost:/boost/range/concepts.hpp `<boost/range/concepts.hpp>`]. These classes may be used in conjunction with the __concept_check__ to ensure that the type of a template parameter is compatible with a range concept. If not, a meaningful compile time error is generated. Checks are provided for the range concepts related to iterator traversal categories. For example, the following line checks that the type `T` models the __forward_range__ concept.
  139. ``
  140. BOOST_CONCEPT_ASSERT(( ForwardRangeConcept<T> ));
  141. ``
  142. An additional concept check is required for the value access property of the range based on the range's iterator type. For example to check for a ForwardReadableRange, the following code is required.
  143. ``
  144. BOOST_CONCEPT_ASSERT(( ForwardRangeConcept<T> ));
  145. BOOST_CONCEPT_ASSERT(( ReadableIteratorConcept<typename range_iterator<T>::type> ));
  146. ``
  147. The following range concept checking classes are provided.
  148. * Class SinglePassRangeConcept checks for __single_pass_range__
  149. * Class ForwardRangeConcept checks for __forward_range__
  150. * Class BidirectionalRangeConcept checks for __bidirectional_range__
  151. * Class RandomAccessRangeConcept checks for __random_access_range__
  152. [heading See also]
  153. [link range.style_guide Range Terminology and style guidelines]
  154. __iterator_concepts__
  155. __concept_check__
  156. [endsect]
  157. [endsect]