set_algorithm.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_SET_ALGORITHM_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_SET_ALGORITHM_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <algorithm>
  16. namespace boost
  17. {
  18. namespace range
  19. {
  20. /// \brief template function includes
  21. ///
  22. /// range-based version of the includes std algorithm
  23. ///
  24. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  25. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  26. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  27. template<class SinglePassRange1, class SinglePassRange2>
  28. inline bool includes(const SinglePassRange1& rng1,
  29. const SinglePassRange2& rng2)
  30. {
  31. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  32. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  33. return std::includes(boost::begin(rng1),boost::end(rng1),
  34. boost::begin(rng2),boost::end(rng2));
  35. }
  36. /// \overload
  37. template<class SinglePassRange1, class SinglePassRange2,
  38. class BinaryPredicate>
  39. inline bool includes(const SinglePassRange1& rng1,
  40. const SinglePassRange2& rng2,
  41. BinaryPredicate pred)
  42. {
  43. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  44. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  45. return std::includes(boost::begin(rng1), boost::end(rng1),
  46. boost::begin(rng2), boost::end(rng2), pred);
  47. }
  48. /// \brief template function set_union
  49. ///
  50. /// range-based version of the set_union std algorithm
  51. ///
  52. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  53. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  54. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  55. template<class SinglePassRange1, class SinglePassRange2,
  56. class OutputIterator>
  57. inline OutputIterator set_union(const SinglePassRange1& rng1,
  58. const SinglePassRange2& rng2,
  59. OutputIterator out)
  60. {
  61. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  62. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  63. return std::set_union(boost::begin(rng1), boost::end(rng1),
  64. boost::begin(rng2), boost::end(rng2), out);
  65. }
  66. /// \overload
  67. template<class SinglePassRange1, class SinglePassRange2,
  68. class OutputIterator, class BinaryPredicate>
  69. inline OutputIterator set_union(const SinglePassRange1& rng1,
  70. const SinglePassRange2& rng2,
  71. OutputIterator out,
  72. BinaryPredicate pred)
  73. {
  74. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  75. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  76. return std::set_union(boost::begin(rng1), boost::end(rng1),
  77. boost::begin(rng2), boost::end(rng2), out, pred);
  78. }
  79. /// \brief template function set_intersection
  80. ///
  81. /// range-based version of the set_intersection std algorithm
  82. ///
  83. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  84. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  85. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  86. template<class SinglePassRange1, class SinglePassRange2,
  87. class OutputIterator>
  88. inline OutputIterator set_intersection(const SinglePassRange1& rng1,
  89. const SinglePassRange2& rng2,
  90. OutputIterator out)
  91. {
  92. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  93. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  94. return std::set_intersection(boost::begin(rng1), boost::end(rng1),
  95. boost::begin(rng2), boost::end(rng2), out);
  96. }
  97. /// \overload
  98. template<class SinglePassRange1, class SinglePassRange2,
  99. class OutputIterator, class BinaryPredicate>
  100. inline OutputIterator set_intersection(const SinglePassRange1& rng1,
  101. const SinglePassRange2& rng2,
  102. OutputIterator out,
  103. BinaryPredicate pred)
  104. {
  105. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  106. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  107. return std::set_intersection(boost::begin(rng1), boost::end(rng1),
  108. boost::begin(rng2), boost::end(rng2),
  109. out, pred);
  110. }
  111. /// \brief template function set_difference
  112. ///
  113. /// range-based version of the set_difference std algorithm
  114. ///
  115. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  116. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  117. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  118. template<class SinglePassRange1, class SinglePassRange2,
  119. class OutputIterator>
  120. inline OutputIterator set_difference(const SinglePassRange1& rng1,
  121. const SinglePassRange2& rng2,
  122. OutputIterator out)
  123. {
  124. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  125. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  126. return std::set_difference(boost::begin(rng1), boost::end(rng1),
  127. boost::begin(rng2), boost::end(rng2), out);
  128. }
  129. /// \overload
  130. template<class SinglePassRange1, class SinglePassRange2,
  131. class OutputIterator, class BinaryPredicate>
  132. inline OutputIterator set_difference(const SinglePassRange1& rng1,
  133. const SinglePassRange2& rng2,
  134. OutputIterator out,
  135. BinaryPredicate pred)
  136. {
  137. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  138. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  139. return std::set_difference(
  140. boost::begin(rng1), boost::end(rng1),
  141. boost::begin(rng2), boost::end(rng2), out, pred);
  142. }
  143. /// \brief template function set_symmetric_difference
  144. ///
  145. /// range-based version of the set_symmetric_difference std algorithm
  146. ///
  147. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  148. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  149. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  150. template<class SinglePassRange1, class SinglePassRange2,
  151. class OutputIterator>
  152. inline OutputIterator
  153. set_symmetric_difference(const SinglePassRange1& rng1,
  154. const SinglePassRange2& rng2,
  155. OutputIterator out)
  156. {
  157. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  158. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  159. return std::set_symmetric_difference(boost::begin(rng1), boost::end(rng1),
  160. boost::begin(rng2), boost::end(rng2), out);
  161. }
  162. /// \overload
  163. template<class SinglePassRange1, class SinglePassRange2,
  164. class OutputIterator, class BinaryPredicate>
  165. inline OutputIterator
  166. set_symmetric_difference(const SinglePassRange1& rng1,
  167. const SinglePassRange2& rng2,
  168. OutputIterator out,
  169. BinaryPredicate pred)
  170. {
  171. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  172. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  173. return std::set_symmetric_difference(
  174. boost::begin(rng1), boost::end(rng1),
  175. boost::begin(rng2), boost::end(rng2), out, pred);
  176. }
  177. } // namespace range
  178. using range::includes;
  179. using range::set_union;
  180. using range::set_intersection;
  181. using range::set_difference;
  182. using range::set_symmetric_difference;
  183. } // namespace boost
  184. #endif // include guard