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 UALGO_H_711AB4214D417A51166694D47A662D6E
|
7 |
|
|
#define UALGO_H_711AB4214D417A51166694D47A662D6E
|
8 |
|
|
|
9 |
|
|
#include "upair.h"
|
10 |
|
|
#include "ualgobase.h"
|
11 |
|
|
#include "ufunction.h"
|
12 |
|
|
#include "upredalgo.h"
|
13 |
|
|
#include "umemory.h"
|
14 |
|
|
#include <stdlib.h> // for rand()
|
15 |
|
|
|
16 |
|
|
namespace ustl {
|
17 |
|
|
|
18 |
|
|
/// Swaps corresponding elements of [first, last) and [result,)
|
19 |
|
|
/// \ingroup SwapAlgorithms
|
20 |
|
|
///
|
21 |
|
|
template <typename ForwardIterator1, typename ForwardIterator2>
|
22 |
|
|
inline ForwardIterator2 swap_ranges (ForwardIterator1 first, ForwardIterator2 last, ForwardIterator2 result)
|
23 |
|
|
{
|
24 |
|
|
for (; first != last; ++first, ++result)
|
25 |
|
|
iter_swap (first, result);
|
26 |
|
|
return (result);
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
/// Returns the first iterator i in the range [first, last) such that
|
30 |
|
|
/// *i == value. Returns last if no such iterator exists.
|
31 |
|
|
/// \ingroup SearchingAlgorithms
|
32 |
|
|
///
|
33 |
|
|
template <typename InputIterator, typename EqualityComparable>
|
34 |
|
|
inline InputIterator find (InputIterator first, InputIterator last, const EqualityComparable& value)
|
35 |
|
|
{
|
36 |
|
|
while (first != last && !(*first == value))
|
37 |
|
|
++ first;
|
38 |
|
|
return (first);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
/// Returns the first iterator such that *i == *(i + 1)
|
42 |
|
|
/// \ingroup SearchingAlgorithms
|
43 |
|
|
///
|
44 |
|
|
template <typename ForwardIterator>
|
45 |
|
|
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
|
46 |
|
|
{
|
47 |
|
|
if (first != last)
|
48 |
|
|
for (ForwardIterator prev = first; ++first != last; ++ prev)
|
49 |
|
|
if (*prev == *first)
|
50 |
|
|
return (prev);
|
51 |
|
|
return (last);
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
/// Returns the pointer to the first pair of unequal elements.
|
55 |
|
|
/// \ingroup SearchingAlgorithms
|
56 |
|
|
///
|
57 |
|
|
template <typename InputIterator>
|
58 |
|
|
pair<InputIterator,InputIterator>
|
59 |
|
|
mismatch (InputIterator first1, InputIterator last1, InputIterator first2)
|
60 |
|
|
{
|
61 |
|
|
while (first1 != last1 && *first1 == *first2)
|
62 |
|
|
++ first1, ++ first2;
|
63 |
|
|
return (make_pair (first1, first2));
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
/// \brief Returns true if two ranges are equal.
|
67 |
|
|
/// This is an extension, present in uSTL and SGI STL.
|
68 |
|
|
/// \ingroup SearchingAlgorithms
|
69 |
|
|
///
|
70 |
|
|
template <typename InputIterator>
|
71 |
|
|
inline bool equal (InputIterator first1, InputIterator last1, InputIterator first2)
|
72 |
|
|
{
|
73 |
|
|
return (mismatch (first1, last1, first2).first == last1);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
/// Count finds the number of elements in [first, last) that are equal
|
77 |
|
|
/// to value. More precisely, the first version of count returns the
|
78 |
|
|
/// number of iterators i in [first, last) such that *i == value.
|
79 |
|
|
/// \ingroup SearchingAlgorithms
|
80 |
|
|
///
|
81 |
|
|
template <typename InputIterator, typename EqualityComparable>
|
82 |
|
|
inline size_t count (InputIterator first, InputIterator last, const EqualityComparable& value)
|
83 |
|
|
{
|
84 |
|
|
size_t total = 0;
|
85 |
|
|
for (; first != last; ++first)
|
86 |
|
|
if (*first == value)
|
87 |
|
|
++ total;
|
88 |
|
|
return (total);
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
///
|
92 |
|
|
/// The first version of transform performs the operation op(*i) for each
|
93 |
|
|
/// iterator i in the range [first, last), and assigns the result of that
|
94 |
|
|
/// operation to *o, where o is the corresponding output iterator. That is,
|
95 |
|
|
/// for each n such that 0 <= n < last - first, it performs the assignment
|
96 |
|
|
/// *(result + n) = op(*(first + n)).
|
97 |
|
|
/// The return value is result + (last - first).
|
98 |
|
|
/// \ingroup MutatingAlgorithms
|
99 |
|
|
/// \ingroup PredicateAlgorithms
|
100 |
|
|
///
|
101 |
|
|
template <typename InputIterator, typename OutputIterator, typename UnaryFunction>
|
102 |
|
|
inline OutputIterator transform (InputIterator first, InputIterator last, OutputIterator result, UnaryFunction op)
|
103 |
|
|
{
|
104 |
|
|
for (; first != last; ++result, ++first)
|
105 |
|
|
*result = op (*first);
|
106 |
|
|
return (result);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
///
|
110 |
|
|
/// The second version of transform is very similar, except that it uses a
|
111 |
|
|
/// Binary Function instead of a Unary Function: it performs the operation
|
112 |
|
|
/// op(*i1, *i2) for each iterator i1 in the range [first1, last1) and assigns
|
113 |
|
|
/// the result to *o, where i2 is the corresponding iterator in the second
|
114 |
|
|
/// input range and where o is the corresponding output iterator. That is,
|
115 |
|
|
/// for each n such that 0 <= n < last1 - first1, it performs the assignment
|
116 |
|
|
/// *(result + n) = op(*(first1 + n), *(first2 + n).
|
117 |
|
|
/// The return value is result + (last1 - first1).
|
118 |
|
|
/// \ingroup MutatingAlgorithms
|
119 |
|
|
/// \ingroup PredicateAlgorithms
|
120 |
|
|
///
|
121 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryFunction>
|
122 |
|
|
inline OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryFunction op)
|
123 |
|
|
{
|
124 |
|
|
for (; first1 != last1; ++result, ++first1, ++first2)
|
125 |
|
|
*result = op (*first1, *first2);
|
126 |
|
|
return (result);
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/// Replace replaces every element in the range [first, last) equal to
|
130 |
|
|
/// old_value with new_value. That is: for every iterator i,
|
131 |
|
|
/// if *i == old_value then it performs the assignment *i = new_value.
|
132 |
|
|
/// \ingroup MutatingAlgorithms
|
133 |
|
|
///
|
134 |
|
|
template <typename ForwardIterator, typename T>
|
135 |
|
|
inline void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
|
136 |
|
|
{
|
137 |
|
|
for (; first != last; ++first)
|
138 |
|
|
if (*first == old_value)
|
139 |
|
|
*first = new_value;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/// Replace_copy copies elements from the range [first, last) to the range
|
143 |
|
|
/// [result, result + (last-first)), except that any element equal to old_value
|
144 |
|
|
/// is not copied; new_value is copied instead. More precisely, for every
|
145 |
|
|
/// integer n such that 0 <= n < last-first, replace_copy performs the
|
146 |
|
|
/// assignment *(result+n) = new_value if *(first+n) == old_value, and
|
147 |
|
|
/// *(result+n) = *(first+n) otherwise.
|
148 |
|
|
/// \ingroup MutatingAlgorithms
|
149 |
|
|
///
|
150 |
|
|
template <typename InputIterator, typename OutputIterator, typename T>
|
151 |
|
|
inline OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value)
|
152 |
|
|
{
|
153 |
|
|
for (; first != last; ++result, ++first)
|
154 |
|
|
*result = (*first == old_value) ? new_value : *first;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
/// Generate assigns the result of invoking gen, a function object that
|
158 |
|
|
/// takes no arguments, to each element in the range [first, last).
|
159 |
|
|
/// \ingroup GeneratorAlgorithms
|
160 |
|
|
/// \ingroup PredicateAlgorithms
|
161 |
|
|
///
|
162 |
|
|
template <typename ForwardIterator, typename Generator>
|
163 |
|
|
inline void generate (ForwardIterator first, ForwardIterator last, Generator gen)
|
164 |
|
|
{
|
165 |
|
|
for (; first != last; ++first)
|
166 |
|
|
*first = gen();
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/// Generate_n assigns the result of invoking gen, a function object that
|
170 |
|
|
/// takes no arguments, to each element in the range [first, first+n).
|
171 |
|
|
/// The return value is first + n.
|
172 |
|
|
/// \ingroup GeneratorAlgorithms
|
173 |
|
|
/// \ingroup PredicateAlgorithms
|
174 |
|
|
///
|
175 |
|
|
template <typename OutputIterator, typename Generator>
|
176 |
|
|
inline OutputIterator generate_n (OutputIterator first, size_t n, Generator gen)
|
177 |
|
|
{
|
178 |
|
|
for (uoff_t i = 0; i != n; ++i, ++first)
|
179 |
|
|
*first = gen();
|
180 |
|
|
return (first);
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/// \brief Reverse reverses a range.
|
184 |
|
|
/// That is: for every i such that 0 <= i <= (last - first) / 2),
|
185 |
|
|
/// it exchanges *(first + i) and *(last - (i + 1)).
|
186 |
|
|
/// \ingroup MutatingAlgorithms
|
187 |
|
|
///
|
188 |
|
|
template <typename BidirectionalIterator>
|
189 |
|
|
inline void reverse (BidirectionalIterator first, BidirectionalIterator last)
|
190 |
|
|
{
|
191 |
|
|
for (; distance (first, --last) > 0; ++first)
|
192 |
|
|
iter_swap (first, last);
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/// \brief Reverses [first,last) and writes it to \p output.
|
196 |
|
|
/// \ingroup MutatingAlgorithms
|
197 |
|
|
///
|
198 |
|
|
template <typename BidirectionalIterator, typename OutputIterator>
|
199 |
|
|
inline OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result)
|
200 |
|
|
{
|
201 |
|
|
for (; first != last; ++result)
|
202 |
|
|
*result = *--last;
|
203 |
|
|
return (result);
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
/// \brief Exchanges ranges [first, middle) and [middle, last)
|
207 |
|
|
/// \ingroup MutatingAlgorithms
|
208 |
|
|
///
|
209 |
|
|
template <typename ForwardIterator>
|
210 |
|
|
ForwardIterator rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last)
|
211 |
|
|
{
|
212 |
|
|
if (first == middle || middle == last)
|
213 |
|
|
return (first);
|
214 |
|
|
reverse (first, middle);
|
215 |
|
|
reverse (middle, last);
|
216 |
|
|
for (;first != middle && middle != last; ++first)
|
217 |
|
|
iter_swap (first, --last);
|
218 |
|
|
reverse (first, (first == middle ? last : middle));
|
219 |
|
|
return (first);
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
/// Specialization for pointers, which can be treated identically.
|
223 |
|
|
template <typename T>
|
224 |
|
|
inline T* rotate (T* first, T* middle, T* last)
|
225 |
|
|
{
|
226 |
|
|
rotate_fast (first, middle, last);
|
227 |
|
|
return (first);
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
/// \brief Exchanges ranges [first, middle) and [middle, last) into \p result.
|
232 |
|
|
/// \ingroup MutatingAlgorithms
|
233 |
|
|
///
|
234 |
|
|
template <typename ForwardIterator, typename OutputIterator>
|
235 |
|
|
inline OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result)
|
236 |
|
|
{
|
237 |
|
|
return (copy (first, middle, copy (middle, last, result)));
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
/// \brief Combines two sorted ranges.
|
241 |
|
|
/// \ingroup SortingAlgorithms
|
242 |
|
|
///
|
243 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
|
244 |
|
|
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
|
245 |
|
|
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
|
246 |
|
|
{
|
247 |
|
|
for (; first1 != last1 && first2 != last2; ++result) {
|
248 |
|
|
if (*first1 < *first2)
|
249 |
|
|
*result = *first1++;
|
250 |
|
|
else
|
251 |
|
|
*result = *first2++;
|
252 |
|
|
}
|
253 |
|
|
if (first1 < last1)
|
254 |
|
|
return (copy (first1, last1, result));
|
255 |
|
|
else
|
256 |
|
|
return (copy (first2, last2, result));
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
/// Combines two sorted ranges from the same container.
|
260 |
|
|
/// \ingroup SortingAlgorithms
|
261 |
|
|
///
|
262 |
|
|
template <typename InputIterator>
|
263 |
|
|
void inplace_merge (InputIterator first, InputIterator middle, InputIterator last)
|
264 |
|
|
{
|
265 |
|
|
for (; middle != last; ++first) {
|
266 |
|
|
while (*first < *middle)
|
267 |
|
|
++ first;
|
268 |
|
|
reverse (first, middle);
|
269 |
|
|
reverse (first, ++middle);
|
270 |
|
|
}
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
/// Remove_copy copies elements that are not equal to value from the range
|
274 |
|
|
/// [first, last) to a range beginning at result. The return value is the
|
275 |
|
|
/// end of the resulting range. This operation is stable, meaning that the
|
276 |
|
|
/// relative order of the elements that are copied is the same as in the
|
277 |
|
|
/// range [first, last).
|
278 |
|
|
/// \ingroup MutatingAlgorithms
|
279 |
|
|
///
|
280 |
|
|
template <typename InputIterator, typename OutputIterator, typename T>
|
281 |
|
|
OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& value)
|
282 |
|
|
{
|
283 |
|
|
for (; first != last; ++first) {
|
284 |
|
|
if (!(*first == value)) {
|
285 |
|
|
*result = *first;
|
286 |
|
|
++ result;
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
return (result);
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
/// Remove_copy copies elements pointed to by iterators in [rfirst, rlast)
|
293 |
|
|
/// from the range [first, last) to a range beginning at result. The return
|
294 |
|
|
/// value is the end of the resulting range. This operation is stable, meaning
|
295 |
|
|
/// that the relative order of the elements that are copied is the same as in the
|
296 |
|
|
/// range [first, last). Range [rfirst, rlast) is assumed to be sorted.
|
297 |
|
|
/// This algorithm is a uSTL extension.
|
298 |
|
|
/// \ingroup MutatingAlgorithms
|
299 |
|
|
///
|
300 |
|
|
template <typename InputIterator, typename OutputIterator, typename RInputIterator>
|
301 |
|
|
OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, RInputIterator rfirst, RInputIterator rlast)
|
302 |
|
|
{
|
303 |
|
|
for (; first != last; ++first) {
|
304 |
|
|
while (rfirst != rlast && *rfirst < first)
|
305 |
|
|
++ rfirst;
|
306 |
|
|
if (rfirst == rlast || first != *rfirst) {
|
307 |
|
|
*result = *first;
|
308 |
|
|
++ result;
|
309 |
|
|
}
|
310 |
|
|
}
|
311 |
|
|
return (result);
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
/// Remove removes from the range [first, last) all elements that are equal to
|
315 |
|
|
/// value. That is, remove returns an iterator new_last such that the range
|
316 |
|
|
/// [first, new_last) contains no elements equal to value. [1] The iterators
|
317 |
|
|
/// in the range [new_last, last) are all still dereferenceable, but the
|
318 |
|
|
/// elements that they point to are unspecified. Remove is stable, meaning
|
319 |
|
|
/// that the relative order of elements that are not equal to value is
|
320 |
|
|
/// unchanged.
|
321 |
|
|
/// \ingroup MutatingAlgorithms
|
322 |
|
|
///
|
323 |
|
|
template <typename ForwardIterator, typename T>
|
324 |
|
|
inline ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& value)
|
325 |
|
|
{
|
326 |
|
|
return (remove_copy (first, last, first, value));
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
/// Unique_copy copies elements from the range [first, last) to a range
|
330 |
|
|
/// beginning with result, except that in a consecutive group of duplicate
|
331 |
|
|
/// elements only the first one is copied. The return value is the end of
|
332 |
|
|
/// the range to which the elements are copied. This behavior is similar
|
333 |
|
|
/// to the Unix filter uniq.
|
334 |
|
|
/// \ingroup MutatingAlgorithms
|
335 |
|
|
///
|
336 |
|
|
template <typename InputIterator, typename OutputIterator>
|
337 |
|
|
OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result)
|
338 |
|
|
{
|
339 |
|
|
if (first != last) {
|
340 |
|
|
*result = *first;
|
341 |
|
|
while (++first != last)
|
342 |
|
|
if (!(*first == *result))
|
343 |
|
|
*++result = *first;
|
344 |
|
|
++ result;
|
345 |
|
|
}
|
346 |
|
|
return (result);
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
/// Every time a consecutive group of duplicate elements appears in the range
|
350 |
|
|
/// [first, last), the algorithm unique removes all but the first element.
|
351 |
|
|
/// That is, unique returns an iterator new_last such that the range [first,
|
352 |
|
|
/// new_last) contains no two consecutive elements that are duplicates.
|
353 |
|
|
/// The iterators in the range [new_last, last) are all still dereferenceable,
|
354 |
|
|
/// but the elements that they point to are unspecified. Unique is stable,
|
355 |
|
|
/// meaning that the relative order of elements that are not removed is
|
356 |
|
|
/// unchanged.
|
357 |
|
|
/// \ingroup MutatingAlgorithms
|
358 |
|
|
///
|
359 |
|
|
template <typename ForwardIterator>
|
360 |
|
|
inline ForwardIterator unique (ForwardIterator first, ForwardIterator last)
|
361 |
|
|
{
|
362 |
|
|
return (unique_copy (first, last, first));
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
/// Returns the furthermost iterator i in [first, last) such that,
|
366 |
|
|
/// for every iterator j in [first, i), *j < value
|
367 |
|
|
/// Assumes the range is sorted.
|
368 |
|
|
/// \ingroup SearchingAlgorithms
|
369 |
|
|
///
|
370 |
|
|
template <typename ForwardIterator, typename LessThanComparable>
|
371 |
|
|
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const LessThanComparable& value)
|
372 |
|
|
{
|
373 |
|
|
ForwardIterator mid;
|
374 |
|
|
while (first != last) {
|
375 |
|
|
mid = advance (first, distance (first,last) / 2);
|
376 |
|
|
if (*mid < value)
|
377 |
|
|
first = mid + 1;
|
378 |
|
|
else
|
379 |
|
|
last = mid;
|
380 |
|
|
}
|
381 |
|
|
return (first);
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
/// Performs a binary search inside the sorted range.
|
385 |
|
|
/// \ingroup SearchingAlgorithms
|
386 |
|
|
///
|
387 |
|
|
template <typename ForwardIterator, typename LessThanComparable>
|
388 |
|
|
inline bool binary_search (ForwardIterator first, ForwardIterator last, const LessThanComparable& value)
|
389 |
|
|
{
|
390 |
|
|
ForwardIterator found = lower_bound (first, last, value);
|
391 |
|
|
return (found != last && !(value < *found));
|
392 |
|
|
}
|
393 |
|
|
|
394 |
|
|
/// Returns the furthermost iterator i in [first,last) such that for
|
395 |
|
|
/// every iterator j in [first,i), value < *j is false.
|
396 |
|
|
/// \ingroup SearchingAlgorithms
|
397 |
|
|
///
|
398 |
|
|
template <typename ForwardIterator, typename LessThanComparable>
|
399 |
|
|
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const LessThanComparable& value)
|
400 |
|
|
{
|
401 |
|
|
ForwardIterator mid;
|
402 |
|
|
while (first != last) {
|
403 |
|
|
mid = advance (first, distance (first,last) / 2);
|
404 |
|
|
if (value < *mid)
|
405 |
|
|
last = mid;
|
406 |
|
|
else
|
407 |
|
|
first = mid + 1;
|
408 |
|
|
}
|
409 |
|
|
return (last);
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
/// Returns pair<lower_bound,upper_bound>
|
413 |
|
|
/// \ingroup SearchingAlgorithms
|
414 |
|
|
///
|
415 |
|
|
template <typename ForwardIterator, typename LessThanComparable>
|
416 |
|
|
inline pair<ForwardIterator,ForwardIterator> equal_range (ForwardIterator first, ForwardIterator last, const LessThanComparable& value)
|
417 |
|
|
{
|
418 |
|
|
pair<ForwardIterator,ForwardIterator> rv;
|
419 |
|
|
rv.second = rv.first = lower_bound (first, last, value);
|
420 |
|
|
while (rv.second != last && !(value < *(rv.second)))
|
421 |
|
|
++ rv.second;
|
422 |
|
|
return (rv);
|
423 |
|
|
}
|
424 |
|
|
|
425 |
|
|
/// Randomly permute the elements of the container.
|
426 |
|
|
/// \ingroup GeneratorAlgorithms
|
427 |
|
|
///
|
428 |
|
|
template <typename RandomAccessIterator>
|
429 |
|
|
void random_shuffle (RandomAccessIterator first, RandomAccessIterator last)
|
430 |
|
|
{
|
431 |
|
|
for (; first != last; ++ first)
|
432 |
|
|
iter_swap (first, first + (rand() % distance (first, last)));
|
433 |
|
|
}
|
434 |
|
|
|
435 |
|
|
/// \brief Generic compare function adaptor to pass to qsort
|
436 |
|
|
/// \ingroup FunctorObjects
|
437 |
|
|
template <typename ConstPointer, typename Compare>
|
438 |
|
|
int qsort_adapter (const void* p1, const void* p2)
|
439 |
|
|
{
|
440 |
|
|
ConstPointer i1 = reinterpret_cast<ConstPointer>(p1);
|
441 |
|
|
ConstPointer i2 = reinterpret_cast<ConstPointer>(p2);
|
442 |
|
|
Compare comp;
|
443 |
|
|
return (comp (*i1, *i2) ? -1 : (comp (*i2, *i1) ? 1 : 0));
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
/// Sorts the container
|
447 |
|
|
/// \ingroup SortingAlgorithms
|
448 |
|
|
/// \ingroup PredicateAlgorithms
|
449 |
|
|
///
|
450 |
|
|
template <typename RandomAccessIterator, typename Compare>
|
451 |
|
|
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare)
|
452 |
|
|
{
|
453 |
|
|
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
|
454 |
|
|
typedef typename iterator_traits<RandomAccessIterator>::const_pointer const_pointer;
|
455 |
|
|
qsort (first, distance (first, last), sizeof(value_type),
|
456 |
|
|
&qsort_adapter<const_pointer, Compare>);
|
457 |
|
|
}
|
458 |
|
|
|
459 |
|
|
/// Sorts the container
|
460 |
|
|
/// \ingroup SortingAlgorithms
|
461 |
|
|
///
|
462 |
|
|
template <typename RandomAccessIterator>
|
463 |
|
|
inline void sort (RandomAccessIterator first, RandomAccessIterator last)
|
464 |
|
|
{
|
465 |
|
|
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
|
466 |
|
|
sort (first, last, less<value_type>());
|
467 |
|
|
}
|
468 |
|
|
|
469 |
|
|
/// Sorts the container preserving order of equal elements.
|
470 |
|
|
/// \ingroup SortingAlgorithms
|
471 |
|
|
/// \ingroup PredicateAlgorithms
|
472 |
|
|
///
|
473 |
|
|
template <typename RandomAccessIterator, typename Compare>
|
474 |
|
|
void stable_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp)
|
475 |
|
|
{
|
476 |
|
|
for (RandomAccessIterator j, i = first; ++i < last;) { // Insertion sort
|
477 |
|
|
for (j = i; j-- > first && comp(*i, *j);) ;
|
478 |
|
|
if (++j != i) rotate (j, i, i + 1);
|
479 |
|
|
}
|
480 |
|
|
}
|
481 |
|
|
|
482 |
|
|
/// Sorts the container
|
483 |
|
|
/// \ingroup SortingAlgorithms
|
484 |
|
|
///
|
485 |
|
|
template <typename RandomAccessIterator>
|
486 |
|
|
inline void stable_sort (RandomAccessIterator first, RandomAccessIterator last)
|
487 |
|
|
{
|
488 |
|
|
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
|
489 |
|
|
stable_sort (first, last, less<value_type>());
|
490 |
|
|
}
|
491 |
|
|
|
492 |
|
|
/// \brief Searches for the first subsequence [first2,last2) in [first1,last1)
|
493 |
|
|
/// \ingroup SearchingAlgorithms
|
494 |
|
|
template <typename ForwardIterator1, typename ForwardIterator2>
|
495 |
|
|
inline ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)
|
496 |
|
|
{
|
497 |
|
|
typedef typename iterator_traits<ForwardIterator1>::value_type value_type;
|
498 |
|
|
return (search (first1, last1, first2, last2, equal_to<value_type>()));
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
/// \brief Searches for the last subsequence [first2,last2) in [first1,last1)
|
502 |
|
|
/// \ingroup SearchingAlgorithms
|
503 |
|
|
template <typename ForwardIterator1, typename ForwardIterator2>
|
504 |
|
|
inline ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)
|
505 |
|
|
{
|
506 |
|
|
typedef typename iterator_traits<ForwardIterator1>::value_type value_type;
|
507 |
|
|
return (find_end (first1, last1, first2, last2, equal_to<value_type>()));
|
508 |
|
|
}
|
509 |
|
|
|
510 |
|
|
/// \brief Searches for the first occurence of \p count \p values in [first, last)
|
511 |
|
|
/// \ingroup SearchingAlgorithms
|
512 |
|
|
template <typename Iterator, typename T>
|
513 |
|
|
inline Iterator search_n (Iterator first, Iterator last, size_t count, const T& value)
|
514 |
|
|
{
|
515 |
|
|
typedef typename iterator_traits<Iterator>::value_type value_type;
|
516 |
|
|
return (search_n (first, last, count, value, equal_to<value_type>()));
|
517 |
|
|
}
|
518 |
|
|
|
519 |
|
|
/// \brief Searches [first1,last1) for the first occurrence of an element from [first2,last2)
|
520 |
|
|
/// \ingroup SearchingAlgorithms
|
521 |
|
|
template <typename InputIterator, typename ForwardIterator>
|
522 |
|
|
inline InputIterator find_first_of (InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2)
|
523 |
|
|
{
|
524 |
|
|
typedef typename iterator_traits<InputIterator>::value_type value_type;
|
525 |
|
|
return (find_first_of (first1, last1, first2, last2, equal_to<value_type>()));
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
/// \brief Returns true if [first2,last2) is a subset of [first1,last1)
|
529 |
|
|
/// \ingroup ConditionAlgorithms
|
530 |
|
|
/// \ingroup SetAlgorithms
|
531 |
|
|
template <typename InputIterator1, typename InputIterator2>
|
532 |
|
|
inline bool includes (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
|
533 |
|
|
{
|
534 |
|
|
typedef typename iterator_traits<InputIterator1>::value_type value_type;
|
535 |
|
|
return (includes (first1, last1, first2, last2, less<value_type>()));
|
536 |
|
|
}
|
537 |
|
|
|
538 |
|
|
/// \brief Merges [first1,last1) with [first2,last2)
|
539 |
|
|
///
|
540 |
|
|
/// Result will contain every element that is in either set. If duplicate
|
541 |
|
|
/// elements are present, max(n,m) is placed in the result.
|
542 |
|
|
///
|
543 |
|
|
/// \ingroup SetAlgorithms
|
544 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
|
545 |
|
|
inline OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
|
546 |
|
|
{
|
547 |
|
|
typedef typename iterator_traits<InputIterator1>::value_type value_type;
|
548 |
|
|
return (set_union (first1, last1, first2, last2, result, less<value_type>()));
|
549 |
|
|
}
|
550 |
|
|
|
551 |
|
|
/// \brief Creates a set containing elements shared by the given ranges.
|
552 |
|
|
/// \ingroup SetAlgorithms
|
553 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
|
554 |
|
|
inline OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
|
555 |
|
|
{
|
556 |
|
|
typedef typename iterator_traits<InputIterator1>::value_type value_type;
|
557 |
|
|
return (set_intersection (first1, last1, first2, last2, result, less<value_type>()));
|
558 |
|
|
}
|
559 |
|
|
|
560 |
|
|
/// \brief Removes from [first1,last1) elements present in [first2,last2)
|
561 |
|
|
/// \ingroup SetAlgorithms
|
562 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
|
563 |
|
|
inline OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
|
564 |
|
|
{
|
565 |
|
|
typedef typename iterator_traits<InputIterator1>::value_type value_type;
|
566 |
|
|
return (set_difference (first1, last1, first2, last2, result, less<value_type>()));
|
567 |
|
|
}
|
568 |
|
|
|
569 |
|
|
/// \brief Performs union of sets A-B and B-A.
|
570 |
|
|
/// \ingroup SetAlgorithms
|
571 |
|
|
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
|
572 |
|
|
inline OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
|
573 |
|
|
{
|
574 |
|
|
typedef typename iterator_traits<InputIterator1>::value_type value_type;
|
575 |
|
|
return (set_symmetric_difference (first1, last1, first2, last2, result, less<value_type>()));
|
576 |
|
|
}
|
577 |
|
|
|
578 |
|
|
/// \brief Returns true if the given range is sorted.
|
579 |
|
|
/// \ingroup ConditionAlgorithms
|
580 |
|
|
template <typename ForwardIterator>
|
581 |
|
|
inline bool is_sorted (ForwardIterator first, ForwardIterator last)
|
582 |
|
|
{
|
583 |
|
|
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
|
584 |
|
|
return (is_sorted (first, last, less<value_type>()));
|
585 |
|
|
}
|
586 |
|
|
|
587 |
|
|
/// \brief Compares two given containers like strcmp compares strings.
|
588 |
|
|
/// \ingroup ConditionAlgorithms
|
589 |
|
|
template <typename InputIterator1, typename InputIterator2>
|
590 |
|
|
inline bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
|
591 |
|
|
{
|
592 |
|
|
typedef typename iterator_traits<InputIterator1>::value_type value_type;
|
593 |
|
|
return (lexicographical_compare (first1, last1, first2, last2, less<value_type>()));
|
594 |
|
|
}
|
595 |
|
|
|
596 |
|
|
/// \brief Creates the next lexicographical permutation of [first,last).
|
597 |
|
|
/// Returns false if no further permutations can be created.
|
598 |
|
|
/// \ingroup GeneratorAlgorithms
|
599 |
|
|
template <typename BidirectionalIterator>
|
600 |
|
|
inline bool next_permutation (BidirectionalIterator first, BidirectionalIterator last)
|
601 |
|
|
{
|
602 |
|
|
typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
|
603 |
|
|
return (next_permutation (first, last, less<value_type>()));
|
604 |
|
|
}
|
605 |
|
|
|
606 |
|
|
/// \brief Creates the previous lexicographical permutation of [first,last).
|
607 |
|
|
/// Returns false if no further permutations can be created.
|
608 |
|
|
/// \ingroup GeneratorAlgorithms
|
609 |
|
|
template <typename BidirectionalIterator>
|
610 |
|
|
inline bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last)
|
611 |
|
|
{
|
612 |
|
|
typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
|
613 |
|
|
return (prev_permutation (first, last, less<value_type>()));
|
614 |
|
|
}
|
615 |
|
|
|
616 |
|
|
/// \brief Returns iterator to the max element in [first,last)
|
617 |
|
|
/// \ingroup SearchingAlgorithms
|
618 |
|
|
template <typename ForwardIterator>
|
619 |
|
|
inline ForwardIterator max_element (ForwardIterator first, ForwardIterator last)
|
620 |
|
|
{
|
621 |
|
|
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
|
622 |
|
|
return (max_element (first, last, less<value_type>()));
|
623 |
|
|
}
|
624 |
|
|
|
625 |
|
|
/// \brief Returns iterator to the min element in [first,last)
|
626 |
|
|
/// \ingroup SearchingAlgorithms
|
627 |
|
|
template <typename ForwardIterator>
|
628 |
|
|
inline ForwardIterator min_element (ForwardIterator first, ForwardIterator last)
|
629 |
|
|
{
|
630 |
|
|
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
|
631 |
|
|
return (min_element (first, last, less<value_type>()));
|
632 |
|
|
}
|
633 |
|
|
|
634 |
|
|
/// \brief Makes [first,middle) a part of the sorted array.
|
635 |
|
|
/// Contents of [middle,last) is undefined. This implementation just calls stable_sort.
|
636 |
|
|
/// \ingroup SortingAlgorithms
|
637 |
|
|
template <typename RandomAccessIterator>
|
638 |
|
|
inline void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last)
|
639 |
|
|
{
|
640 |
|
|
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
|
641 |
|
|
partial_sort (first, middle, last, less<value_type>());
|
642 |
|
|
}
|
643 |
|
|
|
644 |
|
|
/// \brief Puts \p nth element into its sorted position.
|
645 |
|
|
/// In this implementation, the entire array is sorted. I can't think of any
|
646 |
|
|
/// use for it where the time gained would be useful.
|
647 |
|
|
/// \ingroup SortingAlgorithms
|
648 |
|
|
/// \ingroup SearchingAlgorithms
|
649 |
|
|
///
|
650 |
|
|
template <typename RandomAccessIterator>
|
651 |
|
|
inline void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last)
|
652 |
|
|
{
|
653 |
|
|
partial_sort (first, nth, last);
|
654 |
|
|
}
|
655 |
|
|
|
656 |
|
|
/// \brief Like partial_sort, but outputs to [result_first,result_last)
|
657 |
|
|
/// \ingroup SortingAlgorithms
|
658 |
|
|
template <typename InputIterator, typename RandomAccessIterator>
|
659 |
|
|
inline RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last)
|
660 |
|
|
{
|
661 |
|
|
typedef typename iterator_traits<InputIterator>::value_type value_type;
|
662 |
|
|
return (partial_sort_copy (first, last, result_first, result_last, less<value_type>()));
|
663 |
|
|
}
|
664 |
|
|
|
665 |
|
|
} // namespace ustl
|
666 |
|
|
|
667 |
|
|
#endif
|