﻿/**
 * project Faviamotorsport
 * description Hash Table class file. Source: http://www.mojavelinux.com/articles/javascript_hashes.html
 * version 1.0 (23/09/2009)
 * creationdate 23/09/2009
 * author 1001 Acessos - Telecomunicacoes e Informatica, Lda
 */
  
 /**
  * Class that implements a hashtable in JavaScript.
  */
 function HashTable() {

 	//Private
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
   
	/**
	 * Method responsible for adding an object to the hashtable.
	 * @param {string} in_key The key that identifies the object inside the hashtable.
	 * @param {Object} in_value The object that corresponds to the key.
	 * @return {Object} The object that corresponds to the key.
	 */
	this.add = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			else {
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}
	   
		return tmp_previous;
	}

	/**
	 * Method that gets an object from the hashtable.
	 * @param {string} in_key The key that identifies the object inside the hashtable.
	 * @return {Object} The corresponding object.
	 */
	this.get = function(in_key) {
		return this.items[in_key];
	}

	/**
	 * Method responsible for removing an item from the hashtable.
	 * @param {string} in_key The key that identifies the object inside the hashtable.
	 * @return {Object} The removed object.
	 */
	this.remove = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_previous;
	}

	/**
	 * Method that checks if a given item exists in the hashtable.
	 * @param {string} in_key The key that identifies the object inside the hashtable.
	 * @return {boolean} Boolean value that indicates if the item exists in the hashtable or not.
	 */
	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}
 }
