You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xap-commits@incubator.apache.org by mt...@apache.org on 2006/09/19 01:36:54 UTC

svn commit: r447670 - /incubator/xap/trunk/src/xap/util/Vector.js

Author: mturyn
Date: Mon Sep 18 18:36:53 2006
New Revision: 447670

URL: http://svn.apache.org/viewvc?view=rev&rev=447670
Log:
Bringing this back until it can be removed from datasource classes..

Added:
    incubator/xap/trunk/src/xap/util/Vector.js

Added: incubator/xap/trunk/src/xap/util/Vector.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/util/Vector.js?view=auto&rev=447670
==============================================================================
--- incubator/xap/trunk/src/xap/util/Vector.js (added)
+++ incubator/xap/trunk/src/xap/util/Vector.js Mon Sep 18 18:36:53 2006
@@ -0,0 +1,196 @@
+/*
+ * Copyright  2006 The Apache Software Foundation.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+ 
+ /**
+ * TODO compare performance with other Vector implementations and replace this
+ * if there is something better.
+ * 
+ * @author ikaplansky
+ */
+ 
+Xap.provide("xap.util.Vector") ;
+ 
+xap.util.Vector = function( initialCapacity, increment ) {
+	if ( initialCapacity == 0 ) {
+		initialCapacity = 100;
+	}
+	if ( increment == null ) {
+		increment = 10;
+	}
+	this._data = new Array( initialCapacity );
+	this._increment = increment;
+	this._size = 0;
+}
+
+xap.util.Vector.prototype.size = function() {
+	return this._size;
+}
+
+xap.util.Vector.prototype.isEmpty = function() {
+	return this.size() == 0;
+}
+
+xap.util.Vector.prototype.getLastElement = function() {
+	if ( this.size() > 0 ) {
+		return this._data[this.size() - 1];
+	}
+	return null;
+}
+
+xap.util.Vector.prototype.getFirstElement = function() {
+	if( this.size() > 0 ) {
+		return this._data[0];
+	}
+	return null;
+}
+
+xap.util.Vector.prototype.elementAt = function( i ) {
+	if ( i < this.size() && i >= 0 ) {
+		return this._data[i];
+	}
+	throw new BaseException( "IndexOutOfBoundsException in elementAt:" + i ) ;
+}
+
+xap.util.Vector.prototype.addElement = function( obj ) {
+	if( this.size() == this._data.length ) {
+		this.resize();
+	}
+	this._data[this._size++] = obj;
+}
+
+xap.util.Vector.prototype.insertElementAt = function( obj, index ) {
+	if ( index < 0 || index >= this._size ) {
+		throw "IndexOutOfBoundsException in insertElementAt:" + index ;
+	}
+	if ( this._size == this._capacity ) {
+		this.resize();
+	}
+	for ( var i = this.size(); i > index; i-- ) {
+		this._data[i] = this._data[i-1];
+	}
+	this._data[index] = obj;
+	this._size++;
+}
+
+xap.util.Vector.prototype.removeElementAt = function( index ) {
+	if( index < 0 || index >= this._size ) {
+		throw  "IndexOutOfBoundsException in removeElementAt:" + 
+							 index ;
+	}
+	var element = this._data[ index ];
+	for( var i = index; i < this.size()-1; i++ ) {
+		this._data[i] = this._data[ i+1 ];
+	}
+	this._data[this.size()-1] = null;
+	this._size--;
+	return element;
+} 
+
+xap.util.Vector.prototype.removeElement = function( obj ) {
+	for( var i = 0; i < this.size(); i++ ) {
+		var el = this._data[i];
+		if ( el == obj ) {
+			this.removeElementAt( i );
+		}
+	}
+	return null;
+} 
+
+xap.util.Vector.prototype.removeAllElements = function() {
+	this._size = 0;
+	for ( var i = 0; i < this._data.length; i++ ) {
+		this._data[i] = null;
+	}
+}
+
+xap.util.Vector.prototype.indexOf = function( object, startIndex ) {
+	if( startIndex == null ) {
+		startIndex = 0;
+	}
+	for ( var i=startIndex; i < this.size(); i++ ) {
+		if ( this._data[i] == object ) {
+			return i;
+		}
+	}
+	return -1;
+}
+
+xap.util.Vector.prototype.contains = function( object ) {
+	for ( var i=0; i < this.size(); i++ ) {
+		if ( this._data[i] == object ) {
+			return true;
+		}
+	}
+	return false;
+}
+
+xap.util.Vector.prototype.resize = function() {
+	var v = new Array( this._data.length + 
+						  this._increment );
+	
+	for	( var i=0; i < this._data.length; i++ ) {
+		v[i] = this._data[i];
+	}
+	
+	this._data = v;
+}
+
+
+xap.util.Vector.prototype.trimToSize = function() {
+	var trimmed = new Array( this.size() );
+	
+	for ( var i = 0; i < this.size(); i++) {
+		trimmed[i] = this._data[i];
+	}
+	this._size = trimmed.length - 1;
+	this._data = trimmed;
+} 
+
+xap.util.Vector.prototype.clone = function() {
+	var copy = new xap.util.Vector( this.size() );
+	
+	for (var i=0; i < this.size(); i++) {
+		copy.addElement(this._data[i]);
+	}
+	return copy;
+}
+
+xap.util.Vector.prototype.toString = function() {
+	return this._data.toString();
+}
+
+xap.util.Vector.prototype.setElementAt = function( object, index ) {
+	this._data[index] = object;
+}
+
+
+/**
+* Simpler than the Java Collections' method, since we don't
+* need to consider "...An array of <em>what</em>?"
+* @return An array pointing to the data for this xap.util.Vector
+* instance---note that we are <strong>not</strong> copying
+* into new objects, so the result array's contents still
+* point back to this xap.util.Vector instance's <code>_data</code> member.
+**/
+xap.util.Vector.prototype.toArray = function() {
+	var result = new Array( this.size() );
+	
+	for ( var i = 0; i < this.size(); i++) {
+		result[i] = this._data[i];
+	}
+	return result ;
+}