Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function vectorEntry(data)
- {
- this.prev = null;
- this.next = null;
- this.data = data;
- return this;
- }
- //**********************************************************************************
- function vectorEnum()
- {
- if (arguments.length != 2) return null;
- this.curloc = arguments[0];
- this.list = arguments[1];
- //**********************************************************************************
- this.hasMoreElements = testEnum; // S
- this.nextElement = nextEnum; // S
- //**********************************************************************************
- return this;
- }
- //
- function testEnum()
- {
- return (this.curloc != null);
- }
- //
- function nextEnum()
- {
- var temp;
- if (this.curloc == null) {return null;}
- temp = this.list[this.curloc].data;
- this.curloc = this.list[this.curloc].next;
- return temp;
- }
- //**********************************************************************************
- function Vector(capacity,capacityIncrement) // S
- //**********************************************************************************
- //* This Object generates a reasonable emulation of Java's Vector Class. *
- //* Most methods work exactly as they were designed, while others simulate results.*
- //* The ones that fall in the latter category are those dealing with the internal *
- //* array management. Since JavaScript has a dynamically expandable array, there *
- //* is not actual need to mantain it as Java does. However, when methods such as *
- //* capacity(), size(), trimToSize(), etc, are invoked, this object will adjust its*
- //* internal structure to reflect the results of the invocation correctly. *
- //* There is a series of unimplemented methods that correspond to new Java release *
- //* 1.2 additions. As soon as I understand them, they will be implemented. These *
- //* are marked with the legend "[1.2]". *
- //**********************************************************************************
- //* VERSION: 1.00 KEY E: TESTED/ERRORS EXIST. *
- //* DATE: O4/30/1999 S: SUCCESSFULLY TESTED. *
- //* AUTHOR: Antonio F. Gomez U: UNTESTED. *
- //* N: UNIMPLEMENTED. *
- //* D: PARTIALLY IMPLEMENTED *
- //**********************************************************************************
- {
- this.length = 0;
- this.capacityRequest = arguments[0]; // S
- this.capacityIncrement = arguments[1]; // S
- if (arguments.length < 1)
- {this.capacityRequest = 10;} // S
- if (arguments.length < 2)
- {this.capacityIncrement = 0;} // S
- this.elementCount = 0;
- this.first = null;
- this.last = null;
- this.avail = null;
- this.elementData = new Array();
- //**********************************************************************************
- this.addElement = addToEnd; // S
- this.add = addToEnd12; // U [1.2]
- this.addAll = addToVector; // U [1.2]
- this.elementAt = getIndexedElement; // S
- this.get = getIndexedElement; // S [1.2]
- this.firstElement = getFirstElement; // S
- this.lastElement = getLastElement; // S
- //
- this.indexOf = getIndexOfElement; // S
- this.insertElementAt = putIndexedElement; // S
- this.lastIndexOf = getLastIndexOfElement; // S
- this.setElementAt = updateElement; // S
- this.set = updateElement12; // U [1.2]
- //
- this.removeAllElements = deleteAll; // S
- this.removeElement = deleteElement; // S
- this.removeElementAt = deleteThisElement; // S
- this.remove = deleteVectorElement; // U [1.2]
- this.removeAll = deleteCollection // N [1.2]
- this.removeRange = deleteElementRange // N [1.2]
- //
- this.toArray = buildArray; // N [1.2]
- this.capacity = getCapacity; // S
- this.clear = clearVector; // N [1.2]
- this.clone = cloneVector; // S
- this.contains = findElement; // D
- this.containsAll = findAllElements; // N [1.2]
- this.copyInto = extractData; // S
- this.ensureCapacity = ensureAllocation; // N
- this.elements = enumVector; // S
- this.equals = equalsVector; // N [1.2]
- this.isEmpty = testElementCount; // S
- this.iterator = itarateVector; // N [1.2]
- this.listIterator = listVectorIterator; // N [1.2]
- this.ratainAll = retainCollection; // N [1.2]
- this.setSize = setArrayLength; // N
- this.size = getArrayLength; // S
- this.trimToSize = trimCapacity; // N
- this.toString = VectorString; // N
- //
- return this;
- }
- //**********************************************************************************
- function addToEnd()
- {
- var data = arguments[0];
- var nx = this.avail;
- var cx;
- if (nx == null)
- {nx = this.length++;}
- else
- {this.avail = this.elementData[nx].next;}
- this.elementData[nx] = new vectorEntry(data);
- cx = this.last;
- this.last = nx;
- if (cx != null)
- {this.elementData[cx].next = nx; this.elementData[nx].prev = cx;}
- else
- {this.first = nx;}
- this.elementCount++;
- return nx;
- }
- //**********************************************************************************
- function addToEnd12()
- {
- if (arguments.length == 1)
- {
- this.addElement(arguments[0]);
- return true;
- }
- else
- {
- if (arguments.length == 2)
- {
- this.insertElementAt(arguments[1],arguments[0]);
- return true;
- }
- }
- return false;
- }
- //**********************************************************************************
- function addToVector(index,collection) {}
- //**********************************************************************************
- function getIndexedElement()
- {
- var ox = arguments[0];
- if (ox >= this.elementCount || ox < 0) {return null;}
- var cx = this.first;
- while (ox-- > 0) {cx = this.elementData[cx].next;}
- return this.elementData[cx].data;
- }
- //**********************************************************************************
- function getFirstElement()
- {
- if (this.first == null) return null;
- return this.elementData[this.first].data;
- }
- //**********************************************************************************
- function getLastElement()
- {
- if (this.last == null) return null;
- return this.elementData[this.last].data;
- }
- //**********************************************************************************
- function getIndexOfElement()
- {
- var data = arguments[0];
- var cx = this.first;
- var ox = 0;
- var k = 0;
- if (arguments.length == 2) ox = arguments[1];
- if (ox >= this.elementCount || ox < 0) {return -1;}
- while (k < ox) {cx = this.elementData[cx].next; k++;}
- while (cx != null)
- {
- if (this.elementData[cx].data == data) {break;}
- cx = this.elementData[cx].next; k++;
- }
- if (cx == null) {k = -1;}
- return k;
- }
- //**********************************************************************************
- function putIndexedElement()
- {
- var data = arguments[0];
- var ox = arguments[1];
- var px;
- if (ox > this.elementCount) {return false;}
- var cx = this.first;
- while (ox-- > 0) {cx = this.elementData[cx].next;}
- var ne = this.avail;
- if (ne == null) {ne = this.length++;}
- else {this.avail = this.elementData[ne].next;}
- this.elementData[ne] = new vectorEntry(data);
- if (cx != null)
- {
- px = this.elementData[cx].prev;
- if (px != null)
- {
- this.elementData[ne].prev = px;
- this.elementData[px].next = ne;
- }
- else
- {
- this.first = ne;
- }
- this.elementData[ne].next = cx;
- this.elementData[cx].prev = ne;
- }
- else
- {
- if (this.first == null)
- {
- this.first = ne;
- this.last = ne;
- }
- else
- {
- cx = this.last; this.last = ne;
- this.elementData[cx].next = ne; this.elementData[ne].prev = cx;
- }
- }
- this.elementCount++;
- return true;
- }
- //**********************************************************************************
- function getLastIndexOfElement()
- {
- var data = arguments[0];
- var cx = this.last;
- var ox = this.elementCount - 1;
- var k = this.elementCount;
- if (arguments.length == 2) ox = arguments[1];
- if (ox >= this.elementCount || ox < 0) {return -1;}
- while (--k > ox) {cx = this.elementData[cx].prev;}
- while (cx != null)
- {
- if (this.elementData[cx].data == data) {break;}
- cx = this.elementData[cx].prev; k--;
- }
- if (cx == null) {k = -1;}
- return k;
- }
- //**********************************************************************************
- function updateElement()
- {
- var data = arguments[0];
- var ox = arguments[1];
- if (ox > this.elementCount) {return false;}
- var cx = this.first;
- while (ox-- > 0) {cx = this.elementData[cx].next;}
- if (cx == null) {return false;}
- this.elementData[cx].data = data;
- return true;
- }
- //**********************************************************************************
- function updateElement12()
- {
- var temp;
- if (arguments.length == 2)
- {
- temp = this.elementAt(arguments[0]);
- this.setElementAt(arguments[1],arguments[0]);
- return temp;
- }
- else return null;
- }
- //**********************************************************************************
- function deleteAll()
- {
- this.capacity();
- this.length = 0;
- this.elementCount = 0;
- this.first = null;
- this.last = null;
- this.avail = null;
- this.elementData = new Array();
- return true;
- }
- //**********************************************************************************
- function deleteElement()
- {
- var data = arguments[0];
- var cx = this.first;
- while (cx != null)
- {
- if (this.elementData[cx].data == data) {break;};
- cx = this.elementData[cx].next;
- }
- if (cx == null) {return false;}
- var px = this.elementData[cx].prev;
- var nx = this.elementData[cx].next;
- if (px == null) {this.first = nx;}
- else
- {this.elementData[px].next = nx;}
- if (nx == null) {this.last = px;}
- else
- {this.elementData[nx].prev = px;}
- this.elementData[cx].next = this.avail;
- this.avail = cx;
- this.elementCount--;
- return true;
- }
- //**********************************************************************************
- function deleteThisElement()
- {
- var cx = this.first;
- var ox = arguments[0];
- if (ox > this.elementCount) {return false;}
- while (ox-- > 0)
- {cx = this.elementData[cx].next;}
- var px = this.elementData[cx].prev;
- var nx = this.elementData[cx].next;
- if (px == null) {this.first = nx;}
- else
- {this.elementData[px].next = nx;}
- if (nx == null) {this.last = px;}
- else
- {this.elementData[nx].prev = px;}
- this.elementData[cx].next = this.avail;
- this.avail = cx;
- this.elementCount--;
- return true;
- }
- //**********************************************************************************
- function deleteVectorElement()
- {
- var temp;
- if (arguments.length == 1)
- {
- if (typeof argument[0] == "number")
- {
- temp = elementAt(arguments[0]);
- this.removeElementAt(arguments[0]);
- return temp;
- }
- else
- {
- if (typeof argument[0] == "object")
- {
- this.removeElement(arguments[0]);
- return true;
- }
- }
- }
- else return false;
- }
- //**********************************************************************************
- function deleteCollection(collection) {}
- //**********************************************************************************
- function deleteElementRange(fromindex,toindex) {}
- //**********************************************************************************
- function getCapacity()
- {
- if (this.length > this.capacityRequest)
- {if (this.capacityIncrement == 0)
- {
- while (this.capacityRequest < this.length) {this.capacityRequest *= 2;}
- }
- else
- {
- while (this.capacityRequest < this.length)
- {this.capacityRequest += this.capacityIncrement;}
- }
- }
- return this.capacityRequest;
- }
- //**********************************************************************************
- function cloneVector()
- {
- var nv = new Vector();
- nv.length = this.length;
- nv.capacityIncrement = this.capacityIncrement;
- nv.elementCount = this.elementCount;
- nv.first = this.first;
- nv.last = this.last;
- nv.avail = this.avail;
- nv.elementData = new Array(nv.elementData.length);
- for (var cx = 0; cx < nv.length; cx++)
- {
- nv.elementData[cx] = new vectorEntry(this.elementData[cx].data);
- nv.elementData[cx].prev = this.elementData[cx].prev;
- nv.elementData[cx].next = this.elementData[cx].next;
- }
- return nv;
- }
- //**********************************************************************************
- function enumVector()
- {
- return new vectorEnum(this.first,this.elementData);
- }
- //**********************************************************************************
- function testElementCount()
- {
- return this.elementCount == 0;
- }
- //**********************************************************************************
- function findElement()
- {
- var data = arguments[0];
- var cx = this.first;
- while (cx != null && this.elementData[cx].data != data)
- {
- cx = this.elementData[cx].next;
- }
- return cx != null;
- }
- //**********************************************************************************
- function findAllElements(collection) {}
- //**********************************************************************************
- function extractData()
- {
- var na = arguments[0];
- var cx = this.first;
- var k = 0;
- while (cx != null)
- {
- na[k++] = this.elementData[cx].data;
- cx = this.elementData[cx].next;
- }
- return true;
- }
- //**********************************************************************************
- function getArrayLength()
- {
- this.capacity(); return this.length;
- }
- //**********************************************************************************
- function buildArray(object) {}
- //**********************************************************************************
- function clearVector() {}
- //**********************************************************************************
- function setArrayLength(capacity) {}
- //**********************************************************************************
- function ensureAllocation(minCapacity) {}
- //**********************************************************************************
- function equalsVector(object) {return false;}
- //**********************************************************************************
- function trimCapacity(capacity) {}
- //**********************************************************************************
- function itarateVector() {}
- //**********************************************************************************
- function listVectorIterator(index) {}
- //**********************************************************************************
- function retainCollection(collection) {}
- //**********************************************************************************
- function VectorString()
- {
- var out = "[";
- var cx = this.first;
- while (cx != null)
- {
- out += this.elementData[cx].data;
- cx = this.elementData[ cx].next;
- if (cx == null) {break;}
- out += ",";
- }
- out += "]";
- return out;
- }
- //******************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement