STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
is_sorted.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/is_sorted.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_COMMON_IS_SORTED_HEADER
15 #define STXXL_COMMON_IS_SORTED_HEADER
16 
17 #include <stxxl/bits/namespace.h>
18 
20 
21 template <class ForwardIterator>
22 bool is_sorted_helper(ForwardIterator first, ForwardIterator last)
23 {
24  if (first == last)
25  return true;
26 
27  ForwardIterator next = first;
28  for (++next; next != last; first = next, ++next) {
29  if (*next < *first)
30  return false;
31  }
32 
33  return true;
34 }
35 
36 template <class ForwardIterator, class StrictWeakOrdering>
37 bool is_sorted_helper(ForwardIterator first, ForwardIterator last,
38  StrictWeakOrdering comp)
39 {
40  if (first == last)
41  return true;
42 
43  ForwardIterator next = first;
44  for (++next; next != last; first = next, ++next) {
45  if (comp(*next, *first))
46  return false;
47  }
48 
49  return true;
50 }
51 
52 template <class ForwardIterator>
53 bool is_sorted(ForwardIterator first, ForwardIterator last)
54 {
55  return is_sorted_helper(first, last);
56 }
57 
58 template <class ForwardIterator, class StrictWeakOrdering>
59 bool is_sorted(ForwardIterator first, ForwardIterator last,
60  StrictWeakOrdering comp)
61 {
62  return is_sorted_helper(first, last, comp);
63 }
64 
66 
67 #endif // !STXXL_COMMON_IS_SORTED_HEADER
bool is_sorted(ForwardIterator first, ForwardIterator last)
Definition: is_sorted.h:53
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
bool is_sorted_helper(ForwardIterator first, ForwardIterator last)
Definition: is_sorted.h:22
#define STXXL_END_NAMESPACE
Definition: namespace.h:17