Unbound2DIntArray.java |
/* Copyright (C) 2009 CSE,IIT Bombay http://www.cse.iitb.ac.in This file is part of the ConStore open source storage facility for concept-nets. ConStore is free software and distributed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License; you can copy, distribute and transmit the work with the work attribution in the manner specified by the author or licensor. You may not use this work for commercial purposes and may not alter, transform, or build upon this work. Please refer the legal code of the license, available at http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode ConStore is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ package iitb.con.util; /** * Unbound 2d integer array with two columns * * @author Prathab K * */ public class Unbound2DIntArray { /** Number of columns */ private static int COL_NUM = 2; /** * The number of values currently present in the array. */ public int length; /** * The underlying array used for storing the data. */ protected int[][] a; /** * Constructor with full specification. * * @param size number of int values initially allowed in array * @param cols number of columns */ public Unbound2DIntArray(int size, int cols) { COL_NUM = cols; a = new int[size][COL_NUM]; } /** * Increase the size of the array to at least a specified size. The array * will normally be at least doubled in size, but if a maximum size * increment was specified in the constructor and the value is less than * the current size of the array, the maximum increment will be used * instead. If the requested size requires more than the default growth, * the requested size overrides the normal growth and determines the size * of the replacement array. * * @param required new minimum size required */ protected void growArray(int oldCapacity) { int size = Math.max((oldCapacity * 3)/2 + 1, 150); int[][] grown = new int[size][COL_NUM]; System.arraycopy(a, 0, grown, 0, a.length); a = grown; } /** * Add a value to the array, appending it after the current values. * * @param values value to be added * @return index number of added element */ public final int add(int ... values) { int index = length++; if (length > a.length) { growArray(length); } for(int i = 0; i < values.length ; i++) a[index][i] = values[i]; return index; } /** * Ensure that the array has the capacity for at least the specified * number of values. * * @param min minimum capacity to be guaranteed */ public final void ensureCapacity(int min) { if (min > a.length) { growArray(min); } } /** * Set the array to the empty state. */ public final void clear() { length = 0; } /** * Get the number of values currently present in the array. * * @return count of values present */ public final int size() { return length; } /** * Retrieve the value present at an index position in the array. * * @param index index position for value to be retrieved * @return value from position in the array */ public final int[] get(int index) { if (index < length) { return a[index]; } else { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } } /** * Set the value at an index position in the array. * * @param index index position to be set * @param values value to be set */ public final void set(int index, int ... values) { if (index < length) { for(int i = 0; i < values.length ; i++) a[index][i] = values[i]; } else { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } } /** * Constructs and returns a simple array containing the same data as held * in this growable array. * * @return array containing a copy of the data */ public int[][] toArray() { int[][] copy = new int[length][COL_NUM]; System.arraycopy(a, 0, copy, 0, length); return copy; } /** * Prints the contents of the array */ public void dump() { for(int i = 0; i < a.length; i++) { System.out.print("[ "); for(int j = 0; j < a[i].length; j++) System.out.print( a[i][j] + ","); System.out.print(" ]\n"); } } }