hasan's blog (বল্গ)

work for fun!!!

JsHashMap

with one comment

I have tried to implement one JavaScript based Collection object with iterator……
Here is my code snaps….

JsHashMap.js


/////////////////////////////////////////////
///// Js Object //////
///////////////////////////////////////////
// Author: NHM TAnveer Hossain Khan (Hasan)
// http://hasan.we4tech.com
// mail:admin@we4tech.com

// hashmap internal data object
JsObject=function(key, value) {
this._key=key;
this._value=value;
}

// set some methods for JsObject
JsObject.prototype.getKey=function() {
return this._key;
}

// get value
JsObject.prototype.getValue=function() {
return this._value;
}

/////////////////////////////////////////////
//// Iterator ///////
///////////////////////////////////////////
JsIterator=function(array) {
// set internal array
this._array=array;

// create inernal index counter
this._counter=0;

// set _hasNext value
if(array.length>0)
this._hasNext=true;
else
this._hasNext=false;
}

// return boolean value
JsIterator.prototype.hasNext=function() {
return this._hasNext;
}

// return object in next method
JsIterator.prototype.next=function() {
if(this._array.length>this._counter) {
// get object
var rtnObj=this._array[this._counter];
// increment counter value;
this._counter++;
// check is has next true of flase
if(this._array.length>this._counter)
this._hasNext=true;
else
this._hasNext=false;

// return data
return rtnObj;
}
else {
this._hasNext=false;
}
}
// remove object
JsIterator.prototype.remove=function() {
this._array.splice(this._counter,1);
if(this._array.length > this._counter)
this._hasNext=false;

}

/////////////////////////////////////////////
//// HashMap Object ///////
///////////////////////////////////////////

// create JsHashMap class object
JsHashMap=function() {

// init. internal array
this._array=new Array();
// set internal counter value as 0
// this counter will keep track the current index
// of array
this._counter=0;
}

// create add method
// put key and value
JsHashMap.prototype.put=function(key, value) {
// add new value
var newJsObj=new JsObject(key, value);
// add in internal array
this._array[this._counter]=newJsObj;
// increment the internal index counter
this._counter++;
}

// retrive data based on iterator
JsHashMap.prototype.iterator=function() {
// create iterator
var it=new JsIterator(this._array);
// return iterator
return it;
}

// retrive data based on keyword
JsHashMap.prototype.get=function(key) {
// create iterator object
var it=this.iterator();

// iterate untile get success
while(it.hasNext()) {
// fetch object
var getObj=it.next();

// check is found or not
if(getObj.getKey()==key)
return getObj.getValue();
}
}

// remove key and object
JsHashMap.prototype.remove=function(key) {

}

Example

//———————————-
// Usages
//———————————-
var hash=new JsHashMap();
hash.put(“key1″,”my_value1″);
hash.put(“key2″,”my_value2″);
hash.put(“key3″,”my_value3″);
hash.put(“key4″,”my_value4″);

// retrive iterator
var it=hash.iterator();

alert(“FIRST”);
/// check iterator
while(it.hasNext()) {
var getObj=it.next();
alert(getObj.getKey()+”:”+getObj.getValue());
}
alert(“SECOND”);
/// check iterator
while(it.hasNext()) {
it.remove();
}
alert(“THIRD”);
/// check iterator
while(it.hasNext()) {
var getObj=it.next();
alert(getObj.getKey()+”:”+getObj.getValue());
}
// get single object
alert(“KEY2: “+hash.get(“key2″));
alert(“KEY3: “+hash.get(“key3″));

Thank you……

Written by nhm tanveer hossain khan

June 29, 2005 at 10:43 pm

Posted in Other

One Response

Subscribe to comments with RSS.

  1. Some friends told me about this site, and now i’m glad they told me about it. an article here on DesktopLinux.com: http://news.linux.com/news/05/10/10/0213220.shtml?tid=96 , I want to achieve it by not dying! , A false friend and a shadow attend

    Jordan Moore

    October 23, 2005 at 10:51 am


Leave a Reply