From d601548d5379f1192e2b522c7b8df367d84f9849 Mon Sep 17 00:00:00 2001 From: asivache Date: Fri, 29 May 2009 20:15:00 +0000 Subject: [PATCH] added reallocate(int[] orig_array, int new_size) and int[] indexOfAll(String s, int ch); the former is self-explanatory, while the latter returns array of indices of all occurences of ch in the specified string git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@856 348d0f76-0448-11de-a6fe-93d51630548a --- .../org/broadinstitute/sting/utils/Utils.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/java/src/org/broadinstitute/sting/utils/Utils.java b/java/src/org/broadinstitute/sting/utils/Utils.java index 7bccfafff..c9d531180 100755 --- a/java/src/org/broadinstitute/sting/utils/Utils.java +++ b/java/src/org/broadinstitute/sting/utils/Utils.java @@ -406,6 +406,35 @@ public class Utils { } + /** Returns indices of all occurrences of the specified symbol in the string */ + public static int[] indexOfAll(String s, int ch) { + int[] pos = new int[64]; + int z = 0; + + for ( int i = 0 ; i < s.length() ; i++ ) { + if ( s.charAt(i) == ch ) pos[z++] = i; + } + return reallocate(pos,z); + } + + /** Returns new (reallocated) integer array of the specified size, with content + * of the original array orig copied into it. If newSize is + * less than the size of the original array, only first newSize elements will be copied. + * If new size is greater than the size of the original array, the content of the original array will be padded + * with zeros up to the new size. Finally, if new size is the same as original size, no memory reallocation + * will be performed and the original array will be returned instead. + * @param orig + * @param newSize + * @return + */ + public static int[] reallocate(int[] orig, int newSize) { + if ( orig.length == newSize ) return orig; + int[] new_array = new int[newSize]; + int L = ( newSize > orig.length ? orig.length : newSize ); + for ( int i = 0 ; i < L ; i++ ) new_array[i] = orig[i]; + return new_array; + } + /* TEST ME public static void main(String[] argv) { List l1 = new LinkedList();