00001 package edu.ksu.cis.bandera.bir;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import ca.mcgill.sable.util.*;
00036
00037
00038
00039
00040
00041 public class LocVector {
00042
00043 int size = 0;
00044 Location [] data;
00045
00046 public LocVector() { this(5); }
00047 public LocVector(int capacity) {
00048 data = new Location[capacity];
00049 }
00050 public LocVector(Location x) {
00051 this(5);
00052 addElement(x);
00053 }
00054 public void addElement(Location x) {
00055 if (x == null)
00056 throw new RuntimeException("Element cannot be null");
00057 if (size == data.length)
00058 expand();
00059 data[size++] = x;
00060 }
00061 public Location bottom() {
00062 if (size == 0)
00063 throw new RuntimeException("Cannot bottom() empty vector");
00064 return data[0];
00065 }
00066 public boolean contains(Location x) {
00067 for (int i = 0; i < size; i++)
00068 if (data[i].equals(x))
00069 return true;
00070 return false;
00071 }
00072 public Location elementAt(int pos) {
00073 if (pos < 0 || pos >= size)
00074 throw new RuntimeException("Position invalid: " + pos);
00075 return data[pos];
00076 }
00077 void expand() {
00078 Location [] newData = new Location[data.length * 2];
00079 for (int i = 0; i < data.length; i++)
00080 newData[i] = data[i];
00081 data = newData;
00082 }
00083 public Location firstElement() { return elementAt(0); }
00084 public void insertElementAt(Location x, int pos) {
00085 if (x == null)
00086 throw new RuntimeException("Element cannot be null");
00087 if (size == data.length)
00088 expand();
00089 if (pos > size || pos < 0)
00090 throw new RuntimeException("Position invalid: " + pos);
00091 for (int i = size; i > pos; i--)
00092 data[i] = data[i-1];
00093 data[pos] = x;
00094 size++;
00095 }
00096 public Location pop() {
00097 if (size == 0)
00098 throw new RuntimeException("Cannot pop() empty vector");
00099 size--;
00100 return data[size];
00101 }
00102 public void push(Location x) {
00103 addElement(x);
00104 }
00105 public boolean removeElement(Location x) {
00106 int pos = 0;
00107 while (pos < size && data[pos] != x)
00108 pos++;
00109 if (pos == size)
00110 return false;
00111 for (int i = pos; i < size - 1; i++)
00112 data[i] = data[i+1];
00113 size--;
00114 return true;
00115 }
00116 public int size() { return size; }
00117 public Location top() {
00118 if (size == 0)
00119 throw new RuntimeException("Cannot top() empty vector");
00120 return data[size-1];
00121 }
00122 }