zoukankan      html  css  js  c++  java
  • Hash of js

    function HashTable() {
        this.table = new Array(137);
    };
    HashTable.prototype = {
        constructor: HashTable,
        simpleHash: function(data) {
            var total = 0;
            for (var i = 0; i < data.length; ++i) {
                total += data.charCodeAt(i);
            }
            return total % this.table.length;
        },
        showDistro: function () {
            var n = 0;
            for (var i = 0; i < this.table.length; ++i) {
    
                if (this.table[i][0] != undefined) {
                    console.log(i + ": " + this.table[i].join('|--|'));
                }
            }
        },
        put: function (key, data) {
            var pos = this.betterHash(key);
            var index = 0;
    
            if (this.table[pos][index] == undefined) {
                this.table[pos][index] = [key, data];
                ++index;
            } else {
                while (this.table[pos][index] != undefined) {
                    ++index;
                }
                this.table[pos][index] = [key, data];
            }
        },
        get: function(key){
            var index = 0;
            var pos = this.betterHash(key);
    
            if (this.table[pos][index][0] == key) {
                return this.table[pos][index];
            } else {
                while (this.table[pos][index][0] != key) {
                    ++index;
                }
                return this.table[pos][index];
            }
            return undefined;
        },
        betterHash: function (string, arr) {
            const H = 37;
            var total = 0;
            for (var i = 0; i < string.length; ++i) {
                total += H * total + string.charCodeAt(i);
            }
            total = total % this.table.length;
    
            if (total < 0) {
                total += this.table.length-1;
            }
            return parseInt(total);
        },
        buildChains: function () {
    
            //开链法
            for (var i = 0; i < this.table.length; ++i) {
                this.table[i] = new Array();
            }
        }
    };
    
    //线性探测法
    var values = [];
    
    function put(key, data) {
        var pos = this.betterHash(key);
    
        if (this.table[pos] == undefined) {
            this.table[pos] = key;
            this.values[pos] = data;
        } else {
            while (this.table[pos] != undefined) {
                pos++;
            }
            this.table[pos] = key;
            this.values[pos] = data;
        }
    };
    function get(key) {
        var hash = -1;
        hash = this.betterHash(key);
    
        if (hash > -1) {
            for (var i = hash; this.table[i] != undefined; i++) {
                if (this.table[i] == key) {
                    return this.values[i];
                }
            }
        }
        return undefined;
    }
    

      

  • 相关阅读:
    python--网络通信协议
    python--网络编程之socket
    python--内置函数03
    在Mapper中sql语句字段与实体类属性名字之间的关系
    网站引入QQ登录
    子类继承父类时构造函数的相关问题
    java中字符串比较的问题
    Mybatis中一对多与多对一的配置
    Spring中的依赖注入(1)
    P1618 三连击(升级版)
  • 原文地址:https://www.cnblogs.com/lemon-zhang/p/7804847.html
Copyright © 2011-2022 走看看