Thrill  0.1
is_sorted_cmp.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/algorithm/is_sorted_cmp.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2018 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_ALGORITHM_IS_SORTED_CMP_HEADER
12 #define TLX_ALGORITHM_IS_SORTED_CMP_HEADER
13 
14 #include <functional>
15 
16 namespace tlx {
17 
18 //! \addtogroup tlx_algorithm
19 //! \{
20 
21 /*!
22  * Checks if a range is sorted using a three-way Comparator (with memcmp()
23  * semantics). Returns an iterator to the first items not in order.
24  */
25 template <typename ForwardIterator, typename Comparator>
26 ForwardIterator is_sorted_until_cmp(ForwardIterator first, ForwardIterator last,
27  Comparator cmp) {
28  if (first != last) {
29  ForwardIterator next = first;
30  while (++next != last) {
31  if (cmp(*first, *next) > 0)
32  return next;
33  first = next;
34  }
35  }
36  return last;
37 }
38 
39 /*!
40  * Checks if a range is sorted using a three-way Comparator (with memcmp()
41  * semantics).
42  */
43 template <typename ForwardIterator, typename Comparator>
44 bool is_sorted_cmp(ForwardIterator first, ForwardIterator last,
45  Comparator cmp) {
46  return is_sorted_until_cmp(first, last, cmp) == last;
47 }
48 
49 //! \}
50 
51 } // namespace tlx
52 
53 #endif // !TLX_ALGORITHM_IS_SORTED_CMP_HEADER
54 
55 /******************************************************************************/
bool is_sorted_cmp(ForwardIterator first, ForwardIterator last, Comparator cmp)
Checks if a range is sorted using a three-way Comparator (with memcmp() semantics).
ForwardIterator is_sorted_until_cmp(ForwardIterator first, ForwardIterator last, Comparator cmp)
Checks if a range is sorted using a three-way Comparator (with memcmp() semantics).