algorithm.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // (C) Copyright Jeremy Siek 2001.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  3. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. /*
  5. *
  6. * Copyright (c) 1994
  7. * Hewlett-Packard Company
  8. *
  9. * Permission to use, copy, modify, distribute and sell this software
  10. * and its documentation for any purpose is hereby granted without fee,
  11. * provided that the above copyright notice appear in all copies and
  12. * that both that copyright notice and this permission notice appear
  13. * in supporting documentation. Hewlett-Packard Company makes no
  14. * representations about the suitability of this software for any
  15. * purpose. It is provided "as is" without express or implied warranty.
  16. *
  17. *
  18. * Copyright (c) 1996
  19. * Silicon Graphics Computer Systems, Inc.
  20. *
  21. * Permission to use, copy, modify, distribute and sell this software
  22. * and its documentation for any purpose is hereby granted without fee,
  23. * provided that the above copyright notice appear in all copies and
  24. * that both that copyright notice and this permission notice appear
  25. * in supporting documentation. Silicon Graphics makes no
  26. * representations about the suitability of this software for any
  27. * purpose. It is provided "as is" without express or implied warranty.
  28. */
  29. #ifndef BOOST_ALGORITHM_HPP
  30. # define BOOST_ALGORITHM_HPP
  31. # include <boost/detail/iterator.hpp>
  32. // Algorithms on sequences
  33. //
  34. // The functions in this file have not yet gone through formal
  35. // review, and are subject to change. This is a work in progress.
  36. // They have been checked into the detail directory because
  37. // there are some graph algorithms that use these functions.
  38. #include <algorithm>
  39. #include <vector>
  40. namespace boost {
  41. template <typename Iter1, typename Iter2>
  42. Iter1 begin(const std::pair<Iter1, Iter2>& p) { return p.first; }
  43. template <typename Iter1, typename Iter2>
  44. Iter2 end(const std::pair<Iter1, Iter2>& p) { return p.second; }
  45. template <typename Iter1, typename Iter2>
  46. typename boost::detail::iterator_traits<Iter1>::difference_type
  47. size(const std::pair<Iter1, Iter2>& p) {
  48. return std::distance(p.first, p.second);
  49. }
  50. #if 0
  51. // These seem to interfere with the std::pair overloads :(
  52. template <typename Container>
  53. typename Container::iterator
  54. begin(Container& c) { return c.begin(); }
  55. template <typename Container>
  56. typename Container::const_iterator
  57. begin(const Container& c) { return c.begin(); }
  58. template <typename Container>
  59. typename Container::iterator
  60. end(Container& c) { return c.end(); }
  61. template <typename Container>
  62. typename Container::const_iterator
  63. end(const Container& c) { return c.end(); }
  64. template <typename Container>
  65. typename Container::size_type
  66. size(const Container& c) { return c.size(); }
  67. #else
  68. template <typename T>
  69. typename std::vector<T>::iterator
  70. begin(std::vector<T>& c) { return c.begin(); }
  71. template <typename T>
  72. typename std::vector<T>::const_iterator
  73. begin(const std::vector<T>& c) { return c.begin(); }
  74. template <typename T>
  75. typename std::vector<T>::iterator
  76. end(std::vector<T>& c) { return c.end(); }
  77. template <typename T>
  78. typename std::vector<T>::const_iterator
  79. end(const std::vector<T>& c) { return c.end(); }
  80. template <typename T>
  81. typename std::vector<T>::size_type
  82. size(const std::vector<T>& c) { return c.size(); }
  83. #endif
  84. template <class ForwardIterator, class T>
  85. void iota(ForwardIterator first, ForwardIterator last, T value)
  86. {
  87. for (; first != last; ++first, ++value)
  88. *first = value;
  89. }
  90. template <typename Container, typename T>
  91. void iota(Container& c, const T& value)
  92. {
  93. iota(begin(c), end(c), value);
  94. }
  95. // Also do version with 2nd container?
  96. template <typename Container, typename OutIter>
  97. OutIter copy(const Container& c, OutIter result) {
  98. return std::copy(begin(c), end(c), result);
  99. }
  100. template <typename Container1, typename Container2>
  101. bool equal(const Container1& c1, const Container2& c2)
  102. {
  103. if (size(c1) != size(c2))
  104. return false;
  105. return std::equal(begin(c1), end(c1), begin(c2));
  106. }
  107. template <typename Container>
  108. void sort(Container& c) { std::sort(begin(c), end(c)); }
  109. template <typename Container, typename Predicate>
  110. void sort(Container& c, const Predicate& p) {
  111. std::sort(begin(c), end(c), p);
  112. }
  113. template <typename Container>
  114. void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); }
  115. template <typename Container, typename Predicate>
  116. void stable_sort(Container& c, const Predicate& p) {
  117. std::stable_sort(begin(c), end(c), p);
  118. }
  119. template <typename InputIterator, typename Predicate>
  120. bool any_if(InputIterator first, InputIterator last, Predicate p)
  121. {
  122. return std::find_if(first, last, p) != last;
  123. }
  124. template <typename Container, typename Predicate>
  125. bool any_if(const Container& c, Predicate p)
  126. {
  127. return any_if(begin(c), end(c), p);
  128. }
  129. template <typename InputIterator, typename T>
  130. bool container_contains(InputIterator first, InputIterator last, T value)
  131. {
  132. return std::find(first, last, value) != last;
  133. }
  134. template <typename Container, typename T>
  135. bool container_contains(const Container& c, const T& value)
  136. {
  137. return container_contains(begin(c), end(c), value);
  138. }
  139. template <typename Container, typename T>
  140. std::size_t count(const Container& c, const T& value)
  141. {
  142. return std::count(begin(c), end(c), value);
  143. }
  144. template <typename Container, typename Predicate>
  145. std::size_t count_if(const Container& c, Predicate p)
  146. {
  147. return std::count_if(begin(c), end(c), p);
  148. }
  149. template <typename ForwardIterator>
  150. bool is_sorted(ForwardIterator first, ForwardIterator last)
  151. {
  152. if (first == last)
  153. return true;
  154. ForwardIterator next = first;
  155. for (++next; next != last; first = next, ++next) {
  156. if (*next < *first)
  157. return false;
  158. }
  159. return true;
  160. }
  161. template <typename ForwardIterator, typename StrictWeakOrdering>
  162. bool is_sorted(ForwardIterator first, ForwardIterator last,
  163. StrictWeakOrdering comp)
  164. {
  165. if (first == last)
  166. return true;
  167. ForwardIterator next = first;
  168. for (++next; next != last; first = next, ++next) {
  169. if (comp(*next, *first))
  170. return false;
  171. }
  172. return true;
  173. }
  174. template <typename Container>
  175. bool is_sorted(const Container& c)
  176. {
  177. return is_sorted(begin(c), end(c));
  178. }
  179. template <typename Container, typename StrictWeakOrdering>
  180. bool is_sorted(const Container& c, StrictWeakOrdering comp)
  181. {
  182. return is_sorted(begin(c), end(c), comp);
  183. }
  184. } // namespace boost
  185. #endif // BOOST_ALGORITHM_HPP