/* * LIST_AR.java * * Created on February 6, 2008, 1:58 PM * * * MORE UP TO DATE VERSION IS IN LIST_AR_purge with classname LIST * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package list_ar_purge; /** * * @author suchenek */ //Copyright Dr. Marek A. Suchenek, 2005, 2006, 2007 - 2025 public class LIST_AR { private Object[] element; private int end; // the number of elements of the LIST_AR private int resize_factor; //in % of increase; default is 100 (or 2.0 times increase) public LIST_AR() { this.element = new Object[10]; end = 0; resize_factor = 100; } public LIST_AR(int r) { this.element = new Object[10]; end = 0; resize_factor = r; } public void Insert(POSITION_AR p, Object x) { int len = element.length; int ind = p.index; if (end == len) { Object[] temp = new Object[(100 + resize_factor)*len/100]; // System.out.println("New array of size " + ((100 + resize_factor)*len/100) + " created."); for (int i = 0; i < ind; i++) { temp[i] = element[i]; cnt2.incr(); // cost of resizing beyond cost of insert } temp[ind] = x; cnt.incr(); for (int i = ind; i < end; i++) { temp[i + 1] = element[i]; cnt.incr(); // cost of insert } element = temp; } else { for (int i = end; i > ind; i--) { element[i] = element[i - 1]; cnt.incr(); // cost of insert } element[ind] = x; cnt.incr(); // cost of insert } end++; } public void Delete(POSITION_AR p) { cnt.incr(); int len = element.length; int ind = p.index; if ((len >= 10 + (resize_factor/10)) && ((100 + resize_factor)*end <= 50*len)) { // there is a discretion when to downsize Object[] temp = new Object[100*len/(100 + resize_factor)]; // System.out.println("New array of size " + (100*len/(100 + resize_factor)) + " created."); for (int i = 0; i < ind; i++) { temp[i] = element[i]; cnt2.incr(); // cost of resizing beyond cost of delete } for (int i = ind +1; i < end; i++) { temp[i - 1] = element[i]; cnt.incr(); // cost of delete } element = temp; } else { for (int i = ind + 1; i < end; i++) { element[i - 1] = element[i]; cnt.incr(); // cost of delete } } end--; } public Object Select(POSITION_AR p) { cnt.incr(); return element[p.index]; } public Boolean isEmpty() { return end == 0; } public POSITION_AR First() { cnt.incr(); return new POSITION_AR(0); } public POSITION_AR Next(POSITION_AR p) { cnt.incr(); return new POSITION_AR(p.index + 1); } public POSITION_AR Previous(POSITION_AR p) { cnt.incr(); return new POSITION_AR(p.index - 1); } public POSITION_AR End() { cnt.incr(); return new POSITION_AR(end); } public LIST_AR abstractClone() { cnt.incr(); POSITION_AR p; LIST_AR toReturn = new LIST_AR(); for (p = First(); !p.isEqual(End()); p = Next(p)) { toReturn.Insert(toReturn.End(), Select(p)); } return toReturn; } public LIST_AR clone() { cnt.incr(); LIST_AR toReturn = new LIST_AR(); toReturn.element = new Object[this.end]; toReturn.resize_factor = this.resize_factor; toReturn.end = this.end; for (int i = 0; i