-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
lexicographical_compare.hpp
404 lines (372 loc) · 19 KB
/
lexicographical_compare.hpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (c) 2014 Grant Mercer
// Copyright (c) 2017 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// 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)
/// \file parallel/algorithms/lexicographical_compare.hpp
#pragma once
#if defined(DOXYGEN)
namespace hpx {
/// Checks if the first range [first1, last1) is lexicographically less than
/// the second range [first2, last2). uses a provided predicate to compare
/// elements.
///
/// \note Complexity: At most 2 * min(N1, N2) applications of the comparison
/// operation, where N1 = std::distance(first1, last)
/// and N2 = std::distance(first2, last2).
///
/// \tparam InIter1 The type of the source iterators used for the
/// first range (deduced).
/// This iterator type must meet the requirements of an
/// input iterator.
/// \tparam InIter2 The type of the source iterators used for the
/// second range (deduced).
/// This iterator type must meet the requirements of an
/// input iterator.
/// \tparam Pred The type of an optional function/function object to use.
/// Unlike its sequential form, the parallel
/// overload of \a lexicographical_compare requires \a Pred to
/// meet the requirements of \a CopyConstructible. This defaults
/// to std::less<>
///
/// \param first1 Refers to the beginning of the sequence of elements
/// of the first range the algorithm will be applied to.
/// \param last1 Refers to the end of the sequence of elements of
/// the first range the algorithm will be applied to.
/// \param first2 Refers to the beginning of the sequence of elements
/// of the second range the algorithm will be applied to.
/// \param last2 Refers to the end of the sequence of elements of
/// the second range the algorithm will be applied to.
/// \param pred Refers to the comparison function that the first
/// and second ranges will be applied to
///
/// The comparison operations in the parallel \a lexicographical_compare
/// algorithm invoked without an execution policy object execute in sequential
/// order in the calling thread.
///
/// \note Lexicographical comparison is an operation with the
/// following properties
/// - Two ranges are compared element by element
/// - The first mismatching element defines which range
/// is lexicographically
/// \a less or \a greater than the other
/// - If one range is a prefix of another, the shorter range is
/// lexicographically \a less than the other
/// - If two ranges have equivalent elements and are of the same length,
/// then the ranges are lexicographically \a equal
/// - An empty range is lexicographically \a less than any non-empty
/// range
/// - Two empty ranges are lexicographically \a equal
///
/// \returns The \a lexicographically_compare algorithm returns a
/// returns \a bool if the execution policy object is not passed in.
/// The \a lexicographically_compare algorithm returns true
/// if the first range is lexicographically less, otherwise
/// it returns false.
/// range [first2, last2), it returns false.
template <typename InIter1, typename InIter2, typename Pred>
bool lexicographical_compare(InIter1 first1, InIter1 last1, InIter2 first2,
InIter2 last2, Pred&& pred);
/// Checks if the first range [first1, last1) is lexicographically less than
/// the second range [first2, last2). uses a provided predicate to compare
/// elements.
///
/// \note Complexity: At most 2 * min(N1, N2) applications of the comparison
/// operation, where N1 = std::distance(first1, last)
/// and N2 = std::distance(first2, last2).
///
/// \tparam ExPolicy The type of the execution policy to use (deduced).
/// It describes the manner in which the execution
/// of the algorithm may be parallelized and the manner
/// in which it executes the assignments.
/// \tparam FwdIter1 The type of the source iterators used for the
/// first range (deduced).
/// This iterator type must meet the requirements of an
/// forward iterator.
/// \tparam FwdIter2 The type of the source iterators used for the
/// second range (deduced).
/// This iterator type must meet the requirements of an
/// forward iterator.
/// \tparam Pred The type of an optional function/function object to use.
/// Unlike its sequential form, the parallel
/// overload of \a lexicographical_compare requires \a Pred to
/// meet the requirements of \a CopyConstructible. This defaults
/// to std::less<>
///
/// \param policy The execution policy to use for the scheduling of
/// the iterations.
/// \param first1 Refers to the beginning of the sequence of elements
/// of the first range the algorithm will be applied to.
/// \param last1 Refers to the end of the sequence of elements of
/// the first range the algorithm will be applied to.
/// \param first2 Refers to the beginning of the sequence of elements
/// of the second range the algorithm will be applied to.
/// \param last2 Refers to the end of the sequence of elements of
/// the second range the algorithm will be applied to.
/// \param pred Refers to the comparison function that the first
/// and second ranges will be applied to
///
/// The comparison operations in the parallel \a lexicographical_compare
/// algorithm invoked with an execution policy object of type
/// \a sequenced_policy execute in sequential order in the
/// calling thread.
///
/// The comparison operations in the parallel \a lexicographical_compare
/// algorithm invoked with an execution policy object of type
/// \a parallel_policy
/// or \a parallel_task_policy are permitted to execute in an unordered
/// fashion in unspecified threads, and indeterminately sequenced
/// within each thread.
///
/// \note Lexicographical comparison is an operation with the
/// following properties
/// - Two ranges are compared element by element
/// - The first mismatching element defines which range
/// is lexicographically
/// \a less or \a greater than the other
/// - If one range is a prefix of another, the shorter range is
/// lexicographically \a less than the other
/// - If two ranges have equivalent elements and are of the same length,
/// then the ranges are lexicographically \a equal
/// - An empty range is lexicographically \a less than any non-empty
/// range
/// - Two empty ranges are lexicographically \a equal
///
/// \returns The \a lexicographically_compare algorithm returns a
/// \a hpx::future<bool> if the execution policy is of type
/// \a sequenced_task_policy or
/// \a parallel_task_policy and
/// returns \a bool otherwise.
/// The \a lexicographically_compare algorithm returns true
/// if the first range is lexicographically less, otherwise
/// it returns false.
/// range [first2, last2), it returns false.
template <typename FwdIter1, typename FwdIter2, typename Pred>
typename util::detail::algorithm_result<ExPolicy, bool>::type
lexicographical_compare(ExPolicy&& policy, FwdIter1 first1, FwdIter1 last1,
FwdIter2 first2, FwdIter2 last2, Pred&& pred);
} // namespace hpx
#else // DOXYGEN
#include <hpx/config.hpp>
#include <hpx/concepts/concepts.hpp>
#include <hpx/functional/invoke.hpp>
#include <hpx/functional/tag_fallback_invoke.hpp>
#include <hpx/iterator_support/traits/is_iterator.hpp>
#include <hpx/execution/algorithms/detail/predicates.hpp>
#include <hpx/executors/execution_policy.hpp>
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
#include <hpx/parallel/algorithms/detail/distance.hpp>
#include <hpx/parallel/algorithms/for_each.hpp>
#include <hpx/parallel/algorithms/mismatch.hpp>
#include <hpx/parallel/util/detail/algorithm_result.hpp>
#include <hpx/parallel/util/loop.hpp>
#include <hpx/parallel/util/partitioner.hpp>
#include <hpx/parallel/util/zip_iterator.hpp>
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>
namespace hpx { namespace parallel { inline namespace v1 {
///////////////////////////////////////////////////////////////////////////
// lexicographical_compare
namespace detail {
/// \cond NOINTERNAL
struct lexicographical_compare
: public detail::algorithm<lexicographical_compare, bool>
{
lexicographical_compare()
: lexicographical_compare::algorithm("lexicographical_compare")
{
}
template <typename ExPolicy, typename InIter1, typename Sent1,
typename InIter2, typename Sent2, typename Pred, typename Proj1,
typename Proj2>
static bool sequential(ExPolicy, InIter1 first1, Sent1 last1,
InIter2 first2, Sent2 last2, Pred&& pred, Proj1&& proj1,
Proj2&& proj2)
{
for (; (first1 != last1) && (first2 != last2);
++first1, (void) ++first2)
{
if (hpx::util::invoke(pred,
hpx::util::invoke(proj1, *first1),
hpx::util::invoke(proj2, *first2)))
return true;
if (hpx::util::invoke(pred,
hpx::util::invoke(proj2, *first2),
hpx::util::invoke(proj1, *first1)))
return false;
}
return (first1 == last1) && (first2 != last2);
}
template <typename ExPolicy, typename FwdIter1, typename Sent1,
typename FwdIter2, typename Sent2, typename Pred,
typename Proj1, typename Proj2>
static typename util::detail::algorithm_result<ExPolicy, bool>::type
parallel(ExPolicy&& policy, FwdIter1 first1, Sent1 last1,
FwdIter2 first2, Sent2 last2, Pred&& pred, Proj1&& proj1,
Proj2&& proj2)
{
typedef hpx::util::zip_iterator<FwdIter1, FwdIter2>
zip_iterator;
typedef typename zip_iterator::reference reference;
std::size_t count1 = detail::distance(first1, last1);
std::size_t count2 = detail::distance(first2, last2);
// An empty range is lexicographically less than any non-empty
// range
if (count1 == 0 && count2 != 0)
{
return util::detail::algorithm_result<ExPolicy, bool>::get(
true);
}
if (count2 == 0 && count1 != 0)
{
return util::detail::algorithm_result<ExPolicy, bool>::get(
false);
}
std::size_t count = (std::min)(count1, count2);
util::cancellation_token<std::size_t> tok(count);
auto f1 = [tok, pred, proj1, proj2](zip_iterator it,
std::size_t part_count,
std::size_t base_idx) mutable -> void {
util::loop_idx_n(base_idx, it, part_count, tok,
[&pred, &tok, &proj1, &proj2](
reference t, std::size_t i) -> void {
using hpx::get;
using hpx::util::invoke;
if (invoke(pred, invoke(proj1, get<0>(t)),
invoke(proj2, get<1>(t))) ||
invoke(pred, invoke(proj2, get<1>(t)),
invoke(proj1, get<0>(t))))
{
tok.cancel(i);
}
});
};
auto f2 =
[tok, first1, first2, last1, last2, pred, proj1, proj2](
std::vector<hpx::future<void>>&&) mutable -> bool {
std::size_t mismatched = tok.get_data();
std::advance(first1, mismatched);
std::advance(first2, mismatched);
if (first1 != last1 && first2 != last2)
return hpx::util::invoke(pred,
hpx::util::invoke(proj1, *first1),
hpx::util::invoke(proj2, *first2));
return first2 != last2;
};
using hpx::util::make_zip_iterator;
return util::partitioner<ExPolicy, bool, void>::call_with_index(
std::forward<ExPolicy>(policy),
make_zip_iterator(first1, first2), count, 1, std::move(f1),
std::move(f2));
}
};
/// \endcond
} // namespace detail
// clang-format off
template <typename ExPolicy, typename FwdIter1, typename FwdIter2,
typename Pred = detail::less,
HPX_CONCEPT_REQUIRES_(
hpx::is_execution_policy<ExPolicy>::value &&
hpx::traits::is_iterator<FwdIter1>::value &&
hpx::traits::is_iterator<FwdIter2>::value &&
hpx::is_invocable_v<Pred,
typename std::iterator_traits<FwdIter1>::value_type,
typename std::iterator_traits<FwdIter2>::value_type
>
)>
// clang-format on
HPX_DEPRECATED_V(1, 7,
"hpx::parallel::lexicographical_compare is deprecated, use "
"hpx::lexicographical_compare "
"instead") typename util::detail::algorithm_result<ExPolicy, bool>::type
lexicographical_compare(ExPolicy&& policy, FwdIter1 first1,
FwdIter1 last1, FwdIter2 first2, FwdIter2 last2,
Pred&& pred = Pred())
{
static_assert(hpx::traits::is_forward_iterator<FwdIter1>::value,
"Requires at least forward iterator.");
static_assert(hpx::traits::is_forward_iterator<FwdIter2>::value,
"Requires at least forward iterator.");
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 100000
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return detail::lexicographical_compare().call(
std::forward<ExPolicy>(policy), first1, last1, first2, last2,
std::forward<Pred>(pred),
hpx::parallel::util::projection_identity{},
hpx::parallel::util::projection_identity{});
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 100000
#pragma GCC diagnostic pop
#endif
}
}}} // namespace hpx::parallel::v1
namespace hpx {
///////////////////////////////////////////////////////////////////////////
// CPO for hpx::lexicographical_compare
HPX_INLINE_CONSTEXPR_VARIABLE struct lexicographical_compare_t final
: hpx::functional::tag_fallback<lexicographical_compare_t>
{
// clang-format off
template <typename InIter1, typename InIter2,
typename Pred = hpx::parallel::v1::detail::less,
HPX_CONCEPT_REQUIRES_(
hpx::traits::is_iterator<InIter1>::value &&
hpx::traits::is_iterator<InIter2>::value &&
hpx::is_invocable_v<Pred,
typename std::iterator_traits<InIter1>::value_type,
typename std::iterator_traits<InIter2>::value_type
>
)>
// clang-format on
friend bool tag_fallback_invoke(hpx::lexicographical_compare_t,
InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2,
Pred&& pred = Pred())
{
static_assert(hpx::traits::is_input_iterator<InIter1>::value,
"Requires at least input iterator.");
static_assert(hpx::traits::is_input_iterator<InIter2>::value,
"Requires at least input iterator.");
return hpx::parallel::v1::detail::lexicographical_compare().call(
hpx::execution::seq, first1, last1, first2, last2,
std::forward<Pred>(pred),
hpx::parallel::util::projection_identity{},
hpx::parallel::util::projection_identity{});
}
// clang-format off
template <typename ExPolicy, typename FwdIter1, typename FwdIter2,
typename Pred = hpx::parallel::v1::detail::less,
HPX_CONCEPT_REQUIRES_(
hpx::is_execution_policy<ExPolicy>::value &&
hpx::traits::is_iterator<FwdIter1>::value &&
hpx::traits::is_iterator<FwdIter2>::value &&
hpx::is_invocable_v<Pred,
typename std::iterator_traits<FwdIter1>::value_type,
typename std::iterator_traits<FwdIter2>::value_type
>
)>
// clang-format on
friend typename parallel::util::detail::algorithm_result<ExPolicy,
bool>::type
tag_fallback_invoke(hpx::lexicographical_compare_t, ExPolicy&& policy,
FwdIter1 first1, FwdIter1 last1, FwdIter2 first2, FwdIter2 last2,
Pred&& pred = Pred())
{
static_assert(hpx::traits::is_forward_iterator<FwdIter1>::value,
"Requires at least forward iterator.");
static_assert(hpx::traits::is_forward_iterator<FwdIter2>::value,
"Requires at least forward iterator.");
return hpx::parallel::v1::detail::lexicographical_compare().call(
std::forward<ExPolicy>(policy), first1, last1, first2, last2,
std::forward<Pred>(pred),
hpx::parallel::util::projection_identity{},
hpx::parallel::util::projection_identity{});
}
} lexicographical_compare{};
} // namespace hpx
#endif // DOXYGEN