search.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2022 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/search.h
  21. * @brief Parallel implementation base for std::search() and
  22. * std::search_n().
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Felix Putze.
  26. #ifndef _GLIBCXX_PARALLEL_SEARCH_H
  27. #define _GLIBCXX_PARALLEL_SEARCH_H 1
  28. #include <bits/stl_algobase.h>
  29. #include <parallel/parallel.h>
  30. #include <parallel/equally_split.h>
  31. namespace __gnu_parallel
  32. {
  33. /**
  34. * @brief Precalculate __advances for Knuth-Morris-Pratt algorithm.
  35. * @param __elements Begin iterator of sequence to search for.
  36. * @param __length Length of sequence to search for.
  37. * @param __off Returned __offsets.
  38. */
  39. template<typename _RAIter, typename _DifferenceTp>
  40. void
  41. __calc_borders(_RAIter __elements, _DifferenceTp __length,
  42. _DifferenceTp* __off)
  43. {
  44. typedef _DifferenceTp _DifferenceType;
  45. __off[0] = -1;
  46. if (__length > 1)
  47. __off[1] = 0;
  48. _DifferenceType __k = 0;
  49. for (_DifferenceType __j = 2; __j <= __length; __j++)
  50. {
  51. while ((__k >= 0) && !(__elements[__k] == __elements[__j-1]))
  52. __k = __off[__k];
  53. __off[__j] = ++__k;
  54. }
  55. }
  56. // Generic parallel find algorithm (requires random access iterator).
  57. /** @brief Parallel std::search.
  58. * @param __begin1 Begin iterator of first sequence.
  59. * @param __end1 End iterator of first sequence.
  60. * @param __begin2 Begin iterator of second sequence.
  61. * @param __end2 End iterator of second sequence.
  62. * @param __pred Find predicate.
  63. * @return Place of finding in first sequences. */
  64. template<typename __RAIter1,
  65. typename __RAIter2,
  66. typename _Pred>
  67. __RAIter1
  68. __search_template(__RAIter1 __begin1, __RAIter1 __end1,
  69. __RAIter2 __begin2, __RAIter2 __end2,
  70. _Pred __pred)
  71. {
  72. typedef std::iterator_traits<__RAIter1> _TraitsType;
  73. typedef typename _TraitsType::difference_type _DifferenceType;
  74. _GLIBCXX_CALL((__end1 - __begin1) + (__end2 - __begin2));
  75. _DifferenceType __pattern_length = __end2 - __begin2;
  76. // Pattern too short.
  77. if(__pattern_length <= 0)
  78. return __end1;
  79. // Last point to start search.
  80. _DifferenceType __input_length = (__end1 - __begin1) - __pattern_length;
  81. // Where is first occurrence of pattern? defaults to end.
  82. _DifferenceType __result = (__end1 - __begin1);
  83. _DifferenceType *__splitters;
  84. // Pattern too long.
  85. if (__input_length < 0)
  86. return __end1;
  87. omp_lock_t __result_lock;
  88. omp_init_lock(&__result_lock);
  89. _ThreadIndex __num_threads = std::max<_DifferenceType>
  90. (1, std::min<_DifferenceType>(__input_length,
  91. __get_max_threads()));
  92. _DifferenceType __advances[__pattern_length];
  93. __calc_borders(__begin2, __pattern_length, __advances);
  94. # pragma omp parallel num_threads(__num_threads)
  95. {
  96. # pragma omp single
  97. {
  98. __num_threads = omp_get_num_threads();
  99. __splitters = new _DifferenceType[__num_threads + 1];
  100. __equally_split(__input_length, __num_threads, __splitters);
  101. }
  102. _ThreadIndex __iam = omp_get_thread_num();
  103. _DifferenceType __start = __splitters[__iam],
  104. __stop = __splitters[__iam + 1];
  105. _DifferenceType __pos_in_pattern = 0;
  106. bool __found_pattern = false;
  107. while (__start <= __stop && !__found_pattern)
  108. {
  109. // Get new value of result.
  110. #pragma omp flush(__result)
  111. // No chance for this thread to find first occurrence.
  112. if (__result < __start)
  113. break;
  114. while (__pred(__begin1[__start + __pos_in_pattern],
  115. __begin2[__pos_in_pattern]))
  116. {
  117. ++__pos_in_pattern;
  118. if (__pos_in_pattern == __pattern_length)
  119. {
  120. // Found new candidate for result.
  121. omp_set_lock(&__result_lock);
  122. __result = std::min(__result, __start);
  123. omp_unset_lock(&__result_lock);
  124. __found_pattern = true;
  125. break;
  126. }
  127. }
  128. // Make safe jump.
  129. __start += (__pos_in_pattern - __advances[__pos_in_pattern]);
  130. __pos_in_pattern = (__advances[__pos_in_pattern] < 0
  131. ? 0 : __advances[__pos_in_pattern]);
  132. }
  133. } //parallel
  134. omp_destroy_lock(&__result_lock);
  135. delete[] __splitters;
  136. // Return iterator on found element.
  137. return (__begin1 + __result);
  138. }
  139. } // end namespace
  140. #endif /* _GLIBCXX_PARALLEL_SEARCH_H */