forked from stlab/adobe_source_libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted.hpp
More file actions
149 lines (121 loc) · 4.1 KB
/
sorted.hpp
File metadata and controls
149 lines (121 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2013 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#ifndef ADOBE_ALGORITHM_SORTED_HPP
#define ADOBE_ALGORITHM_SORTED_HPP
#include <algorithm>
#include <functional>
#include <iterator>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
/**************************************************************************************************/
namespace adobe {
/**************************************************************************************************/
/*!
\defgroup sorted sorted
\ingroup sorting
*/
/*!
\ingroup sorted
*/
template <typename I, // I models InputIterator
typename O>
// O models StrictWeakOrdering on value_type(I)
I sorted(I f, I l, O o) {
f = std::adjacent_find(
f, l,
[&o](const auto& first, const auto& next) {
return !std::invoke(o, first, next);
});
if (f != l)
++f;
return f;
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I> // I models InputIterator, value_type(I) models LessThanComparable
I sorted(I f, I l) {
return sorted(f, l, less());
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I, // I models InputIterator
typename O>
// O models StrictWeakOrdering on value_type(I)
inline bool is_sorted(I f, I l, O o) {
return std::adjacent_find(
f, l,
[&o](const auto& first, const auto& next) {
return !std::invoke(o, first, next);
}) == l;
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I> // I models InputIterator, value_type(I) models LessThanComparable
inline bool is_sorted(I f, I l) {
return is_sorted(f, l, less());
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I, // I models ForwardIterator
typename C, // C models StrictWeakOrdering(T, T)
typename P>
// P models UnaryFunction(value_type(I)) -> T
inline bool is_sorted(I f, I l, C c, P p) {
return std::adjacent_find(
f, l,
[&c, &p](const auto& first, const auto& next) {
return !std::invoke(
c,
std::invoke(p, first),
std::invoke(p, next)
);
}
) == l;
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I, // I models ForwardRange
typename C, // C models StrictWeakOrdering(T, T)
typename P>
// P models UnaryFunction(value_type(I)) -> T
inline bool is_sorted(const I& r, C c, P p) {
return is_sorted(boost::begin(r), boost::end(r), c, p);
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I, // I models ForwardRange
typename C>
// C models StrictWeakOrdering(T, T)
inline bool is_sorted(const I& r, C c) {
return is_sorted(boost::begin(r), boost::end(r), c,
identity<typename std::iterator_traits<I>::value_type>());
}
/**************************************************************************************************/
/*!
\ingroup sorted
*/
template <typename I> // I models ForwardRange
inline bool is_sorted(const I& r) {
return std::is_sorted(boost::begin(r), boost::end(r), less());
}
/**************************************************************************************************/
} // namespace adobe
/**************************************************************************************************/
#endif
/**************************************************************************************************/