
<!-- $Id: rcMap.js,v 1.2 2005/12/20 15:47:08 masoud Exp $ -->

/**
 * Title: JS Map Object Library
 * Author: Gautam Aggarwal
 *
 * Prototypes have been used on JS objects as opposed to inner
 * functions purely to save on memory and not for OOP hierarchy.
 *
 **/

/************************* Map Object and related methods ***************/
// Object that stores name value pair of arguments passed in a JS function
function Map() {
    this.length = 0;
    this.elementData = [];
    for (var i = 0; i < arguments.length; i=i+2) {
        if (typeof(arguments[i + 1]) != 'undefined') {
            // sets key values in elementData array
            this.set(arguments[i], arguments[i+1]);
        }
    }
}

// set the value in the array for the given key
// return true if successful else false
Map.prototype.set = function(arrayKey, arrayVal) {
    if (typeof(arrayVal) != 'undefined') {
        if (typeof(this.elementData[arrayKey]) == 'undefined') {
            this.length++;
        }
        return this.elementData[arrayKey] = arrayVal;
    }
    return false;
}

// gets the value in the array given the key
Map.prototype.get = function(arrayKey) {
      return this.elementData[arrayKey];
}

// removes the key and value in the array given the key
// and returns  the removed value
Map.prototype.remove = function(arrayKey) {
    var arrayVal;
    if (typeof(this.elementData[arrayKey]) != 'undefined') {
        arrayVal = this.elementData[arrayKey];
        delete this.elementData[arrayKey];
        this.length--;
    }

    return arrayVal;
}

// returns size of this list
Map.prototype.size = function() {
    return this.length;
}

// returns true if this key is defined in array
Map.prototype.contains = function(arrayKey) {
    return typeof(this.elementData[arrayKey]) != 'undefined';
}

