Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / include / parallel / checkers.h
blob1c6bc355d175c38b1585c8fb4bb17001f43862d2
1 // -*- C++ -*-
3 // Copyright (C) 2007, 2008 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,
58 Comparator comp
59 = std::less<typename std::iterator_traits<InputIterator>::
60 value_type>())
62 if (begin == end)
63 return true;
65 InputIterator current(begin), recent(begin);
67 unsigned long long position = 1;
68 for (current++; current != end; current++)
70 if (comp(*current, *recent))
72 printf("is_sorted: check failed before position %i.\n",
73 position);
74 return false;
76 recent = current;
77 position++;
80 return true;
83 /**
84 * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
85 * Prints the position in case an unordered pair is found.
86 * @param begin Begin iterator of sequence.
87 * @param end End iterator of sequence.
88 * @param first_failure The first failure is returned in this variable.
89 * @param comp Comparator.
90 * @return @c true if sorted, @c false otherwise.
92 // XXX Comparator default template argument
93 template<typename InputIterator, typename Comparator>
94 bool
95 is_sorted_failure(InputIterator begin, InputIterator end,
96 InputIterator& first_failure,
97 Comparator comp
98 = std::less<typename std::iterator_traits<InputIterator>::
99 value_type>())
101 if (begin == end)
102 return true;
104 InputIterator current(begin), recent(begin);
106 unsigned long long position = 1;
107 for (current++; current != end; current++)
109 if (comp(*current, *recent))
111 first_failure = current;
112 printf("is_sorted: check failed before position %lld.\n",
113 position);
114 return false;
116 recent = current;
117 position++;
120 first_failure = end;
121 return true;
125 * @brief Check whether @c [begin, @c end) is sorted according to @c comp.
126 * Prints all unordered pair, including the surrounding two elements.
127 * @param begin Begin iterator of sequence.
128 * @param end End iterator of sequence.
129 * @param comp Comparator.
130 * @return @c true if sorted, @c false otherwise.
132 template<typename InputIterator, typename Comparator>
133 bool
134 // XXX Comparator default template argument
135 is_sorted_print_failures(InputIterator begin, InputIterator end,
136 Comparator comp
137 = std::less<typename std::iterator_traits
138 <InputIterator>::value_type>())
140 if (begin == end)
141 return true;
143 InputIterator recent(begin);
144 bool ok = true;
146 for (InputIterator pos(begin + 1); pos != end; pos++)
148 if (comp(*pos, *recent))
150 printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
151 *(pos- 1), *pos, *(pos + 1));
152 ok = false;
154 recent = pos;
156 return ok;
160 #endif