详解es6Map实现原理及实现自己的Map方法

		//Map
        //1.不重复
        //2.字符串 对象 NaN null [] function(){} 10
        //3.set get delete has clear方法

        function myMap() {
            this.bucketLength = 8;
            this.init();
        }

        myMap.prototype.init = function () {
            // 初始化 桶 8
            this.bucket = new Array(this.bucketLength);
            for (var i = 0; i < this.bucket.length; i++) {
                this.bucket[i] = {
                    type: 'bucket_' + i,
                    next: null
                }
            }
        }
        // 
        // 1. [0, 8)
        // 2. 重复算值固定
        myMap.prototype.makeHash = function (key) {
            let hash = 0;
            // string   
            if (typeof key !== 'string') {
                if (typeof key == 'number') {
                    //number NaN 
                    hash = Object.is(key, NaN) ? 0 : key;
                } else if (typeof key == 'object') {
                    // null {} []
                    hash = 1;
                } else if (typeof key == 'boolean') {
                    // true false boolean
                    hash = Number(key);
                } else {
                    // undefined  function(){}
                    hash = 2;
                }
            } else {
                // string
                // 'a' 'ab' 'asdasdadasda';
                // 长度大于等于3 前三个字符 ascii 累加 
                for (let i = 0; i < 3; i++) {
                    // key[]
                    hash += key[i] ? key[i].charCodeAt(0) : 0;
                }
            }
            return hash % 8;
        }

        myMap.prototype.set = function (key, value) {
            let hash = this.makeHash(key);
            let oTempBucket = this.bucket[hash];
            while (oTempBucket.next) {
                if (oTempBucket.next.key == key) {
                    oTempBucket.next.value = value;
                    return;
                } else {
                    oTempBucket = oTempBucket.next;
                }
            };
            oTempBucket.next = {
                key: key,
                value: value,
                next: null
            };
        }

        myMap.prototype.get = function (key) {
            let hash = this.makeHash(key);
            let oTempBucket = this.bucket[hash];
            while (oTempBucket) {
                if (oTempBucket.key == key) {
                    return oTempBucket.value;
                } else {
                    oTempBucket = oTempBucket.next;
                }
            }
            return undefined;
        }

        myMap.prototype.delete = function (key) {
            let hash = this.makeHash(key);
            let oTempBucket = this.bucket[hash];
            while (oTempBucket.next) {
                if (oTempBucket.next.key == key) {
                    oTempBucket.next = oTempBucket.next.next;
                    return true;
                } else {
                    oTempBucket = oTempBucket.next;
                }
            }
            return false;
        }

        myMap.prototype.has = function (key) {
            let hash = this.makeHash(key);
            let oTempBucket = this.bucket[hash];
            while (oTempBucket) {
                if (oTempBucket.next && oTempBucket.next.key == key) {
                    return true;
                } else {
                    oTempBucket = oTempBucket.next;
                }
            }
            return false;
        };

        myMap.prototype.clear = function (key) {
            this.init();
        };
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值