Advertisement
Guest User

ArrayQueue

a guest
Mar 6th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. /**
  2.  * Created by artem on 27/02/15.
  3.  */
  4. public class ArrayQueue {
  5.     private final static int DEFAULT_ARRAY_SIZE = 8;
  6.  
  7.     private Object a[] = new Object[DEFAULT_ARRAY_SIZE];
  8.     private int l = 0, r = 0, size = 0;
  9.  
  10.     private void ensureCapacity() {
  11.         if (size == a.length) {
  12.             Object newA[] = new Object[a.length * 2];
  13.  
  14.             System.arraycopy(a, l, newA, newA.length - (a.length - l), a.length - l);
  15.             System.arraycopy(a, 0, newA, 0, r);
  16.  
  17.             l += a.length;
  18.             a = newA;
  19.         }
  20.     }
  21.  
  22.     // true
  23.     // ----------------
  24.     // a[r++] = e
  25.     public void enqueue(Object e) {
  26.         ensureCapacity();
  27.         a[r++] = e;
  28.  
  29.         if (r == a.length) {
  30.             r = 0;
  31.         }
  32.  
  33.         ++size;
  34.     }
  35.  
  36.     // true
  37.     // ----------------
  38.     // a[--l] = e
  39.     public void push(Object e) {
  40.         ensureCapacity();
  41.  
  42.         --l;
  43.         if (l == -1)
  44.             l = a.length - 1;
  45.  
  46.         a[l] = e;
  47.  
  48.         ++size;
  49.     }
  50.  
  51.     // size > 0
  52.     // ----------------
  53.     // result = a[l]
  54.     public Object element() {
  55.         return a[l];
  56.     }
  57.  
  58.     // size > 0
  59.     // ----------------
  60.     // result = a[r - 1]
  61.     public Object peek() {
  62.         int rl = r - 1;
  63.         if (rl == -1) {
  64.             rl = a.length - 1;
  65.         }
  66.         return a[rl];
  67.     }
  68.  
  69.     // size > 0
  70.     // ----------------
  71.     // result = a[l']
  72.     // l = l' + 1
  73.     // size == size' - 1
  74.     public Object dequeue() {
  75.         Object next = element();
  76.         a[l++] = null;
  77.  
  78.         if (l == a.length) {
  79.             l = 0;
  80.         }
  81.  
  82.         --size;
  83.         return next;
  84.     }
  85.  
  86.     // size > 0
  87.     // ----------------
  88.     // result = a[r' - 1]
  89.     // r = r' - 1
  90.     // a[r' - 1] == null
  91.     // size == size' - 1
  92.     public Object remove() {
  93.         r = r - 1;
  94.         if (r == -1) {
  95.             r = a.length - 1;
  96.         }
  97.  
  98.         Object last = a[r];
  99.         a[r] = null;
  100.  
  101.         --size;
  102.  
  103.         return last;
  104.     }
  105.  
  106.     // true
  107.     // ----------------
  108.     // result = size
  109.     public int size() {
  110.         return size;
  111.     }
  112.  
  113.     // true
  114.     // ----------------
  115.     // result = size == 0
  116.     public boolean isEmpty() {
  117.         return size == 0;
  118.     }
  119.  
  120.     // true
  121.     // ----------------
  122.     // a == []
  123.     // l == r == size == 0
  124.     public void clear() {
  125.         a = new Object[DEFAULT_ARRAY_SIZE];
  126.         l = r = size = 0;
  127.     }
  128.  
  129.     // true
  130.     // ----------------
  131.     // result[i] = a[l + i]
  132.     // result.length = size
  133.     public Object[] toArray() {
  134.         Object array[] = new Object[size];
  135.  
  136.         for (int i = 0; i < size; ++i)
  137.             array[i] = a[(l + i) % a.length];
  138.  
  139.         return array;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement