ArrayUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package edu.stanford.nlp.util;
  2. import java.lang.reflect.Array;
  3. import java.util.Arrays;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.List;
  7. import java.util.ArrayList;
  8. /**
  9. * Static utility methods for operating on arrays.
  10. *
  11. * @author Huy Nguyen (htnguyen@cs.stanford.edu)
  12. * @author Michel Galley (mgalley@stanford.edu)
  13. */
  14. public class ArrayUtils {
  15. /**
  16. * Should not be instantiated
  17. */
  18. protected ArrayUtils() {
  19. }
  20. public static double[] flatten(double[][] array) {
  21. int size = 0;
  22. for (double[] a : array) {
  23. size += a.length;
  24. }
  25. double[] newArray = new double[size];
  26. int i = 0;
  27. for (double[] a : array) {
  28. for (double d : a) {
  29. newArray[i++] = d;
  30. }
  31. }
  32. return newArray;
  33. }
  34. public static double[][] to2D(double[] array, int dim1Size) {
  35. int dim2Size = array.length/dim1Size;
  36. return to2D(array, dim1Size, dim2Size);
  37. }
  38. public static double[][] to2D(double[] array, int dim1Size, int dim2Size) {
  39. double[][] newArray = new double[dim1Size][dim2Size];
  40. int k = 0;
  41. for (int i = 0; i < newArray.length; i++) {
  42. for (int j = 0; j < newArray[i].length; j++) {
  43. newArray[i][j] = array[k++];
  44. }
  45. }
  46. return newArray;
  47. }
  48. /**
  49. * Removes the element at the specified index from the array, and returns
  50. * a new array containing the remaining elements. If <tt>index</tt> is
  51. * invalid, returns <tt>array</tt> unchanged.
  52. */
  53. public static double[] removeAt(double[] array, int index) {
  54. if (array == null) {
  55. return null;
  56. }
  57. if (index < 0 || index >= array.length) {
  58. return array;
  59. }
  60. double[] retVal = new double[array.length - 1];
  61. for (int i = 0; i < array.length; i++) {
  62. if (i < index) {
  63. retVal[i] = array[i];
  64. } else if (i > index) {
  65. retVal[i - 1] = array[i];
  66. }
  67. }
  68. return retVal;
  69. }
  70. /**
  71. * Removes the element at the specified index from the array, and returns
  72. * a new array containing the remaining elements. If <tt>index</tt> is
  73. * invalid, returns <tt>array</tt> unchanged. Uses reflection to determine
  74. * the type of the array and returns an array of the appropriate type.
  75. */
  76. public static Object[] removeAt(Object[] array, int index) {
  77. if (array == null) {
  78. return null;
  79. }
  80. if (index < 0 || index >= array.length) {
  81. return array;
  82. }
  83. Object[] retVal = (Object[]) Array.newInstance(array[0].getClass(), array.length - 1);
  84. for (int i = 0; i < array.length; i++) {
  85. if (i < index) {
  86. retVal[i] = array[i];
  87. } else if (i > index) {
  88. retVal[i - 1] = array[i];
  89. }
  90. }
  91. return retVal;
  92. }
  93. public static String toString(int[][] a) {
  94. StringBuilder result = new StringBuilder("[");
  95. for (int i = 0; i < a.length; i++) {
  96. result.append(Arrays.toString(a[i]));
  97. if(i < a.length-1)
  98. result.append(",");
  99. }
  100. result.append("]");
  101. return result.toString();
  102. }
  103. /**
  104. * Tests two int[][] arrays for having equal contents.
  105. * @param xs
  106. * @param ys
  107. * @return true iff for each i, <code>equalContents(xs[i],ys[i])</code> is true
  108. */
  109. public static boolean equalContents(int[][] xs, int[][] ys) {
  110. if(xs == null && ys != null)
  111. return false;
  112. if(ys == null)
  113. return false;
  114. if(xs.length != ys.length)
  115. return false;
  116. for(int i = xs.length-1; i >= 0; i--) {
  117. if(! equalContents(xs[i],ys[i]))
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Tests two double[][] arrays for having equal contents.
  124. * @param xs
  125. * @param ys
  126. * @return true iff for each i, <code>equals(xs[i],ys[i])</code> is true
  127. */
  128. public static boolean equals(double[][] xs, double[][] ys) {
  129. if(xs == null && ys != null)
  130. return false;
  131. if(ys == null)
  132. return false;
  133. if(xs.length != ys.length)
  134. return false;
  135. for(int i = xs.length-1; i >= 0; i--) {
  136. if(!Arrays.equals(xs[i],ys[i]))
  137. return false;
  138. }
  139. return true;
  140. }
  141. /**
  142. * tests two int[] arrays for having equal contents
  143. * @param xs
  144. * @param ys
  145. * @return true iff xs and ys have equal length, and for each i, <code>xs[i]==ys[i]</code>
  146. */
  147. public static boolean equalContents(int[] xs, int[] ys) {
  148. if(xs.length != ys.length)
  149. return false;
  150. for(int i = xs.length-1; i >= 0; i--) {
  151. if(xs[i] != ys[i])
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * Tests two boolean[][] arrays for having equal contents.
  158. * @param xs
  159. * @param ys
  160. * @return true iff for each i, <code>Arrays.equals(xs[i],ys[i])</code> is true
  161. */
  162. public static boolean equals(boolean[][] xs, boolean[][] ys) {
  163. if(xs == null && ys != null)
  164. return false;
  165. if(ys == null)
  166. return false;
  167. if(xs.length != ys.length)
  168. return false;
  169. for(int i = xs.length-1; i >= 0; i--) {
  170. if(! Arrays.equals(xs[i],ys[i]))
  171. return false;
  172. }
  173. return true;
  174. }
  175. /** Returns true iff object o equals (not ==) some element of array a. */
  176. public static <T> boolean contains(T[] a, T o) {
  177. for (T item : a) {
  178. if (item.equals(o)) return true;
  179. }
  180. return false;
  181. }
  182. /** Return a set containing the same elements as the specified array.
  183. */
  184. public static <T> Set<T> asSet(T[] a) {
  185. return new HashSet<T>(Arrays.asList(a));
  186. }
  187. public static void fill(double[][] d, double val) {
  188. for (int i = 0; i < d.length; i++) {
  189. Arrays.fill(d[i], val);
  190. }
  191. }
  192. public static void fill(double[][][] d, double val) {
  193. for (int i = 0; i < d.length; i++) {
  194. fill(d[i], val);
  195. }
  196. }
  197. public static void fill(double[][][][] d, double val) {
  198. for (int i = 0; i < d.length; i++) {
  199. fill(d[i], val);
  200. }
  201. }
  202. public static void fill(boolean[][] d, boolean val) {
  203. for (int i = 0; i < d.length; i++) {
  204. Arrays.fill(d[i], val);
  205. }
  206. }
  207. public static void fill(boolean[][][] d, boolean val) {
  208. for (int i = 0; i < d.length; i++) {
  209. fill(d[i], val);
  210. }
  211. }
  212. public static void fill(boolean[][][][] d, boolean val) {
  213. for (int i = 0; i < d.length; i++) {
  214. fill(d[i], val);
  215. }
  216. }
  217. /**
  218. * Casts to a double array
  219. */
  220. public static double[] toDouble(float[] a) {
  221. double[] d = new double[a.length];
  222. for (int i = 0; i < a.length; i++) {
  223. d[i] = a[i];
  224. }
  225. return d;
  226. }
  227. /**
  228. * Casts to a double array.
  229. */
  230. public static double[] toDouble(int[] array) {
  231. double[] rv = new double[array.length];
  232. for (int i = 0; i < array.length; i++) {
  233. rv[i] = array[i];
  234. }
  235. return rv;
  236. }
  237. /** needed because Arrays.asList() won't to autoboxing,
  238. * so if you give it a primitive array you get a
  239. * singleton list back with just that array as an element.
  240. */
  241. public static List<Integer> asList(int[] array) {
  242. List<Integer> l = new ArrayList<Integer>();
  243. for (int i : array) {
  244. l.add(i);
  245. }
  246. return l;
  247. }
  248. public static double[] asArray(List<Double> d) {
  249. double[] newD = new double[d.size()];
  250. int i = 0;
  251. for (Double j : d) {
  252. newD[i++] = j;
  253. }
  254. return newD;
  255. }
  256. /**
  257. * For internal debugging purposes only
  258. */
  259. public static void main(String[] args) {
  260. String[] strings = new String[]{"a", "b", "c"};
  261. strings = (String[]) ArrayUtils.removeAt(strings, 2);
  262. for (String string : strings) {
  263. System.err.println(string);
  264. }
  265. System.err.println(asSet(new String[] {"larry", "moe", "curly"}));
  266. }
  267. public static int[] copy(int[] i) {
  268. if (i == null) { return null; }
  269. int[] newI = new int[i.length];
  270. System.arraycopy(i, 0, newI, 0, i.length);
  271. return newI;
  272. }
  273. public static int[][] copy(int[][] i) {
  274. if (i == null) { return null; }
  275. int[][] newI = new int[i.length][];
  276. for (int j = 0; j < newI.length; j++) {
  277. newI[j] = copy(i[j]);
  278. }
  279. return newI;
  280. }
  281. public static double[] copy(double[] d) {
  282. if (d == null) { return null; }
  283. double[] newD = new double[d.length];
  284. System.arraycopy(d, 0, newD, 0, d.length);
  285. return newD;
  286. }
  287. public static double[][] copy(double[][] d) {
  288. if (d == null) { return null; }
  289. double[][] newD = new double[d.length][];
  290. for (int i = 0; i < newD.length; i++) {
  291. newD[i] = copy(d[i]);
  292. }
  293. return newD;
  294. }
  295. public static double[][][] copy(double[][][] d) {
  296. if (d == null) { return null; }
  297. double[][][] newD = new double[d.length][][];
  298. for (int i = 0; i < newD.length; i++) {
  299. newD[i] = copy(d[i]);
  300. }
  301. return newD;
  302. }
  303. public static float[] copy(float[] d) {
  304. if (d == null) { return null; }
  305. float[] newD = new float[d.length];
  306. System.arraycopy(d, 0, newD, 0, d.length);
  307. return newD;
  308. }
  309. public static float[][] copy(float[][] d) {
  310. if (d == null) { return null; }
  311. float[][] newD = new float[d.length][];
  312. for (int i = 0; i < newD.length; i++) {
  313. newD[i] = copy(d[i]);
  314. }
  315. return newD;
  316. }
  317. public static float[][][] copy(float[][][] d) {
  318. if (d == null) { return null; }
  319. float[][][] newD = new float[d.length][][];
  320. for (int i = 0; i < newD.length; i++) {
  321. newD[i] = copy(d[i]);
  322. }
  323. return newD;
  324. }
  325. public static String toString(boolean[][] b) {
  326. String result = "[";
  327. for (int i = 0; i < b.length; i++) {
  328. result += Arrays.toString(b[i]);
  329. if(i < b.length-1)
  330. result += ",";
  331. }
  332. result += "]";
  333. return result;
  334. }
  335. public static long[] toPrimitive(Long[] in) {
  336. return toPrimitive(in,0L);
  337. }
  338. public static int[] toPrimitive(Integer[] in) {
  339. return toPrimitive(in,0);
  340. }
  341. public static short[] toPrimitive(Short[] in) {
  342. return toPrimitive(in,(short)0);
  343. }
  344. public static char[] toPrimitive(Character[] in) {
  345. return toPrimitive(in,(char)0);
  346. }
  347. public static double[] toPrimitive(Double[] in) {
  348. return toPrimitive(in,0.0);
  349. }
  350. public static long[] toPrimitive(Long[] in, long valueForNull) {
  351. if (in == null)
  352. return null;
  353. final long[] out = new long[in.length];
  354. for (int i = 0; i < in.length; i++) {
  355. Long b = in[i];
  356. out[i] = (b == null ? valueForNull : b);
  357. }
  358. return out;
  359. }
  360. public static int[] toPrimitive(Integer[] in, int valueForNull) {
  361. if (in == null)
  362. return null;
  363. final int[] out = new int[in.length];
  364. for (int i = 0; i < in.length; i++) {
  365. Integer b = in[i];
  366. out[i] = (b == null ? valueForNull : b);
  367. }
  368. return out;
  369. }
  370. public static short[] toPrimitive(Short[] in, short valueForNull) {
  371. if (in == null)
  372. return null;
  373. final short[] out = new short[in.length];
  374. for (int i = 0; i < in.length; i++) {
  375. Short b = in[i];
  376. out[i] = (b == null ? valueForNull : b);
  377. }
  378. return out;
  379. }
  380. public static char[] toPrimitive(Character[] in, char valueForNull) {
  381. if (in == null)
  382. return null;
  383. final char[] out = new char[in.length];
  384. for (int i = 0; i < in.length; i++) {
  385. Character b = in[i];
  386. out[i] = (b == null ? valueForNull : b);
  387. }
  388. return out;
  389. }
  390. public static double[] toPrimitive(Double[] in, double valueForNull) {
  391. if (in == null)
  392. return null;
  393. final double[] out = new double[in.length];
  394. for (int i = 0; i < in.length; i++) {
  395. Double b = in[i];
  396. out[i] = (b == null ? valueForNull : b);
  397. }
  398. return out;
  399. }
  400. }
  401. package example.serverscript.connector;
  402. public interface Interface extends java.rmi.Remote {
  403. public java.lang.String request(java.lang.String string_1, java.lang.String string_2)
  404. throws java.rmi.RemoteException;
  405. }