ht
ht
is a HashTable implementation in javascript that can be used in both node and the browser.
Installation
npm install ht
Or download the source (minified)
Note ht
depends on declare.js
, extended
, is-extended
, and array-extended
Usage
To create a new HashTable
var Ht = require("ht");
var ht = new Ht();
put(key, value)
Adds a new key value pair to the hash table, ht supports any value as a key.
var key = {}, key2 = {};
ht.put(key, "value1");
ht.put(key2, "value2");
ht.get(key);
ht.get(key2);
If your key contains a hashCode
, function then that will be used to put the key value pair into the hash table.
function Person(first, last){
this.firstName = first;
this.lastName = last;
}
Person.prototype.hashCode = function(){
return this.firstName + this.lastName;
}
ht.put(new Person("bob", "yukon"), "this is bob");
ht.put(new Person("sally", "yukon"), "this is sally");
ht.get(new Person("bob", "yukon"));
get(key)
Get a a value based on the key.
ht.put(new Date(2013, 1, 22), "value1");
ht.put(new Date(2013, 1, 23), "value2");
ht.get(new Date(2013, 1, 22));
ht.get(new Date(2013, 1, 23);
set(key)
Set a a value based on the key.
ht.put(new Date(2013, 1, 22), "value1");
ht.put(new Date(2013, 1, 23), "value2");
ht.get(new Date(2013, 1, 22));
ht.get(new Date(2013, 1, 23);
ht.set(new Date(2013, 1, 22), "new value1");
ht.set(new Date(2013, 1, 23), "new value2");
ht.get(new Date(2013, 1, 22));
ht.get(new Date(2013, 1, 23);
remove(key)
Removes a key value pair.
ht.put(new Date(2013, 1, 22), "value1");
ht.put(new Date(2013, 1, 23), "value2");
ht.get(new Date(2013, 1, 22));
ht.get(new Date(2013, 1, 23);
ht.remove(new Date(2013, 1, 22));
ht.remove(new Date(2013, 1, 23);
ht.get(new Date(2013, 1, 22));
ht.get(new Date(2013, 1, 23);
contains(key)
Returns true or false if the table does or does not contain a given key value pair.
ht.put(new Date(2013, 1, 22), "value1");
ht.put(new Date(2013, 1, 23), "value2");
ht.contains(new Date(2013, 1, 22));
ht.contains(new Date(2013, 1, 23);
concat(hashTable
Concats two hash tables together into a new one.
var ht1 = new Ht(), ht2 = new Ht();
ht1.put(new Date(2013, 1, 22), "value1");
ht1.put(new Date(2013, 1, 23), "value2");
var key = {}, key2 = {};
ht2.put(key, "value1");
ht2.put(key2, "value2");
var ht3 = ht1.concat(ht2);
ht3.contains(key);
ht3.contains(key2);
ht1.contains(new Date(2013, 1, 22));
ht1.contains(new Date(2013, 1, 23));
clear
Clear all items from the hash table
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.clear();
ht.contains("key1");
ht.contains("key2");
keys
Returns an array of all keys in the table.
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.keys();
values
Gets all values in the hash table.
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.values();
entrySet
Returns an array of all key value pairs in the table.
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.entrySet();
isEmpty
Returns true if the table contains any values, false otherwise.
var ht = new Ht();
ht.isEmpty();
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.isEmpty();
Array methods.
Each hash table contains the following array like methods.
Note each method will pass a key and value to the iterator instead of a value and index.
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.forEach(function(key, value){
console.log(key + " : " + value);
});
forEach
filter
map
every
some
reduce
reduceRight