/*
|
* ----------------------------------------------------
|
* Class - ArrayList
|
* ----------------------------------------------------
|
*/
|
ArrayList = function(){
|
/* Properties */
|
this.increment = 10;
|
this.size = 0;
|
this.data = new Array(this.increment);
|
}
|
|
ArrayList.EMPTY_LIST = new ArrayList();
|
ArrayList.prototype.type = "ArrayList";
|
|
// reverse the ArrayList
|
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;
|
}
|
|
// getCapacity() -- returns the number of elements the vector can hold
|
ArrayList.prototype.getCapacity = function(){
|
return this.data.length;
|
}
|
|
// getSize() -- returns the current size of the vector
|
ArrayList.prototype.getSize = function(){
|
return this.size;
|
}
|
|
// isEmpty() -- checks to see if the Vector has any elements
|
ArrayList.prototype.isEmpty = function(){
|
return this.getSize() == 0;
|
}
|
|
// getLastElement() -- returns the last element
|
ArrayList.prototype.getLastElement = function(){
|
if (this.data[this.getSize() - 1] != null) {
|
return this.data[this.getSize() - 1];
|
}
|
}
|
|
// getFirstElement() -- returns the first element
|
ArrayList.prototype.getFirstElement = function(){
|
if (this.data[0] != null) {
|
return this.data[0];
|
}
|
}
|
|
// getElementAt() -- returns an element at a specified index
|
ArrayList.prototype.get = function(/*:int*/i){
|
return this.data[i];
|
}
|
|
// add() -- adds a element at the end of the Vector
|
ArrayList.prototype.add = function(obj){
|
if (this.getSize() == this.data.length) {
|
this.resize();
|
}
|
this.data[this.size++] = obj;
|
}
|
|
// add() -- adds a element at the end of the Vector
|
ArrayList.prototype.addAll = function(obj){
|
for (var i = 0; i < obj.getSize(); i++) {
|
this.add(obj.get(i));
|
}
|
}
|
|
// indexOf() -- returns the index of a searched element
|
ArrayList.prototype.remove = function(obj){
|
var index = this.indexOf(obj);
|
if (index >= 0)
|
return this.removeElementAt(index);
|
return null;
|
}
|
|
// insertElementAt() -- inserts an element at a given position
|
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++;
|
}
|
|
// removeElementAt() -- removes an element at a specific index
|
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;
|
}
|
|
// removeAllElements() -- removes all elements in the Vector
|
ArrayList.prototype.removeAllElements = function(){
|
this.size = 0;
|
|
for (var i = 0; i < this.data.length; i++) {
|
this.data[i] = null;
|
}
|
}
|
|
// indexOf() -- returns the index of a searched element
|
ArrayList.prototype.indexOf = function(obj){
|
for (var i = 0; i < this.getSize(); i++) {
|
if (this.data[i] == obj) {
|
return i;
|
}
|
}
|
return -1;
|
}
|
|
// contains() -- returns true if the element is in the Vector, otherwise false
|
ArrayList.prototype.contains = function(obj){
|
for (var i = 0; i < this.getSize(); i++) {
|
if (this.data[i] == obj) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
// resize() -- increases the size of the Vector
|
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;
|
}
|
|
// trimToSize() -- trims the vector down to it's size
|
ArrayList.prototype.trimToSize = function(){
|
// nothing to do
|
if (this.data.length == this.size)
|
return;
|
|
var temp = new Array(this.getSize());
|
|
for (var i = 0; i < this.getSize(); i++) {
|
temp[i] = this.data[i];
|
}
|
this.size = temp.length;
|
this.data = temp;
|
}
|
|
// sort() - sorts the collection based on a field name - f
|
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;
|
}
|
}
|
|
// clone() -- copies the contents of a Vector to another Vector returning the new Vector.
|
ArrayList.prototype.clone = function(){
|
var newVector = new ArrayList(this.size);
|
|
for (var i = 0; i < this.size; i++) {
|
newVector.add(this.data[i]);
|
}
|
|
return newVector;
|
}
|
|
// overwriteElementAt() - overwrites the element with an object at the specific index.
|
ArrayList.prototype.overwriteElementAt = function(obj, index){
|
this.data[index] = obj;
|
}
|
|
ArrayList.prototype.getPersistentAttributes = function(){
|
return {
|
data: this.data,
|
increment: this.increment,
|
size: this.getSize()
|
};
|
}
|
/*
|
* ----------------------------------------------------
|
* End - ArrayList
|
* ----------------------------------------------------
|
*/
|