Coverage Report - org.perfidix.example.list.Array
 
Classes in this File Line Coverage Branch Coverage Complexity
Array
0%
0/45
0%
0/6
1.2
 
 1  
 /**
 2  
  * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  * * Redistributions of source code must retain the above copyright
 8  
  * notice, this list of conditions and the following disclaimer.
 9  
  * * Redistributions in binary form must reproduce the above copyright
 10  
  * notice, this list of conditions and the following disclaimer in the
 11  
  * documentation and/or other materials provided with the distribution.
 12  
  * * Neither the name of the University of Konstanz nor the
 13  
  * names of its contributors may be used to endorse or promote products
 14  
  * derived from this software without specific prior written permission.
 15  
  * 
 16  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 17  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 18  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 19  
  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 20  
  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 21  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 22  
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 23  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 24  
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 25  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26  
  */
 27  
 package org.perfidix.example.list;
 28  
 
 29  
 import java.util.Arrays;
 30  
 
 31  
 /**
 32  
  * This class provides convenience methods for handling arrays and serves as an
 33  
  * extension to the {@link Arrays} class of Java.
 34  
  * 
 35  
  * @author BaseX Team 2005-11, BSD License
 36  
  * @author Christian Gruen
 37  
  */
 38  
 public final class Array {
 39  
     /** Default factor for resizing dynamic arrays. */
 40  
     public static final double RESIZE = 1.5;
 41  
 
 42  
     /** Private constructor. */
 43  0
     private Array() {
 44  0
     }
 45  
 
 46  
     /**
 47  
      * Copies the specified array.
 48  
      * 
 49  
      * @param a
 50  
      *            array to be copied
 51  
      * @param s
 52  
      *            new array size
 53  
      * @return new array
 54  
      */
 55  
     public static byte[][] copyOf(final byte[][] a, final int s) {
 56  0
         final byte[][] tmp = new byte[s][];
 57  0
         System.arraycopy(a, 0, tmp, 0, Math.min(s, a.length));
 58  0
         return tmp;
 59  
     }
 60  
 
 61  
     /**
 62  
      * Copies the specified array.
 63  
      * 
 64  
      * @param a
 65  
      *            array to be copied
 66  
      * @param s
 67  
      *            new array size
 68  
      * @return new array
 69  
      */
 70  
     public static int[][] copyOf(final int[][] a, final int s) {
 71  0
         final int[][] tmp = new int[s][];
 72  0
         System.arraycopy(a, 0, tmp, 0, Math.min(s, a.length));
 73  0
         return tmp;
 74  
     }
 75  
 
 76  
     /**
 77  
      * Copies the specified array.
 78  
      * 
 79  
      * @param a
 80  
      *            array to be copied
 81  
      * @param s
 82  
      *            new array size
 83  
      * @return new array
 84  
      */
 85  
     public static String[] copyOf(final String[] a, final int s) {
 86  0
         final String[] tmp = new String[s];
 87  0
         System.arraycopy(a, 0, tmp, 0, Math.min(s, a.length));
 88  0
         return tmp;
 89  
     }
 90  
 
 91  
     /**
 92  
      * Adds an entry to the end of an array and returns the new array.
 93  
      * 
 94  
      * @param ar
 95  
      *            array to be resized
 96  
      * @param e
 97  
      *            entry to be added
 98  
      * @param <T>
 99  
      *            array type
 100  
      * @return array
 101  
      */
 102  
     public static <T> T[] add(final T[] ar, final T e) {
 103  0
         final int s = ar.length;
 104  0
         final T[] t = Arrays.copyOf(ar, s + 1);
 105  0
         t[s] = e;
 106  0
         return t;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Adds an entry to the end of an array and returns the new array.
 111  
      * 
 112  
      * @param ar
 113  
      *            array to be resized
 114  
      * @param e
 115  
      *            entry to be added
 116  
      * @return array
 117  
      */
 118  
     public static int[] add(final int[] ar, final int e) {
 119  0
         final int s = ar.length;
 120  0
         final int[] t = Arrays.copyOf(ar, s + 1);
 121  0
         t[s] = e;
 122  0
         return t;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Moves entries inside an array.
 127  
      * 
 128  
      * @param ar
 129  
      *            array
 130  
      * @param pos
 131  
      *            position
 132  
      * @param off
 133  
      *            move offset
 134  
      * @param l
 135  
      *            length
 136  
      */
 137  
     public static void move(final Object ar, final int pos, final int off, final int l) {
 138  0
         System.arraycopy(ar, pos, ar, pos + off, l);
 139  0
     }
 140  
 
 141  
     /**
 142  
      * Removes an array entry at the specified position.
 143  
      * 
 144  
      * @param ar
 145  
      *            array to be resized
 146  
      * @param p
 147  
      *            position
 148  
      * @param <T>
 149  
      *            array type
 150  
      * @return array
 151  
      */
 152  
     public static <T> T[] delete(final T[] ar, final int p) {
 153  0
         final int s = ar.length - 1;
 154  0
         move(ar, p + 1, -1, s - p);
 155  0
         return Arrays.copyOf(ar, s);
 156  
     }
 157  
 
 158  
     /**
 159  
      * Reverses the order of the elements in the given array.
 160  
      * 
 161  
      * @param arr
 162  
      *            array
 163  
      */
 164  
     public static void reverse(final byte[] arr) {
 165  0
         reverse(arr, 0, arr.length);
 166  0
     }
 167  
 
 168  
     /**
 169  
      * Reverses the order of all elements in the given interval.
 170  
      * 
 171  
      * @param arr
 172  
      *            array
 173  
      * @param pos
 174  
      *            position of first element of the interval
 175  
      * @param len
 176  
      *            length of the interval
 177  
      */
 178  
     public static void reverse(final byte[] arr, final int pos, final int len) {
 179  0
         for (int l = pos, r = pos + len - 1; l < r; l++, r--) {
 180  0
             final byte tmp = arr[l];
 181  0
             arr[l] = arr[r];
 182  0
             arr[r] = tmp;
 183  
         }
 184  0
     }
 185  
 
 186  
     /**
 187  
      * Reverses the order of all elements in the given interval.
 188  
      * 
 189  
      * @param arr
 190  
      *            array
 191  
      * @param pos
 192  
      *            position of first element of the interval
 193  
      * @param len
 194  
      *            length of the interval
 195  
      */
 196  
     public static void reverse(final Object[] arr, final int pos, final int len) {
 197  0
         for (int l = pos, r = pos + len - 1; l < r; l++, r--) {
 198  0
             final Object tmp = arr[l];
 199  0
             arr[l] = arr[r];
 200  0
             arr[r] = tmp;
 201  
         }
 202  0
     }
 203  
 
 204  
     /**
 205  
      * Returns a value for a new array size, which will always be larger than
 206  
      * the specified value.
 207  
      * 
 208  
      * @param old
 209  
      *            old size
 210  
      * @return resulting size
 211  
      */
 212  
     public static int newSize(final int old) {
 213  0
         return newSize(old, RESIZE);
 214  
     }
 215  
 
 216  
     /**
 217  
      * Returns a value for a new array size, which will always be larger than
 218  
      * the specified value.
 219  
      * 
 220  
      * @param old
 221  
      *            old size
 222  
      * @param factor
 223  
      *            resize factor; must be larger than or equal to 1
 224  
      * @return resulting size
 225  
      */
 226  
     public static int newSize(final int old, final double factor) {
 227  0
         return (int)(old * factor) + 1;
 228  
     }
 229  
 
 230  
     /**
 231  
      * Swaps two entries of the given int array.
 232  
      * 
 233  
      * @param arr
 234  
      *            array
 235  
      * @param a
 236  
      *            first position
 237  
      * @param b
 238  
      *            second position
 239  
      */
 240  
     public static void swap(final int[] arr, final int a, final int b) {
 241  0
         final int c = arr[a];
 242  0
         arr[a] = arr[b];
 243  0
         arr[b] = c;
 244  0
     }
 245  
 
 246  
     /**
 247  
      * Swaps arr[a .. (a+n-1)] with arr[b .. (b+n-1)].
 248  
      * 
 249  
      * @param arr
 250  
      *            order array
 251  
      * @param a
 252  
      *            first offset
 253  
      * @param b
 254  
      *            second offset
 255  
      * @param n
 256  
      *            number of values
 257  
      */
 258  
     public static void swap(final int[] arr, final int a, final int b, final int n) {
 259  0
         for (int i = 0; i < n; ++i)
 260  0
             swap(arr, a + i, b + i);
 261  0
     }
 262  
 }