zoukankan      html  css  js  c++  java
  • JavaScript 散列表(HashTable)

    TypeScript方式实现源码

    // 特性:
    // 散列算法的作用是尽可能快地在数据结构中找到一个值。 在之前的章节中, 你已经知道如果
    // 要在数据结构中获得一个值(使用get方法) ,需要遍历整个数据结构来找到它。如果使用散列
    // 函数,就知道值的具体位置,因此能够快速检索到该值。散列函数的作用是给定一个键值,然后
    // 返回值在表中的地址
    
    //  put(key,value):向散列表增加一个新的项(也能更新散列表)
    //  remove(key):根据键值从散列表中移除值
    //  get(key):返回根据键值检索到的特定的值
    //  loseloseHashCode(key):散列函数
    //  put(key):
     1 /**
     2  * 散列表
     3  * @desc 与Set类相似,ECMAScript 6同样包含了一个Map类的实现,即我们所说的字典
     4  */
     5 class HashTable {
     6     private table = [];
     7     public put(key, value) {
     8         let position = HashTable.loseloseHashCode(key);
     9         console.log('position' + '-' + key);
    10         this.table[position] = value;
    11     }
    12     public remove(key) {
    13         this.table[HashTable.loseloseHashCode(key)] = undefined;
    14     }
    15     public get(key) {
    16         return this.table[HashTable.loseloseHashCode(key)];
    17     }
    18     private static loseloseHashCode(key) {
    19         // 不够完善
    20         // let hash = 0;
    21         // for (let i = 0; i < key.length; i++) {
    22         //     hash += key.charCodeAt(i);
    23         // }
    24         // return hash % 37;
    25         // 更好的实现方式
    26         let hash = 5381;
    27         for (var i = 0; i < key.length; i++) {
    28             hash = hash * 33 + key.charCodeAt(i);
    29         }
    30         return hash % 1013;
    31     }
    32 }
    散列表 HashTable

    JavaScript方式实现源码

     1 /**
     2  * 散列表
     3  * @desc 与Set类相似,ECMAScript 6同样包含了一个Map类的实现,即我们所说的字典
     4  */
     5 var HashTable = (function () {
     6     function HashTable() {
     7         this.table = [];
     8     }
     9     HashTable.prototype.put = function (key, value) {
    10         var position = HashTable.loseloseHashCode(key);
    11         console.log('position' + '-' + key);
    12         this.table[position] = value;
    13     };
    14     HashTable.prototype.remove = function (key) {
    15         this.table[HashTable.loseloseHashCode(key)] = undefined;
    16     };
    17     HashTable.prototype.get = function (key) {
    18         return this.table[HashTable.loseloseHashCode(key)];
    19     };
    20     HashTable.loseloseHashCode = function (key) {
    21         // 不够完善
    22         // let hash = 0;
    23         // for (let i = 0; i < key.length; i++) {
    24         //     hash += key.charCodeAt(i);
    25         // }
    26         // return hash % 37;
    27         // 更好的实现方式
    28         var hash = 5381;
    29         for (var i = 0; i < key.length; i++) {
    30             hash = hash * 33 + key.charCodeAt(i);
    31         }
    32         return hash % 1013;
    33     };
    34     return HashTable;
    35 }());
    散列表 HashTable
  • 相关阅读:
    关于git你日常工作中会用到的一些东西
    require.context
    vue-cli3.0 使用postcss-plugin-px2rem(推荐)和 postcss-pxtorem(postcss-px2rem)自动转换px为rem 的配置方法;
    div实现富文本编辑框
    webpack-bundle-analyzer打包文件分析工具
    web页面调用支付宝支付
    ajax回调中window.open弹出的窗口会被浏览器拦截的解决方法
    Django 文件上传
    Django 序列化 前端通过ajax来获取数据库中的数据
    Django Form组件 基于源码的扩展
  • 原文地址:https://www.cnblogs.com/menu/p/6973110.html
Copyright © 2011-2022 走看看