StoreWithArray.pizza

public abstract class StoreWithArray<T> implements STORE<T> {

   ObjectWrapper<T>[] array = new ObjectWrapper[10];

   int length = 0;

   public boolean isEmpty() {
      return length == 0;
   }

   protected void growTo(int newsize) {
   // Assume newsize > length 
      if(newsize > array.length) {
         int growsize = array.length*2;
         while (newsize > growsize) {growsize = 2*growsize;};
         ObjectWrapper<T>[] newArray = new ObjectWrapper[growsize];
         for (int i = 0; i < length; i++) {
            newArray[i] = array[i];
         };
         array = newArray;
      };
   }

   protected void shrinkTo(int newsize) {
   // Assume newsize <= length
      if(newsize <= array.length/2) {
         int shrinksize = array.length/2;
         while (newsize <= shrinksize/2 & shrinksize >= 2) {
            shrinksize = shrinksize/2;
         };
         ObjectWrapper<T>[] newArray = new ObjectWrapper[shrinksize];
         for (int i = 0; i < length; i++) {
            newArray[i] = array[i];
         };
         array = newArray;
      };
   }

   public void add(T o) {
      int ol = length;
      growTo(length+1);
      length++;
      array[ol] = new ObjectWrapper(o);
   }

   public void addAll(T[] a) {
      for(int i = 0; i < a.length; i++) {
         add(a[i]);
      }
   }

   public T remove() {
      if (isEmpty()) {throw new EmptyStoreException();};
      T o = array[--length].object;
      shrinkTo(length);
      return o;
   }
}

class ObjectWrapper<T> {
   T object;
   ObjectWrapper(T o) {object = o;}
}


On to PQueue1.pizza Part of Priority Queues mit Pizza
Andreas Podelski, Abdelwaheb Ayari, Hubert Baumeister
February 25, 1997

Imprint | Data Protection