[Slashhack-cvs] slashhack/resources/js/jslib/ds chainDictionary.js,NONE,1.1 dictionary.js,NONE,1.1
Brought to you by:
fletch
|
From: Dave F. <fl...@us...> - 2004-10-21 03:49:24
|
Update of /cvsroot/slashhack/slashhack/resources/js/jslib/ds In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4907/ds Added Files: chainDictionary.js dictionary.js Log Message: Task #106454 - include jslib. --- NEW FILE: dictionary.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) *************************************/ if (typeof(JS_LIB_LOADED)=='boolean') { const JS_DICTIONARY_FILE = "dictionary.js"; const JS_DICTIONARY_LOADED = true; function Parameter(k, v) { this.key = k; this.value = v; } Parameter.prototype = { key:null, value:null } //stores a set of keys and associated values function Dictionary() { this._array = new Array(); } Dictionary.prototype = { _array: null, _iterind: 0, //if key exists, will replace current value with value arg put: function(key,value) { if( key ==null || value == null ) return this; var ind = -1; for(var i = 0; i < this._array.length; i++ ) if( this._array[i].key == key ) { ind = i; break; } if( ind == -1 ) { var p = new Parameter(key,value); this._array.push(p); } else { this._array[ind].value = value; } return this; }, get: function(key) { for(var i = 0; i < this._array.length; i++ ) if( this._array[i].key == key ) return this._array[i].value; return null; }, remove: function(key) { for(var i = 0; i < this._array.length; i++ ) if( this._array[i].key == key ) { this._array.splice(i,1); } }, keys: function() { var list = new Array(); for(var i = 0; i < this._array.length; i++ ) list.push(this._array[i].key); return list; }, //checks if dict has a key, and if it does, sets value to //the value in dict hasKey: function(key, value) { value = null; for(var i = 0; i < this._array.length; i++ ) if( this._array[i].key == key ) { value = this._array[i].value; return true; } return false; }, get size() { return _array.length; }, //object related toString: function() { return "Array :" + _array.length; }, //iterator related //iterates over each Parameter resetIterator: function() { this._iterind = 0; }, hasMoreElements: function() { if( this._iterind < this._array.length ) return true; else return false; }, next: function(key, value) { return this._array[this._iterind++]; } } jslibDebug('*** load: '+JS_DICTIONARY_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_dictionary);\n\n"); }; --- NEW FILE: chainDictionary.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) *************************************/ //string keys and values if (typeof(JS_LIB_LOADED)=='boolean') { if (typeof(JS_DICTIONARY_LOADED)!='boolean') include(jslib_ds_dictionary); const JS_CHAINDICTIONARY_FILE = "chainDictionary.js"; const JS_CHAINDICTIONARY_LOADED = true; function ChainDictionary() { this._default = new Dictionary(); this._chained = new Array(); this.put(_default); //add the default to the chain as well } ChainDictionary.prototype = { _default: null, _chained: null, //iterator related _chainind: null, //if key exists, will replace current value with value arg put: function(key,value) { if( key == null || value == null) return this; for( var i = 0; i < this._chained.size(); i++ ) { if( this._chained.hasKey(key) ) { this._chained[i].put(key, value); dictind = i; break; } } if( dictind == -1 ) { this._default.put(key, value); } return this; }, put: function(dictionary) { if( dictionary == null ) return this; this._chained.push(dictionary); return this; } get: function(key) { for( var i = 0; i < this._chained.size(); i++ ) { var value = this._chained[i].get(key); if( value ) return value; } return null; }, remove: function(key) { for(var i = 0; i < this._chained.size(); i++ ) remove(key); }, keys: function() { var list = new Array(); for(var i = 0; i < this._chained.length; i++ ) list.concat(this._chained.keys()); return list; }, toString: function() { var size; for(var i = 0; i < this._chained.length; i++ ) size += this._chained.size(); return "Array :" + size; }, get size() { var size; for(var i = 0; i < this._chained.length; i++ ) size += this._chained.size(); return size; }, //iterator //iterates over each Parameter resetIterator: function() { this._chainind = 0; for(var i = 0; i < this._chained.length; i++ ) this._chained[i].resetIterator(); }, hasMoreElements: function() { //if there are more dicts after the current in the chain if ( this._chainind < this._chained.length -1 ) return true; //if there are more elements in the current dict else return this._chained[_chainind].hasMoreElements(); }, next: function() { if( !this._chained[_chainind].hasMoreElements() ) this._chainind++; return this._chained[_chainin].getNext(); } } jslibDebug('*** load: '+JS_CHAINDICTIONARY_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_chaindictionary);\n\n"); }; |