Mercurial > projects > orange
diff 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 |
line wrap: on
line diff
--- a/orange/util/collection/Array.d Tue Oct 19 10:22:10 2010 +0200 +++ b/orange/util/collection/Array.d Fri Nov 19 11:14:55 2010 +0100 @@ -19,7 +19,10 @@ version = Phobos; import std.c.string : memmove; -} +} + +import orange.core.string; +import orange.util.Traits; /** * Inserts the specified element at the specified position in the array. Shifts the @@ -276,6 +279,36 @@ } /** + * Returns $(D_KEYWORD true) if the array contains the given pattern. + * + * Params: + * arr = the array to check if it contains the element + * pattern = the pattern whose presence in the array is to be tested + * + * Returns: $(D_KEYWORD true) if the array contains the given pattern + */ +bool contains (T) (T[] arr, T[] pattern) +{ + static if (isChar!(T)) + { + version (Tango) + return tango.text.Util.containsPattern(arr, pattern); + + else + return stdString.indexOf(arr, element) != -1; + } + + else + { + version (Tango) + return tango.core.Array.contains(arr, pattern); + + else + return !algorithm.find(arr, pattern).empty; + } +} + +/** * Returns $(D_KEYWORD true) if this array contains no elements. * * Params: @@ -433,7 +466,7 @@ * * Returns: the array */ -T[] replace (T, U = size_t) (ref T[] arr, U pos, U n, T[] elements) +/*T[] replace (T, U = size_t) (ref T[] arr, U pos, U n, T[] elements) in { assert(pos <= arr.length, "mambo.collection.Array.replace: The position was greter than the length of the array"); @@ -491,9 +524,54 @@ } return arr; +}*/ + +/** + * Replaces all the occurences of $(D_PARAM pattern) with $(D_PARAM replacement) + * + * Params: + * arr = the array to do the raplce in + * pattern = the pattern to match + * replacement = the values to subsitute + * + * Returns: the passed in array with all the patterns subsituted + */ +T[] replace (T : wchar) (T[] arr, dchar pattern, dchar replacement) +{ + foreach (i, dchar e ; arr) + if (e == pattern) + arr[i] = replacement; + + return arr; } /** + * Replaces all the occurences of $(D_PARAM pattern) with $(D_PARAM replacement) + * + * Params: + * arr = the array to do the raplce in + * pattern = the pattern to match + * replacement = the values to subsitute + * + * Returns: the passed in array with all the patterns subsituted + */ +/*T[] replace (T) (T[] arr, T pattern, T replacement) +{ + version (Tango) + tango.core.Array.replace(arr, pattern, replacement); + + + else + { + foreach (ref e ; arr) + if (e == pattern) + e = replacement; + } + + return arr; +}*/ + +/** * Erases a part of the array content, shortening the length of the array. * * Params: