Monday, November 06, 2006

JavaScript Hashtable Implementation

I needed a hashtable implementation in JavaScript and here's what I’ve come up. It is used like this:

var ht = new Hashtable();
ht.put(“key”, “value”);
var val = ht.get(“key”); // returns null if not found


The implementation is like this:

function Hashtable(){
this.hash = new Array();
this.keys = new Array();

this.location = 0;
}

Hashtable.prototype.hash = null;
Hashtable.prototype.keys = null;
Hashtable.prototype.location = null;

Hashtable.prototype.get = function (key) {
return this.hash[key];
}

Hashtable.prototype.put = function (key, value) {
if (value == null)
return null;

if (this.hash[key] == null)
this.keys[this.keys.length] = key;

this.hash[key] = value;
}

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home