algobase.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // -*- C++ -*-
  2. // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/algobase.h
  21. * @brief Parallel STL function calls corresponding to the
  22. * stl_algobase.h header. The functions defined here mainly do case
  23. * switches and call the actual parallelized versions in other files.
  24. * Inlining policy: Functions that basically only contain one
  25. * function call, are declared inline.
  26. * This file is a GNU parallel extension to the Standard C++ Library.
  27. */
  28. // Written by Johannes Singler and Felix Putze.
  29. #ifndef _GLIBCXX_PARALLEL_ALGOBASE_H
  30. #define _GLIBCXX_PARALLEL_ALGOBASE_H 1
  31. #include <bits/stl_algobase.h>
  32. #include <parallel/base.h>
  33. #include <parallel/tags.h>
  34. #include <parallel/settings.h>
  35. #include <parallel/find.h>
  36. #include <parallel/find_selectors.h>
  37. namespace std
  38. {
  39. namespace __parallel
  40. {
  41. // NB: equal and lexicographical_compare require mismatch.
  42. // Sequential fallback
  43. template<typename InputIterator1, typename InputIterator2>
  44. inline pair<InputIterator1, InputIterator2>
  45. mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
  46. __gnu_parallel::sequential_tag)
  47. { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2); }
  48. // Sequential fallback
  49. template<typename InputIterator1, typename InputIterator2,
  50. typename Predicate>
  51. inline pair<InputIterator1, InputIterator2>
  52. mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
  53. Predicate pred, __gnu_parallel::sequential_tag)
  54. { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
  55. // Sequential fallback for input iterator case
  56. template<typename InputIterator1, typename InputIterator2,
  57. typename Predicate, typename IteratorTag1, typename IteratorTag2>
  58. inline pair<InputIterator1, InputIterator2>
  59. mismatch_switch(InputIterator1 begin1, InputIterator1 end1,
  60. InputIterator2 begin2, Predicate pred, IteratorTag1,
  61. IteratorTag2)
  62. { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
  63. // Parallel mismatch for random access iterators
  64. template<typename RandomAccessIterator1, typename RandomAccessIterator2,
  65. typename Predicate>
  66. pair<RandomAccessIterator1, RandomAccessIterator2>
  67. mismatch_switch(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
  68. RandomAccessIterator2 begin2, Predicate pred,
  69. random_access_iterator_tag, random_access_iterator_tag)
  70. {
  71. if (_GLIBCXX_PARALLEL_CONDITION(true))
  72. {
  73. RandomAccessIterator1 res =
  74. __gnu_parallel::find_template(begin1, end1, begin2, pred,
  75. __gnu_parallel::
  76. mismatch_selector()).first;
  77. return make_pair(res , begin2 + (res - begin1));
  78. }
  79. else
  80. return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred);
  81. }
  82. // Public interface
  83. template<typename InputIterator1, typename InputIterator2>
  84. inline pair<InputIterator1, InputIterator2>
  85. mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
  86. {
  87. typedef std::iterator_traits<InputIterator1> iterator1_traits;
  88. typedef std::iterator_traits<InputIterator2> iterator2_traits;
  89. typedef typename iterator1_traits::value_type value1_type;
  90. typedef typename iterator2_traits::value_type value2_type;
  91. typedef typename iterator1_traits::iterator_category iterator1_category;
  92. typedef typename iterator2_traits::iterator_category iterator2_category;
  93. typedef __gnu_parallel::equal_to<value1_type, value2_type> equal_to_type;
  94. return mismatch_switch(begin1, end1, begin2, equal_to_type(),
  95. iterator1_category(), iterator2_category());
  96. }
  97. // Public interface
  98. template<typename InputIterator1, typename InputIterator2,
  99. typename Predicate>
  100. inline pair<InputIterator1, InputIterator2>
  101. mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
  102. Predicate pred)
  103. {
  104. typedef std::iterator_traits<InputIterator1> iterator1_traits;
  105. typedef std::iterator_traits<InputIterator2> iterator2_traits;
  106. typedef typename iterator1_traits::iterator_category iterator1_category;
  107. typedef typename iterator2_traits::iterator_category iterator2_category;
  108. return mismatch_switch(begin1, end1, begin2, pred, iterator1_category(),
  109. iterator2_category());
  110. }
  111. // Sequential fallback
  112. template<typename InputIterator1, typename InputIterator2>
  113. inline bool
  114. equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
  115. __gnu_parallel::sequential_tag)
  116. { return _GLIBCXX_STD_P::equal(begin1, end1, begin2); }
  117. // Sequential fallback
  118. template<typename InputIterator1, typename InputIterator2,
  119. typename Predicate>
  120. inline bool
  121. equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
  122. Predicate pred, __gnu_parallel::sequential_tag)
  123. { return _GLIBCXX_STD_P::equal(begin1, end1, begin2, pred); }
  124. // Public interface
  125. template<typename InputIterator1, typename InputIterator2>
  126. inline bool
  127. equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
  128. { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2).first == end1; }
  129. // Public interface
  130. template<typename InputIterator1, typename InputIterator2,
  131. typename Predicate>
  132. inline bool
  133. equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
  134. Predicate pred)
  135. {
  136. return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred).first
  137. == end1;
  138. }
  139. // Sequential fallback
  140. template<typename InputIterator1, typename InputIterator2>
  141. inline bool
  142. lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
  143. InputIterator2 begin2, InputIterator2 end2,
  144. __gnu_parallel::sequential_tag)
  145. { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
  146. begin2, end2); }
  147. // Sequential fallback
  148. template<typename InputIterator1, typename InputIterator2,
  149. typename Predicate>
  150. inline bool
  151. lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
  152. InputIterator2 begin2, InputIterator2 end2,
  153. Predicate pred, __gnu_parallel::sequential_tag)
  154. { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
  155. begin2, end2, pred); }
  156. // Sequential fallback for input iterator case
  157. template<typename InputIterator1, typename InputIterator2,
  158. typename Predicate, typename IteratorTag1, typename IteratorTag2>
  159. inline bool
  160. lexicographical_compare_switch(InputIterator1 begin1, InputIterator1 end1,
  161. InputIterator2 begin2, InputIterator2 end2,
  162. Predicate pred, IteratorTag1, IteratorTag2)
  163. { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
  164. begin2, end2, pred); }
  165. // Parallel lexicographical_compare for random access iterators
  166. // Limitation: Both valuetypes must be the same
  167. template<typename RandomAccessIterator1, typename RandomAccessIterator2,
  168. typename Predicate>
  169. bool
  170. lexicographical_compare_switch(RandomAccessIterator1 begin1,
  171. RandomAccessIterator1 end1,
  172. RandomAccessIterator2 begin2,
  173. RandomAccessIterator2 end2, Predicate pred,
  174. random_access_iterator_tag,
  175. random_access_iterator_tag)
  176. {
  177. if (_GLIBCXX_PARALLEL_CONDITION(true))
  178. {
  179. typedef iterator_traits<RandomAccessIterator1> traits1_type;
  180. typedef typename traits1_type::value_type value1_type;
  181. typedef iterator_traits<RandomAccessIterator2> traits2_type;
  182. typedef typename traits2_type::value_type value2_type;
  183. typedef __gnu_parallel::equal_from_less<Predicate, value1_type,
  184. value2_type> equal_type;
  185. // Longer sequence in first place.
  186. if ((end1 - begin1) < (end2 - begin2))
  187. {
  188. typedef pair<RandomAccessIterator1, RandomAccessIterator2>
  189. pair_type;
  190. pair_type mm = mismatch_switch(begin1, end1, begin2,
  191. equal_type(pred),
  192. random_access_iterator_tag(),
  193. random_access_iterator_tag());
  194. return (mm.first == end1) || bool(pred(*mm.first, *mm.second));
  195. }
  196. else
  197. {
  198. typedef pair<RandomAccessIterator2, RandomAccessIterator1>
  199. pair_type;
  200. pair_type mm = mismatch_switch(begin2, end2, begin1,
  201. equal_type(pred),
  202. random_access_iterator_tag(),
  203. random_access_iterator_tag());
  204. return (mm.first != end2) && bool(pred(*mm.second, *mm.first));
  205. }
  206. }
  207. else
  208. return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
  209. begin2, end2, pred);
  210. }
  211. // Public interface
  212. template<typename InputIterator1, typename InputIterator2>
  213. inline bool
  214. lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
  215. InputIterator2 begin2, InputIterator2 end2)
  216. {
  217. typedef iterator_traits<InputIterator1> traits1_type;
  218. typedef typename traits1_type::value_type value1_type;
  219. typedef typename traits1_type::iterator_category iterator1_category;
  220. typedef iterator_traits<InputIterator2> traits2_type;
  221. typedef typename traits2_type::value_type value2_type;
  222. typedef typename traits2_type::iterator_category iterator2_category;
  223. typedef __gnu_parallel::less<value1_type, value2_type> less_type;
  224. return lexicographical_compare_switch(begin1, end1, begin2, end2,
  225. less_type(), iterator1_category(),
  226. iterator2_category());
  227. }
  228. // Public interface
  229. template<typename InputIterator1, typename InputIterator2,
  230. typename Predicate>
  231. inline bool
  232. lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
  233. InputIterator2 begin2, InputIterator2 end2,
  234. Predicate pred)
  235. {
  236. typedef iterator_traits<InputIterator1> traits1_type;
  237. typedef typename traits1_type::iterator_category iterator1_category;
  238. typedef iterator_traits<InputIterator2> traits2_type;
  239. typedef typename traits2_type::iterator_category iterator2_category;
  240. return lexicographical_compare_switch(begin1, end1, begin2, end2, pred,
  241. iterator1_category(),
  242. iterator2_category());
  243. }
  244. } // end namespace
  245. } // end namespace
  246. #endif /* _GLIBCXX_PARALLEL_ALGOBASE_H */