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"
51template<
class In,
class Out>
52Out
copy(In first, In last, Out dst) {
66template<
class In,
class Out>
83template<
class In,
class Out,
class Op>
84Out
copy_if(In first, In last, Out dst, Op op) {
85 while (first != last) {
108template<
class In,
class Out>
109Out
move(In first, In last, Out dst) {
110 while (first != last)
123template<
class In,
class Out>
125 while (first != last)
140template<
class In,
class Out,
class Op>
141Out
move_if(In first, In last, Out dst, Op op) {
142 while (first != last) {
168signed char *
fill(
signed char *first,
signed char *last,
Value val) {
169 memset(first, (val & 0xFF), last - first);
182unsigned char *
fill(
unsigned char *first,
unsigned char *last,
Value val) {
183 memset(first, (val & 0xFF), last - first);
197 memset(first, (val & 0xFF), last - first);
213template<
class In,
class Value>
215 while (first != last)
224template<
class In,
class T>
225In
find(In first, In last,
const T &v) {
226 while (first != last) {
238template<
class In,
class Pred>
240 while (first != last) {
252template<
class In,
class Op>
254 while (first != last)
260void reverse(T first, T last) {
261 for (; first != last && first != --last; ++first)
270unsigned int distance(T *first, T *last) {
275unsigned int distance(T first, T last) {
277 while (first != last) {
285T *sortChoosePivot(T *first, T *last) {
286 return first + distance(first, last) / 2;
290T sortChoosePivot(T first, T last) {
291 unsigned int n = distance(first, last);
298template<
typename T,
class StrictWeakOrdering>
299T 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);
348template<
typename T,
class StrictWeakOrdering>
349void 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);
390#pragma warning(disable: 4146)
447template<
class It,
class Dat>
448void replace(It begin, It end,
const Dat &original,
const Dat &replaced) {
449 for (; begin != end; ++begin) {
450 if (*begin == original) {
465template<
class It,
class T>
466It
remove(It first, It last,
const T& val) {
467 first =
find(first, last, val);
470 while (++i != last) {
490template<
typename RandomIt,
typename V,
typename Comp = Less<V> >
491RandomIt
lowerBound(RandomIt first, RandomIt last,
const V &val, Comp comp = {}) {
492 while (first < last) {
493 const RandomIt mid = first + distance(first, last) / 2;
512template<
typename RandomIt,
typename V,
typename Comp = Less<V> >
513RandomIt
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))
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
T nextHigher2(T v)
Definition: algorithm.h:423
T gcd(T a, T b)
Definition: algorithm.h:397
Out copy_if(In first, In last, Out dst, Op op)
Definition: algorithm.h:84
void replace(It begin, It end, const Dat &original, const Dat &replaced)
Definition: algorithm.h:448
RandomIt lowerBound(RandomIt first, RandomIt last, const V &val, Comp comp={})
Definition: algorithm.h:491
Op for_each(In first, In last, Op f)
Definition: algorithm.h:253
It remove(It first, It last, const T &val)
Definition: algorithm.h:466
In find_if(In first, In last, Pred p)
Definition: algorithm.h:239
Out copy_backward(In first, In last, Out dst)
Definition: algorithm.h:67
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Out move_if(In first, In last, Out dst, Op op)
Definition: algorithm.h:141
Out move_backward(In first, In last, Out dst)
Definition: algorithm.h:124
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
RandomIt upperBound(RandomIt first, RandomIt last, const V &val, Comp comp={})
Definition: algorithm.h:513
void SWAP(T &a, T &b)
Definition: util.h:467
Definition: algorithm.h:29