Set.prototype = new Collection();

/**
 *	A collection that contains no duplicate elements. More formally, sets 
 *	contain no pair of elements e1 and e2 such that e1.equals(e2), and at most 
 *	one null element. As implied by  its name, this interface models the
 *	mathematical set abstraction.
 *	@author			Todd Ditchendorf
 *	@constructor	Instanciates internal private array.
 *	@see			<code>{@link Collection}</code>
 *	@see			<code>{@link Iterator}</code>
 */
function Set(a) {
	if (a && !a instanceof Array)
		throw new Error("IllegalArgumentException: Set's constructor's only " +
								"argument must be an Array object");
	this._array = (a) ? a : new Array();
}

/**
 *	Ensures that this collection contains the specified element
 *	@param	Object o		Add this object.
 *	@return	boolean
 */
Set.prototype.add = function (o) {
	if (this._array.length == 0) {
		this._array[0] = o;
		return true;
	} else {
		for ( var i = 0; i < this._array.length; i++ ) {
			if (this._array[i] == o) {
				return false;
			}
		}
		this._array[this._array.length] = o;
		return true;
	}
};

/**
 *	Adds all of the elements in the specified collection to this Set.
 *	@param	Array a		New Array to be added to this Set.  ??? change this to Collection?
 *	@return	boolean	
 */
Set.prototype.addAll = function (a) {
	if (typeof a != "object")
		throw new Error("IllegalArgumentException: " +
								"Usage: Set.addAll(<Array>);");
	var bool = false;
	outerloop:
	for ( var i = 0; i < a.length; i++ ) {
		innerloop:
		for ( var j = 0; j < this._array.length; j++ ) {
			if ( a[i] == this._array[j] ) {
				continue outerloop;
			}
		}
		this._array[this._array.length] = a[i];
		bool = true;
	}
	return bool;
};

/**
 *	Removes a single instance of the specified element from this  collection, 
 *	if it is present
 *	@param	Object o	Element to be removed from this Set.
 *	@return	boolean
 */
Set.prototype.remove = function (o) {
	var theNum;
	var bool = false;
	var tempArray = new Array();
	for ( var i = 0; i < this._array.length; i++ ) {
		if ( this._array[i] == o ) {
			theNum = i;
			bool = true;
		}
	}
	for ( var i = 0; i < this._array.length; i++ ) {
		if ( i != theNum ) {
			tempArray[tempArray.length] = this._array[i];
		}
	}
	this._array = tempArray;
	return bool;
};

/**
 *	Removes all this collection's elements that are also contained in the 
 *	specified array. 
 *	@param	a		New array to be added to the Set.
 *	@return	boolean
 */
Set.prototype.removeAll = function (a) {
	if (typeof a != "object") {
		throw new Error("IllegalArgumentException: " +
							"Usage: Set.removeAll(<Array>);");
	}
	var bool = false;
	var newArray = new Array();
	outerloop:
	for ( var i = 0; i < a.length; i++ ) {
		innerloop:
		for ( var j = 0; j < this._array.length; j++ ) {
			if ( a[i] == this._array[j] ) {
				this._array.splice(j,1);
				bool = true;
				continue outerloop;
			}
		}
	}
	return bool;
};

Set.prototype.toString = function () {
	return "Set ("+this._array+")";
};
