ScummVM API documentation
algorithm.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/********************************************
23 DISCLAIMER:
24
25 This is a wrapper code to mimic the relevant std:: class
26 Please use it ONLY when porting an existing code e.g. from the original sources
27
28 For all new development please use classes from Common::
29 *********************************************/
30
31#ifndef COMMON_STD_ALGORITHM_H
32#define COMMON_STD_ALGORITHM_H
33
34#include "common/algorithm.h"
35#include "common/util.h"
36
37namespace Std {
38
39template<typename T> inline T abs(T x) {
40 return ABS(x);
41}
42template<typename T> inline T min(T a, T b) {
43 return MIN(a, b);
44}
45template<typename T> inline T max(T a, T b) {
46 return MAX(a, b);
47}
48template<typename T> inline T clip(T v, T amin, T amax) {
49 return CLIP(v, amin, amax);
50}
51template<typename T> inline T sqrt(T x) {
52 return ::sqrt(x);
53}
54template<typename T> inline void swap(T &a, T &b) {
55 SWAP(a, b);
56}
57
58template<class In, class Value>
59In fill(In first, In last, const Value &val) {
60 return Common::fill(first, last, val);
61}
62
63template<typename T, class StrictWeakOrdering>
64void sort(T first, T last, StrictWeakOrdering comp) {
65 Common::sort(first, last, comp);
66}
67
68template<typename T>
69void sort(T *first, T *last) {
70 Common::sort(first, last, Common::Less<T>());
71}
72
73template<class T>
74void sort(T first, T last) {
75 Common::sort(first, last);
76}
77
78template<class In, class T>
79In find(In first, In last, const T &v) {
80 return Common::find(first, last, v);
81}
82
83template<class T>
84T unique(T first, T last) {
85 T pos;
86 for (pos = first + 1; pos < last; ++pos) {
87 // Check for duplicate
88 for (T existingPos = first; existingPos < last; ++existingPos) {
89 if (*pos == *existingPos) {
90 // Found a match, so shift values over the duplicate
91 while (pos < (last - 1)) {
92 T &prior = pos;
93 ++pos;
94 prior = pos;
95 }
96
97 --last;
98 break;
99 }
100 }
101 }
102
103 return pos;
104}
105
106template<class ForwardIt, class T>
107ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T &value) {
108 for (ForwardIt it = first; it < last; ++it) {
109 if (*it >= value)
110 return it;
111 }
112
113 return last;
114}
115
116template<class ForwardIt, class T>
117ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T &value) {
118 for (ForwardIt it = first; it < last; ++it) {
119 if (*it > value)
120 return it;
121 }
122
123 return last;
124}
125
126template<class ForwardIt, class T, class Compare>
127ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T &value, Compare comp) {
128 for (ForwardIt it = first; it < last; ++it) {
129 if (comp(value, *it))
130 return it;
131 }
132
133 return last;
134}
135
136template<class ForwardIt>
137ForwardIt next(ForwardIt it, int n = 1) {
138 ForwardIt it2 = it;
139 while (n > 0) { ++it2; --n; }
140 while (n < 0) { --it2; ++n; }
141 return it2;
142}
143
144template<class BidirIt>
145BidirIt prev(BidirIt it, int n = 1) {
146 BidirIt it2 = it;
147 while (n > 0) { --it2; --n; }
148 while (n < 0) { ++it2; ++n; }
149 return it2;
150}
151
152} // namespace Std
153
154#endif
In fill(In first, In last, const Value &val)
Definition: algorithm.h:214
void sort(T first, T last)
Definition: algorithm.h:378
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
In find(In first, In last, const T &v)
Definition: algorithm.h:225
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
T CLIP(T v, T amin, T amax)
Definition: util.h:69
T ABS(T x)
Definition: util.h:60
void SWAP(T &a, T &b)
Definition: util.h:467
T MIN(T a, T b)
Definition: util.h:63
T MAX(T a, T b)
Definition: util.h:66
Definition: func.h:70
Definition: lobject.h:59