↑ Grab this Headline Animator

Thursday, November 20, 2008

Implementation of the Hashtable to extend the Array

Array is the low order complexity data fetching technique, use it as a data structure that keeps parameters in a form of key-value pair.It has low memory signature and quick data fetching, we cannot iterate through the array elements using direct key-value interface.

Web applications developed for IE based browsers have the option to use the Microsoft Dictionary object. Massive use of the object may cause high memory signature and low application performance.

This article introduces a possible solution to this problem, a solution which is deployed in real world running applications.

JavaScript Hashtable Object

The idea behind using this object is having the properties of associative Array bundled with low order computational complexity data fetching, by adding new methods and capabilities.

Let's start by defining the class members:

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

We have defined 3 member variables:

hash - An associative Array that holds the data.
keys - A standard Array that holds the Array keys. We will use it when we need to iterate on our list of values.
location - Points at the current value.
After defining the class member variables we define the class constructor:

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

this.location = 0;

Clear and simple. We initialize our two arrays and our pointer.

After finishing with the definitions part we can start coding...

The first two function that we implement are get and put. These are the main functions for data storage and retrieval.

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;

The get function simply returns the value from the Array by the supplied key, while put is required for a little more work. It initially checks whether the value equals to null (if so the process stops). Then the function checks whether this key is already defined. If so the value is replaced with the new argument supplied. Otherwise, a new key is generated and the value is stored accordingly.

Another set of functions let us iterate thru the Array. Instead of introducing the code here we will show how we can use it:

//declare an instance
var items = new Hashtable();

//add 3 values to the hash
items.put("key1", "value1");
items.put("key2", "value2");
items.put("key3", "value3");

//just show that all works well

//start iterating the hash
//Just to be on the safe side (maybe someone has used it before?)
while ({ //While we have more elements
//print the key and the value
alert (items.getKey() + " = " + items.getValue());

The JavaScript Hashtable Object has some more interesting methods:

add - Merges two Hashtables
toString - Returns a textual representation of the Object (mainly for debugging)
getKeyOfValue - Returns a list of keys that hold a certain value

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles