Stxxl  1.3.2
is_sorted.h
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_IS_SORTED_HEADER
15 #define STXXL_IS_SORTED_HEADER
16 
17 #include <stxxl/bits/namespace.h>
18 
19 
20 __STXXL_BEGIN_NAMESPACE
21 
22 template <class _ForwardIter>
23 bool is_sorted_helper(_ForwardIter __first, _ForwardIter __last)
24 {
25  if (__first == __last)
26  return true;
27 
28  _ForwardIter __next = __first;
29  for (++__next; __next != __last; __first = __next, ++__next) {
30  if (*__next < *__first)
31  return false;
32  }
33 
34  return true;
35 }
36 
37 template <class _ForwardIter, class _StrictWeakOrdering>
38 bool is_sorted_helper(_ForwardIter __first, _ForwardIter __last,
39  _StrictWeakOrdering __comp)
40 {
41  if (__first == __last)
42  return true;
43 
44  _ForwardIter __next = __first;
45  for (++__next; __next != __last; __first = __next, ++__next) {
46  if (__comp(*__next, *__first))
47  return false;
48  }
49 
50  return true;
51 }
52 
53 template <class _ForwardIter>
54 bool is_sorted(_ForwardIter __first, _ForwardIter __last)
55 {
56  return is_sorted_helper(__first, __last);
57 }
58 
59 template <class _ForwardIter, class _StrictWeakOrdering>
60 bool is_sorted(_ForwardIter __first, _ForwardIter __last,
61  _StrictWeakOrdering __comp)
62 {
63  return is_sorted_helper(__first, __last, __comp);
64 }
65 
66 __STXXL_END_NAMESPACE
67 
68 #endif // !STXXL_IS_SORTED_HEADER