• 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;
    }
    

      

  • 相关阅读:
    grafan+cadvisor+prometheus监控docker
    容器化tomcat9.0
    Vue的四种特殊attribute:is key ref v-slot (更新中)
    properties 和 attributes的区别
    vue父子组件(更新中)
    发布者订阅者模式(vue双向绑定原理)
    开发中一些好的逻辑;
    vue项目根据不同环境调用不同请求地址(2)
    linux上删除文件名乱码的文件
    Oracle的SQL优化
  • 原文地址:https://www.cnblogs.com/lemon-zhang/p/7804847.html
Copyright © 2020-2023  润新知