/* * Copyright (c) 1996 Dennis Heimbigner. email dennis@cs.colorado.edu */ package pp.src.jbf; import java.util.*; public class Int_Vector { protected static final int defaultsize = 16; protected int _length; protected int _contents[]; protected int _delta; public int[] contents() { return _contents;} public void contents(int v[]) { _contents = v; } public Int_Vector(int initsize, int delta) { super(); _contents = new int[initsize]; _delta = delta; } public Int_Vector(int initsize) { this(initsize,0); } public Int_Vector() { this(defaultsize); } protected void extendto(int sz) { int oldsz = _contents.length; if(sz > oldsz) { int newbuf[]; int newsz; if(oldsz == 0) oldsz = defaultsize; if(_delta > 0) { newsz = oldsz + _delta; } else { newsz = 2*oldsz; } newbuf = new int[newsz]; if(oldsz > 0) System.arraycopy(_contents, 0, newbuf, 0, _length); _contents = newbuf; } } public void setSize(int newsz) { if(newsz > _length) { extendto(newsz); } else { int i; for(i=newsz;i < _length;i++) { _contents[i] = 0; } } _length = newsz; } public int capacity() { return _contents.length; } public int size() { return _length; } public boolean isEmpty() { return _length == 0; } public boolean contains(int e) { return indexOf(e, 0) >= 0; } public int indexOf(int e) { return indexOf(e, 0); } public int indexOf(int e, int index) { int i; for(i=index;i < _length;i++) { if(e == _contents[i]) return i; } return -1; } public int lastIndexOf(int e) { return lastIndexOf(e, _length); } public int lastIndexOf(int e, int index) { int i; for(i=index-1;i >= 0;i--) { if(e == _contents[i]) return i; } return -1; } public int elementAt(int index) { if(index < 0 || index >= _length) { throw new ArrayIndexOutOfBoundsException(""+index); } return _contents[index]; } public int firstElement() { return elementAt(0); } public int lastElement() { return elementAt(_length-1); } public void setElementAt(int e, int index) { if(index < 0 || index >= _length) { throw new ArrayIndexOutOfBoundsException(index + ""); } _contents[index] = e; } public void removeElementAt(int index) { if(index < 0 || index >= _length) { throw new ArrayIndexOutOfBoundsException(index + ""); } int i = _length - index - 1; if(i > 0) { System.arraycopy(_contents, index+1, _contents, index, i); } _length--; _contents[_length] = 0; } public void insertElementAt(int e, int index) { if(index < 0 || index >= _length + 1) { throw new ArrayIndexOutOfBoundsException(index + ""); } extendto(_length+1); System.arraycopy(_contents,index,_contents,index+1,_length - index); _contents[index] = e; _length++; } public void addElement(int e) { extendto(_length+1); _contents[_length++] = e; } public boolean removeElement(int e) { int i = indexOf(e); if(i >= 0) { removeElementAt(i); return true; } return false; } public void removeElementRange(int first, int count) { if(count < 0 || first < 0 || first+count > _length) { throw new ArrayIndexOutOfBoundsException("range "+first+"/"+count); } int mv = (_length - count); if(count > 0 && mv > 0) { System.arraycopy(_contents,first+count,_contents,0,mv); } _length -= count; for(int i=0;i 0) b.append(", "); b.append(_contents[i]); } b.append("]"); return b.toString(); } }