/** * This is a quick class to get around JavaScript's * poor support for associative arrays. Specifically, * without this class, properties of the object would * be included as keys in the associative array. This * class ensures that we deal only with array elements * we added to the array and not any properties of the * container we're using. See Side Bar 1. * * $Horde: incubator/Horde_Form/js/simplehashtable.js,v 1.1 2005/08/18 04:47:46 mwarden Exp $ * * Copyright 2005 Matt Warden <mwarden@gmail.com> * * See the enclosed file COPYING for license information (LGPL). If you * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. * * @author Matt Warden <mwarden@gmail.com> * @access public */ function SimpleHashtable() { this._data = new Object(); this._keys = new Array(); } // end constructor
function SimpleHashtable_count() { return this._keys.length; }
/** * Lisp-style method to call callback on every * key-value pair of the array. * * @since 1.1 * @author Marcus <m-lists@bristav.se> */ function SimpleHashtable_each(callback) { for(var i=0 ; i<this._keys.length; i++) { var key = this._keys[i]; callback(key, this.get(key)); } } // end function SimpleHashtable_each
function SimpleHashtable_getKey(raw) { // python style naming convention, // to avoid conflicts with actual // attributes (see Side Bar 1). return '__'+ raw +'__'; } // end function SimpleHashtable_getKey
function SimpleHashtable_get(nam) { var key = this.getKey(nam); // retreive value if exists, else null var val = (this._data[key]) ? this._data[key] : null; return val; } // end function SimpleHashtable_get
function SimpleHashtable_put(nam, val) { // if missing arg if (!nam) return false; var key = this.getKey(nam); // if key doesn't already exist add // to keys array. var exists = true; if (!this._data[key]) { exists = false; this._keys[this._keys.length] = nam; } // return old value if set, or else null var oldval = exists ? this._data[key] : null; this._data[key] = val; return oldval; } // end function SimpleHashtable_put
function SimpleHashtable_keys() { // return a copy of the array, otherwise // the user of this class could accidentally // modify our array, since we'd be passing // them a reference. return this._keys.slice(0); } // end function SimpleHashtable_keys
function SimpleHashtable_containsKey(nam) { // get() returns null if not found // or if found and is null. In both // conditions, we want to return false. // Otherwise, we want to return true. return (this.get(nam) != null); } // end function SimpleHashtable_containsKey
// creating the class this way is more memory efficient // if we are using multiple instances of this data // structure. SimpleHashtable.prototype.getKey = SimpleHashtable_getKey; SimpleHashtable.prototype.get = SimpleHashtable_get; SimpleHashtable.prototype.put = SimpleHashtable_put; SimpleHashtable.prototype.keys = SimpleHashtable_keys; SimpleHashtable.prototype.count = SimpleHashtable_count; SimpleHashtable.prototype.containsKey = SimpleHashtable_containsKey; SimpleHashtable.prototype.each = SimpleHashtable_each;
|