From a47824d68072450fb3d72f4d2ac780bd8d5005a0 Mon Sep 17 00:00:00 2001 From: asivache Date: Wed, 4 Aug 2010 15:30:48 +0000 Subject: [PATCH] A couple of type specific implementations of a single extend() method: takes an array (byte[] or short[] currently) and "extends" it to the left or to the right by the specified number of elements. Returns newly allocated array, with the content of original array copied in (if we extend by n elements to the left, then the returned array will have n default-filled elements *followed* by the content of the old array). git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@3932 348d0f76-0448-11de-a6fe-93d51630548a --- .../org/broadinstitute/sting/utils/Utils.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/java/src/org/broadinstitute/sting/utils/Utils.java b/java/src/org/broadinstitute/sting/utils/Utils.java index 2f65f9cee..c6f27c527 100755 --- a/java/src/org/broadinstitute/sting/utils/Utils.java +++ b/java/src/org/broadinstitute/sting/utils/Utils.java @@ -359,6 +359,78 @@ public class Utils { return new_array; } + + /** + * Returns a copy of array a, extended with additional n elements to the right (if n > 0 ) or -n elements to the + * left (if n<0), copying the values form the original array. Newly added elements are filled with value v. Note that + * if array a is being padded to the left, first (-n) elements of the returned array are v's, followed by the content of + * array a. + * @param a original array + * @param n number of (v-filled) elements to append to a on the right (n>0) or on the left (n<0) + * @return + */ + public static byte [] extend(final byte[] a, int n, byte v) { + + byte [] newA; + + if ( n > 0 ) { + newA = Arrays.copyOf(a, a.length+n); + if ( v != 0) { // java pads with 0's for us, so there is nothing to do if v==0 + for ( int i = a.length; i < newA.length ; i++ ) newA[i] = v; + } + return newA; + } + + // we are here only if n < 0: + n = (-n); + newA = new byte[ a.length + n ]; + int i; + if ( v!= 0 ) { + i = 0; + for( ; i < n; i++ ) newA[i] = v; + } else { + i = n; + } + for ( int j = 0 ; j < a.length ; i++, j++) newA[i]=a[j]; + return newA; + } + + + /** + * Returns a copy of array a, extended with additional n elements to the right (if n > 0 ) or -n elements to the + * left (if n<0), copying the values form the original array. Newly added elements are filled with value v. Note that + * if array a is padded to the left, first (-n) elements of the returned array are v's, followed by the content of + * array a. + * @param a original array + * @param n number of (v-filled) elements to append to a on the right (n>0) or on the left (n<0) + * @return + */ + public static short [] extend(final short[] a, int n, short v) { + + short [] newA; + + if ( n > 0 ) { + newA = Arrays.copyOf(a, a.length+n); + if ( v != 0) { // java pads with 0's for us, so there is nothing to do if v==0 + for ( int i = a.length; i < newA.length ; i++ ) newA[i] = v; + } + return newA; + } + + // we are here only if n < 0: + n = (-n); + newA = new short[ a.length + n ]; + int i; + if ( v!= 0 ) { + i = 0; + for( ; i < n; i++ ) newA[i] = v; + } else { + i = n; + } + for ( int j = 0 ; j < a.length ; i++, j++) newA[i]=a[j]; + return newA; + } + /* TEST ME public static void main(String[] argv) { List l1 = new LinkedList();