2
* <p>Implements a sortable hashtable which accepts non-<code>null</code> String
3
* keys and non-<code>null</code> values.</p>
5
* <p>This object is primarily intended to address the shortcomings of using an
6
* Object as a hashtable, by managing the names of the properties which are
7
* stored in an Object.</p>
9
* <p>HashTable does not define a hashing function, as Object's built-in
10
* hashing is used for storing and retrieving items.</p>
12
* <p>Initial values are taken from any Objects passed into the constructor:
13
* if another HashTable is given as an argument, its contents will be taken.</p>
17
var HashTable = function()
20
* Storage object - values are stored as properties whose names are hashtable
29
* A list of hashtable keys.
36
for (var i = 0; i < arguments.length; i++)
38
this._putFromObject(arguments[i]);
43
* Puts all properties from a given HashTable or Object into the hashtable.
45
* @param {Object} source an object whose values will be put into the hashtable.
48
HashTable.prototype._putFromObject = function(source)
50
if (source.constructor == HashTable)
52
source.forEach(function(k, v)
59
for (property in source)
61
if (source.hasOwnProperty(property))
63
this.put(property, source[property]);
70
* Clears the hashtable.
72
HashTable.prototype.clear = function()
79
* An iterator which calls the given function, passing key, value, item index and a
80
* reference to this Hashtable for each item in the hashtable.
82
* @param {Function} callback the function to be called for each item.
83
* @param {Object} context an optional context object for the calls to block.
85
* @see "Enumerating Javascript Objects", http://dean.edwards.name/weblog/2006/07/enum/
87
HashTable.prototype.forEach = function(callback, context)
89
for (var i = 0, l = this._keys.length; i < l; i++)
91
callback.call(context, this._keys[i], this._store[this._keys[i]], i, this);
96
* Retrieves the item with the given key.
98
* @param {String} key the key for the item to be retrieved.
99
* @return the item stored in this HashTable with the given key if one exists,
100
* <code>null</code> otherwise.
102
HashTable.prototype.get = function(key)
105
if (typeof(this._store[key]) != "undefined")
107
result = this._store[key];
113
* Determines if the hashtable contains the given key.
115
* @param {String} key the key to be searched for.
116
* @return <code>true</code> if this HashTable contains the given key,
117
* <code>false</code> otherwise.
120
HashTable.prototype.hasKey = function(key)
123
this.forEach(function(k)
135
* Determines if the hashtable contains the given value.
137
* @param {Object} value the value to be searched for.
138
* @return <code>true</code> if this HashTable contains the given value,
139
* <code>false</code> otherwise.
142
HashTable.prototype.hasValue = function(value)
145
this.forEach(function(k, v)
157
* Creates Object representations of the items in the hashtable.
159
* @return the items in the hashtable, represented as Objects with "key"
160
* and "value" properties.
163
HashTable.prototype.items = function()
166
this.forEach(function(k, v)
168
items.push({"key": k, "value": v});
174
* Retrieves the hashtable's keys.
176
* @return the hashtable's keys.
179
HashTable.prototype.keys = function()
182
this.forEach(function(key)
190
* Puts an item into the hashtable.
192
* @param {String} key the key under which the item should be stored.
193
* @param {Object} value the item to be stored.
195
HashTable.prototype.put = function(key, value)
197
if (key == undefined || key == null || typeof(key) != "string"
198
|| value == undefined || value == null)
203
if (typeof(this._store[key]) == "undefined")
205
this._keys.push(key);
208
this._store[key] = value;
212
* Removes an item from the hashtable and returns it.
214
* @param {String} key the key for the item to be removed.
215
* @return the item which was removed, or <code>null</code> if the item did not
218
HashTable.prototype.remove = function(key)
221
for (var i = 0, l = this._keys.length; i < l; i++)
223
if (key == this._keys[i])
225
result = this._store[key];
226
delete(this._store[key]);
227
this._keys.splice(i, 1);
235
* Determines the number of entries in the hashtable.
237
* @return the number of entries in this HashTable.
240
HashTable.prototype.size = function()
242
return this._keys.length;
246
* Sorts the keys of the hashtable.
248
* @param {Function} comparator an optional function which will be used to sort
249
* the keys - if not provided, they will be sorted
250
* lexographically (in dictionary order).
252
HashTable.prototype.sort = function(comparator)
254
if (typeof(comparator) == "function")
256
this._keys.sort(comparator);
265
* Creates a String representation of the hashtable.
267
* @return a String representation of this {@link HashTable}.
270
HashTable.prototype.toString = function()
273
this.forEach(function(key, value, index)
279
result += key + ": " + value;
286
* Updates the hashtable with the values contained in a given {@link HashTable}
289
* @param {Object} source an object whose values will be put into the hashtable.
291
HashTable.prototype.update = function(source)
293
this._putFromObject(source);
297
* Retrieves the hashtable's values.
299
* @return the hashtable's values.
302
HashTable.prototype.values = function()
305
this.forEach(function(key, value)
b'\\ No newline at end of file'