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 objects

A 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 Properties
PropertyTypeDefault ValueDescription
entrySetArray

an array of objects. Each object contains a key, and value property.

keysArray

all keys contained in the table

valuesArray

all values contained in the table

Constructor

Defined collections/HashTable.js

clear Function Public


Defined collections/HashTable.js

Clears out all items from the table.

Source
function (){
   this.__map = {};
           
}
    

concat Function Public


Defined collections/HashTable.js

Returns a new HashTable containing the values of this HashTable, and the other table.
DOES NOT CHANGE THE ORIGINAL!

Arguments Returns Source
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");
   }
           
}
    

contains Function Public


Defined collections/HashTable.js

Tests if the table contains a particular key

Arguments Returns Source
function (key){
   var hash = hashFunction(key), ret = false;
   var bucket = null;
   if ((bucket = this.__map[hash]) != null) {
       ret = bucket.find(key) != null;
   }
   return ret;
           
}
    

every Function Public


Defined collections/HashTable.js

Determines if every item meets the condition returned by the callback.

Arguments Returns Source
function (){
   var es = this.__entrySet();
   return es.every.apply(es, arguments);
           
}
    

filter Function Public


Defined collections/HashTable.js

Creates a new HashTable containg values that passed the filtering function.

Arguments Returns Source
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;
           
}
    

forEach Function Public


Defined collections/HashTable.js

Loop through each value in the hashtable

Arguments Source
function (cb,scope){
   var es = this.__entrySet(), l = es.length, f = cb.bind(scope || this);
   es.forEach.apply(es, arguments);
           
}
    

get Function Public


Defined collections/HashTable.js

Get the value corresponding to the key.

Arguments Returns Source
function (key){
   var hash = hashFunction(key), ret = null;
   var bucket = null;
   if ((bucket = this.__map[hash]) != null) {
       ret = bucket.find(key);
   }
   return ret;
           
}
    

map Function Public


Defined collections/HashTable.js

Loop through each value in the hashtable, collecting the value returned by the callback function.

Arguments Returns Source
function (){
   var es = this.__entrySet(), ret = new this._static();
   return es.map.apply(es, arguments);
           
}
    

put Function Public


Defined collections/HashTable.js

Put a key, value pair into the table

NOTE : the collection will not check if the key previously existed.

Arguments Returns Source
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;
           
}
    

reduce Function Public


Defined collections/HashTable.js

Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.

Arguments Source
function (){
   var es = this.__entrySet();
   return es.reduce.apply(es, arguments);
           
}
    

reduceRight Function Public


Defined collections/HashTable.js

Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.

Arguments Source
function (){
   var es = this.__entrySet();
   return es.reduceRight.apply(es, arguments);
           
}
    

remove Function Public


Defined collections/HashTable.js

Remove a key value pair from the table.

Arguments Returns Source
function (key){
   var hash = hashFunction(key), ret = null;
   var bucket = this.__map[hash];
   if (bucket) {
       ret = bucket.remove(key);
   }
   return ret;
           
}
    

set Function Public


Defined collections/HashTable.js

Set the value of a previously existing key,value pair or create a new entry.

Arguments Returns Source
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;
           
}
    

some Function Public


Defined collections/HashTable.js

Determines if some items meet the condition returned by the callback.

Arguments Returns Source
function (){
   var es = this.__entrySet();
   return es.some.apply(es, arguments);
           
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta