fix ThreadPool bug
[org.ibex.util.git] / src / org / ibex / util / Basket.java
index f3b2504..d4348ce 100644 (file)
@@ -26,9 +26,8 @@ public interface Basket extends Serializable {
     public interface RandomAccess extends List { }
 
     public interface Queue extends Basket {
-        // FIXME
-        //public void   enqueue(Object o);
-        //public Object dequeue();
+        public void   enqueue(Object o);
+        public Object dequeue();
     }
 
     public interface Stack extends Basket {
@@ -60,6 +59,16 @@ public interface Basket extends Serializable {
         public Array(int initialCapacity) { o = new Object[initialCapacity]; }
         public Array(Object entry) { this(1); add(entry); }
 
+        public void   enqueue(Object o) { add(o); }
+
+        // FEATURE: make this more efficient with general wraparound
+        public Object dequeue() {
+            if (size==0) return null;
+            Object ret = o[0];
+            for(int i=1; i<size; i++) o[i-1]=o[i];
+            return ret;
+        }
+
         public void add(Object obj) { add(size, obj); }
         public void add(int i, Object obj) {
             size(size + 1);
@@ -67,9 +76,11 @@ public interface Basket extends Serializable {
             o[i] = obj; size++;
         }
         public Object set(int i, Object obj) {
-            if (i >= size) throw new IndexOutOfBoundsException(
+            if (i >= o.length) throw new IndexOutOfBoundsException(
                 "index "+i+" is beyond list boundary "+size);
-            Object old = o[i]; o[i] = obj; return old;
+            Object old = o[i]; o[i] = obj;
+           size = Math.max(i+1, size);
+           return old;
         }
         public Object get(int i) {
             if (i >= size) throw new IndexOutOfBoundsException(
@@ -162,10 +173,6 @@ public interface Basket extends Serializable {
         public void push(Object o) { add(o); }
     }
 
-    //public class Tree implements RandomAccess { } FIXME
-
-    //public class IndexedTree extends Tree { } FIXME
-
     /** Implementation of a hash table using Radke's quadratic residue
      *  linear probing. Uses a single array to store all entries.
      *
@@ -173,7 +180,7 @@ public interface Basket extends Serializable {
      *
      * @author adam@ibex.org, crawshaw@ibex.org
      */
-    public abstract class Hash implements Basket {
+    public class Hash implements Basket, Map {
         static final long serialVersionUID = 3948384093L;
 
         /** Used internally to record used slots. */
@@ -314,7 +321,6 @@ public interface Basket extends Serializable {
             int dest = orig * indexmultiple;
             int tries = 1;
             boolean plus = true;
-
             while (entries[dest] != null) {
                 if (equals(k, entries[dest])) return dest;
                 dest = Math.abs((orig + (plus ? 1 : -1) * tries * tries) % (entries.length / indexmultiple)) * indexmultiple;
@@ -347,10 +353,6 @@ public interface Basket extends Serializable {
         }
     }
 
-    public class HashMap extends Hash implements Map {
-        public HashMap() { super(); }
-        public HashMap(int x, float y) { super(x,y); }
-        public HashMap(int x, int z, float y) { super(x,z,y); }
-    }
+    // FIXME, BalancedTree goes here
 
 }