<!--
//######################
//# HashTable object
//# Description: A javascript implementation of a hastable
//#              Keys are use to access an list of items.
//# Dated:       11/12/2006 - 11/14/2006
//# Author       (c) Philip Jahmani Chauvet 
//# Notice:      Free to use, download, publish, but keep my name on this code.
//######################
//# Note: Needs the vector.js file
//######################
function HashTable()
{
    this.hashkeys = new Vector();
    this.getHashSize=getHashSize;        
    this.setHash=setHash;
    this.getHashValue=getHashValue;
    this.getHashKeys=getHashKeys;
    this.getHashValues=getHashValues;
    this.removeHash=removeHash;            
}
function getHashSize()
{
    return this.hashkeys.size();
}
function setHash( key, value  )
{
    var hobj = new HashObj( key, value );    
    this.hashkeys.add( hobj );
}
function getHashValue( key )
{
    if ( key == null )
        return null;
        
    for (y=0; y<this.getHashSize(); y++)
    {
        var tmpval = this.hashkeys.get( y );
        //alert( tmpval );
        if ( tmpval != null && tmpval.getHashObjKey() == key )
        {
            return tmpval.getHashObjValue();
        }
    }    
    
    return null;
}
function getHashKeys()
{
    var haskeys = new Array();    
    for (y=0; y<this.getHashSize(); y++)
    {
        var tmpval = this.hashkeys.get( y );
        if ( tmpval != null  )
        {
            haskeys[ y ] = tmpval.getHashObjKey();
        }
    }    
    
    return haskeys;
}
function getHashValues()
{
    var haskeys = new Array();    
    for (y=0; y<this.getHashSize(); y++)
    {
 
       var tmpval = this.hashkeys.get( y );
        if ( tmpval != null  )
        {
            haskeys[ y ] = tmpval.getHashObjValue();
        }
    }    
    
    return haskeys;
}
function removeHash( key )
{
    if ( key == null )
        return null;
        
    for (y=0; y<this.getHashSize(); y++)
    {
        var tmpval = this.hashkeys.get( y );
        if ( tmpval != null && tmpval.getHashObjKey() == key )
        {
            this.hashkeys.remove( y );
            break;
        }
    }    
}

HashTable.prototype.toString=hashtableobjToString;
function hashtableobjToString()
{
   var msg = "HashTable.toString()\n";
       msg += "  hashSize=" + this.getHashSize() + "\n";
       //msg += "hashkeys=" + this.hashkeys + "\n";
       msg += " " + this.hashkeys.show() + "\n";
       ;
   /*
       msg += "  key=" + this.getKey() + "\n";
       */
   
   return msg;
}

//######################
//The hash object
//######################
function HashObj( tmpkey, tmpval )
{
    this.hashobjkey = tmpkey;
    this.hashobjval = tmpval;
    this.getHashObjKey=getHashObjKey;
    this.getHashObjValue=getHashObjValue;
}
function getHashObjKey()
{
    return this.hashobjkey;
}
function getHashObjValue()
{
    return this.hashobjval;
}

HashObj.prototype.toString=hashobjToString;
function hashobjToString()
{
   var msg = "HashObj.toString()\n";
       msg += "  hashobjval=" + this.getHashObjValue() + "\n";

   return msg;
}

function test()
{
    /*
    var hobj = new HashObj( "aa", "valsss" );
    alert( hobj );
    
    var has = new HashTable();
    has.setHash( "key1", "value1"  )
    has.setHash( "key2", "value2"  )
    
    //alert( has );
   
    var key = "key1";
    alert( "Get " + key + " --  " + has.getHashValue( key ) );
    key = "key2";
    alert( "Get " + key + " --  " + has.getHashValue( key ) );
    */
    
    //alert( has.getHashKeys() + " -- " + has.getHashValues() );
}

//#################
//test();
//#################
//-->
