|
1 /* |
|
2 * Copyright (c) 2002-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Generic operation definitions. |
|
15 * |
|
16 */ |
|
17 |
|
18 /* |
|
19 * %version: 11 % |
|
20 */ |
|
21 |
|
22 #ifndef ALGORITHM_H |
|
23 #define ALGORITHM_H |
|
24 |
|
25 /** |
|
26 * global operator!= for type T |
|
27 * @param aLhs left hand side |
|
28 * @param aRhs right hand side |
|
29 * @return ETrue equal, EFalse not equal |
|
30 */ |
|
31 template< class T > |
|
32 inline TBool operator!= ( |
|
33 const T& aLhs, |
|
34 const T& aRhs) |
|
35 { |
|
36 return !( aLhs == aRhs ); |
|
37 } |
|
38 |
|
39 /** |
|
40 * global operator > for type T |
|
41 * @param aLhs left hand side |
|
42 * @param aRhs right hand side |
|
43 * @return ETrue greater, EFalse else |
|
44 */ |
|
45 template< class T > |
|
46 inline TBool operator > ( |
|
47 const T& aLhs, |
|
48 const T& aRhs) |
|
49 { |
|
50 return ( aRhs < aLhs ); |
|
51 } |
|
52 |
|
53 /** |
|
54 * global operator <= for type T |
|
55 * @param aLhs left hand side |
|
56 * @param aRhs right hand side |
|
57 * @return ETrue smaller or equal, EFalse else |
|
58 */ |
|
59 template< class T > |
|
60 inline TBool operator <= ( |
|
61 const T& aLhs, |
|
62 const T& aRhs) |
|
63 { |
|
64 return !( aRhs < aLhs ); |
|
65 } |
|
66 |
|
67 /** |
|
68 * global operator >= for type T |
|
69 * @param aLhs left hand side |
|
70 * @param aRhs right hand side |
|
71 * @return ETrue greater or equal, EFalse else |
|
72 */ |
|
73 template< class T > |
|
74 inline TBool operator >= ( |
|
75 const T& aLhs, |
|
76 const T& aRhs) |
|
77 { |
|
78 return !( aLhs < aRhs ); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Just like equal in C++ STL for testing equality for T type |
|
83 * Checks weather elements in the range [aBeg, aEnd) are equal |
|
84 * to the elements in the range starting with aCmpBeg |
|
85 * Complexity: linear |
|
86 * @param aBeg begin of the search range |
|
87 * @param aEnd end ( one past last element ) of the search range |
|
88 * @param aCmpBeg begin of the range to be compared with |
|
89 * @return ETrue equal, EFalse not equal |
|
90 */ |
|
91 template< class T > |
|
92 inline TBool equal( |
|
93 const T* aBeg, |
|
94 const T*const aEnd, |
|
95 const T* aCmpBeg) |
|
96 { |
|
97 while ( aBeg != aEnd ) |
|
98 { |
|
99 if ( *aBeg != *aCmpBeg ) |
|
100 { |
|
101 return EFalse; |
|
102 } |
|
103 ++aBeg; |
|
104 ++aCmpBeg; |
|
105 } |
|
106 |
|
107 return ETrue; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Just like find in C++ STL |
|
112 * Returns the position of the first element in the range [aBeg, aEnd) |
|
113 * that has a value equal to aValue |
|
114 * Complexity: linear |
|
115 * @param aBeg begin of range |
|
116 * @param aEnd end ( one past last element ) of the range |
|
117 * @param aValue value to be searched |
|
118 * @return the position of the first element in the range (aBeg, aEnd] |
|
119 * that has a value equal to aValue. aEnd is returned if no matching |
|
120 * element is found |
|
121 */ |
|
122 template< class T, class J > |
|
123 inline T* find( |
|
124 const T* aBeg, |
|
125 const T* const aEnd, |
|
126 const J& aValue) |
|
127 { |
|
128 while ( aBeg != aEnd ) |
|
129 { |
|
130 if ( *aBeg == aValue ) |
|
131 { |
|
132 break; |
|
133 } |
|
134 |
|
135 ++aBeg; |
|
136 } |
|
137 |
|
138 return const_cast<T*>(aBeg); |
|
139 } |
|
140 |
|
141 /** |
|
142 * Just like find_if in C++ STL |
|
143 * Returns the position of the first element in the range [aBeg, aEnd) |
|
144 * for which the unary predicate op(elem) yields true |
|
145 * Complexity: linear |
|
146 * @param aBeg begin of range |
|
147 * @param aEnd end ( one past last element ) of the range |
|
148 * @param aUnaryPredicate a unary predicate |
|
149 * @return the position of the first element in the range (aBeg, aEnd] |
|
150 * for which the unary predicate op(elem) yields true. |
|
151 * aEnd is returned if no matching element is found |
|
152 * NOTE: aUnaryPredicate should not change its state during a function call |
|
153 */ |
|
154 template< class T, class J > |
|
155 inline T* find_if( |
|
156 const T* aBeg, |
|
157 const T* const aEnd, |
|
158 const J& aUnaryPredicate) |
|
159 { |
|
160 while ( aBeg != aEnd ) |
|
161 { |
|
162 if ( aUnaryPredicate( *aBeg ) ) |
|
163 { |
|
164 break; |
|
165 } |
|
166 |
|
167 ++aBeg; |
|
168 } |
|
169 |
|
170 return const_cast<T*>(aBeg); |
|
171 } |
|
172 |
|
173 /** |
|
174 * Just like fill in C++ STL for T type |
|
175 * Assigns aValue to each element in the range [aBeg, aEnd) |
|
176 * The caller must ensure that the destination range is big enough |
|
177 * Complexity: linear |
|
178 * @param aBeg begin of range |
|
179 * @param aEnd end ( one past last element ) of the range |
|
180 * @param aValue value to be assigned |
|
181 */ |
|
182 template< class T, class J> |
|
183 inline void fill( |
|
184 T* aBeg, |
|
185 const T* const aEnd, |
|
186 const J& aValue) |
|
187 { |
|
188 while ( aBeg != aEnd ) |
|
189 { |
|
190 *aBeg = aValue; |
|
191 ++aBeg; |
|
192 } |
|
193 } |
|
194 |
|
195 /** |
|
196 * Just like fill_n in C++ STL for T type |
|
197 * Assigns aValue to the first aNum elements in the range starting with aBeg |
|
198 * The caller must ensure that the destination range is big enough |
|
199 * Complexity: linear |
|
200 * @param aBeg begin of range |
|
201 * @param aNum number of elements to be processed |
|
202 * @param aValue value to be assigned |
|
203 */ |
|
204 template< class T, class J > |
|
205 inline void fill_n( |
|
206 T* aBeg, |
|
207 TUint32 aNum, |
|
208 const J& aValue) |
|
209 { |
|
210 while ( aNum ) |
|
211 { |
|
212 *aBeg = aValue; |
|
213 ++aBeg; |
|
214 --aNum; |
|
215 } |
|
216 } |
|
217 |
|
218 /** |
|
219 * Just like copy in C++ STL |
|
220 * @param aSrc begin of copy |
|
221 * @param aSrcEnd end of copy |
|
222 * @param aDest target of copy |
|
223 * @return aDest |
|
224 */ |
|
225 template<class T> |
|
226 inline T* copy( |
|
227 const T* aSrc, |
|
228 const T* aSrcEnd, |
|
229 T* aDest) |
|
230 { |
|
231 while ( aSrc != aSrcEnd ) |
|
232 { |
|
233 *aDest = *aSrc; |
|
234 ++aSrc; |
|
235 ++aDest; |
|
236 } |
|
237 |
|
238 return aDest; |
|
239 } |
|
240 |
|
241 template<class T> |
|
242 inline T* reverse_copy( |
|
243 const T* aSrcBeg, |
|
244 const T* aSrcEnd, |
|
245 T* aDest) |
|
246 { |
|
247 --aSrcEnd; |
|
248 while ( aSrcEnd >= aSrcBeg ) |
|
249 { |
|
250 *aDest = *aSrcEnd; |
|
251 --aSrcEnd; |
|
252 ++aDest; |
|
253 } |
|
254 |
|
255 return aDest; |
|
256 } |
|
257 |
|
258 /** |
|
259 * Does a looped copy for T type |
|
260 * @param aSource source of the copy |
|
261 * @param aDest destination where to copy |
|
262 * @param aCount number of iterations |
|
263 * @return aDest |
|
264 */ |
|
265 template<class T> |
|
266 inline TAny* assign( |
|
267 const T* aSource, |
|
268 T* aDest, |
|
269 const TInt aCount) |
|
270 { |
|
271 TAny* origdest = static_cast<TAny*>(aDest); |
|
272 for ( TInt idx = 0 ; idx != aCount ; ++idx, ++aSource, ++aDest ) |
|
273 { |
|
274 *aDest = *aSource; |
|
275 } |
|
276 |
|
277 return origdest; |
|
278 } |
|
279 |
|
280 #endif // ALGORITHM_H |