/*  
COPYRIGHT EC-SERVER ALL RIGHT RESERVED
Author : Carl 
$Id: hashutil.js,v 1.1.1.1 2007/09/14 08:49:02 carl Exp $
*/
function hashUtil() {
	this.Storage = new Object();
	this.RevStorage = new Object();
	this.queueSeq = "";
	this.Sep = ";;";
	this.Pointer = 0;
	this.Length=0;
	return this;
}

/**
*  Push key and value in this.Storage and this.RevStorage;

*   @param string key
*   @param string value
*   @return null.
*/  
hashUtil.prototype.put = function(key,value) {
	if(this.hasKey(key)) this.remove(key);
	this.queueSeq = key+this.Sep+this.queueSeq;
	this.Storage[key] = value;
	this.RevStorage[value] = key;
	this.Length++;
}

/**
*  Push key and value in this.Storage and this.RevStorage;

*   @param string key
*   @param string value
*   @return null.
*/  
hashUtil.prototype.push = function(key,value) {
	this.put(key,value);
}

/**
*  check Hash is empty or not .
*
*   @return boolean.
*/  
hashUtil.prototype.isEmpty = function() {
	if(this.Length>0) return false;
	return true;
}

/**
*   Pop the first value of the hash & remove the value from hash .
*
*   @return value.
*/
hashUtil.prototype.pop = function() {
	var l_key = this.getFirstKey();
	var l_val = this.getFirst();
	this.remove(l_key);
	return l_val;
}

/**
*   return the value in the hash at the current hash pointer .
*
*   @return value 
*/
hashUtil.prototype.read = function() {
	
	var ret = this.queueSeq.split(this.Sep);
	if(typeof(ret[this.Pointer])=="undefined") return "";

	var l_key = ret[this.Pointer++];
	var l_val = this.get(l_key);
	return l_val;
}
/**
*   check the read pointer reach end of file or not .
*
*   @return boolean 
*/
hashUtil.prototype.isEOF = function() {
	if(this.Pointer>=this.Length) return true;
	return false;
}

/**
*  get val by special key from this.Storage ;

*  @param string key
*  @return string  val.
*/
hashUtil.prototype.get = function(key) {
	if(typeof this.Storage[key] == "undefined") return null;
	else return this.Storage[key];
}

/**
*  get key by special val from this.RevStorage ;

*   @param string val
*   @return string key
*/
hashUtil.prototype.getKey = function(val) {
	if(typeof this.RevStorage[val] == "undefined") return null;
	return this.RevStorage[val];
}

/**
*   move  special key to this.queueSeq's head ;

*   @param string key
*   @return null.
*/
hashUtil.prototype.mvToFirst = function(key) {
	this.queueSeq = this.queueSeq.replace(key+this.Sep,"");
	this.queueSeq = key+this.Sep+this.queueSeq;
}


/**
*   get value by key which was in the first position of this.queueSeq;

*   @return string val.
*/
hashUtil.prototype.getFirst = function() {
	while(true) {
		var key = this.queueSeq.substring(0,this.queueSeq.indexOf(this.Sep));
		if(typeof this.Storage[key] == "undefined") {
			this.queueSeq = this.queueSeq.substring(this.queueSeq.indexOf(this.Sep));
		}
		return this.Storage[key];
	}
}

/**
*   get key which was in the first position of this.queueSeq;

*   @return string key.
*/
hashUtil.prototype.getFirstKey = function() {
	while(true) {
		var key = this.queueSeq.substring(0,this.queueSeq.indexOf(this.Sep));
//if(key==null) break;
		if(typeof this.Storage[key] == "undefined") break;
		return key;
	}
}

/**
*   check if the key exist in this.Storage;

*   @param string key
*   @return bool
*/
hashUtil.prototype.hasKey = function(key) {
	if(typeof this.Storage[key] == "undefined") return false;
	return true; 
}

/**
*   check if the val exist in this.RevStorage;

*   @param string val
*   @return bool
*/
hashUtil.prototype.hasValue = function(val) {
	if(typeof this.RevStorage[val] == "undefined") return false;
	return true; 
}

/**
* remove special key in this.Storage and reset this.queueSeq and this.RevStorage;

* @param string key
* @return null.
*/
hashUtil.prototype.remove = function(key) {
	this.queueSeq = this.queueSeq.replace(key+this.Sep,"");

	var val = this.Storage[key] ;
	delete this.Storage[key];
	delete this.RevStorage[val];
	this.Length--;
}

/**
*   reset this.RevStorage and RevStorage;

*   @return null.
*/
hashUtil.prototype.reset = function() {
	this.Storage = new Object();
	this.RevStorage = new Object();
	this.queueSeq = "";
	this.Length=0;
	this.Pointer = 0;
	return this;
}
/**
*   reset read pointer 
*   @return null.
*/
hashUtil.prototype.resetPointer = function() {
	this.Pointer=0;
}

