散列(hashtable)的javascript實現

原由

最近在看《數據結構與算法--javascript描述》,而後上npmjs.org去搜索,想找合適的庫參考並記錄下來,以備之後用時能拿來即用,最沒有發現很合本身意的,因而就決定本身一一實現出來。javascript

npmjs相關庫

  • hashtable 以C++插件方式實現;java

  • simple-hashtable 代碼風格很是值得借鑑,以單鏈表實現開鏈法來解決碰撞。git

編程思路

  • 以鏈表來解決實現開鏈法來解決碰撞,並使用本身寫的單鏈表庫LinkedList;github

  • 用裸對象來存儲;算法

  • ValuePair簡單封裝鍵值對;npm

  • 以模塊模式組織代碼;編程

本身的實現

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.jsui

(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若已經存在,修改value值爲新值
            currNode = currNode.next;
            if(currNode.element.key == key){
                currNode.element.value = value;
                break;
            }
        }

        if(currNode.next == null && currNode.element.value != value){                  //key不存在,加入新值.注意邊界值
            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) {                //霍納算法,質數取37
        var hashValue = 6011;
        for (var i = 0; i < key.length; i++) {
            hashValue = hashValue * 37 + key.charCodeAt(i);
        }
        return hashValue % 1019;
    }

    module.exports = Hashtable;

})();

源代碼地址

https://github.com/zhoutk/js-data-struct
http://git.oschina.net/zhoutk/jsDataStructs
相關文章
相關標籤/搜索