Merged with mainline at revision 128810.
[official-gcc.git] / libstdc++-v3 / include / parallel / checkers.h
blobb34ee0519272a6ba8cef2468854beb2f97d02e7b
1 // -*- C++ -*-
3 // Copyright (C) 2007 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
31 /** @file parallel/checkers.h
32 * @brief Routines for checking the correctness of algorithm results.
33 * This file is a GNU parallel extension to the Standard C++ Library.
36 // Written by Johannes Singler.
38 #ifndef _GLIBCXX_PARALLEL_CHECKERS
39 #define _GLIBCXX_PARALLEL_CHECKERS 1
41 #include <functional>
42 #include <cstdio>
43 #include <bits/stl_algobase.h>
45 namespace __gnu_parallel
47 /**
48 * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
49 * @param begin Begin iterator of sequence.
50 * @param end End iterator of sequence.
51 * @param comp Comparator.
52 * @return @c true if sorted, @c false otherwise.
54 // XXX Comparator default template argument
55 template<typename InputIterator, typename Comparator>
56 bool
57 is_sorted(InputIterator begin, InputIterator end, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>())
59 if (begin == end)
60 return true;
62 InputIterator current(begin), recent(begin);
64 unsigned long long position = 1;
65 for (current++; current != end; current++)
67 if (comp(*current, *recent))
69 printf("is_sorted: check failed before position %i.\n", position);
70 return false;
72 recent = current;
73 position++;
76 return true;
79 /**
80 * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
81 * Prints the position in case an misordered pair is found.
82 * @param begin Begin iterator of sequence.
83 * @param end End iterator of sequence.
84 * @param first_failure The first failure is returned in this variable.
85 * @param comp Comparator.
86 * @return @c true if sorted, @c false otherwise.
88 // XXX Comparator default template argument
89 template<typename InputIterator, typename Comparator>
90 bool
91 is_sorted_failure(InputIterator begin, InputIterator end, InputIterator& first_failure, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>())
93 if (begin == end)
94 return true;
96 InputIterator current(begin), recent(begin);
98 unsigned long long position = 1;
99 for (current++; current != end; current++)
101 if (comp(*current, *recent))
103 first_failure = current;
104 printf("is_sorted: check failed before position %lld.\n", position);
105 return false;
107 recent = current;
108 position++;
111 first_failure = end;
112 return true;
116 * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
117 * Prints all misordered pair, including the surrounding two elements.
118 * @param begin Begin iterator of sequence.
119 * @param end End iterator of sequence.
120 * @param comp Comparator.
121 * @return @c true if sorted, @c false otherwise.
123 template<typename InputIterator, typename Comparator>
124 bool
125 // XXX Comparator default template argument
126 is_sorted_print_failures(InputIterator begin, InputIterator end, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>())
128 if (begin == end)
129 return true;
131 InputIterator recent(begin);
132 bool ok = true;
134 for (InputIterator pos(begin + 1); pos != end; pos++)
136 if (comp(*pos, *recent))
138 printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
139 *(pos- 1), *pos, *(pos + 1));
140 ok = false;
142 recent = pos;
144 return ok;
148 #endif