OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [cxx/] [ustl/] [current/] [include/] [ustl/] [uctralgo.h] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
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 UCTRALGO_H_0D1AEDFA74B09791489FE25B1EC644B0
7
#define UCTRALGO_H_0D1AEDFA74B09791489FE25B1EC644B0
8
 
9
namespace ustl {
10
 
11
/// Copy copies elements from the range [first, last) to the range
12
/// [result, result + (last - first)). That is, it performs the assignments
13
/// *result = *first, *(result + 1) = *(first + 1), and so on. [1] Generally,
14
/// for every integer n from 0 to last - first, copy performs the assignment
15
/// *(result + n) = *(first + n). Assignments are performed in forward order,
16
/// i.e. in order of increasing n. 
17
/// \ingroup MutatingAlgorithms
18
///
19
template <typename Container, typename OutputIterator>
20
inline OutputIterator copy (const Container& ctr, OutputIterator result)
21
{
22
    return (copy (ctr.begin(), ctr.end(), result));
23
}
24
 
25
/// Copy_if copies elements from the range [first, last) to the range
26
/// [result, result + (last - first)) if pred(*i) returns true.
27
/// \ingroup MutatingAlgorithms
28
///
29
template <typename Container, typename OutputIterator, typename Predicate>
30
inline OutputIterator copy_if (Container& ctr, OutputIterator result, Predicate pred)
31
{
32
    return (copy_if (ctr.begin(), ctr.end(), result, pred));
33
}
34
 
35
/// For_each applies the function object f to each element in the range
36
/// [first, last); f's return value, if any, is ignored. Applications are
37
/// performed in forward order, i.e. from first to last. For_each returns
38
/// the function object after it has been applied to each element.
39
/// \ingroup MutatingAlgorithms
40
///
41
template <typename Container, typename UnaryFunction>
42
inline UnaryFunction for_each (Container& ctr, UnaryFunction f)
43
{
44
    return (for_each (ctr.begin(), ctr.end(), f));
45
}
46
 
47
/// For_each applies the function object f to each element in the range
48
/// [first, last); f's return value, if any, is ignored. Applications are
49
/// performed in forward order, i.e. from first to last. For_each returns
50
/// the function object after it has been applied to each element.
51
/// \ingroup MutatingAlgorithms
52
///
53
template <typename Container, typename UnaryFunction>
54
inline UnaryFunction for_each (const Container& ctr, UnaryFunction f)
55
{
56
    return (for_each (ctr.begin(), ctr.end(), f));
57
}
58
 
59
/// Returns the first iterator i in the range [first, last) such that
60
/// *i == value. Returns last if no such iterator exists. 
61
/// \ingroup SearchingAlgorithms
62
///
63
template <typename Container, typename EqualityComparable>
64
inline typename Container::const_iterator find (const Container& ctr, const EqualityComparable& value)
65
{
66
    return (find (ctr.begin(), ctr.end(), value));
67
}
68
template <typename Container, typename EqualityComparable>
69
inline typename Container::iterator find (Container& ctr, const EqualityComparable& value)
70
{
71
    return (find (ctr.begin(), ctr.end(), value));
72
}
73
 
74
/// Returns the first iterator i in the range [first, last) such that
75
/// pred(*i) is true. Returns last if no such iterator exists.
76
/// \ingroup SearchingAlgorithms
77
///
78
template <typename Container, typename Predicate>
79
inline typename Container::const_iterator find_if (const Container& ctr, Predicate pred)
80
{
81
    return (find_if (ctr.begin(), ctr.end(), pred));
82
}
83
template <typename Container, typename Predicate>
84
inline typename Container::iterator find_if (Container& ctr, Predicate pred)
85
{
86
    return (find_if (ctr.begin(), ctr.end(), pred));
87
}
88
 
89
/// Count finds the number of elements in [first, last) that are equal
90
/// to value. More precisely, the first version of count returns the
91
/// number of iterators i in [first, last) such that *i == value.
92
/// \ingroup ConditionAlgorithms
93
///
94
template <typename Container, typename EqualityComparable>
95
inline size_t count (const Container& ctr, const EqualityComparable& value)
96
{
97
    return (count (ctr.begin(), ctr.end(), value));
98
}
99
 
100
/// Count_if finds the number of elements in [first, last) that satisfy the
101
/// predicate pred. More precisely, the first version of count_if returns the
102
/// number of iterators i in [first, last) such that pred(*i) is true.
103
/// \ingroup ConditionAlgorithms
104
///
105
template <typename Container, typename Predicate>
106
inline size_t count_if (const Container& ctr, Predicate pred)
107
{
108
    return (count_if (ctr.begin(), ctr.end(), pred));
109
}
110
 
111
/// The first version of transform performs the operation op(*i) for each
112
/// iterator i in the range [first, last), and assigns the result of that
113
/// operation to *o, where o is the corresponding output iterator. That is,
114
/// for each n such that 0 <= n < last - first, it performs the assignment
115
/// *(result + n) = op(*(first + n)).
116
/// The return value is result + (last - first).
117
/// \ingroup MutatingAlgorithms
118
///
119
template <typename Container, typename UnaryFunction>
120
inline void transform (Container& ctr, UnaryFunction op)
121
{
122
    transform (ctr.begin(), ctr.end(), ctr.begin(), op);
123
}
124
 
125
/// The first version of transform performs the operation op(*i) for each
126
/// iterator i in the range [first, last), and assigns the result of that
127
/// operation to *o, where o is the corresponding output iterator. That is,
128
/// for each n such that 0 <= n < last - first, it performs the assignment
129
/// *(result + n) = op(*(first + n)).
130
/// The return value is result + (last - first).
131
/// \ingroup MutatingAlgorithms
132
///
133
template <typename Container, typename OutputIterator, typename UnaryFunction>
134
inline OutputIterator transform (Container& ctr, OutputIterator result, UnaryFunction op)
135
{
136
    return (transform (ctr.begin(), ctr.end(), result, op));
137
}
138
 
139
/// The second version of transform is very similar, except that it uses a
140
/// Binary Function instead of a Unary Function: it performs the operation
141
/// op(*i1, *i2) for each iterator i1 in the range [first1, last1) and assigns
142
/// the result to *o, where i2 is the corresponding iterator in the second
143
/// input range and where o is the corresponding output iterator. That is,
144
/// for each n such that 0 <= n < last1 - first1, it performs the assignment
145
/// *(result + n) = op(*(first1 + n), *(first2 + n).
146
/// The return value is result + (last1 - first1).
147
/// \ingroup MutatingAlgorithms
148
///
149
template <typename Container, typename InputIterator, typename OutputIterator, typename BinaryFunction>
150
inline OutputIterator transform (Container& ctr, InputIterator first, OutputIterator result, BinaryFunction op)
151
{
152
    return (transform (ctr.begin(), ctr.end(), first, result, op));
153
}
154
 
155
/// Replace replaces every element in the range [first, last) equal to
156
/// old_value with new_value. That is: for every iterator i,
157
/// if *i == old_value then it performs the assignment *i = new_value.
158
/// \ingroup MutatingAlgorithms
159
///
160
template <typename Container, typename T>
161
inline void replace (Container& ctr, const T& old_value, const T& new_value)
162
{
163
    replace (ctr.begin(), ctr.end(), old_value, new_value);
164
}
165
 
166
/// Replace_if replaces every element in the range [first, last) for which
167
/// pred returns true with new_value. That is: for every iterator i, if
168
/// pred(*i) is true then it performs the assignment *i = new_value.
169
/// \ingroup MutatingAlgorithms
170
///
171
template <typename Container, typename Predicate, typename T>
172
inline void replace_if (Container& ctr, Predicate pred, const T& new_value)
173
{
174
    replace_if (ctr.begin(), ctr.end(), pred, new_value);
175
}
176
 
177
/// Replace_copy copies elements from the range [first, last) to the range
178
/// [result, result + (last-first)), except that any element equal to old_value
179
/// is not copied; new_value is copied instead. More precisely, for every
180
/// integer n such that 0 <= n < last-first, replace_copy performs the
181
/// assignment *(result+n) = new_value if *(first+n) == old_value, and
182
/// *(result+n) = *(first+n) otherwise.
183
/// \ingroup MutatingAlgorithms
184
///
185
template <typename Container, typename OutputIterator, typename T>
186
inline OutputIterator replace_copy (const Container& ctr, OutputIterator result, const T& old_value, const T& new_value)
187
{
188
    return (replace_copy (ctr.begin(), ctr.end(), result, old_value, new_value));
189
}
190
 
191
/// Replace_copy_if copies elements from the range [first, last) to the range
192
/// [result, result + (last-first)), except that any element for which pred is
193
/// true is not copied; new_value is copied instead. More precisely, for every
194
/// integer n such that 0 <= n < last-first, replace_copy_if performs the
195
/// assignment *(result+n) = new_value if pred(*(first+n)),
196
/// and *(result+n) = *(first+n) otherwise.
197
/// \ingroup MutatingAlgorithms
198
///
199
template <typename Container, typename OutputIterator, typename Predicate, typename T>
200
inline OutputIterator replace_copy_if (const Container& ctr, OutputIterator result, Predicate pred, const T& new_value)
201
{
202
    return (replace_copy_if (ctr.begin(), ctr.end(), result, pred, new_value));
203
}
204
 
205
/// Fill assigns the value value to every element in the range [first, last).
206
/// That is, for every iterator i in [first, last),
207
/// it performs the assignment *i = value.
208
/// \ingroup GeneratorAlgorithms
209
///
210
template <typename Container, typename T>
211
inline void fill (Container& ctr, const T& value)
212
{
213
    fill (ctr.begin(), ctr.end(), value);
214
}
215
 
216
/// Generate assigns the result of invoking gen, a function object that
217
/// takes no arguments, to each element in the range [first, last).
218
/// \ingroup GeneratorAlgorithms
219
///
220
template <typename Container, typename Generator>
221
inline void generate (Container& ctr, Generator gen)
222
{
223
    generate (ctr.begin(), ctr.end(), gen);
224
}
225
 
226
/// Randomly permute the elements of the container.
227
/// \ingroup GeneratorAlgorithms
228
///
229
template <typename Container>
230
inline void random_shuffle (Container& ctr)
231
{
232
    random_shuffle (ctr.begin(), ctr.end());
233
}
234
 
235
/// Remove_copy copies elements that are not equal to value from the range
236
/// [first, last) to a range beginning at result. The return value is the
237
/// end of the resulting range. This operation is stable, meaning that the
238
/// relative order of the elements that are copied is the same as in the
239
/// range [first, last).
240
/// \ingroup MutatingAlgorithms
241
///
242
template <typename Container, typename OutputIterator, typename T>
243
inline OutputIterator remove_copy (const Container& ctr, OutputIterator result, const T& value)
244
{
245
    return (remove_copy (ctr.begin(), ctr.end(), result, value));
246
}
247
 
248
/// Remove_copy_if copies elements from the range [first, last) to a range
249
/// beginning at result, except that elements for which pred is true are not
250
/// copied. The return value is the end of the resulting range. This operation
251
/// is stable, meaning that the relative order of the elements that are copied
252
/// is the same as in the range [first, last).
253
/// \ingroup MutatingAlgorithms
254
///
255
template <typename Container, typename OutputIterator, typename Predicate>
256
inline OutputIterator remove_copy_if (const Container& ctr, OutputIterator result, Predicate pred)
257
{
258
    return (remove_copy_if (ctr.begin(), ctr.end(), result, pred));
259
}
260
 
261
/// Remove removes from the range [first, last) all elements that are equal to
262
/// value. That is, remove returns an iterator new_last such that the range
263
/// [first, new_last) contains no elements equal to value. Remove is stable,
264
/// meaning that the relative order of elements that are not equal to value is
265
/// unchanged.
266
/// \ingroup MutatingAlgorithms
267
///
268
template <typename Container, typename T>
269
inline void remove (Container& ctr, const T& value)
270
{
271
    ctr.erase (remove_copy (ctr.begin(), ctr.end(), ctr.begin(), value), ctr.end());
272
}
273
 
274
/// Remove removes from the range [first, last) all elements that have an iterator
275
/// in range [rfirst, rlast). The range is assumed to be sorted. That is, remove
276
/// returns an iterator new_last such that the range [first, new_last) contains
277
/// no elements whose iterators are in [rfirst, rlast). Remove is stable,
278
/// meaning that the relative order of elements that are not equal to value is
279
/// unchanged. This version of the algorithm is a uSTL extension.
280
/// \ingroup MutatingAlgorithms
281
///
282
template <typename Container, typename ForwardIterator>
283
inline void remove (Container& ctr, ForwardIterator rfirst, ForwardIterator rlast)
284
{
285
    ctr.erase (remove_copy (ctr.begin(), ctr.end(), ctr.begin(), rfirst, rlast), ctr.end());
286
}
287
 
288
/// Remove_if removes from the range [first, last) every element x such that
289
/// pred(x) is true. That is, remove_if returns an iterator new_last such that
290
/// the range [first, new_last) contains no elements for which pred is true.
291
/// The iterators in the range [new_last, last) are all still dereferenceable,
292
/// but the elements that they point to are unspecified. Remove_if is stable,
293
/// meaning that the relative order of elements that are not removed is
294
/// unchanged.
295
/// \ingroup MutatingAlgorithms
296
///
297
template <typename Container, typename Predicate>
298
inline void remove_if (Container& ctr, Predicate pred)
299
{
300
    ctr.erase (remove_copy_if (ctr.begin(), ctr.end(), ctr.begin(), pred), ctr.end());
301
}
302
 
303
/// Unique_copy copies elements from the range [first, last) to a range
304
/// beginning with result, except that in a consecutive group of duplicate
305
/// elements only the first one is copied. The return value is the end of
306
/// the range to which the elements are copied. This behavior is similar
307
/// to the Unix filter uniq.
308
/// \ingroup MutatingAlgorithms
309
///
310
template <typename Container, typename OutputIterator>
311
inline OutputIterator unique_copy (const Container& ctr, OutputIterator result)
312
{
313
    return (unique_copy (ctr.begin(), ctr.end(), result));
314
}
315
 
316
/// Every time a consecutive group of duplicate elements appears in the range
317
/// [first, last), the algorithm unique removes all but the first element.
318
/// That is, unique returns an iterator new_last such that the range [first,
319
/// new_last) contains no two consecutive elements that are duplicates.
320
/// The iterators in the range [new_last, last) are all still dereferenceable,
321
/// but the elements that they point to are unspecified. Unique is stable,
322
/// meaning that the relative order of elements that are not removed is
323
/// unchanged.
324
/// \ingroup MutatingAlgorithms
325
///
326
template <typename Container>
327
inline void unique (Container& ctr)
328
{
329
    ctr.erase (unique_copy (ctr.begin(), ctr.end(), ctr.begin()), ctr.end());
330
}
331
 
332
/// Every time a consecutive group of duplicate elements appears in the range
333
/// [first, last), the algorithm unique removes all but the first element.
334
/// That is, unique returns an iterator new_last such that the range [first,
335
/// new_last) contains no two consecutive elements that are duplicates.
336
/// The iterators in the range [new_last, last) are all still dereferenceable,
337
/// but the elements that they point to are unspecified. Unique is stable,
338
/// meaning that the relative order of elements that are not removed is
339
/// unchanged.
340
/// \ingroup MutatingAlgorithms
341
///
342
template <typename Container, typename BinaryPredicate>
343
inline void unique (Container& ctr, BinaryPredicate binary_pred)
344
{
345
    ctr.erase (unique_copy (ctr.begin(), ctr.end(), ctr.begin(), binary_pred), ctr.end());
346
}
347
 
348
/// Reverse reverses a range.
349
/// That is: for every i such that 0 <= i <= (last - first) / 2),
350
/// it exchanges *(first + i) and *(last - (i + 1)).
351
/// \ingroup MutatingAlgorithms
352
///
353
template <typename Container>
354
inline void reverse (Container& ctr)
355
{
356
    reverse (ctr.begin(), ctr.end());
357
}
358
 
359
/// Exchanges ranges [first, middle) and [middle, last)
360
/// \ingroup MutatingAlgorithms
361
///
362
template <typename Container>
363
inline void rotate (Container& ctr, off_t offset)
364
{
365
    assert (size_t(offset > 0 ? offset : -offset) < ctr.size());
366
    if (offset > 0)
367
        rotate (ctr.begin(), ctr.end() - offset, ctr.end());
368
    else
369
        rotate (ctr.begin(), ctr.begin() - offset, ctr.end());
370
}
371
 
372
/// Returns the furthermost iterator i in [first, last) such that,
373
/// for every iterator j in [first, i), *j < value
374
/// Assumes the range is sorted.
375
/// \ingroup SearchingAlgorithms
376
///
377
template <typename Container, typename LessThanComparable>
378
inline typename Container::const_iterator lower_bound (const Container& ctr, const LessThanComparable& value)
379
{
380
    return (lower_bound (ctr.begin(), ctr.end(), value));
381
}
382
template <typename Container, typename LessThanComparable>
383
inline typename Container::iterator lower_bound (Container& ctr, const LessThanComparable& value)
384
{
385
    return (lower_bound (ctr.begin(), ctr.end(), value));
386
}
387
 
388
/// Returns the furthermost iterator i in [first,last) such that for
389
/// every iterator j in [first,i), value < *j is false.
390
/// \ingroup SearchingAlgorithms
391
///
392
template <typename Container, typename LessThanComparable>
393
inline typename Container::const_iterator upper_bound (const Container& ctr, const LessThanComparable& value)
394
{
395
    return (upper_bound (ctr.begin(), ctr.end(), value));
396
}
397
template <typename Container, typename LessThanComparable>
398
inline typename Container::iterator upper_bound (Container& ctr, const LessThanComparable& value)
399
{
400
    return (upper_bound (ctr.begin(), ctr.end(), value));
401
}
402
 
403
/// Performs a binary search for \p value.
404
/// Assumes the range is sorted.
405
/// \ingroup SearchingAlgorithms
406
///
407
template <typename Container>
408
inline bool binary_search (const Container& ctr, const typename Container::value_type& value)
409
{
410
    return (binary_search (ctr.begin(), ctr.end(), value));
411
}
412
template <typename Container>
413
inline bool binary_search (Container& ctr, const typename Container::value_type& value)
414
{
415
    return (binary_search (ctr.begin(), ctr.end(), value));
416
}
417
 
418
/// Returns pair<lower_bound,upper_bound>
419
/// \ingroup SearchingAlgorithms
420
///
421
template <typename Container, typename LessThanComparable>
422
inline pair<typename Container::const_iterator,typename Container::const_iterator> equal_range (const Container& ctr, const LessThanComparable& value)
423
{
424
    return (equal_range (ctr.begin(), ctr.end(), value));
425
}
426
template <typename Container, typename LessThanComparable>
427
inline pair<typename Container::iterator,typename Container::iterator> equal_range (Container& ctr, const LessThanComparable& value)
428
{
429
    return (equal_range (ctr.begin(), ctr.end(), value));
430
}
431
 
432
/// Sorts the container
433
/// \ingroup SortingAlgorithms
434
///
435
template <typename Container>
436
inline void sort (Container& ctr)
437
{
438
    sort (ctr.begin(), ctr.end());
439
}
440
 
441
/// Sorts the container
442
/// \ingroup SortingAlgorithms
443
///
444
template <typename Container, typename Compare>
445
inline void sort (Container& ctr, Compare comp)
446
{
447
    sort (ctr.begin(), ctr.end(), comp);
448
}
449
 
450
/// Sorts the container
451
/// \ingroup SortingAlgorithms
452
///
453
template <typename Container>
454
inline void stable_sort (Container& ctr)
455
{
456
    stable_sort (ctr.begin(), ctr.end());
457
}
458
 
459
/// Sorts the container
460
/// \ingroup SortingAlgorithms
461
///
462
template <typename Container, typename Compare>
463
inline void stable_sort (Container& ctr, Compare comp)
464
{
465
    stable_sort (ctr.begin(), ctr.end(), comp);
466
}
467
 
468
} // namespace ustl
469
 
470
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.