/*
* LingPipe v. 3.9
* Copyright (C) 2003-2010 Alias-i
*
* This program is licensed under the Alias-i Royalty Free License
* Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Alias-i
* Royalty Free License Version 1 for more details.
*
* You should have received a copy of the Alias-i Royalty Free License
* Version 1 along with this program; if not, visit
* http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact
* Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211,
* +1 (718) 290-9170.
*/
//package com.aliasi.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Static utility methods for processing arrays.
*
* @author Bob Carpenter
* @version 4.0.0
* @since LingPipe1.0
*/
public class Arrays {
/**
* Returns a copy of the specified array of objects of the
* specified size. The runtime type of the array returned will be
* that of the specified input array.
*
* As many of the original elements as will fit
* in the new array are copied into the returned array. If the
* new size is longer, the remaining elements will be null.
*
* @param xs Original array.
* @param newSize Size of returned array.
* @return New array of specified length with as many elements copied
* from the original array as will fit.
* @param type of objects in array.
*/
public static E[] reallocate(E[] xs, int newSize) {
@SuppressWarnings("unchecked") // must work because of type system and reflect.Array
E[] ys
= (E[])
java.lang.reflect.Array
.newInstance(xs.getClass().getComponentType(), newSize);
int end = java.lang.Math.min(xs.length,newSize);
for (int i = 0; i < end; ++i)
ys[i] = xs[i];
return ys;
}
/**
* Returns a copy of the specified array of integers of the
* specified size. As many of the original elements as will
* fit in the new array are copied. If the new size is longer, the
* remaining elements will be 0.
*
* @param xs Original array.
* @param newSize Length of returned array.
* @return New array of specified length with as many elements
* copied from the original array as will fit.
*/
public static int[] reallocate(int[] xs, int newSize) {
int[] ys = new int[newSize];
int end = java.lang.Math.min(xs.length,newSize);
for (int i = 0; i < end; ++i)
ys[i] = xs[i];
return ys;
}
/**
* Reallocates the specified integer array to be 50 percent
* longer, with a minimum growth in length of one element. All of
* the elements of the specified array will be copied into the
* resulting array and the remaining elements initialized to zero
* (0
).
*
* @param xs Array to reallocate.
* @return Result of reallocation.
*/
public static int[] reallocate(int[] xs) {
int len = (xs.length * 3) / 2;
return reallocate(xs,len == xs.length ? xs.length + 1 : len);
}
}