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

      

  • 相关阅读:
    set集合 浅层拷贝会和深层拷贝
    "is"与"=="
    元组和字典
    运算符和列表
    Python 基础语法
    supervisor 安装配置详解
    如何运行vue项目
    过目不忘JS正则表达式
    vue Bus总线
    Robot Framework 环境安装(一)
  • 原文地址:https://www.cnblogs.com/lemon-zhang/p/7804847.html
Copyright © 2011-2022 走看看