ArrayList.js
Summary
No overview generated for 'ArrayList.js'
draw2d.ArrayList=function()
{
this.increment = 10;
this.size = 0;
this.data = new Array(this.increment);
}
draw2d.ArrayList.EMPTY_LIST = new draw2d.ArrayList();
draw2d.ArrayList.prototype.reverse=function()
{
var newData = new Array(this.size);
for (var i=0; i<this.size; i++)
{
newData[i] = this.data[this.size-i-1];
}
this.data = newData;
}
draw2d.ArrayList.prototype.getCapacity=function()
{
return this.data.length;
}
draw2d.ArrayList.prototype.getSize=function()
{
return this.size;
}
draw2d.ArrayList.prototype.isEmpty=function()
{
return this.getSize() == 0;
}
draw2d.ArrayList.prototype.getLastElement=function()
{
if (this.data[this.getSize() - 1] != null) {
return this.data[this.getSize() - 1];
}
}
draw2d.ArrayList.prototype.getFirstElement=function()
{
if (this.data[0] != null) {
return this.data[0];
}
}
draw2d.ArrayList.prototype.get=function(i)
{
return this.data[i];
}
draw2d.ArrayList.prototype.add=function(obj)
{
if(this.getSize() == this.data.length)
{
this.resize();
}
this.data[this.size++] = obj;
}
draw2d.ArrayList.prototype.addAll=function(obj)
{
for (var i=0;i<obj.getSize(); i++)
{
this.add(obj.get(i));
}
}
draw2d.ArrayList.prototype.remove=function(obj)
{
var index = this.indexOf(obj);
if(index>=0)
return this.removeElementAt(index);
return null;
}
draw2d.ArrayList.prototype.insertElementAt=function(obj, index)
{
if (this.size == this.capacity)
{
this.resize();
}
for (var i=this.getSize(); i > index; i--)
{
this.data[i] = this.data[i-1];
}
this.data[index] = obj;
this.size++;
}
draw2d.ArrayList.prototype.removeElementAt=function(index)
{
var element = this.data[index];
for(var i=index; i<(this.getSize()-1); i++)
{
this.data[i] = this.data[i+1];
}
this.data[this.getSize()-1] = null;
this.size--;
return element;
}
draw2d.ArrayList.prototype.removeAllElements=function()
{
this.size = 0;
for (var i=0; i<this.data.length; i++)
{
this.data[i] = null;
}
}
draw2d.ArrayList.prototype.indexOf=function(obj)
{
for (var i=0; i<this.getSize(); i++)
{
if (this.data[i] == obj)
{
return i;
}
}
return -1;
}
draw2d.ArrayList.prototype.contains=function(obj)
{
for (var i=0; i<this.getSize(); i++)
{
if (this.data[i] == obj)
{
return true;
}
}
return false;
}
draw2d.ArrayList.prototype.resize=function()
{
newData = new Array(this.data.length + this.increment);
for (var i=0; i< this.data.length; i++)
{
newData[i] = this.data[i];
}
this.data = newData;
}
draw2d.ArrayList.prototype.trimToSize=function()
{
var temp = new Array(this.getSize());
for (var i = 0; i < this.getSize(); i++)
{
temp[i] = this.data[i];
}
this.size = temp.length - 1;
this.data = temp;
}
draw2d.ArrayList.prototype.sort=function(f)
{
var i, j;
var currentValue;
var currentObj;
var compareObj;
var compareValue;
for(i=1; i<this.getSize();i++)
{
currentObj = this.data[i];
currentValue = currentObj[f];
j= i-1;
compareObj = this.data[j];
compareValue = compareObj[f];
while(j >=0 && compareValue > currentValue)
{
this.data[j+1] = this.data[j];
j--;
if (j >=0) {
compareObj = this.data[j];
compareValue = compareObj[f];
}
}
this.data[j+1] = currentObj;
}
}
draw2d.ArrayList.prototype.clone=function()
{
var newVector = new draw2d.ArrayList(this.size);
for (var i=0; i<this.size; i++) {
newVector.add(this.data[i]);
}
return newVector;
}
draw2d.ArrayList.prototype.overwriteElementAt=function(obj, index)
{
this.data[index] = obj;
}
Documentation generated by
JSDoc on Thu Feb 7 23:45:47 2008