package freenet.support; /** * Sorted array of long's. */ public class SortedLongSet { private long[] data; private int length; private static final int MIN_SIZE = 32; /** * Default constructor */ public SortedLongSet() { this.data = new long[MIN_SIZE]; for(int i=0;itrue, if the set is empty. */ public synchronized boolean isEmpty() { return length == 0; } /** * Check if num exist in this set. * * @param num * @return true, if num exist. */ public synchronized boolean contains(long num) { int x = binarySearch(num); if(x >= 0) return true; else return false; } /** * Remove an item * * @param item * the item to be removed */ public synchronized void remove(long item) { int x = binarySearch(item); if(x >= 0) { if(x < length-1) System.arraycopy(data, x+1, data, x, length-x-1); data[--length] = Long.MAX_VALUE; } if((length*4 < data.length) && (length > MIN_SIZE)) { long[] newData = new long[Math.max(data.length/2, MIN_SIZE)]; System.arraycopy(data, 0, newData, 0, length); for(int i=length;i0) { if(item <= lastItem) throw new IllegalStateException("Verify failed!"); } lastItem = item; } for(int i=length;itrue, if we added the item. */ public synchronized boolean push(long num) { int x = binarySearch(num); if(x >= 0) return false; // insertion point x = -x-1; push(num, x); return true; } /** * Add the item. * * @throws {@link IllegalArgumentException} * if the item already exist * @return true, if we added the item. */ public synchronized void add(long num) { int x = binarySearch(num); if(x >= 0) throw new IllegalArgumentException(); // already exists // insertion point x = -x-1; push(num, x); } private synchronized void push(long num, int x) { boolean logMINOR = Logger.shouldLog(Logger.MINOR, this); if(logMINOR) Logger.minor(this, "Insertion point: "+x+" length "+length+" data.length "+data.length); // Move the data if(length == data.length) { if(logMINOR) Logger.minor(this, "Expanding from "+length+" to "+length*2); long[] newData = new long[length*2]; System.arraycopy(data, 0, newData, 0, data.length); for(int i=length;i