/*
 * โครงสร้างข้อมูล : ฉบับวาจาวา
 * http://www.cp.eng.chula.ac.th/~somchai/books
 */
package dataStructures;

import java.util.NoSuchElementException;

/**
 * คลาสที่สร้างแถวคอยเชิงบุริมภาพด้วย ArrayList
 * @author สมชาย ประสิทธิ์จูตระกูล
 */
public class ArrayPQ implements PriorityQueue {
  private ArrayList list;
  
  public ArrayPQ(int cap) {
    list = new ArrayList(cap);
  }
  public boolean isEmpty() { return list.isEmpty(); }
  public int size() { return list.size(); }
  public void enqueue(Object e) { list.add(e); }
  public Object peek() { return list.get(maxIndex()); }
  public Object dequeue() {
    int max = maxIndex();
    Object result = list.get(max);
    list.remove(max);
    return result;
  }
  private int maxIndex() {
    if (isEmpty()) throw new NoSuchElementException();
    int max = 0;
    for (int i = 1; i < list.size(); i++) {
      Comparable d = (Comparable) list.get(i);
      if (d.compareTo(list.get(max)) < 0) max = i;
    }
    return max;
  }
}