JavaScript implementation of dictionary (Dictionary)
Programming Ideas:
- Using the Raw object Datastore to store the elements;
- Two methods to get the dictionary length are implemented, one for variable tracking and one for real-time computation.
Code:
function () {"Use strict";
function Dictionary () {this._size = 0;
This.datastore = Object.create (null);
} Dictionary.prototype.isEmpty = function () {return this._size = = 0;
};
Dictionary.prototype.size = function () {return this._size;
};
Dictionary.prototype.clear = function () {for (var key in This.datastore) {delete This.datastore[key];
} this._size = 0;
};
Dictionary.prototype.add = function (key, value) {This.datastore[key] = value;
this._size++;
};
Dictionary.prototype.find = function (key) {return this.datastore[key];
};
Dictionary.prototype.count = function () {var n = 0;
for (var key in This.datastore) {n++;
} return N;
};
Dictionary.prototype.remove = function (key) {delete This.datastore[key];
this._size--;
}; Dictionary.prototype.showAll = function () {for (var key in This.datastore) {Console.log (key + "->" + this.data
Store[key]);
}
}; Module. exports = Dictionary;
})();
JavaScript implementation of Hash (Hashtable)
Programming Ideas:
- In order to solve the collision by the chain list, and use the single chain table LinkedList (see http://www.jb51.net/article/86394.htm before jb51);
- To store with a bare object;
- Valuepair simple encapsulation key value pairs;
- Organize code in modular mode;
Code:
Valuepair.js
(function () {
"use strict";
function Valuepair (key, value) {
This.key = key;
This.value = value;
}
ValuePair.prototype.toString = function () {return
"[+ This.key +]:" + This.value + "]";
Module.exports = Valuepair;
}) ();
Hashtable.js
(function () {"Use strict";
var Valuepair = require ("./lib/valuepair");
var LinkedList = require ("./linkedlist");
function Hashtable () {this.table = Object.create (null);
this._size = 0;
} Hashtable.prototype.isEmpty = function () {return this._size = = 0;
};
Hashtable.prototype.size = function () {return this._size;
};
Hashtable.prototype.remove = function (key) {var index = hashcode (key);
if (this.table[index] = = null) {return false;
}else{var currnode = This.table[index].gethead ();
while (currnode.next) {currnode = Currnode.next;
if (CurrNode.element.key = = key) {this.table[index].remove (currnode.element);
this._size--;
return true;
return false;
}
};
Hashtable.prototype.get = function (key) {var index = hashcode (key);
if (this.table[index] = = null) {return null;
}else{var currnode = This.table[index].gethead (); while (currnode.next) {currnode = Currnode.next;
if (CurrNode.element.key = = key) {return currnode.element;
} return null;
}
};
Hashtable.prototype.put = function (key, value) {var index = hashcode (key);
if (this.table[index] = = null) {This.table[index] = new LinkedList ();
var currnode = This.table[index].gethead ();
while (Currnode.next) {//key If it already exists, modify the value to the new value Currnode = Currnode.next;
if (CurrNode.element.key = = key) {CurrNode.element.value = value;
Break } if (Currnode.next = = null && currNode.element.value!= value) {//key does not exist, add new value. Note the boundary value this.
Table[index].add (New Valuepair (Key,value));
this._size++;
return to this;
};
Hashtable.prototype.display = function () {for (var key in this.table) {var currnode = This.table[key].gethead ();
while (currnode.next) {currnode = Currnode.next; Console.log (CurrNode.element.toString ());
}
}
}; /*********************** Utility functions ********************************/function hashcode (key) {//Horner algorithm, prime number fetch
Panax notoginseng var hashvalue = 6011;
for (var i = 0; i < key.length i++) {hashvalue = HashValue * Notoginseng + key.charcodeat (i);
return HashValue% 1019;
} module.exports = Hashtable;
})();