123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- package edu.stanford.nlp.util;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.List;
- import java.util.ArrayList;
- /**
- * Static utility methods for operating on arrays.
- *
- * @author Huy Nguyen (htnguyen@cs.stanford.edu)
- * @author Michel Galley (mgalley@stanford.edu)
- */
- public class ArrayUtils {
- /**
- * Should not be instantiated
- */
- protected ArrayUtils() {
- }
- public static double[] flatten(double[][] array) {
- int size = 0;
- for (double[] a : array) {
- size += a.length;
- }
- double[] newArray = new double[size];
- int i = 0;
- for (double[] a : array) {
- for (double d : a) {
- newArray[i++] = d;
- }
- }
- return newArray;
- }
- public static double[][] to2D(double[] array, int dim1Size) {
- int dim2Size = array.length/dim1Size;
- return to2D(array, dim1Size, dim2Size);
- }
- public static double[][] to2D(double[] array, int dim1Size, int dim2Size) {
- double[][] newArray = new double[dim1Size][dim2Size];
- int k = 0;
- for (int i = 0; i < newArray.length; i++) {
- for (int j = 0; j < newArray[i].length; j++) {
- newArray[i][j] = array[k++];
- }
- }
- return newArray;
- }
- /**
- * Removes the element at the specified index from the array, and returns
- * a new array containing the remaining elements. If <tt>index</tt> is
- * invalid, returns <tt>array</tt> unchanged.
- */
- public static double[] removeAt(double[] array, int index) {
- if (array == null) {
- return null;
- }
- if (index < 0 || index >= array.length) {
- return array;
- }
- double[] retVal = new double[array.length - 1];
- for (int i = 0; i < array.length; i++) {
- if (i < index) {
- retVal[i] = array[i];
- } else if (i > index) {
- retVal[i - 1] = array[i];
- }
- }
- return retVal;
- }
- /**
- * Removes the element at the specified index from the array, and returns
- * a new array containing the remaining elements. If <tt>index</tt> is
- * invalid, returns <tt>array</tt> unchanged. Uses reflection to determine
- * the type of the array and returns an array of the appropriate type.
- */
- public static Object[] removeAt(Object[] array, int index) {
- if (array == null) {
- return null;
- }
- if (index < 0 || index >= array.length) {
- return array;
- }
- Object[] retVal = (Object[]) Array.newInstance(array[0].getClass(), array.length - 1);
- for (int i = 0; i < array.length; i++) {
- if (i < index) {
- retVal[i] = array[i];
- } else if (i > index) {
- retVal[i - 1] = array[i];
- }
- }
- return retVal;
- }
- public static String toString(int[][] a) {
- StringBuilder result = new StringBuilder("[");
- for (int i = 0; i < a.length; i++) {
- result.append(Arrays.toString(a[i]));
- if(i < a.length-1)
- result.append(",");
- }
- result.append("]");
- return result.toString();
- }
- /**
- * Tests two int[][] arrays for having equal contents.
- * @param xs
- * @param ys
- * @return true iff for each i, <code>equalContents(xs[i],ys[i])</code> is true
- */
- public static boolean equalContents(int[][] xs, int[][] ys) {
- if(xs == null && ys != null)
- return false;
- if(ys == null)
- return false;
- if(xs.length != ys.length)
- return false;
- for(int i = xs.length-1; i >= 0; i--) {
- if(! equalContents(xs[i],ys[i]))
- return false;
- }
- return true;
- }
- /**
- * Tests two double[][] arrays for having equal contents.
- * @param xs
- * @param ys
- * @return true iff for each i, <code>equals(xs[i],ys[i])</code> is true
- */
- public static boolean equals(double[][] xs, double[][] ys) {
- if(xs == null && ys != null)
- return false;
- if(ys == null)
- return false;
- if(xs.length != ys.length)
- return false;
- for(int i = xs.length-1; i >= 0; i--) {
- if(!Arrays.equals(xs[i],ys[i]))
- return false;
- }
- return true;
- }
-
- /**
- * tests two int[] arrays for having equal contents
- * @param xs
- * @param ys
- * @return true iff xs and ys have equal length, and for each i, <code>xs[i]==ys[i]</code>
- */
- public static boolean equalContents(int[] xs, int[] ys) {
- if(xs.length != ys.length)
- return false;
- for(int i = xs.length-1; i >= 0; i--) {
- if(xs[i] != ys[i])
- return false;
- }
- return true;
- }
- /**
- * Tests two boolean[][] arrays for having equal contents.
- * @param xs
- * @param ys
- * @return true iff for each i, <code>Arrays.equals(xs[i],ys[i])</code> is true
- */
- public static boolean equals(boolean[][] xs, boolean[][] ys) {
- if(xs == null && ys != null)
- return false;
- if(ys == null)
- return false;
- if(xs.length != ys.length)
- return false;
- for(int i = xs.length-1; i >= 0; i--) {
- if(! Arrays.equals(xs[i],ys[i]))
- return false;
- }
- return true;
- }
- /** Returns true iff object o equals (not ==) some element of array a. */
- public static <T> boolean contains(T[] a, T o) {
- for (T item : a) {
- if (item.equals(o)) return true;
- }
- return false;
- }
- /** Return a set containing the same elements as the specified array.
- */
- public static <T> Set<T> asSet(T[] a) {
- return new HashSet<T>(Arrays.asList(a));
- }
- public static void fill(double[][] d, double val) {
- for (int i = 0; i < d.length; i++) {
- Arrays.fill(d[i], val);
- }
- }
- public static void fill(double[][][] d, double val) {
- for (int i = 0; i < d.length; i++) {
- fill(d[i], val);
- }
- }
- public static void fill(double[][][][] d, double val) {
- for (int i = 0; i < d.length; i++) {
- fill(d[i], val);
- }
- }
- public static void fill(boolean[][] d, boolean val) {
- for (int i = 0; i < d.length; i++) {
- Arrays.fill(d[i], val);
- }
- }
- public static void fill(boolean[][][] d, boolean val) {
- for (int i = 0; i < d.length; i++) {
- fill(d[i], val);
- }
- }
- public static void fill(boolean[][][][] d, boolean val) {
- for (int i = 0; i < d.length; i++) {
- fill(d[i], val);
- }
- }
- /**
- * Casts to a double array
- */
- public static double[] toDouble(float[] a) {
- double[] d = new double[a.length];
- for (int i = 0; i < a.length; i++) {
- d[i] = a[i];
- }
- return d;
- }
- /**
- * Casts to a double array.
- */
- public static double[] toDouble(int[] array) {
- double[] rv = new double[array.length];
- for (int i = 0; i < array.length; i++) {
- rv[i] = array[i];
- }
- return rv;
- }
-
- /** needed because Arrays.asList() won't to autoboxing,
- * so if you give it a primitive array you get a
- * singleton list back with just that array as an element.
- */
- public static List<Integer> asList(int[] array) {
- List<Integer> l = new ArrayList<Integer>();
- for (int i : array) {
- l.add(i);
- }
- return l;
- }
- public static double[] asArray(List<Double> d) {
- double[] newD = new double[d.size()];
- int i = 0;
- for (Double j : d) {
- newD[i++] = j;
- }
- return newD;
- }
-
- /**
- * For internal debugging purposes only
- */
- public static void main(String[] args) {
- String[] strings = new String[]{"a", "b", "c"};
- strings = (String[]) ArrayUtils.removeAt(strings, 2);
- for (String string : strings) {
- System.err.println(string);
- }
- System.err.println(asSet(new String[] {"larry", "moe", "curly"}));
- }
- public static int[] copy(int[] i) {
- if (i == null) { return null; }
- int[] newI = new int[i.length];
- System.arraycopy(i, 0, newI, 0, i.length);
- return newI;
- }
- public static int[][] copy(int[][] i) {
- if (i == null) { return null; }
- int[][] newI = new int[i.length][];
- for (int j = 0; j < newI.length; j++) {
- newI[j] = copy(i[j]);
- }
- return newI;
- }
-
- public static double[] copy(double[] d) {
- if (d == null) { return null; }
- double[] newD = new double[d.length];
- System.arraycopy(d, 0, newD, 0, d.length);
- return newD;
- }
-
- public static double[][] copy(double[][] d) {
- if (d == null) { return null; }
- double[][] newD = new double[d.length][];
- for (int i = 0; i < newD.length; i++) {
- newD[i] = copy(d[i]);
- }
- return newD;
- }
- public static double[][][] copy(double[][][] d) {
- if (d == null) { return null; }
- double[][][] newD = new double[d.length][][];
- for (int i = 0; i < newD.length; i++) {
- newD[i] = copy(d[i]);
- }
- return newD;
- }
- public static float[] copy(float[] d) {
- if (d == null) { return null; }
- float[] newD = new float[d.length];
- System.arraycopy(d, 0, newD, 0, d.length);
- return newD;
- }
-
- public static float[][] copy(float[][] d) {
- if (d == null) { return null; }
- float[][] newD = new float[d.length][];
- for (int i = 0; i < newD.length; i++) {
- newD[i] = copy(d[i]);
- }
- return newD;
- }
- public static float[][][] copy(float[][][] d) {
- if (d == null) { return null; }
- float[][][] newD = new float[d.length][][];
- for (int i = 0; i < newD.length; i++) {
- newD[i] = copy(d[i]);
- }
- return newD;
- }
-
- public static String toString(boolean[][] b) {
- String result = "[";
- for (int i = 0; i < b.length; i++) {
- result += Arrays.toString(b[i]);
- if(i < b.length-1)
- result += ",";
- }
- result += "]";
- return result;
- }
- public static long[] toPrimitive(Long[] in) {
- return toPrimitive(in,0L);
- }
- public static int[] toPrimitive(Integer[] in) {
- return toPrimitive(in,0);
- }
- public static short[] toPrimitive(Short[] in) {
- return toPrimitive(in,(short)0);
- }
- public static char[] toPrimitive(Character[] in) {
- return toPrimitive(in,(char)0);
- }
- public static double[] toPrimitive(Double[] in) {
- return toPrimitive(in,0.0);
- }
- public static long[] toPrimitive(Long[] in, long valueForNull) {
- if (in == null)
- return null;
- final long[] out = new long[in.length];
- for (int i = 0; i < in.length; i++) {
- Long b = in[i];
- out[i] = (b == null ? valueForNull : b);
- }
- return out;
- }
- public static int[] toPrimitive(Integer[] in, int valueForNull) {
- if (in == null)
- return null;
- final int[] out = new int[in.length];
- for (int i = 0; i < in.length; i++) {
- Integer b = in[i];
- out[i] = (b == null ? valueForNull : b);
- }
- return out;
- }
- public static short[] toPrimitive(Short[] in, short valueForNull) {
- if (in == null)
- return null;
- final short[] out = new short[in.length];
- for (int i = 0; i < in.length; i++) {
- Short b = in[i];
- out[i] = (b == null ? valueForNull : b);
- }
- return out;
- }
- public static char[] toPrimitive(Character[] in, char valueForNull) {
- if (in == null)
- return null;
- final char[] out = new char[in.length];
- for (int i = 0; i < in.length; i++) {
- Character b = in[i];
- out[i] = (b == null ? valueForNull : b);
- }
- return out;
- }
- public static double[] toPrimitive(Double[] in, double valueForNull) {
- if (in == null)
- return null;
- final double[] out = new double[in.length];
- for (int i = 0; i < in.length; i++) {
- Double b = in[i];
- out[i] = (b == null ? valueForNull : b);
- }
- return out;
- }
- }
- package example.serverscript.connector;
- public interface Interface extends java.rmi.Remote {
- public java.lang.String request(java.lang.String string_1, java.lang.String string_2)
- throws java.rmi.RemoteException;
- }
|