Example of dictionary and hash table structure for implementing key values in JavaScript

  • 2021-06-28 10:08:34
  • OfStack

javascript implementation of dictionary (Dictionary)
Programming ideas:

Element storage using the bare object datastore; Two methods to get dictionary length are implemented, one is variable tracking and the other is real-time calculation.

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 ideas:

Chain list to solve the collision, and use the self-written single-chain list library LinkedList (see https://www.ofstack.com/article/86394.htm before jb51); Store with bare objects; 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 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 value Value is new 
      currNode = currNode.next;
      if(currNode.element.key == key){
        currNode.element.value = value;
        break;
      }
    }

    if(currNode.next == null && currNode.element.value != value){         //key Non-existent , Add a new value . Notice 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) {        // Horner algorithm , Prime number extraction 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 articles: