OSDN Git Service

2009-09-17 Johannes Singler <singler@ira.uka.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / parallel / checkers.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file parallel/checkers.h
26  *  @brief Routines for checking the correctness of algorithm results.
27  *  This file is a GNU parallel extension to the Standard C++ Library.
28  */
29
30 // Written by Johannes Singler.
31
32 #ifndef _GLIBCXX_PARALLEL_CHECKERS_H
33 #define _GLIBCXX_PARALLEL_CHECKERS_H 1
34
35 #include <functional>
36 #include <cstdio>
37 #include <bits/stl_algobase.h>
38
39 namespace __gnu_parallel
40 {
41   /**
42    * @brief Check whether @__c [__begin, @__c __end) is sorted according
43    * to @__c __comp.
44    * @param __begin Begin iterator of sequence.
45    * @param __end End iterator of sequence.
46    * @param __comp Comparator.
47    * @return @__c true if sorted, @__c false otherwise.
48    */
49   // XXX Compare default template argument
50   template<typename _IIter, typename _Compare>
51     bool
52     __is_sorted(_IIter __begin, _IIter __end,
53               _Compare __comp
54               = std::less<typename std::iterator_traits<_IIter>::
55               _ValueType>())
56     {
57       if (__begin == __end)
58         return true;
59
60       _IIter __current(__begin), __recent(__begin);
61
62       unsigned long long __position = 1;
63       for (__current++; __current != __end; __current++)
64         {
65           if (__comp(*__current, *__recent))
66             {
67               printf("__is_sorted: check failed before position %__i.\n",
68                      __position);
69               return false;
70             }
71           __recent = __current;
72           __position++;
73         }
74
75       return true;
76     }
77
78   /**
79    * @brief Check whether @__c [__begin, @__c __end) is sorted according to
80    * @__c __comp.
81    * Prints the position in case an unordered 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.
87    */
88   // XXX Compare default template argument
89   template<typename _IIter, typename _Compare>
90     bool
91     is_sorted_failure(_IIter __begin, _IIter __end,
92                       _IIter& __first_failure,
93                       _Compare __comp
94                       = std::less<typename std::iterator_traits<_IIter>::
95                       _ValueType>())
96     {
97       if (__begin == __end)
98         return true;
99
100       _IIter __current(__begin), __recent(__begin);
101
102       unsigned long long __position = 1;
103       for (__current++; __current != __end; __current++)
104         {
105           if (__comp(*__current, *__recent))
106             {
107               __first_failure = __current;
108               printf("__is_sorted: check failed before position %lld.\n",
109                      __position);
110               return false;
111             }
112           __recent = __current;
113           __position++;
114         }
115
116       __first_failure = __end;
117       return true;
118     }
119
120   /**
121    * @brief Check whether @__c [__begin, @__c __end) is sorted according to
122    * @__c __comp.
123    * Prints all unordered pair, including the surrounding two elements.
124    * @param __begin Begin iterator of sequence.
125    * @param __end End iterator of sequence.
126    * @param __comp Comparator.
127    * @return @__c true if sorted, @__c false otherwise.
128    */
129   template<typename _IIter, typename _Compare>
130     bool
131     // XXX Compare default template argument
132     is_sorted_print_failures(_IIter __begin, _IIter __end,
133                              _Compare __comp
134                              = std::less<typename std::iterator_traits
135                              <_IIter>::value_type>())
136     {
137       if (__begin == __end)
138         return true;
139
140       _IIter __recent(__begin);
141       bool __ok = true;
142
143       for (_IIter __pos(__begin + 1); __pos != __end; __pos++)
144         {
145           if (__comp(*__pos, *__recent))
146             {
147               printf("%ld: %d %d %d %d\n", __pos - __begin, *(__pos - 2),
148                      *(__pos- 1), *__pos, *(__pos + 1));
149               __ok = false;
150             }
151           __recent = __pos;
152         }
153       return __ok;
154     }
155 }
156
157 #endif /* _GLIBCXX_PARALLEL_CHECKERS_H */