Mercurial > projects > dwt-mac
comparison dwt/dwthelper/array.d @ 126:38807a925e24
Fixed compile errors, support for SWT language files
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Fri, 16 Jan 2009 23:35:40 +0100 |
parents | 63a09873578e |
children | ad4e1fe71a5a |
comparison
equal
deleted
inserted
replaced
125:5583f8eeee6c | 126:38807a925e24 |
---|---|
190 * | 190 * |
191 * Throws: AssertException if the length of the array is 0 | 191 * Throws: AssertException if the length of the array is 0 |
192 */ | 192 */ |
193 alias remove removeElement; | 193 alias remove removeElement; |
194 | 194 |
195 T[] arrayIndexRemove(T, U = size_t)(T[] arr, U n) { | |
196 if (n is 0) | |
197 return arr[1..$]; | |
198 if (n > arr.length) | |
199 return arr; | |
200 if (n is arr.length-1) | |
201 return arr[0..n-1]; | |
202 // else | |
203 return arr[0..n] ~ arr[n+1..$]; | |
204 } | |
205 | |
195 /** | 206 /** |
196 * Returns the index of the first occurrence of the specified element | 207 * Returns the index of the first occurrence of the specified element |
197 * in the array, or -1 if the array does not contain the element. | 208 * in the array, or -1 if the array does not contain the element. |
198 * | 209 * |
199 * Params: | 210 * Params: |
200 * arr = the array to get the index of the element from | 211 * arr = the array to get the index of the element from |
201 * element = the element to find | 212 * element = the element to find |
213 * start = the index of where to start searching from | |
202 * | 214 * |
203 * Returns: the index of the element or -1 if it's not in the array | 215 * Returns: the index of the element or -1 if it's not in the array |
204 * | 216 * |
205 * Throws: AssertException if the length of the array is 0 | 217 * Throws: AssertException if the length of the array is 0 |
206 * Throws: AssertException if the return value is less than -1 or | 218 * Throws: AssertException if the return value is less than -1 or |
207 * greater than the length of the array - 1. | 219 * greater than the length of the array - 1. |
208 */ | 220 */ |
209 size_t indexOf (T, U = uint) (T[] arr, T element, U start = 0) | 221 size_t indexOf (T, U = size_t) (T[] arr, T[] match, U start = 0) |
210 in | 222 in |
211 { | 223 { |
212 assert(arr.length > 0); | 224 assert(arr.length > 0); |
213 assert(start >= 0); | 225 assert(start >= 0); |
214 assert(element !is null); | |
215 } | 226 } |
216 out (result) | 227 out (result) |
217 { | 228 { |
218 assert(result >= -1 && result < arr.length); | 229 assert(result >= -1 && result < arr.length); |
219 } | 230 } |
220 body | 231 body |
221 { | 232 { |
222 uint index; | 233 size_t index = tango.text.Util.locatePattern(arr, match, start); |
234 | |
235 if (index != arr.length) | |
236 return index; | |
237 | |
238 else | |
239 return -1; | |
240 } | |
241 | |
242 /** | |
243 * Returns the index of the first occurrence of the specified element | |
244 * in the array, or -1 if the array does not contain the element. | |
245 * | |
246 * Params: | |
247 * arr = the array to get the index of the element from | |
248 * element = the element to find | |
249 * start = the index of where to start searching from | |
250 * | |
251 * Returns: the index of the element or -1 if it's not in the array | |
252 * | |
253 * Throws: AssertException if the length of the array is 0 | |
254 * Throws: AssertException if the return value is less than -1 or | |
255 * greater than the length of the array - 1. | |
256 */ | |
257 size_t indexOf (T, U = size_t) (T[] arr, T element, U start = 0) | |
258 in | |
259 { | |
260 assert(arr.length > 0); | |
261 assert(start >= 0); | |
262 } | |
263 out (result) | |
264 { | |
265 assert(result >= -1 && result < arr.length); | |
266 } | |
267 body | |
268 { | |
269 size_t index; | |
223 | 270 |
224 version (Tango) | 271 version (Tango) |
225 index = tango.text.Util.locate(arr, element); | 272 index = cast(size_t) tango.text.Util.locate(arr, element, start); |
226 | 273 |
227 else | 274 else |
228 { | 275 { |
229 if (start > arr.length) | 276 if (start > arr.length) |
230 start = arr.length; | 277 start = arr.length; |
231 | 278 |
232 index = privateIndexOf(arr.ptr, element, arr.length - start) + start; | 279 index = cast(size_t) privateIndexOf(arr.ptr, element, arr.length - start) + start; |
233 } | 280 } |
234 | 281 |
235 if (index == arr.length) | 282 if (index != arr.length) |
236 index = -1; | 283 return index; |
237 | 284 else |
238 return index; | 285 return -1; |
239 | 286 |
240 version (Phobos) | 287 version (Phobos) |
241 { | 288 { |
242 // tango.text.Util.locate | 289 // tango.text.Util.locate |
243 uint privateIndexOf (T* str, T match, uint length) | 290 uint privateIndexOf (T* str, T match, uint length) |