]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/parallel/checkers.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / parallel / checkers.h
1 // -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
30
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.
34  */
35
36 // Written by Johannes Singler.
37
38 #ifndef _GLIBCXX_PARALLEL_CHECKERS
39 #define _GLIBCXX_PARALLEL_CHECKERS 1
40
41 #include <functional>
42 #include <cstdio>
43 #include <bits/stl_algobase.h>
44
45 namespace __gnu_parallel
46 {
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.
53    */
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>())
61     {
62       if (begin == end)
63         return true;
64
65       InputIterator current(begin), recent(begin);
66
67       unsigned long long position = 1;
68       for (current++; current != end; current++)
69         {
70           if (comp(*current, *recent))
71             {
72               printf("is_sorted: check failed before position %i.\n",
73                      position);
74               return false;
75             }
76           recent = current;
77           position++;
78         }
79
80       return true;
81     }
82
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.
91    */
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>())
100     {
101       if (begin == end)
102         return true;
103
104       InputIterator current(begin), recent(begin);
105
106       unsigned long long position = 1;
107       for (current++; current != end; current++)
108         {
109           if (comp(*current, *recent))
110             {
111               first_failure = current;
112               printf("is_sorted: check failed before position %lld.\n",
113                      position);
114               return false;
115             }
116           recent = current;
117           position++;
118         }
119
120       first_failure = end;
121       return true;
122     }
123
124   /**
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.
131    */
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>())
139     {
140       if (begin == end)
141         return true;
142
143       InputIterator recent(begin);
144       bool ok = true;
145
146       for (InputIterator pos(begin + 1); pos != end; pos++)
147         {
148           if (comp(*pos, *recent))
149             {
150               printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
151                      *(pos- 1), *pos, *(pos + 1));
152               ok = false;
153             }
154           recent = pos;
155         }
156       return ok;
157     }
158 }
159
160 #endif