Created
September 19, 2014 17:18
-
-
Save cdosborn/fe9111bc0f0b3ef627f8 to your computer and use it in GitHub Desktop.
A simple hash table implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Map() { | |
var dict = {}; | |
return { | |
add_key_value_pair: function(key, value) { | |
dict[key] = value; | |
}, | |
get_value: function(key) { | |
var val = dict[key]; | |
if (val === undefined) { | |
throw new Error("Key not found >:|"); | |
} | |
return val; | |
}, | |
remove_key: function(key) { | |
if (dict[key] === undefined) { | |
throw new Error("Key not found >:|"); | |
} else { | |
delete dict[key]; | |
} | |
} | |
} | |
} | |
function MapList() { | |
var list = []; | |
return { | |
add_key_value_pair: function(key, value) { | |
var next = [key, value]; | |
list.push(next); | |
}, | |
get_value: function(key) { | |
var found = list.filter(function(elem) { | |
return elem[0] === key; | |
}); | |
if (found.length === 0) { | |
throw "Key not found >:|"; | |
} | |
return found[0][1]; | |
}, | |
remove_key: function(key) { | |
var found = list.filter(function(elem) { | |
return elem[0] !== key; | |
}); | |
if (found.length === list.length) { | |
throw "Key not found >:|"; | |
} | |
list = found; | |
} | |
} | |
} | |
//var m = MapList(); | |
//m.add_key_value_pair("0", "apple"); | |
//m.add_key_value_pair("1", "mango"); | |
//var test1 = m.get_value("0") === "apple"; | |
//var test2; | |
//try { | |
// m.add_key_value_pair("4", "apple"); | |
// m.remove_key("4"); | |
// m.get_value("4"); | |
// test2 = false; | |
//} catch (e) { | |
// test2 = true; | |
//} | |
// | |
//console.log( "test1: " + test1 + "\ntest2: " + test2 ); | |
function Tree(key,value) { | |
var d = [key, value]; | |
var left = null; | |
var right = null; | |
return { | |
add: function(key, value) { | |
var k = d[0], v = d[1]; | |
if (key > k) { | |
if (right === null) | |
right = Tree(key, value); | |
else | |
right.add(key,value); | |
} else if (key < k) { | |
if (left === null) | |
left = Tree(key, value); | |
else | |
left.add(key,value); | |
} else { | |
d = [key, value]; | |
} | |
}, | |
get: function(key) { | |
var k = d[0], v = d[1]; | |
if (key === k) { | |
return v; | |
} else if (key > k) { | |
if (right === null) { | |
throw "Key not found >:|" | |
} else { | |
return right.get(key); | |
} | |
} else { | |
if (left === null) { | |
throw "Key not found >:|" | |
} else { | |
return left.get(key); | |
} | |
} | |
}, | |
remove: function(key) { | |
throw "Psych"; | |
} | |
} | |
} | |
function MapTree() { | |
var bst = null; | |
return { | |
add_key_value_pair: function(key, value) { | |
if (bst === null) | |
bst = Tree(key, value); | |
else | |
bst.add(key,value); | |
}, | |
get_value: function(key) { | |
return bst.get(key); | |
}, | |
remove_key: function(key) { | |
bst.remove(key); | |
} | |
} | |
} | |
(function(n) { | |
var m = Map(); | |
var mL = MapList(); | |
var mT = MapTree(); | |
console.log("Adding key value pairs.."); | |
for(var i = 0; i < n; i++) { | |
m.add_key_value_pair(i, "cool"); | |
mL.add_key_value_pair(i, "cool"); | |
mT.add_key_value_pair(i, "cool"); | |
} | |
console.log("Timing map.."); | |
var mStart = new Date(); | |
for(var i = 0; i < n; i++) { | |
m.get_value(i); | |
} | |
var mEnd = new Date(); | |
var mDiff = mEnd - mStart;; | |
console.log("Timing map list.."); | |
var mLStart = new Date(); | |
for(var i = 0; i < n; i++) { | |
mL.get_value(i); | |
} | |
var mLEnd = new Date(); | |
var mLDiff = mLEnd - mLStart; | |
console.log("Timing map tree.."); | |
var mTStart = new Date(); | |
for(var i = 0; i < n; i++) { | |
mT.get_value(i); | |
} | |
var mTEnd = new Date(); | |
var mTDiff = mTEnd - mTStart; | |
console.log("Map lookup: " + mDiff + "ms"); | |
console.log("MapList lookup: " + mLDiff + "ms"); | |
console.log("MapTree lookup: " + mTDiff + "ms"); | |
console.log("cool"); | |
})(10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment