1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef UPREDALGO_H_2CB058AE0807A01A2F6A51BA5D5820A5
|
7 |
|
|
#define UPREDALGO_H_2CB058AE0807A01A2F6A51BA5D5820A5
|
8 |
|
|
|
9 |
|
|
namespace ustl {
|
10 |
|
|
|
11 |
|
|
/// Copy_if copies elements from the range [first, last) to the range
|
12 |
|
|
/// [result, result + (last - first)) if pred(*i) returns true.
|
13 |
|
|
/// \ingroup MutatingAlgorithms
|
14 |
|
|
/// \ingroup PredicateAlgorithms
|
15 |
|
|
///
|
16 |
|
|
template <typename InputIterator, typename OutputIterator, typename Predicate>
|
17 |
|
|
inline OutputIterator copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred)
|
18 |
|
|
{
|
19 |
|
|
for (; first != last; ++first) {
|
20 |
|
|
if (pred(*first)) {
|
21 |
|
|
*result = *first;
|
22 |
|
|
++ result;
|
23 |
|
|
}
|
24 |
|
|
}
|
25 |
|
|
return (result);
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
/// Returns the first iterator i in the range [first, last) such that
|
29 |
|
|
/// pred(*i) is true. Returns last if no such iterator exists.
|
30 |
|
|
/// \ingroup SearchingAlgorithms
|
31 |
|
|
/// \ingroup PredicateAlgorithms
|
32 |
|
|
///
|
33 |
|
|
template <typename InputIterator, typename Predicate>
|
34 |
|
|
inline InputIterator find_if (InputIterator first, InputIterator last, Predicate pred)
|
35 |
|
|
{
|
36 |
|
|
while (first != last && !pred (*first))
|
37 |
|
|
++ first;
|
38 |
|
|
return (first);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
/// Returns the first iterator such that p(*i, *(i + 1)) == true.
|
42 |
|
|
/// \ingroup SearchingAlgorithms
|
43 |
|
|
/// \ingroup PredicateAlgorithms
|
44 |
|
|
///
|
45 |
|
|
template <typename ForwardIterator, typename BinaryPredicate>
|
46 |
|
|
inline ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last, BinaryPredicate p)
|
47 |
|
|
{
|
48 |
|
|
if (first != last)
|
49 |
|
|
for (ForwardIterator prev = first; ++first != last; ++ prev)
|
50 |
|
|
if (p (*prev, *first))
|
51 |
|
|
return (prev);
|
52 |
|
|
return (last);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
/// Returns the pointer to the first pair of unequal elements.
|
56 |
|
|
/// \ingroup SearchingAlgorithms
|
57 |
|
|
/// \ingroup PredicateAlgorithms
|
58 |
|
|
///
|
59 |
|
|
template <typename InputIterator, typename BinaryPredicate>
|
60 |
|
|
inline pair<InputIterator,InputIterator>
|
61 |
|
|
mismatch (InputIterator first1, InputIterator last1, InputIterator first2, BinaryPredicate comp)
|
62 |
|
|
{
|
63 |
|
|
while (first1 != last1 && comp(*first1, *first2))
|
64 |
|
|
++ first1, ++ first2;
|
65 |
|
|
return (make_pair (first1, first2));
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
/// Returns true if two ranges are equal.
|
69 |
|
|
/// This is an extension, present in uSTL and SGI STL.
|
70 |
|
|
/// \ingroup ConditionAlgorithms
|
71 |
|
|
/// \ingroup PredicateAlgorithms
|
72 |
|
|
///
|
73 |
|
|
template <typename InputIterator, typename BinaryPredicate>
|
74 |
|
|
inline bool equal (InputIterator first1, InputIterator last1, InputIterator first2, BinaryPredicate comp)
|
75 |
|
|
{
|
76 |
|
|
return (mismatch (first1, last1, first2, comp).first == last1);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
/// Count_if finds the number of elements in [first, last) that satisfy the
|
80 |
|
|
/// predicate pred. More precisely, the first version of count_if returns the
|
81 |
|
|
/// number of iterators i in [first, last) such that pred(*i) is true.
|
82 |
|
|
/// \ingroup ConditionAlgorithms
|
83 |
|
|
/// \ingroup PredicateAlgorithms
|
84 |
|
|
///
|
85 |
|
|
template <typename InputIterator, typename Predicate>
|
86 |
|
|
inline size_t count_if (InputIterator first, InputIterator last, Predicate pred)
|
87 |
|
|
{
|
88 |
|
|
size_t total = 0;
|
89 |
|
|
for (; first != last; ++first)
|
90 |
|
|
if (pred (*first))
|
91 |
|
|
++ total;
|
92 |
|
|
return (total);
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
/// Replace_if replaces every element in the range [first, last) for which
|
96 |
|
|
/// pred returns true with new_value. That is: for every iterator i, if
|
97 |
|
|
/// pred(*i) is true then it performs the assignment *i = new_value.
|
98 |
|
|
/// \ingroup MutatingAlgorithms
|
99 |
|
|
/// \ingroup PredicateAlgorithms
|
100 |
|
|
///
|
101 |
|
|
template <typename ForwardIterator, typename Predicate, typename T>
|
102 |
|
|
inline void replace_if (ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value)
|
103 |
|
|
{
|
104 |
|
|
for (; first != last; ++first)
|
105 |
|
|
if (pred (*first))
|
106 |
|
|
*first = new_value;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/// Replace_copy_if copies elements from the range [first, last) to the range
|
110 |
|
|
/// [result, result + (last-first)), except that any element for which pred is
|
111 |
|
|
/// true is not copied; new_value is copied instead. More precisely, for every
|
112 |
|
|
/// integer n such that 0 <= n < last-first, replace_copy_if performs the
|
113 |
|
|
/// assignment *(result+n) = new_value if pred(*(first+n)),
|
114 |
|
|
/// and *(result+n) = *(first+n) otherwise.
|
115 |
|
|
/// \ingroup MutatingAlgorithms
|
116 |
|
|
/// \ingroup PredicateAlgorithms
|
117 |
|
|
///
|
118 |
|
|
template <typename InputIterator, typename OutputIterator, typename Predicate, typename T>
|
119 |
|
|
inline OutputIterator replace_copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value)
|
120 |
|
|
{
|
121 |
|
|
for (; first != last; ++result, ++first)
|
122 |
|
|
*result = pred(*first) ? new_value : *first;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/// Remove_copy_if copies elements from the range [first, last) to a range
|
126 |
|
|
/// beginning at result, except that elements for which pred is true are not
|
127 |
|
|
/// copied. The return value is the end of the resulting range. This operation
|
128 |
|
|
/// is stable, meaning that the relative order of the elements that are copied
|
129 |
|
|
/// is the same as in the range [first, last).
|
130 |
|
|
/// \ingroup MutatingAlgorithms
|
131 |
|
|
/// \ingroup PredicateAlgorithms
|
132 |
|
|
///
|
133 |
|
|
template <typename InputIterator, typename OutputIterator, typename Predicate>
|
134 |
|
|
inline OutputIterator remove_copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred)
|
135 |
|
|
{
|
136 |
|
|
for (; first != last; ++first)
|
137 |
|
|
if (pred (*first))
|
138 |
|
|
*result++ = *first;
|
139 |
|
|
return (result);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/// Remove_if removes from the range [first, last) every element x such that
|
143 |
|
|
/// pred(x) is true. That is, remove_if returns an iterator new_last such that
|
144 |
|
|
/// the range [first, new_last) contains no elements for which pred is true.
|
145 |
|
|
/// The iterators in the range [new_last, last) are all still dereferenceable,
|
146 |
|
|
/// but the elements that they point to are unspecified. Remove_if is stable,
|
147 |
|
|
/// meaning that the relative order of elements that are not removed is
|
148 |
|
|
/// unchanged.
|
149 |
|
|
/// \ingroup MutatingAlgorithms
|
150 |
|
|
/// \ingroup PredicateAlgorithms
|
151 |
|
|
///
|
152 |
|
|
template <typename ForwardIterator, typename Predicate>
|
153 |
|
|
inline ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, Predicate pred)
|
154 |
|
|
{
|
155 |
|
|
return (remove_copy_if (first, last, first, pred));
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/// The reason there are two different versions of unique_copy is that there
|
159 |
|
|
/// are two different definitions of what it means for a consecutive group of
|
160 |
|
|
/// elements to be duplicates. In the first version, the test is simple
|
161 |
|
|
/// equality: the elements in a range [f, l) are duplicates if, for every
|
162 |
|
|
/// iterator i in the range, either i == f or else *i == *(i-1). In the second,
|
163 |
|
|
/// the test is an arbitrary Binary Predicate binary_pred: the elements in
|
164 |
|
|
/// [f, l) are duplicates if, for every iterator i in the range, either
|
165 |
|
|
/// i == f or else binary_pred(*i, *(i-1)) is true.
|
166 |
|
|
/// \ingroup MutatingAlgorithms
|
167 |
|
|
/// \ingroup PredicateAlgorithms
|
168 |
|
|
///
|
169 |
|
|
template <typename InputIterator, typename OutputIterator, typename BinaryPredicate>
|
170 |
|
|
OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred)
|
171 |
|
|
{
|
172 |
|
|
if (first != last) {
|
173 |
|
|
*result = *first;
|
174 |
|
|
while (++first != last)
|
175 |
|
|
if (!binary_pred (*first, *result))
|
176 |
|
|
*++result = *first;
|
177 |
|
|
++ result;
|
178 |
|
|
}
|
179 |
|
|
return (result);
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
/// Every time a consecutive group of duplicate elements appears in the range
|
183 |
|
|
/// [first, last), the algorithm unique removes all but the first element.
|
184 |
|
|
/// That is, unique returns an iterator new_last such that the range [first,
|
185 |
|
|
/// new_last) contains no two consecutive elements that are duplicates.
|
186 |
|
|
/// The iterators in the range [new_last, last) are all still dereferenceable,
|
187 |
|
|
/// but the elements that they point to are unspecified. Unique is stable,
|
188 |
|
|
/// meaning that the relative order of elements that are not removed is
|
189 |
|
|
/// unchanged.
|
190 |
|
|
/// \ingroup MutatingAlgorithms
|
191 |
|
|
/// \ingroup PredicateAlgorithms
|
192 |
|
|
///
|
193 |
|
|
template <typename ForwardIterator, typename BinaryPredicate>
|
194 |
|
|
inline ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)
|
195 |
|
|
{
|
196 |
|
|
return (unique_copy (first, last, first, binary_pred));
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
/// Returns the furthermost iterator i in [first, last) such that,
|
200 |
|
|
/// for every iterator j in [first, i), comp(*j, value) is true.
|
201 |
|
|
/// Assumes the range is sorted.
|
202 |
|
|
/// \ingroup SearchingAlgorithms
|
203 |
|
|
/// \ingroup PredicateAlgorithms
|
204 |
|
|
///
|
205 |
|
|
template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
|
206 |
|
|
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& value, StrictWeakOrdering comp)
|
207 |
|
|
{
|
208 |
|
|
ForwardIterator mid;
|
209 |
|
|
while (first != last) {
|
210 |
|
|
mid = advance (first, distance (first,last) / 2);
|
211 |
|
|
if (comp (*mid, value))
|
212 |
|
|
first = mid + 1;
|
213 |
|
|
else
|
214 |
|
|
last = mid;
|
215 |
|
|
}
|
216 |
|
|
return (first);
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/// Performs a binary search inside the sorted range.
|
220 |
|
|
/// \ingroup SearchingAlgorithms
|
221 |
|
|
/// \ingroup PredicateAlgorithms
|
222 |
|
|
///
|
223 |
|
|
template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
|
224 |
|
|
inline bool binary_search (ForwardIterator first, ForwardIterator last, const T& value, StrictWeakOrdering comp)
|
225 |
|
|
{
|
226 |
|
|
ForwardIterator found = lower_bound (first, last, value, comp);
|
227 |
|
|
return (found != last && !comp(*found, value));
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
/// Returns the furthermost iterator i in [first,last) such that for
|
231 |
|
|
/// every iterator j in [first,i), comp(value,*j) is false.
|
232 |
|
|
/// \ingroup SearchingAlgorithms
|
233 |
|
|
/// \ingroup PredicateAlgorithms
|
234 |
|
|
///
|
235 |
|
|
template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
|
236 |
|
|
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& value, StrictWeakOrdering comp)
|
237 |
|
|
{
|
238 |
|
|
ForwardIterator mid;
|
239 |
|
|
while (first != last) {
|
240 |
|
|
mid = advance (first, distance (first,last) / 2);
|
241 |
|
|
if (comp (value, *mid))
|
242 |
|
|
last = mid;
|
243 |
|
|
else
|
244 |
|
|
first = mid + 1;
|
245 |
|
|
}
|
246 |
|
|
return (last);
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
/// Returns pair<lower_bound,upper_bound>
|
250 |
|
|
/// \ingroup SearchingAlgorithms
|
251 |
|
|
/// \ingroup PredicateAlgorithms
|
252 |
|
|
///
|
253 |
|
|
template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
|
254 |
|
|
inline pair<ForwardIterator,ForwardIterator> equal_range (ForwardIterator first, ForwardIterator last, const T& value, StrictWeakOrdering comp)
|
255 |
|
|
{
|
256 |
|
|
pair<ForwardIterator,ForwardIterator> rv;
|
257 |
|
|
rv.second = rv.first = lower_bound (first, last, value, comp);
|
258 |
|
|
while (rv.second != last && !comp(value, *(rv.second)))
|
259 |
|
|
++ rv.second;
|
260 |
|
|
return (rv);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
/// \brief Puts \p nth element into its sorted position.
|
264 |
|
|
/// In this implementation, the entire array is sorted. The performance difference is
|
265 |
|
|
/// so small and the function use is so rare, there is no need to have code for it.
|
266 |
|
|
/// \ingroup SortingAlgorithms
|
267 |
|
|
/// \ingroup SearchingAlgorithms
|
268 |
|
|
/// \ingroup PredicateAlgorithms
|
269 |
|
|
///
|
270 |
|
|
template <typename RandomAccessIterator, typename Compare>
|
271 |
|
|
inline void nth_element (RandomAccessIterator first, RandomAccessIterator, RandomAccessIterator last, Compare comp)
|
272 |
|
|
{
|
273 |
|
|
sort (first, last, comp);
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
/// \brief Searches for the first subsequence [first2,last2) in [first1,last1)
|
277 |
|
|
/// \ingroup SearchingAlgorithms
|
278 |
|
|
/// \ingroup PredicateAlgorithms
|
279 |
|
|
template <typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
280 |
|
|
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate comp)
|
281 |
|
|
{
|
282 |
|
|
const ForwardIterator1 slast = last1 - distance(first2, last2) + 1;
|
283 |
|
|
for (; first1 < slast; ++first1) {
|
284 |
|
|
ForwardIterator2 i = first2;
|
285 |
|
|
ForwardIterator1 j = first1;
|
286 |
|
|
for (; i != last2 && comp(*j, *i); ++i, ++j) ;
|
287 |
|
|
if (i == last2)
|
288 |
|
|
return (first1);
|
289 |
|
|
}
|
290 |
|
|
return (last1);
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
/// \brief Searches for the last subsequence [first2,last2) in [first1,last1)
|
294 |
|
|
/// \ingroup SearchingAlgorithms
|
295 |
|
|
/// \ingroup PredicateAlgorithms
|
296 |
|
|
template <typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
297 |
|
|
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate comp)
|
298 |
|
|
{
|
299 |
|
|
ForwardIterator1 s = last1 - distance(first2, last2);
|
300 |
|
|
for (; first1 < s; --s) {
|
301 |
|
|
ForwardIterator2 i = first2, j = s;
|
302 |
|
|
for (; i != last2 && comp(*j, *i); ++i, ++j) ;
|
303 |
|
|
if (i == last2)
|
304 |
|
|
return (s);
|
305 |
|
|
}
|
306 |
|
|
return (last1);
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
/// \brief Searches for the first occurence of \p count \p values in [first, last)
|
310 |
|
|
/// \ingroup SearchingAlgorithms
|
311 |
|
|
/// \ingroup PredicateAlgorithms
|
312 |
|
|
template <typename Iterator, typename T, typename BinaryPredicate>
|
313 |
|
|
Iterator search_n (Iterator first, Iterator last, size_t count, const T& value, BinaryPredicate comp)
|
314 |
|
|
{
|
315 |
|
|
size_t n = 0;
|
316 |
|
|
for (; first != last; ++first) {
|
317 |
|
|
if (!comp (*first, value))
|
318 |
|
|
n = 0;
|
319 |
|
|
else if (++n == count)
|
320 |
|
|
return (first - --n);
|
321 |
|
|
}
|
322 |
|
|
return (last);
|
323 |
|
|
}
|
324 |
|
|
|
325 |
|
|
/// \brief Searches [first1,last1) for the first occurrence of an element from [first2,last2)
|
326 |
|
|
/// \ingroup SearchingAlgorithms
|
327 |
|
|
/// \ingroup PredicateAlgorithms
|
328 |
|
|
template <typename InputIterator, typename ForwardIterator, typename BinaryPredicate>
|
329 |
|
|
InputIterator find_first_of (InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate comp)
|
330 |
|
|
{
|
331 |
|
|
for (; first1 != last1; ++first1)
|
332 |
|
|
for (ForwardIterator i = first2; i != last2; ++i)
|
333 |
|
|
if (comp (*first1, *i))
|
334 |
|
|
return (first1);
|
335 |
|
|
return (first1);
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
/// \brief Returns true if [first2,last2) is a subset of [first1,last1)
|
339 |
|
|
/// \ingroup ConditionAlgorithms
|
340 |
|
|
/// \ingroup SetAlgorithms
|
341 |
|
|
/// \ingroup PredicateAlgorithms
|
342 |
|
|
template <typename InputIterator1, typename InputIterator2, typename StrictWeakOrdering>
|
343 |
|
|
bool includes (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, StrictWeakOrdering comp)
|
344 |
|
|
{
|
345 |
|
|
for (; (first1 != last1) & (first2 != last2); ++first1) {
|
346 |
|
|
if (comp (*first2, *first1))
|
347 |
|
|
return (false);
|
348 |
|
|
first2 += !comp (*first1, *first2);
|
349 |
|
|
}
|
350 |
|
|
return (first2 == last2);
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
/// \brief Merges [first1,last1) with [first2,last2)
|
354 |
|
|
///
|
355 |
|
|
/// Result will contain every element that is in either set. If duplicate
|
356 |
|
|
/// elements are present, max(n,m) is placed in the result.
|
357 |
|
|
///
|
358 |
|
|
/// \ingroup SetAlgorithms
|
359 |
|
|
/// \ingroup PredicateAlgorithms
|
360 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakOrdering>
|
361 |
|
|
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakOrdering comp)
|
362 |
|
|
{
|
363 |
|
|
for (; (first1 != last1) & (first2 != last2); ++result) {
|
364 |
|
|
if (comp (*first2, *first1))
|
365 |
|
|
*result = *first2++;
|
366 |
|
|
else {
|
367 |
|
|
first2 += !comp (*first1, *first2);
|
368 |
|
|
*result = *first1++;
|
369 |
|
|
}
|
370 |
|
|
}
|
371 |
|
|
return (copy (first2, last2, copy (first1, last1, result)));
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
/// \brief Creates a set containing elements shared by the given ranges.
|
375 |
|
|
/// \ingroup SetAlgorithms
|
376 |
|
|
/// \ingroup PredicateAlgorithms
|
377 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakOrdering>
|
378 |
|
|
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakOrdering comp)
|
379 |
|
|
{
|
380 |
|
|
while ((first1 != last1) & (first2 != last2)) {
|
381 |
|
|
bool b1ge2 = !comp (*first1, *first2), b2ge1 = !comp (*first2, *first1);
|
382 |
|
|
if (b1ge2 & b2ge1)
|
383 |
|
|
*result++ = *first1;
|
384 |
|
|
first1 += b2ge1;
|
385 |
|
|
first2 += b1ge2;
|
386 |
|
|
}
|
387 |
|
|
return (result);
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
/// \brief Removes from [first1,last1) elements present in [first2,last2)
|
391 |
|
|
/// \ingroup SetAlgorithms
|
392 |
|
|
/// \ingroup PredicateAlgorithms
|
393 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakOrdering>
|
394 |
|
|
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakOrdering comp)
|
395 |
|
|
{
|
396 |
|
|
while ((first1 != last1) & (first2 != last2)) {
|
397 |
|
|
bool b1ge2 = !comp (*first1, *first2), b2ge1 = !comp (*first2, *first1);
|
398 |
|
|
if (!b1ge2)
|
399 |
|
|
*result++ = *first1;
|
400 |
|
|
first1 += b2ge1;
|
401 |
|
|
first2 += b1ge2;
|
402 |
|
|
}
|
403 |
|
|
return (copy (first1, last1, result));
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
/// \brief Performs union of sets A-B and B-A.
|
407 |
|
|
/// \ingroup SetAlgorithms
|
408 |
|
|
/// \ingroup PredicateAlgorithms
|
409 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakOrdering>
|
410 |
|
|
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakOrdering comp)
|
411 |
|
|
{
|
412 |
|
|
while ((first1 != last1) & (first2 != last2)) {
|
413 |
|
|
bool b1l2 = comp (*first1, *first2), b2l1 = comp (*first2, *first1);
|
414 |
|
|
if (b1l2)
|
415 |
|
|
*result++ = *first1;
|
416 |
|
|
else if (b2l1)
|
417 |
|
|
*result++ = *first2;
|
418 |
|
|
first1 += !b2l1;
|
419 |
|
|
first2 += !b1l2;
|
420 |
|
|
}
|
421 |
|
|
return (copy (first2, last2, copy (first1, last1, result)));
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
/// \brief Returns true if the given range is sorted.
|
425 |
|
|
/// \ingroup ConditionAlgorithms
|
426 |
|
|
/// \ingroup PredicateAlgorithms
|
427 |
|
|
template <typename ForwardIterator, typename StrictWeakOrdering>
|
428 |
|
|
bool is_sorted (ForwardIterator first, ForwardIterator last, StrictWeakOrdering comp)
|
429 |
|
|
{
|
430 |
|
|
for (ForwardIterator i = first; ++i < last; ++first)
|
431 |
|
|
if (comp (*i, *first))
|
432 |
|
|
return (false);
|
433 |
|
|
return (true);
|
434 |
|
|
}
|
435 |
|
|
|
436 |
|
|
/// \brief Compares two given containers like strcmp compares strings.
|
437 |
|
|
/// \ingroup ConditionAlgorithms
|
438 |
|
|
/// \ingroup PredicateAlgorithms
|
439 |
|
|
template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
|
440 |
|
|
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate comp)
|
441 |
|
|
{
|
442 |
|
|
for (; (first1 != last1) & (first2 != last2); ++first1, ++first2) {
|
443 |
|
|
if (comp (*first1, *first2))
|
444 |
|
|
return (true);
|
445 |
|
|
if (comp (*first2, *first1))
|
446 |
|
|
return (false);
|
447 |
|
|
}
|
448 |
|
|
return ((first1 == last1) & (first2 != last2));
|
449 |
|
|
}
|
450 |
|
|
|
451 |
|
|
/// \brief Creates the next lexicographical permutation of [first,last).
|
452 |
|
|
/// Returns false if no further permutations can be created.
|
453 |
|
|
/// \ingroup GeneratorAlgorithms
|
454 |
|
|
/// \ingroup PredicateAlgorithms
|
455 |
|
|
template <typename BidirectionalIterator, typename StrictWeakOrdering>
|
456 |
|
|
bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, StrictWeakOrdering comp)
|
457 |
|
|
{
|
458 |
|
|
if (distance (first, last) < 2)
|
459 |
|
|
return (false);
|
460 |
|
|
BidirectionalIterator i = last;
|
461 |
|
|
for (--i; i != first; ) {
|
462 |
|
|
--i;
|
463 |
|
|
if (comp (i[0], i[1])) {
|
464 |
|
|
BidirectionalIterator j = last;
|
465 |
|
|
while (!comp (*i, *--j)) ;
|
466 |
|
|
iter_swap (i, j);
|
467 |
|
|
reverse (i + 1, last);
|
468 |
|
|
return (true);
|
469 |
|
|
}
|
470 |
|
|
}
|
471 |
|
|
reverse (first, last);
|
472 |
|
|
return (false);
|
473 |
|
|
}
|
474 |
|
|
|
475 |
|
|
/// \brief Creates the previous lexicographical permutation of [first,last).
|
476 |
|
|
/// Returns false if no further permutations can be created.
|
477 |
|
|
/// \ingroup GeneratorAlgorithms
|
478 |
|
|
/// \ingroup PredicateAlgorithms
|
479 |
|
|
template <typename BidirectionalIterator, typename StrictWeakOrdering>
|
480 |
|
|
bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last, StrictWeakOrdering comp)
|
481 |
|
|
{
|
482 |
|
|
if (distance (first, last) < 2)
|
483 |
|
|
return (false);
|
484 |
|
|
BidirectionalIterator i = last;
|
485 |
|
|
for (--i; i != first; ) {
|
486 |
|
|
--i;
|
487 |
|
|
if (comp(i[1], i[0])) {
|
488 |
|
|
BidirectionalIterator j = last;
|
489 |
|
|
while (!comp (*--j, *i)) ;
|
490 |
|
|
iter_swap (i, j);
|
491 |
|
|
reverse (i + 1, last);
|
492 |
|
|
return (true);
|
493 |
|
|
}
|
494 |
|
|
}
|
495 |
|
|
reverse (first, last);
|
496 |
|
|
return (false);
|
497 |
|
|
}
|
498 |
|
|
|
499 |
|
|
/// \brief Returns iterator to the max element in [first,last)
|
500 |
|
|
/// \ingroup SearchingAlgorithms
|
501 |
|
|
/// \ingroup PredicateAlgorithms
|
502 |
|
|
template <typename ForwardIterator, typename BinaryPredicate>
|
503 |
|
|
inline ForwardIterator max_element (ForwardIterator first, ForwardIterator last, BinaryPredicate comp)
|
504 |
|
|
{
|
505 |
|
|
ForwardIterator result = first;
|
506 |
|
|
for (; first != last; ++first)
|
507 |
|
|
if (comp (*result, *first))
|
508 |
|
|
result = first;
|
509 |
|
|
return (result);
|
510 |
|
|
}
|
511 |
|
|
|
512 |
|
|
/// \brief Returns iterator to the min element in [first,last)
|
513 |
|
|
/// \ingroup SearchingAlgorithms
|
514 |
|
|
/// \ingroup PredicateAlgorithms
|
515 |
|
|
template <typename ForwardIterator, typename BinaryPredicate>
|
516 |
|
|
inline ForwardIterator min_element (ForwardIterator first, ForwardIterator last, BinaryPredicate comp)
|
517 |
|
|
{
|
518 |
|
|
ForwardIterator result = first;
|
519 |
|
|
for (; first != last; ++first)
|
520 |
|
|
if (comp (*first, *result))
|
521 |
|
|
result = first;
|
522 |
|
|
return (result);
|
523 |
|
|
}
|
524 |
|
|
|
525 |
|
|
/// \brief Makes [first,middle) a part of the sorted array.
|
526 |
|
|
/// Contents of [middle,last) is undefined. This implementation just calls stable_sort.
|
527 |
|
|
/// \ingroup SortingAlgorithms
|
528 |
|
|
/// \ingroup PredicateAlgorithms
|
529 |
|
|
template <typename RandomAccessIterator, typename StrictWeakOrdering>
|
530 |
|
|
inline void partial_sort (RandomAccessIterator first, RandomAccessIterator, RandomAccessIterator last, StrictWeakOrdering comp)
|
531 |
|
|
{
|
532 |
|
|
stable_sort (first, last, comp);
|
533 |
|
|
}
|
534 |
|
|
|
535 |
|
|
/// \brief Like partial_sort, but outputs to [result_first,result_last)
|
536 |
|
|
/// \ingroup SortingAlgorithms
|
537 |
|
|
/// \ingroup PredicateAlgorithms
|
538 |
|
|
template <typename InputIterator, typename RandomAccessIterator, typename StrictWeakOrdering>
|
539 |
|
|
RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, StrictWeakOrdering comp)
|
540 |
|
|
{
|
541 |
|
|
RandomAccessIterator rend = result_first;
|
542 |
|
|
for (; first != last; ++first) {
|
543 |
|
|
RandomAccessIterator i = result_first;
|
544 |
|
|
for (; i != rend && comp (*i, *first); ++i) ;
|
545 |
|
|
if (i == result_last)
|
546 |
|
|
continue;
|
547 |
|
|
rend += (rend < result_last);
|
548 |
|
|
copy_backward (i, rend - 1, rend);
|
549 |
|
|
*i = *first;
|
550 |
|
|
}
|
551 |
|
|
return (rend);
|
552 |
|
|
}
|
553 |
|
|
|
554 |
|
|
/// \brief Like partition, but preserves equal element order.
|
555 |
|
|
/// \ingroup SortingAlgorithms
|
556 |
|
|
/// \ingroup PredicateAlgorithms
|
557 |
|
|
template <typename ForwardIterator, typename Predicate>
|
558 |
|
|
ForwardIterator stable_partition (ForwardIterator first, ForwardIterator last, Predicate pred)
|
559 |
|
|
{
|
560 |
|
|
if (first == last)
|
561 |
|
|
return (first);
|
562 |
|
|
ForwardIterator l, r, m = advance (first, distance (first, last) / 2);
|
563 |
|
|
if (first == m)
|
564 |
|
|
return (pred(*first) ? last : first);
|
565 |
|
|
l = stable_partition (first, m, pred);
|
566 |
|
|
r = stable_partition (m, last, pred);
|
567 |
|
|
rotate (l, m, r);
|
568 |
|
|
return (advance (l, distance (m, r)));
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
/// \brief Splits [first,last) in two by \p pred.
|
572 |
|
|
///
|
573 |
|
|
/// Creates two ranges [first,middle) and [middle,last), where every element
|
574 |
|
|
/// in the former is less than every element in the latter.
|
575 |
|
|
/// The return value is middle.
|
576 |
|
|
///
|
577 |
|
|
/// \ingroup SortingAlgorithms
|
578 |
|
|
/// \ingroup PredicateAlgorithms
|
579 |
|
|
template <typename ForwardIterator, typename Predicate>
|
580 |
|
|
inline ForwardIterator partition (ForwardIterator first, ForwardIterator last, Predicate pred)
|
581 |
|
|
{
|
582 |
|
|
return (stable_partition (first, last, pred));
|
583 |
|
|
}
|
584 |
|
|
|
585 |
|
|
} // namespace ustl
|
586 |
|
|
|
587 |
|
|
#endif
|