ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
37 namespace Std {
38 
39 template<typename T> inline T abs(T x) {
40  return ABS(x);
41 }
42 template<typename T> inline T min(T a, T b) {
43  return MIN(a, b);
44 }
45 template<typename T> inline T max(T a, T b) {
46  return MAX(a, b);
47 }
48 template<typename T> inline T clip(T v, T amin, T amax) {
49  return CLIP(v, amin, amax);
50 }
51 template<typename T> inline T sqrt(T x) {
52  return ::sqrt(x);
53 }
54 template<typename T> inline void swap(T &a, T &b) {
55  SWAP(a, b);
56 }
57 
58 template<class In, class Value>
59 In fill(In first, In last, const Value &val) {
60  return Common::fill(first, last, val);
61 }
62 
63 template<typename T, class StrictWeakOrdering>
64 void sort(T first, T last, StrictWeakOrdering comp) {
65  Common::sort(first, last, comp);
66 }
67 
68 template<typename T>
69 void sort(T *first, T *last) {
70  Common::sort(first, last, Common::Less<T>());
71 }
72 
73 template<class T>
74 void sort(T first, T last) {
75  Common::sort(first, last);
76 }
77 
78 template<class In, class T>
79 In find(In first, In last, const T &v) {
80  return Common::find(first, last, v);
81 }
82 
83 template<class T>
84 T 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 
106 template<class ForwardIt, class T>
107 ForwardIt 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 
116 template<class ForwardIt, class T>
117 ForwardIt 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 
126 template<class ForwardIt, class T, class Compare>
127 ForwardIt 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 
136 template<class ForwardIt>
137 ForwardIt 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 
144 template<class BidirIt>
145 BidirIt 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
void sort(T first, T last)
Definition: algorithm.h:378
In find(In first, In last, const T &v)
Definition: algorithm.h:225
void SWAP(T &a, T &b)
Definition: util.h:84
T CLIP(T v, T amin, T amax)
Definition: util.h:67
In fill(In first, In last, const Value &val)
Definition: algorithm.h:214
Definition: lobject.h:59
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
T MIN(T a, T b)
Definition: util.h:61
T MAX(T a, T b)
Definition: util.h:64
T ABS(T x)
Definition: util.h:58
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
Definition: algorithm.h:37
Definition: func.h:70