/*
	FILENAME: jsweld.collections.js
	AUTHOR: Philip Siedow-Thompson (psiedow@co.weld.co.us)
	WRITTEN: 4/2/2009
	REVISIONS: none
	DEPENDENCIES: none
*/


		/* 
			FUNCTION:
			DESCRIPTION:
			ARGUMENTS:
			RETURNS:
		*/

var jsweld;
	jsweld = (jsweld)?jsweld:{};
	jsweld.collections = {};
	
	/*------------------------------------------------------------------------------------
		JSWELD.COLLECTIONS.ORDEREDLIST - A List that is ordered by a comparator
	--------------------------------------------------------------------------------------*/
	jsweld.collections.orderedlist = function()
	{
		var iterator = function(a_array)
		{
			this.cursor = 0;
			this.values = a_array;	
		}
			iterator.prototype.hasNext = function()
			{
				return (this.cursor < this.values.length);	
			}
			iterator.prototype.next = function()
			{
				var retObj = (this.cursor < this.values.length)?this.values[this.cursor]:null;
				this.cursor++;
				
				return retObj;
			}
		
		var orderedlist = function(a_comparator)
		{
			this.comparator = a_comparator;	
			this.values = new Array();
		}
			orderedlist.prototype.addItem = function(a_item)
			{
				var added = false;
				
				for(var i=0; i<this.values.length; i++)
				{
					if(this.comparator(a_item,this.values[i])<0)
					{
						this.values.splice(i,0,a_item);
						added = true;
						break;
					}
				}
				if(!added)this.values.push(a_item);
			}
			orderedlist.prototype.getItem = function(a_index)
			{
				var retObj = null;
				
				if(a_index<this.values.length && a_index>=0)
				{
					retObj = this.values[a_index];
				}
				return retObj;
			}
			orderedlist.prototype.getIterator = function()
			{
				return new iterator(this.values);	
			}
			orderedlist.prototype.getRange = function(a_start, a_end)
			{
				var tArray = new Array();
				
				for(var i=0; i<this.values.length; i++)
				{
					if(this.comparator(a_start,this.values[i])<=0)
					{
						if(this.comparator(a_end,this.values[i])>=0)tArray.push(this.values[i]);
						else break;
					}
				}
				
				return new iterator(tArray);
			}
			orderedlist.prototype.getRangeNum = function(a_start, a_num)
			{
				var tArray = new Array();
				var counter = a_num;
				
				for(var i=0; i<this.values.length; i++)
				{
					if(this.comparator(a_start,this.values[i])<=0)
					{
						if(counter>0)
						{
							tArray.push(this.values[i]);
							counter--;
						}
					}
				}
				
				return new iterator(tArray);	
			}
		return orderedlist;
	}();
	
