zoukankan      html  css  js  c++  java
  • js数据结构hashMap -----hashMap

    // 散列表  hashMap
            // 散列函数
            /**
            给定一个key参数,我们就能根据组成key的每个字符的ASCII码值
            的和得到一个数字。所以, 首先需要一个变量来存储这个总和(行{1})。
            然后,遍历key(行{2})并将从ASCII表中查到 的每个字符对应的ASCII值加到
            hash变量中(可以使用JavaScript的String类中的charCodeAt 方法——行{3})。
            最后,返回hash值。为了得到比较小的数值,我们会使用hash值和一个任意 数
            做除法的余数(mod)
            **/
            var loseloseHashCode = function (key) {
                var hash = 0;                          //{1}
                for (var i = 0; i < key.length; i++) { //{2}
                    hash += key.charCodeAt(i);         //{3}
                }
                return hash % 37; //{4}
            };
    
            var djb2HashCode = function (key) {
                var hash = 5381; //{1}
                for (var i = 0; i < key.length; i++) { //{2}
                    hash = hash * 33 + key.charCodeAt(i); //{3}
                }
                return hash % 1013
            }
            function HashTable() {
                var table = [];
                this.put = function (key, value) {
                    var position = djb2HashCode(key); //{5}
                    console.log(position + ' - ' + key); //{6}
                    table[position] = value; //{7}
                };
                this.get = function (key) {
                    return table[djb2HashCode(key)];
                };
                this.remove = function (key) {
                    table[djb2HashCode(key)] = undefined;
                }
                this.print = function () {
                    for (var i = 0; i < table.length; ++i) { //{1}
                        if (table[i] !== undefined) {        //{2}
                            console.log(i + ": " + table[i]);//{3}
                        }
                    }
                };
            }
            var hash = new HashTable();
            hash.put('Gandalf', 'gandalf@email.com');
            hash.put('John', 'johnsnow@email.com');
            hash.put('Tyrion', 'tyrion@email.com');
    
            console.log(hash.get('Gandalf'));
            console.log(hash.get('Loiane'));
            /**
            有时候,一些键会有相同的散列值。不同的值在散列表中对应相同位置的时候,
            我们称其为冲突。例如,我们看看下面的代码会得到怎样的输出结果
            **/
            var hash = new HashTable();
            hash.put('Gandalf', 'gandalf@email.com');
            hash.put('John', 'johnsnow@email.com');
            hash.put('Tyrion', 'tyrion@email.com');
            hash.put('Aaron', 'aaron@email.com');
            hash.put('Donnie', 'donnie@email.com');
            hash.put('Ana', 'ana@email.com');
            hash.put('Jonathan', 'jonathan@email.com');
            hash.put('Jamie', 'jamie@email.com');
            hash.put('Sue', 'sue@email.com');
            hash.put('Mindy', 'mindy@email.com');
            hash.put('Paul', 'paul@email.com');
            hash.put('Nathan', 'nathan@email.com');
            hash.print()
  • 相关阅读:
    《应用Yii1.1和PHP5进行敏捷Web开发》学习笔记(转)
    YII 小模块功能
    Netbeans代码配色主题大搜集
    opensuse 启动巨慢 解决方法 90s多
    opensuse 安装 网易云音乐 rpm netease music
    linux qq rpm deb opensuse
    openSUSE 安装 alien
    第一行代码 Android 第2版
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
  • 原文地址:https://www.cnblogs.com/vali/p/9603132.html
Copyright © 2011-2022 走看看