22 #ifndef COMMON_ALGORITHM_H 23 #define COMMON_ALGORITHM_H 25 #include "common/scummsys.h" 26 #include "common/func.h" 27 #include "common/util.h" 51 template<
class In,
class Out>
52 Out
copy(In first, In last, Out dst) {
66 template<
class In,
class Out>
83 template<
class In,
class Out,
class Op>
84 Out
copy_if(In first, In last, Out dst, Op op) {
85 while (first != last) {
108 template<
class In,
class Out>
109 Out
move(In first, In last, Out dst) {
110 while (first != last)
123 template<
class In,
class Out>
125 while (first != last)
140 template<
class In,
class Out,
class Op>
141 Out
move_if(In first, In last, Out dst, Op op) {
142 while (first != last) {
167 template<
class Value>
168 signed char *
fill(
signed char *first,
signed char *last,
Value val) {
169 memset(first, (val & 0xFF), last - first);
181 template<
class Value>
182 unsigned char *
fill(
unsigned char *first,
unsigned char *last,
Value val) {
183 memset(first, (val & 0xFF), last - first);
195 template<
class Value>
197 memset(first, (val & 0xFF), last - first);
213 template<
class In,
class Value>
215 while (first != last)
224 template<
class In,
class T>
225 In
find(In first, In last,
const T &v) {
226 while (first != last) {
238 template<
class In,
class Pred>
240 while (first != last) {
252 template<
class In,
class Op>
254 while (first != last)
260 void reverse(T first, T last) {
261 for (; first != last && first != --last; ++first)
270 unsigned int distance(T *first, T *last) {
275 unsigned int distance(T first, T last) {
277 while (first != last) {
285 T *sortChoosePivot(T *first, T *last) {
286 return first + distance(first, last) / 2;
290 T sortChoosePivot(T first, T last) {
291 unsigned int n = distance(first, last);
298 template<
typename T,
class StrictWeakOrdering>
299 T sortPartition(T first, T last, T pivot, StrictWeakOrdering &comp) {
305 for (sorted = first; first != last; ++first) {
306 if (!comp(*last, *first)) {
308 SWAP(*first, *sorted);
314 SWAP(*last, *sorted);
348 template<
typename T,
class StrictWeakOrdering>
349 void sort(T first, T last, StrictWeakOrdering comp) {
353 T pivot = sortChoosePivot(first, last);
354 pivot = sortPartition(first, last, pivot, comp);
355 sort<T, StrictWeakOrdering>(first, pivot, comp);
356 sort<T, StrictWeakOrdering>(++pivot, last, comp);
388 #if defined(_MSC_VER) 389 #pragma warning(push) 390 #pragma warning(disable: 4146) 415 #if defined(_MSC_VER) 447 template<
class It,
class Dat>
448 void replace(It begin, It end,
const Dat &original,
const Dat &replaced) {
449 for (; begin != end; ++begin) {
450 if (*begin == original) {
465 template<
class It,
class T>
466 It
remove(It first, It last,
const T& val) {
467 first =
find(first, last, val);
470 while (++i != last) {
490 template<
typename RandomIt,
typename V,
typename Comp = Less<V> >
491 RandomIt
lowerBound(RandomIt first, RandomIt last,
const V &val, Comp comp = {}) {
492 while (first < last) {
493 const RandomIt mid = first + distance(first, last) / 2;
512 template<
typename RandomIt,
typename V,
typename Comp = Less<V> >
513 RandomIt
upperBound(RandomIt first, RandomIt last,
const V &val, Comp comp = {}) {
514 while (first < last) {
515 const RandomIt mid = first + distance(first, last) / 2;
516 if (!comp(val, *mid))
Op for_each(In first, In last, Op f)
Definition: algorithm.h:253
T nextHigher2(T v)
Definition: algorithm.h:423
In find(In first, In last, const T &v)
Definition: algorithm.h:225
T gcd(T a, T b)
Definition: algorithm.h:397
void SWAP(T &a, T &b)
Definition: util.h:84
Out move_if(In first, In last, Out dst, Op op)
Definition: algorithm.h:141
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
RandomIt lowerBound(RandomIt first, RandomIt last, const V &val, Comp comp={})
Definition: algorithm.h:491
void replace(It begin, It end, const Dat &original, const Dat &replaced)
Definition: algorithm.h:448
RandomIt upperBound(RandomIt first, RandomIt last, const V &val, Comp comp={})
Definition: algorithm.h:513
Definition: algorithm.h:29
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Out move_backward(In first, In last, Out dst)
Definition: algorithm.h:124
Out copy_if(In first, In last, Out dst, Op op)
Definition: algorithm.h:84
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
In find_if(In first, In last, Pred p)
Definition: algorithm.h:239
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
Out copy_backward(In first, In last, Out dst)
Definition: algorithm.h:67