checkers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/checkers.h
  21. * @brief Routines for checking the correctness of algorithm results.
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_CHECKERS_H
  26. #define _GLIBCXX_PARALLEL_CHECKERS_H 1
  27. #include <functional>
  28. #include <cstdio>
  29. #include <bits/stl_algobase.h>
  30. namespace __gnu_parallel
  31. {
  32. /**
  33. * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
  34. * @param begin Begin iterator of sequence.
  35. * @param end End iterator of sequence.
  36. * @param comp Comparator.
  37. * @return @c true if sorted, @c false otherwise.
  38. */
  39. // XXX Comparator default template argument
  40. template<typename InputIterator, typename Comparator>
  41. bool
  42. is_sorted(InputIterator begin, InputIterator end,
  43. Comparator comp
  44. = std::less<typename std::iterator_traits<InputIterator>::
  45. value_type>())
  46. {
  47. if (begin == end)
  48. return true;
  49. InputIterator current(begin), recent(begin);
  50. unsigned long long position = 1;
  51. for (current++; current != end; current++)
  52. {
  53. if (comp(*current, *recent))
  54. {
  55. printf("is_sorted: check failed before position %i.\n",
  56. position);
  57. return false;
  58. }
  59. recent = current;
  60. position++;
  61. }
  62. return true;
  63. }
  64. /**
  65. * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
  66. * Prints the position in case an unordered pair is found.
  67. * @param begin Begin iterator of sequence.
  68. * @param end End iterator of sequence.
  69. * @param first_failure The first failure is returned in this variable.
  70. * @param comp Comparator.
  71. * @return @c true if sorted, @c false otherwise.
  72. */
  73. // XXX Comparator default template argument
  74. template<typename InputIterator, typename Comparator>
  75. bool
  76. is_sorted_failure(InputIterator begin, InputIterator end,
  77. InputIterator& first_failure,
  78. Comparator comp
  79. = std::less<typename std::iterator_traits<InputIterator>::
  80. value_type>())
  81. {
  82. if (begin == end)
  83. return true;
  84. InputIterator current(begin), recent(begin);
  85. unsigned long long position = 1;
  86. for (current++; current != end; current++)
  87. {
  88. if (comp(*current, *recent))
  89. {
  90. first_failure = current;
  91. printf("is_sorted: check failed before position %lld.\n",
  92. position);
  93. return false;
  94. }
  95. recent = current;
  96. position++;
  97. }
  98. first_failure = end;
  99. return true;
  100. }
  101. /**
  102. * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
  103. * Prints all unordered pair, including the surrounding two elements.
  104. * @param begin Begin iterator of sequence.
  105. * @param end End iterator of sequence.
  106. * @param comp Comparator.
  107. * @return @c true if sorted, @c false otherwise.
  108. */
  109. template<typename InputIterator, typename Comparator>
  110. bool
  111. // XXX Comparator default template argument
  112. is_sorted_print_failures(InputIterator begin, InputIterator end,
  113. Comparator comp
  114. = std::less<typename std::iterator_traits
  115. <InputIterator>::value_type>())
  116. {
  117. if (begin == end)
  118. return true;
  119. InputIterator recent(begin);
  120. bool ok = true;
  121. for (InputIterator pos(begin + 1); pos != end; pos++)
  122. {
  123. if (comp(*pos, *recent))
  124. {
  125. printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
  126. *(pos- 1), *pos, *(pos + 1));
  127. ok = false;
  128. }
  129. recent = pos;
  130. }
  131. return ok;
  132. }
  133. }
  134. #endif /* _GLIBCXX_PARALLEL_CHECKERS_H */