import java.util.Enumeration; import java.util.Vector; public class Table { static private int cnt = 0; private String tableName; private String[] columnNames; private int[][] rows; public Table(String tableName, String[] columnNames, int[][] rows) { if (columnNames.length != rows[0].length) { throw new IllegalArgumentException( "number of column names != # of columns"); } this.tableName = tableName; this.columnNames = columnNames; this.rows = rows; } public int findColumn(String colName) throws Exception { for (int i = 0; i < columnNames.length; i++) { if (columnNames[i].equals(colName)) { return i + 1; } } throw new Exception("column " + colName + " not found in table " + tableName); } public int[] absolute(int rowNum) throws Exception { if (rowNum > rows.length) { throw new Exception("row requested, " + rowNum + " > " + rows.length); } return rows[rowNum - 1]; } public Table project(String[] colNames) throws Exception { int[] colIdxs = getColIdxs(colNames); int[][] newRows = new int[rows.length][colNames.length]; projectInto(newRows, colIdxs); return new Table(tableName + cnt++, colNames, newRows); } private int[] getColIdxs(String[] colNames) throws Exception { int[] idxs = new int[colNames.length]; for (int i = 0; i < colNames.length; i++) { idxs[i] = findColumn(colNames[i]) - 1; } return idxs; } private void projectInto(int[][] newRows, int[] colIdxs) { for (int rowIdx = 0; rowIdx < rows.length; rowIdx++ ) { for (int curCol = 0; curCol < colIdxs.length; curCol++) { newRows[rowIdx][curCol] = rows[rowIdx][colIdxs[curCol]]; } } } public Table select(String colName, Op op, int value) throws Exception { Vector newRows = new Vector(); int colIdx = findColumn(colName) - 1; for (int i = 0; i < rows.length; i++) { if (op.compareTo(rows[i][colIdx], value)) { newRows.add(rows[i].clone()); } } return new Table(tableName + cnt++, columnNames, vectorToPrimArray(newRows)); } private int[][] vectorToPrimArray(Vector vec) { int[][] newRows = new int[vec.size()][]; int i = 0; for (Enumeration e = vec.elements(); e.hasMoreElements(); i++) { newRows[i] = ((int[]) e.nextElement()); } return newRows; } public Table join(String selfColName, Table table, String otherColName) throws Exception { String[] newColNames = makeJoinColumnNames(table.columnNames); int thisIdx = findColumn(selfColName) - 1; int otherIdx = table.findColumn(otherColName) - 1; Vector newRows = new Vector(); for (int thisRowIdx = 0; thisRowIdx < rows.length; thisRowIdx++) { for (int otherRowIdx = 0; otherRowIdx < table.rows.length; otherRowIdx++) { int[] thisRow = rows[thisRowIdx]; int[] otherRow = table.rows[otherRowIdx]; if (thisRow[thisIdx] == otherRow[otherIdx]) { newRows.add(joinRows(thisRow, otherRow)); } } } return new Table(tableName + "&" + table.tableName, newColNames, vectorToPrimArray(newRows)); } private String[] makeJoinColumnNames(String[] otherColNames) { String[] newColNames = new String[columnNames.length + otherColNames.length]; System.arraycopy(columnNames, 0, newColNames, 0, columnNames.length); System.arraycopy(otherColNames, 0, newColNames, columnNames.length, otherColNames.length); return newColNames; } private static int[] joinRows(int[] r1, int[] r2) { int[] newRow = new int[r1.length + r2.length]; arrayCopy(r1, 0, newRow, 0, r1.length); arrayCopy(r2, 0, newRow, r1.length, r2.length); return newRow; } private static void arrayCopy(int[] src, int srcPos, int[] dest, int destPos, int length) { for (int srcIdx = srcPos, destIdx = destPos, cnt = 0; cnt < length; cnt++) { dest[destIdx++] = src[srcIdx++]; } } }