Examples of dictionary and hash table structures that correspond to key values in JavaScript, javascript

Source: Internet
Author: User

Examples of dictionary and hash table structures that correspond to key values in JavaScript, javascript

Dictionary javascript implementation
Programming logic:

  • The raw object datastore is used for element storage;
  • Two methods are implemented to get the dictionary length, one is variable tracking and the other is real-time computing.

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.datastore[key]);    }  };  module.exports = Dictionary;})();

Javascript Implementation of Hash (hashtable)
Programming logic:

  • The chain table to solve the implementation of the Chain Method to Solve the collision, and use their own single-chain table library listing (see jb51 before the http://www.bkjia.com/article/86394.htm );
  • Use bare objects for storage;
  • ValuePair simply encapsulates key-value pairs;
  • Organize code in module 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 sort list = require (". /Optional List "); 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. tabl E [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. ta Ble [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 partition list ();} var currNode = this. table [index]. getHead (); while (currNode. next) {// if the key already exists, change the value to the new value: currNode = curr Node. next; if (currNode. element. key = key) {currNode. element. value = value; break;} if (currNode. next = null & currNode. element. value! = Value) {// The key does not exist. Add a new value. note the boundary value this. table [index]. add (new ValuePair (key, value); this. _ size ++;} return 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) {// Horna algorithm, prime number: 37 var hashValue = 6011; for (var I = 0; I <key. length; I ++) {hashValue = hashValue * 37 + key. charCodeAt (I);} return hashValue % 1019;} module. exports = Hashtable ;})();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.