comparison orange/util/collection/Array.d @ 27:fc315d786f24 experimental

Added unit testing.
author Jacob Carlborg <doob@me.com>
date Fri, 19 Nov 2010 11:14:55 +0100
parents 99c52d46822a
children 511d1ef4e299
comparison
equal deleted inserted replaced
26:78e5fef4bbf2 27:fc315d786f24
17 else 17 else
18 { 18 {
19 version = Phobos; 19 version = Phobos;
20 20
21 import std.c.string : memmove; 21 import std.c.string : memmove;
22 } 22 }
23
24 import orange.core.string;
25 import orange.util.Traits;
23 26
24 /** 27 /**
25 * Inserts the specified element at the specified position in the array. Shifts the 28 * Inserts the specified element at the specified position in the array. Shifts the
26 * element currently at that position (if any) and any subsequent elements to the right. 29 * element currently at that position (if any) and any subsequent elements to the right.
27 * 30 *
271 assert(arr.length > 0, "mambo.collection.Array.contains: The length of the array was 0"); 274 assert(arr.length > 0, "mambo.collection.Array.contains: The length of the array was 0");
272 } 275 }
273 body 276 body
274 { 277 {
275 return arr.indexOf!(T, size_t)(element) < size_t.max; 278 return arr.indexOf!(T, size_t)(element) < size_t.max;
279 }
280
281 /**
282 * Returns $(D_KEYWORD true) if the array contains the given pattern.
283 *
284 * Params:
285 * arr = the array to check if it contains the element
286 * pattern = the pattern whose presence in the array is to be tested
287 *
288 * Returns: $(D_KEYWORD true) if the array contains the given pattern
289 */
290 bool contains (T) (T[] arr, T[] pattern)
291 {
292 static if (isChar!(T))
293 {
294 version (Tango)
295 return tango.text.Util.containsPattern(arr, pattern);
296
297 else
298 return stdString.indexOf(arr, element) != -1;
299 }
300
301 else
302 {
303 version (Tango)
304 return tango.core.Array.contains(arr, pattern);
305
306 else
307 return !algorithm.find(arr, pattern).empty;
308 }
276 } 309 }
277 310
278 /** 311 /**
279 * Returns $(D_KEYWORD true) if this array contains no elements. 312 * Returns $(D_KEYWORD true) if this array contains no elements.
280 * 313 *
431 * Throws: 464 * Throws:
432 * AssertException if pos is greater than the length of the array 465 * AssertException if pos is greater than the length of the array
433 * 466 *
434 * Returns: the array 467 * Returns: the array
435 */ 468 */
436 T[] replace (T, U = size_t) (ref T[] arr, U pos, U n, T[] elements) 469 /*T[] replace (T, U = size_t) (ref T[] arr, U pos, U n, T[] elements)
437 in 470 in
438 { 471 {
439 assert(pos <= arr.length, "mambo.collection.Array.replace: The position was greter than the length of the array"); 472 assert(pos <= arr.length, "mambo.collection.Array.replace: The position was greter than the length of the array");
440 } 473 }
441 body 474 body
489 } 522 }
490 523
491 } 524 }
492 525
493 return arr; 526 return arr;
494 } 527 }*/
528
529 /**
530 * Replaces all the occurences of $(D_PARAM pattern) with $(D_PARAM replacement)
531 *
532 * Params:
533 * arr = the array to do the raplce in
534 * pattern = the pattern to match
535 * replacement = the values to subsitute
536 *
537 * Returns: the passed in array with all the patterns subsituted
538 */
539 T[] replace (T : wchar) (T[] arr, dchar pattern, dchar replacement)
540 {
541 foreach (i, dchar e ; arr)
542 if (e == pattern)
543 arr[i] = replacement;
544
545 return arr;
546 }
547
548 /**
549 * Replaces all the occurences of $(D_PARAM pattern) with $(D_PARAM replacement)
550 *
551 * Params:
552 * arr = the array to do the raplce in
553 * pattern = the pattern to match
554 * replacement = the values to subsitute
555 *
556 * Returns: the passed in array with all the patterns subsituted
557 */
558 /*T[] replace (T) (T[] arr, T pattern, T replacement)
559 {
560 version (Tango)
561 tango.core.Array.replace(arr, pattern, replacement);
562
563
564 else
565 {
566 foreach (ref e ; arr)
567 if (e == pattern)
568 e = replacement;
569 }
570
571 return arr;
572 }*/
495 573
496 /** 574 /**
497 * Erases a part of the array content, shortening the length of the array. 575 * Erases a part of the array content, shortening the length of the array.
498 * 576 *
499 * Params: 577 * Params: