using BitSet in ListObjectPool

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_opentype@376 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
index a3c35be..569c555 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
@@ -16,10 +16,9 @@
 package edu.uci.ics.asterix.runtime.util.container;
 
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.List;
 
-import org.apache.hadoop.io.BooleanWritable;
-
 /**
  * Object pool backed by a list.
  * 
@@ -44,12 +43,7 @@
     /**
      * bits indicating which element is in use
      */
-    private List<BooleanWritable> usedBits = new ArrayList<BooleanWritable>();
-
-    /**
-     * the start index for searching
-     */
-    private int minStartIndex = 0;
+    private BitSet usedBits = new BitSet();
 
     public ListObjectPool(IElementFactory<E, T> factory) {
         this.factory = factory;
@@ -57,29 +51,19 @@
 
     @Override
     public E allocate(T arg) {
-        boolean continuous = true;
-        for (int i = minStartIndex; i < pool.size(); i++) {
-            if (!usedBits.get(i).get()) {
-                // the two cases where an element in the pool is a match
-                if ((arg == null && args.get(i) == null)
-                        || (arg != null && args.get(i) != null && arg.equals(args.get(i)))) {
-                    // the element is not used and the arg is the same as
-                    // input arg
-                    if (continuous) {
-                        minStartIndex++;
-                    }
-                    usedBits.get(i).set(true);
-                    return pool.get(i);
-                } else {
-                    // a unmatched element blocked
-                    // free slots are not continuous from the beginning free
-                    // slot
-                    continuous = false;
-                }
-            } else {
-                if (continuous) {
-                    minStartIndex++;
-                }
+        int freeSlot = -1;
+        while (freeSlot + 1 < pool.size()) {
+            freeSlot = usedBits.nextClearBit(freeSlot + 1);
+            if (freeSlot >= pool.size())
+                break;
+
+            // the two cases where an element in the pool is a match
+            if ((arg == null && args.get(freeSlot) == null)
+                    || (arg != null && args.get(freeSlot) != null && arg.equals(args.get(freeSlot)))) {
+                // the element is not used and the arg is the same as
+                // input arg
+                usedBits.set(freeSlot);
+                return pool.get(freeSlot);
             }
         }
 
@@ -87,14 +71,12 @@
         E element = factory.createElement(arg);
         pool.add(element);
         args.add(arg);
-        usedBits.add(new BooleanWritable(true));
+        usedBits.set(pool.size() - 1);
         return element;
     }
 
     @Override
     public void reset() {
-        for (int i = 0; i < usedBits.size(); i++)
-            usedBits.get(i).set(false);
-        minStartIndex = 0;
+        usedBits.clear();
     }
 }