Implementation of a HashTable for javascript. This HashTable implementation allows one to use anything as a key.
NOTE: THIS IS ~ 3 times slower than javascript native objectsA use case for this collection is when one needs to store items in which the key will not be a string, or number
Extends Instance PropertiesProperty | Type | Default Value | Description |
entrySet | Array | an array of objects. Each object contains a key, and value property. | |
keys | Array | all keys contained in the table | |
values | Array | all values contained in the table | |
Clears out all items from the table.
Sourcefunction (){ this.__map = {}; }
Returns a new HashTable containing the values of this HashTable, and the other table. DOES NOT CHANGE THE ORIGINAL!
Argumentsthe hash table to concat with this.
comb.collections.HashTable
a new HashTable containing all values from both tables.
function (hashTable){ if (hashTable instanceof this._static) { var ret = new this._static(); var otherEntrySet = hashTable.entrySet.concat(this.entrySet); for (var i = otherEntrySet.length - 1; i >= 0; i--) { var e = otherEntrySet[i]; ret.put(e.key, e.value); } return ret; } else { throw new TypeError("When joining hashtables the joining arg must be a HashTable"); } }
Tests if the table contains a particular key
Argumentsthe key to test
Boolean
true if it exitsts false otherwise.
function (key){ var hash = hashFunction(key), ret = false; var bucket = null; if ((bucket = this.__map[hash]) != null) { ret = bucket.find(key) != null; } return ret; }
Determines if every item meets the condition returned by the callback.
ArgumentsFunction
: Function to callback with each item, the first aruguments is an object containing a key and value field
this
] Object
: scope to call the function in
Boolean
True if every item passed false otherwise
function (){ var es = this.__entrySet(); return es.every.apply(es, arguments); }
Creates a new HashTable containg values that passed the filtering function.
ArgumentsFunction to callback with each item, the first aruguments is an object containing a key and value field
the scope to call the function.
comb.collections.HashTable
the HashTable containing the values that passed the filter.
function (cb,scope){ var es = this.__entrySet(), ret = new this._static(); es = es.filter.apply(es, arguments); for (var i = es.length - 1; i >= 0; i--) { var e = es[i]; ret.put(e.key, e.value); } return ret; }
Loop through each value in the hashtable
Argumentsthe function to call with an object containing a key and value field
the scope to call the funciton in
function (cb,scope){ var es = this.__entrySet(), l = es.length, f = cb.bind(scope || this); es.forEach.apply(es, arguments); }
Get the value corresponding to the key.
Argumentsthe key used to look up the value
null if not found, or the value.
function (key){ var hash = hashFunction(key), ret = null; var bucket = null; if ((bucket = this.__map[hash]) != null) { ret = bucket.find(key); } return ret; }
Loop through each value in the hashtable, collecting the value returned by the callback function.
ArgumentsFunction
: Function to callback with each item, the first aruguments is an object containing a key and value field
this
] Object
: scope to call the function in
Array
an array containing the mapped values.
function (){ var es = this.__entrySet(), ret = new this._static(); return es.map.apply(es, arguments); }
Put a key, value pair into the table
NOTE : the collection will not check if the key previously existed.
Argumentsthe key to look up the object.
the value that corresponds to the key.
the value
function (key,value){ var hash = hashFunction(key); var bucket = null; if ((bucket = this.__map[hash]) == null) { bucket = (this.__map[hash] = new Bucket()); } bucket.pushValue(key, value); return value; }
Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
ArgumentsFunction
: Function to execute on each value in the array.
Value to use as the first argument to the first call of the callback..
function (){ var es = this.__entrySet(); return es.reduce.apply(es, arguments); }
Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
ArgumentsFunction
: Function to execute on each value in the array.
Value to use as the first argument to the first call of the callback..
function (){ var es = this.__entrySet(); return es.reduceRight.apply(es, arguments); }
Remove a key value pair from the table.
Argumentsthe key of the key value pair to remove.
the removed value.
function (key){ var hash = hashFunction(key), ret = null; var bucket = this.__map[hash]; if (bucket) { ret = bucket.remove(key); } return ret; }
Set the value of a previously existing key,value pair or create a new entry.
Argumentsthe key to be be used
the value to be set
the value.
function (key,value){ var hash = hashFunction(key), ret = null, bucket = null, map = this.__map; if ((bucket = map[hash]) != null) { ret = bucket.set(key, value); } else { ret = (map[hash] = new Bucket()).pushValue(key, value); } return ret; }
Determines if some items meet the condition returned by the callback.
ArgumentsFunction
: Function to callback with each item, the first aruguments is an object containing a key and value field
this
] Object
: scope to call the function in
Boolean
True if some items passed false otherwise
function (){ var es = this.__entrySet(); return es.some.apply(es, arguments); }
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git