/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Viewer *| (C) Copyright IBM Corp. 2001, 2011 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ // // This class implement a generic Dictionary data structure. // var DICTIONARY_INVALID_KEY = -1; var DICTIONARY_SUCCESS = 1; /* Constructor Parameters: Returns: */ function CDictionary() { // CDictionary uses an array to store key/value pairs. this.m_aValues = {}; } /* Adds a key/value pair in the dictionary. If the key was used before, its value will be overwrite. Parameters: key (string) - The key to look for in the dictionary. value (object) - The value associated to the key Returns: */ function CDictionary_add(sKey, oValue) { if (typeof sKey != "string" && typeof sKey != "number") { return DICTIONARY_INVALID_KEY; } this.m_aValues[sKey] = oValue; return DICTIONARY_SUCCESS; } /* Returns true if the keys is already used. Parameters: key (string) - The key to look for in the dictionary. Returns: a boolean. */ function CDictionary_exists(sKey) { if (typeof sKey != "string" && typeof sKey != "number") { return false; } return (typeof this.m_aValues[sKey] != "undefined"); } /* Returns the associated to a key in the dictionary. Returns null if the key doesn't exists. Parameters: key (string) - the key to look for Returns: an object, associated to the key. */ function CDictionary_get(sKey) { if (typeof sKey != "string" && typeof sKey != "number") { return null; } if (this.exists(sKey) === true) { return this.m_aValues[sKey]; } else { return null; } } /* Returns all keys used in this dictionary. Ordered alphabetically. Returns an empty array if the dictionary is empty. Parameters: Returns: a sorted array of strings. */ function CDictionary_keys() { var aKeys = []; for (var idxValue in this.m_aValues) { aKeys.push(idxValue); } return aKeys.sort(); } /* Remove a key/value pair from this dictionary. Parameters: key (string) to remove from the dictionary. Returns: Returns the object removed. Null if the key didn't exists. */ function CDictionary_remove(sKey) { if (typeof sKey != "string" && typeof sKey != "number") { return DICTIONARY_INVALID_KEY; } var oRetVal = this.get(sKey); delete this.m_aValues[sKey]; return oRetVal; } /* Removes all key/value pairs from this dictionary. Parameters: Returns: */ function CDictionary_removeAll() { this.m_aValues = []; return DICTIONARY_SUCCESS; } /* Adds all of the entries from parameter oNewDictionary into this dictionary. Any duplicate values already in this dictionary will be overwritten by the new values in the new dictionary. Parameters: oNewDictionay (object - CDictionary) a new dictionary of >=1 length. Returns: */ function CDictionary_append(oNewDictionary) { if (oNewDictionary instanceof CDictionary && oNewDictionary.keys().length > 0) { var aKeys = oNewDictionary.keys(); for (var idxKeys = 0; idxKeys < aKeys.length; idxKeys++) { this.add(aKeys[idxKeys], oNewDictionary.get(aKeys[idxKeys])); } } } // Add functions to the CDictionary class CDictionary.prototype.add = CDictionary_add; CDictionary.prototype.exists = CDictionary_exists; CDictionary.prototype.get = CDictionary_get; CDictionary.prototype.keys = CDictionary_keys; CDictionary.prototype.remove = CDictionary_remove; CDictionary.prototype.removeAll = CDictionary_removeAll; CDictionary.prototype.append = CDictionary_append;