首页 > 其他 > 详细

哈希表

时间:2019-10-12 14:30:31      阅读:86      评论:0      收藏:0      [点我收藏+]
function HashTable() {
            //桶的个数
            this.limit = 7

            this.storage = []
            //数据的个数
            this.count = 0
            //把字符串转化成比较大的数字: hashcode
            //根据hashcode分配到对应的链表
            HashTable.prototype.makeHash = function (str, size) {
                //初始化hashcode的值
                var hashcode = 0
                //霍顿算法 计算hashcode的值
                for (var i = 0; i < str.length; i++) {
                    hashcode = 37 * hashcode + str.charCodeAt(i)
                }
                //取余操作
                var index = hashcode % size
                return index
            }

            //插入 和 修改
            HashTable.prototype.put = function (key, value) {
                //获取桶的编号索引
                var index = this.makeHash(key, this.limit)
                //获取到对应的桶
                var bucket = this.storage[index]
                //如果桶不存在,就创建桶
                if (this.storage[index] == null) {
                    bucket = []
                    this.storage[index] = bucket
                }
                //桶存在,查看是否存在该值,存在就进行修改
                for (var i = 0; i < bucket.length; i++) {
                    var item = bucket[i]
                    if (item[0] == key) {
                        item[1] = value
                        return
                    }
                }
                //该值不存在,加入该值
                this.storage[index].push([key, value])
                //子项的总数 + 1
                this.count += 1
                if(this.count / this.limit > 0.75) {
                    var newlimit = this.getPrime(this.limit * 2) 
                    this.resize(newlimit)
                }
                return true
            }
            //获取值
            HashTable.prototype.get = function (key) {
                //获取桶的编号索引
                var index = this.makeHash(key, this.limit)
                //获取到对应的桶
                var bucket = this.storage[index]
                //如果桶不存在 
                if (!bucket) {
                    return null
                } else {
                    //桶存在,找对应的值
                    for (var i = 0; i < bucket.length; i++) {
                        var item = bucket[i]
                        if (item[0] == key) {
                            return item[1]
                        }
                    }
                    //找不到
                    return null
                }
            }
            //删除
            HashTable.prototype.delete = function (key) {
                //获取桶的编号索引
                var index = this.makeHash(key, this.limit)
                //获取到对应的桶
                var bucket = this.storage[index]
                //如果桶不存在 
                if (!bucket) {
                    return null
                }
                //桶存在,找对应的值,找到的话就删除
                for (var i = 0; i < bucket.length; i++) {
                    var item = bucket[i]
                    if (item[0] == key) {
                        bucket.splice(i, 1)
                        this.count --
                        if(this.limit > 7 && this.count / this.limit < 0.25) {
                            var newlimit = this.getPrime(Math.floor(this.limit / 2))
                            this.resize(newlimit)
                        }
                        return item[1]
                    }
                }
                //桶内不存在该值
                return null
            }
            //获取哈希表的子项个数
            HashTable.prototype.size = function () {
                return this.count
            }
            //哈希表是否为空
            HashTable.prototype.isEmpty = function () {
                return this.count == 0
            }
            //哈希表扩容
            HashTable.prototype.resize = function (newlimit) {
                var oldStorage = this.storage
                this.limit = newlimit
                this.storage = []
                this.count = 0
                for(var i = 0; i < oldStorage.length; i++) {
                    if(!oldStorage[i]) {
                        continue
                    }
                    for(var j = 0; j < oldStorage[i].length; j++) {
                        var key = oldStorage[i][j][0]
                        var value = oldStorage[i][j][1]
                        this.put(key, value)
                    }
                }
            }
            //判断质数 
            HashTable.prototype.isPrime = function (num) {
                var temp = parseInt(Math.sqrt(num))
                for(var i = 2; i <= temp; i++) {
                    if(num % i == 0) {
                        return false
                    }
                }
                return true
            }
            //产生质数
            HashTable.prototype.getPrime = function (num) {
                    while(this.isPrime(num)) {
                        num ++
                    }
                    return num
            }
        }

 

哈希表

原文:https://www.cnblogs.com/CoderZX/p/11661144.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!