Update of /cvsroot/pyxida/Util-PRP/src/edu/harvard/syrah/prp In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17572/src/edu/harvard/syrah/prp Added Files: PrimitiveArray.java PUtil.java Stat2.java LongArray.java Triple.java Debug.java SortedList.java BitArray.java Log.java IntIntArray.java LogFormatter.java ByteArray.java UnknownPropertyException.java ByteArrayClassLoader.java Stat.java PTimer.java ANSI.java NetUtil.java IntArray.java POut.java PError.java RateCalc.java Pair.java TypedMap.java Log Message: Initial checkin --- NEW FILE: PUtil.java --- /* * Created on Apr 28, 2004 */ package edu.harvard.syrah.prp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.InvalidMarkException; import java.util.*; /** * Useful static utility methods. * * @author prp */ public abstract class PUtil { static Log log = new Log(PUtil.class); private static final long GARBAGE_COLLECTION_DELAY = 1 * 1000; private static Random random = new Random(); public static void sleep(long time) { if (time == 0) return; try { Thread.sleep(time); } catch (InterruptedException e2) { /* ignore */ } } public static long getRandomLong() { return random.nextLong(); } public static int getRandomInt() { return random.nextInt(); } public static long getRandomPosLong() { return Math.abs(random.nextLong()); } public static int getRandomPosInt(int maxInt) { return random.nextInt(maxInt); } public static int getRandomPosInt() { return Math.abs(random.nextInt()); } public static String getRandomString(int minLength, int maxLength) { int start = 'a'; int end = 'z'; StringBuffer sb = new StringBuffer(); for (int i = 0; i < random.nextInt(maxLength - minLength) + minLength; i++) { sb.append((char) (random.nextInt(end - start) + start)); } return sb.toString(); } public static <T> T getRandomObject(Collection<T> collection) { // Is this set empty? if (collection.isEmpty()) return null; Iterator<T> objIt = collection.iterator(); int index = getRandomPosInt(collection.size()) + 1; T obj = null; for (int i = 0; i < index; i++) { obj = objIt.next(); } return obj; } public static Random getRandom() { return random; } public static double getMax(double[] array) { double max = 0; for (int i=0; i < array.length; i++) { if (array[i] > max) max = array[i]; } return max; } public static int getMax(int[] array) { int max = 0; for (int i=0; i < array.length; i++) { if (array[i] > max) max = array[i]; } return max; } public static double getLength(double[] vector) { double length = 0; for (int i = 0; i < vector.length; i++) length += vector[i] * vector[i]; length = Math.sqrt(length); return length; } public static void block() { while (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(100000); } catch (InterruptedException e) { /* ignore */ } } } public static void gc() { System.gc(); System.runFinalization(); System.gc(); try { Thread.sleep(GARBAGE_COLLECTION_DELAY); } catch (InterruptedException e) { POut.abort("Util.gc(): Garbage Collection interrupted."); } } public static void delay(double seconds) { Object dummy = new Object(); synchronized (dummy) { try { dummy.wait(Math.round(seconds * 1000)); } catch (InterruptedException e) { /* ignore */ } } } public static InputStream createInputStream(final ByteBuffer buffer) { return new InputStream() { public synchronized int read() throws IOException { if (!buffer.hasRemaining()) return -1; return buffer.get(); } public synchronized int read(byte[] bytes, int off, int len) throws IOException { if (!buffer.hasRemaining()) return -1; len = Math.min(len, buffer.remaining()); buffer.get(bytes, off, len); return len; } }; } public static byte[] convertInputStreamToByteArray(InputStream is) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1000]; int read = -1; try { while ((read = is.read(buf, 0, buf.length)) != -1) { baos.write(buf, 0, read); } } catch (IOException e) { log.error("Error converting byte array from stream: " + e); } return baos.toByteArray(); } /** * This methods extends a buffer to a new size by copying it. * * @param oldBuffer * @param newSize * @return */ public static ByteBuffer extendByteBuffer(ByteBuffer oldBuffer, int newSize) { int currentPosition = oldBuffer.position(); int limitPosition = oldBuffer.limit(); int markPosition = 0; try { oldBuffer.reset(); markPosition = oldBuffer.position(); } catch (InvalidMarkException e) { // No mark has been set yet } oldBuffer.position(0); ByteBuffer newBuffer = ByteBuffer.allocate(newSize); newBuffer.put(oldBuffer); newBuffer.position(markPosition); newBuffer.mark(); newBuffer.position(currentPosition); newBuffer.limit(limitPosition); return newBuffer; } public static String getDiffTimeStr(long time1, long time2) { long diff = (time2 - time1) / 1000; int days = (int) (diff / (3600 * 24)); diff -= days * 3600 * 24; int hours = (int) (diff / 3600); diff -= (hours * 3600); int mins = (int) (diff / 60); diff -= mins * 60; int secs = (int) diff; StringBuffer sb = new StringBuffer(); if (days > 1) sb.append(days + " days "); if (days == 1) sb.append(days + " day "); if (hours > 1) sb.append(hours + " hours "); if (hours == 1) sb.append(hours + " hour "); if (mins > 1) sb.append(mins + " mins "); if (mins == 1) sb.append(mins + " min "); if (secs > 1) sb.append(secs + " secs "); if (secs == 1) sb.append(secs + " sec "); if (sb.length() > 0) sb.deleteCharAt(sb.length() - 1); return sb.toString(); } public static final void main(String[] args) { Set<String> set = new HashSet<String>(); set.add("One"); set.add("Two"); set.add("Three"); set.add("Four"); set.add("Five"); for (int i = 0; i < 10; i++) { POut.p((String) getRandomObject(set)); } } } --- NEW FILE: BitArray.java --- /* * Title: ByteArray * Description: Implements a variable size byte array.<p> * Copyright: Copyright (c) 2001 <p> * Company: Computer Laboratory, Cambridge University <p> * @author Peter Pietzuch <pr...@cl...> * @version 1.0 * * (C) Copyright 2001-2003 Peter R Pietzuch * * This file is part of Util. * * Util is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Util 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. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with DSSim; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package edu.harvard.syrah.prp; public class BitArray extends PrimitiveArray { private boolean[] a; public BitArray() { super(); } public BitArray (int initialSize, int growStep) { super(initialSize, growStep); } public BitArray(int initialSize) { super(initialSize); } protected void init(int initialSize) { a = new boolean[initialSize]; } /** * Adds a new byte value to the array. The array is extended if necessary by doubling its size. * * @param byte Byte to be added. */ public void add(boolean b) { if ((len + 1) > a.length) { if (growthStep == 0) multiplySize(2); else resize(a.length + growthStep); } a[len++] = b; } /** * Adds an byte array to the variable size array. All the values are copied. * * @param b Byte array to be copied. * @param size Size of b to be copied. */ public void addAll(boolean[] b, int size) { if (size == 0) return; if ((len + size) > a.length) { if (growthStep == 0) multiplySize(((len + size) / a.length) + 1); else { int factor = ((len + size - a.length) / growthStep) + 1; resize ((factor * growthStep) + a.length); } } for (int i = 0; i < size; i++) a[len++] = b[i]; } /** * Returns the byte value at a particular index. * * @param index Index * @return Byte value */ public boolean get(int index) { if (index >= len) throw new IndexOutOfBoundsException(); return a[index]; } /** * Sets a value in the array to a new value. The index must exist. * * @param value New value * @param index Index */ public void set(boolean value, int index) { if (index >= len) throw new IndexOutOfBoundsException(); a[index] = value; } /** * Returns a new byte array which has the optimal size. This is done by copying. * * @return Byte Array */ public boolean[] getArrayCopy() { boolean[] retA = new boolean[len]; for (int i = 0; i < len; i++) retA[i] = a[i]; return retA; } public boolean[] getArray() { return a; } protected void resize(int newSize) { boolean[] newA = new boolean[newSize]; for (int i = 0; i < a.length; i++) newA[i] = a[i]; a = newA; } public int capacity() { return a.length; } public static void main(String[] args) { Debug.p("hello"); BitArray a1 = new BitArray(100, 100); boolean[] ba = {true, false, true, true, false}; for (int i = 0; i < 1000; i++) { a1.addAll(ba, ba.length); } System.out.println("a1.size() = " + a1.size()); //for (int i = 0; i < a1.size(); i++) //System.out.print(a1.elementAt(i) + " "); System.out.println(); } } --- NEW FILE: PTimer.java --- /* * @author Last modified by $Author: prp $ * @version $Revision: 1.1 $ on $Date: 2007/11/19 17:50:10 $ * @since Feb 2, 2005 */ package edu.harvard.syrah.prp; import edu.harvard.syrah.prp.ANSI.Color; /** * */ public class PTimer { private static long hardLimit = Long.MAX_VALUE; private long startTime; private boolean isStarted = false; public PTimer() { this(true); } public PTimer(boolean start) { if (start) { startTime = System.nanoTime(); isStarted = true; } } public static void setHardLimit(long newHardLimit) { if (newHardLimit > 0) { hardLimit = newHardLimit; } } public void start() { startTime = System.nanoTime(); isStarted = true; } public boolean isStarted() { return isStarted; } public double stop() { isStarted = false; return (System.nanoTime() - startTime) / 1000000d; } public void setBack(double value) { startTime += (value * 1000000d); } public double lap() { long currentTime = System.nanoTime(); double runningTime = (currentTime - startTime) /1000000d; startTime = currentTime; return runningTime; } public void lap(Log log, String what) { double runningTime = (System.nanoTime() - startTime) / 1000000d; String output = "Lap : " + what + " " + ANSI.color(Color.GREEN, POut.toString(runningTime)) + " ms"; log.main(output); if (hardLimit < runningTime) { log.info(ANSI.color(Color.LIGHTRED, "Timer hit hard limit of " + hardLimit + " ms: " + output)); } } public void stop(Log log, String what) { isStarted = false; double runningTime = (System.nanoTime() - startTime) / 1000000d; String output = "Stop: " + what + " " + ANSI.color(Color.GREEN, POut.toString(runningTime)) + " ms"; log.main(output); if (hardLimit < runningTime) { log.info(ANSI.color(Color.LIGHTRED, "Timer hit hard limit of " + hardLimit + " ms: " + output)); } } public double getTime() { return (System.nanoTime() - startTime) / 1000000d; } public String toString() { return POut.toString(getTime()) + " ms"; } } --- NEW FILE: ByteArrayClassLoader.java --- /* * SBON * * @author Last modified by $Author: prp $ * @version $Revision: 1.1 $ on $Date: 2007/11/19 17:50:10 $ * @since Jan 17, 2005 */ package edu.harvard.syrah.prp; import java.util.HashMap; import java.util.Map; import edu.harvard.syrah.prp.Log; /** * * This is an implementation of a Java class loader that loads a class from a byte array. * */ public class ByteArrayClassLoader extends ClassLoader { private static final Log log = new Log(ByteArrayClassLoader.class); private Map<String, byte[]> classByteCodeTable = new HashMap<String, byte[]>(); public void addClassByteCode(String className, byte[] byteCode) { classByteCodeTable.put(className, byteCode); } public synchronized Class< ? > loadClass(String name, boolean resolve) throws ClassNotFoundException { log.debug("Trying to load the className=" + name + " dynamically."); Class<?> newClass = findLoadedClass(name); if (newClass != null) return newClass; SecurityManager sm = System.getSecurityManager(); if (sm != null) { int i = name.lastIndexOf('.'); if (i >= 0) sm.checkPackageAccess(name.substring(0, i)); } if (sm != null) { int i = name.lastIndexOf('.'); if (i >= 0) sm.checkPackageDefinition(name.substring(0, i)); } try { //if (parent != null) // newClass = parent.loadClass(name, resolve); //else // Call this method in both 1.1 and 1.2 newClass = findSystemClass(name); if (newClass != null) return newClass; } catch (ClassNotFoundException e) { // Not a system class, simply continue } log.debug("Considering byte code cache."); byte[] byteCode = classByteCodeTable.get(name); //log.debug("byteCode=" + Util.toString(byteCode)); newClass = defineClass(null, byteCode, 0, byteCode.length); log.debug("newClass=" + newClass); if (resolve) resolveClass(newClass); return newClass; } } --- NEW FILE: SortedList.java --- /* * @author Last modified by $Author: prp $ * @version $Revision: 1.1 $ on $Date: 2007/11/19 17:50:10 $ * @since Sep 8, 2005 */ package edu.harvard.syrah.prp; import java.io.Serializable; import java.util.*; public class SortedList<T> implements List<T>, Serializable { private static final Log log = new Log(SortedList.class); static final long serialVersionUID = 1000000000L; private int maxSize; private Comparator<? super T> comp; private List<T> list = new LinkedList<T>(); public SortedList(Comparator<? super T> comp) { this(0, comp); } public SortedList(int maxSize, Comparator<? super T> comp) { this.maxSize = maxSize; this.comp = comp; } public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } public boolean contains(Object o) { return list.contains(o); } public Iterator<T> iterator() { return list.iterator(); } public Object[] toArray() { return list.toArray(); } public <U> U[] toArray(U[] a) { return list.toArray(a); } public boolean add(T o) { list.add(o); Collections.sort(list, comp); if (maxSize != 0 && list.size() > maxSize) { list.remove(list.size() - 1); } return true; } public boolean remove(Object o) { return list.remove(o); } public boolean containsAll(Collection< ? > c) { return list.containsAll(c); } public boolean addAll(Collection< ? extends T> c) { return list.addAll(c); } public boolean addAll(int index, Collection< ? extends T> c) { return list.addAll(index, c); } public boolean removeAll(Collection< ? > c) { return list.removeAll(c); } public boolean retainAll(Collection< ? > c) { return list.retainAll(c); } public void clear() { list.clear(); } public T get(int index) { return list.get(index); } public T set(int index, T element) { return list.set(index, element); } public void add(int index, T element) { list.add(index, element); } public T remove(int index) { return list.remove(index); } public int indexOf(Object o) { return list.indexOf(o); } public int lastIndexOf(Object o) { return list.lastIndexOf(o); } public ListIterator<T> listIterator() { return list.listIterator(); } public ListIterator<T> listIterator(int index) { return listIterator(index); } public List<T> subList(int fromIndex, int toIndex) { return list.subList(fromIndex, toIndex); } } --- NEW FILE: POut.java --- /* * @author Last modified by $Author: prp $ * @version $Revision: 1.1 $ on $Date: 2007/11/19 17:50:10 $ * @since Aug 16, 2005 */ package edu.harvard.syrah.prp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.text.NumberFormat; import java.util.Collection; import java.util.Iterator; import java.util.Map; public class POut { private static final Log log = new Log(POut.class); public static final String NL = System.getProperty("line.separator"); private static final int VSPACE_LINES = 3; private static boolean debugState = true; private static NumberFormat fpFormat = NumberFormat.getNumberInstance(); private static NumberFormat intFormat = NumberFormat.getIntegerInstance(); static { fpFormat.setMaximumFractionDigits(2); fpFormat.setMinimumFractionDigits(2); fpFormat.setGroupingUsed(false); intFormat.setGroupingUsed(false); intFormat.setMinimumFractionDigits(0); intFormat.setMaximumFractionDigits(0); } public static String toString(double[] vector, String sep) { StringBuffer sb = new StringBuffer(); sb.append("("); if (vector != null) { for (int i = 0; i < vector.length; i++) { sb.append(fpFormat.format(vector[i])); if (i < (vector.length - 1)) sb.append(sep); } } else sb.append("null"); sb.append(")"); return sb.toString(); } public static String toString(long[] vector, String sep) { StringBuffer sb = new StringBuffer(); sb.append("("); if (vector != null) { for (int i = 0; i < vector.length; i++) { sb.append(intFormat.format(vector[i])); if (i < (vector.length - 1)) sb.append(sep); } } else sb.append("null"); sb.append(")"); return sb.toString(); } public static String toString(Object[] vector) { return toString(vector, " "); } public static String toString(Object[] vector, String sep) { StringBuffer sb = new StringBuffer(); if (vector != null) { for (int i = 0; i < vector.length; i++) { sb.append(vector[i]); if (i < (vector.length - 1)) sb.append(sep); } } return sb.toString(); } public static String toString(double[] vector) { return toString(vector, " "); } public static String toString(long[] vector) { return toString(vector, " "); } public static String toString(byte[] vector, String sep) { StringBuffer sb = new StringBuffer(); sb.append("("); if (vector != null) { for (int i = 0; i < vector.length; i++) { sb.append(vector[i]); if (i < (vector.length - 1)) sb.append(sep); } } sb.append(")"); return sb.toString(); } public static String toString(byte[] vector) { return toString(vector, " "); } public static String toString(int[] vector, String sep) { StringBuffer sb = new StringBuffer(); sb.append("("); if (vector != null) { for (int i = 0; i < vector.length; i++) { sb.append(vector[i]); if (i < (vector.length - 1)) sb.append(sep); } } sb.append(")"); return sb.toString(); } public static String toString(double[][] matrix) { StringBuffer str = new StringBuffer(); str.append("("); if (matrix != null) { for (int i = 0; i < matrix.length; i++) { str.append(toString(matrix[i], ",")); if (i != (matrix.length - 1)) str.append(" "); } } else { str.append("null"); } str.append(")"); return str.toString(); } public static String toString(int[] vector) { return toString(vector, " "); } public static String toString(int[][] matrix, int space) { StringBuffer str = new StringBuffer(); str.append("\n"); if (matrix.length == 0) { str.append("empty"); } else { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { for (int k = (new Integer(matrix[i][j])).toString().length(); k < space; k++) str.append(" "); str.append(matrix[i][j] + " "); } if (i != matrix.length - 1) str.append("\n"); } } //str.append("\n"); return str.toString(); } public static String toString(int[][] matrix) { return toString(matrix, 2); } public static String toString(Object[][] matrix, int space) { StringBuffer str = new StringBuffer(); str.append("\n\n"); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (matrix[i][j] == null) { for (int k = 0; k < space; k++) str.append ("-"); str.append(" "); } else { for (int k = matrix[i][j].toString().length(); k < space; k++) str.append(" "); str.append(matrix[i][j] + " "); } } str.append("\n"); } str.append("\n"); return str.toString(); } @SuppressWarnings("unchecked") public static String toString(Iterator it) { return toString(it, ","); } @SuppressWarnings("unchecked") public static String toString(Iterator it, String sep) { StringBuffer strBuffer = new StringBuffer(); while (it.hasNext()) { strBuffer.append(it.next()); if (it.hasNext()) strBuffer.append(sep); } return strBuffer.toString(); } public static String toString(Object[][] matrix) { return toString(matrix, 2); } @SuppressWarnings("unchecked") public static String toString(Map m) { StringBuffer strBuffer = new StringBuffer(); strBuffer.append("{"); for (Iterator it = m.keySet().iterator(); it.hasNext();) { Object key = it.next(); Object value = m.get(key); strBuffer.append("("); strBuffer.append(key != null ? key.toString() : "null"); strBuffer.append(","); strBuffer.append(value != null ? value.toString() : "null"); strBuffer.append(")"); if (it.hasNext()) strBuffer.append(", "); } strBuffer.append("}"); return strBuffer.toString(); } @SuppressWarnings("unchecked") public static String toString(Collection c) { return toString(c, ","); } @SuppressWarnings("unchecked") public static String toString(Collection c, String sep) { if (c != null) return toString(c.iterator(), sep); else return "null"; } public static String toString(double d) { return fpFormat.format(d); } public static String toString(int i, int digits) { return prependFixedLength(String.valueOf(i), digits); } public static void p() { System.out.println(); } public static void p(String what) { System.out.println(what); } public static void pStart(String what) { POut.pStart(what, false); } public static void pStart(String what, boolean newline) { if (newline) System.out.println("[" + what + "... "); else System.out.print("[" + what + "... "); } public static void pDone() { System.out.println("Done.]\n"); } public static void waitForReturn(String descr) { System.out.println("Press Return " + descr + "..."); try { (new BufferedReader (new InputStreamReader (System.in))).readLine(); } catch (IOException e) { /* ignore */ } } public static void waitForReturn() { waitForReturn("to continue"); } /** * Pads a string with whitspace or truncates it before adding it to a StringBuffer * @param sb * @param appendString * @param desiredLength */ public static void appendFixedLength(StringBuffer sb, String appendString, int desiredLength) { int currentLength = appendString.length(); if (currentLength < desiredLength) { sb.append(appendString); for (int i = 0; i < (desiredLength - currentLength); i++) sb.append(" "); } else { sb.append(appendString.substring(0, desiredLength)); } } public static String appendFixedLength(String what, int desiredLength) { StringBuffer padding = new StringBuffer(); for (int i = 0; i < desiredLength - what.length(); i++) { padding.append(" "); } return what + padding.toString(); } public static String prependFixedLength(String what, int desiredLength) { StringBuffer padding = new StringBuffer(); for (int i = 0; i < desiredLength - what.length(); i++) { padding.append(" "); } return padding.toString() + what; } public static String prependFL(String what, int desiredLength) { return prependFixedLength(what, desiredLength); } public static String prependFL(int i, int desiredLength) { return prependFixedLength(String.valueOf(i), desiredLength); } public static String print(ByteBuffer byteBuffer) { byteBuffer.rewind(); StringBuffer sB = new StringBuffer(); for (int i = 0; i < byteBuffer.limit(); i++) { sB.append(byteBuffer.get()); sB.append(" "); } byteBuffer.rewind(); return "{" + sB.toString() + "}"; } public static String printChar(ByteBuffer byteBuffer) { int pos = byteBuffer.position(); byteBuffer.rewind(); StringBuffer sB = new StringBuffer(); while (byteBuffer.remaining() > 0) { sB.append((char) byteBuffer.get()); } byteBuffer.position(pos); return "{" + sB.toString() + "}"; } public static void vspace() { for (int i = 0; i < VSPACE_LINES; i++) System.out.println(); } public static String ws(int length) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { sb.append(" "); } return sb.toString(); } public static void appendWS(StringBuffer sb, int length) { for (int i = 0; i < length; i++) { sb.append(" "); } } public static void debug(String msg) { if (debugState) PUtil.log.debug(msg); //System.out.println("Debug: " + msg); } // TODO should use log.config to do this sort of thing. public static void setDebugState(boolean state) { debugState = state; } public static void abort() { POut.abort("Program abort."); } public static void abort(String msg) { //System.out.println("Abort: " + msg); PUtil.log.error(msg); Thread.dumpStack(); System.exit(-1); } public static void main(String[] args) { String split[] = "imperial, university".split("\\,\\s"); System.out.println(split[0] + "/" + split[1] + "/"); } } --- NEW FILE: PrimitiveArray.java --- /* * @author Last modified by $Author: prp $ * @version $Revision: 1.1 $ on $Date: 2007/11/19 17:50:10 $ * @since Jul 25, 2006 */ package edu.harvard.syrah.prp; public abstract class PrimitiveArray { protected int len = 0; protected int growthStep = 0; /** * The default initial size of the array. */ protected static final int DEFAULT_INITIAL_SIZE = 128; /** * Creates a new array. */ protected PrimitiveArray() { this(DEFAULT_INITIAL_SIZE); } /** * If this constructor is used, the array starts with an initial size and grows by stepSize. * * @param growthStep Step size */ protected PrimitiveArray(int initialSize, int growthStep) { this(initialSize); this.growthStep = growthStep; } /** * Creates a new array. * * @param initialSize The initial size of the array. */ protected PrimitiveArray(int initialSize) { init(initialSize); } protected abstract void init(int initialSize); /** * Returns the number of bytes stored in the array. * * @return Size of the array data. */ public int size() { return len; } /** * Sets the steps that are used for growing the array. * * @param growStep Step size. */ public void setGrowStep(int growStep) { this.growthStep = growStep; } public boolean isEmpty() { return len == 0; } public abstract int capacity(); /** * Makes the array at least this big. * * @param minCapacity Minimal capicity required. */ public void ensureCapacity(int minCapacity) { if (capacity() < minCapacity) resize(minCapacity); } public void fill(int length) { ensureCapacity(length); this.len = length; } protected void multiplySize(int factor) { resize(capacity() == 0 ? factor : factor * capacity()); } protected abstract void resize(int minCapacity); } --- NEW FILE: IntArray.java --- /* * Title: ByteArray * Description: Implements a variable size byte array.<p> * Copyright: Copyright (c) 2001 <p> * Company: Computer Laboratory, Cambridge University <p> * @author Peter Pietzuch <pr...@cl...> * @version 1.0 * * (C) Copyright 2001-2003 Peter R Pietzuch * * This file is part of Util. * * Util is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Util 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. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with DSSim; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package edu.harvard.syrah.prp; public class IntArray extends PrimitiveArray{ private int[] a; public IntArray() { super(); } public IntArray (int initialSize, int growStep) { super(initialSize, growStep); } public IntArray(int initialSize) { super(initialSize); } protected void init(int initialSize) { a = new int[initialSize]; } /** * Adds a new byte value to the array. The array is extended if necessary by doubling its size. * * @param byte Byte to be added. */ public void add(int b) { if ((len + 1) > a.length) { if (growthStep == 0) multiplySize(2); else resize(a.length + growthStep); } a[len++] = b; } /** * Adds an byte array to the variable size array. All the values are copied. * * @param b Byte array to be copied. * @param size Size... [truncated message content] |