javascript哈希表

创建哈希函数

    // 讲字符串转换成大的数字
    // 数组大小
        function hashFunc(str,size) {
            var hashCode = 0;
            for(var i=0;i<str.length;i++){
                hashCode = 37 * hashCode + str.charCodeAt(i);
            }

            var index =  hashCode % size;
            return index;
        }
        alert(hashFunc('abc',7));
        alert(hashFunc('bcd',7));
        alert(hashFunc('xddd',7));

 哈希表常用操作

 function HashTable() {
            // 属性
            this.storage = [];
            this.count = 0;
            this.limit = 7;
            // 方法
            HashTable.prototype.hashFunc = function (str, size) {
                var hashCode = 0;
                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) {
                // 根据key获取index
                var index = this.hashFunc(key, this.limit);
                // 根据index取出对应的bucket
                var bucket = this.storage[index];
                // 判断bucket是否为空
                if (bucket == null) {
                    bucket = [];
                    this.storage[index] = bucket;
                }
                // 判断是否是修改数据
                for (var i = 0; i < bucket.length; i++) {
                    var tuple = bucket[i];
                    if (tuple[0] == key) {
                        tuple[1] = value;
                        return;
                    }
                }
                bucket.push([key, value]);
                this.count += 1;

                if (this.count > this.limit * 0.75) {
                    var newSize = this.limit * 2;
                    var newPrime = this.getPrime(newSize);
                    this.resize(newPrime);
                }
            }

            HashTable.prototype.get = function (key) {
                // 根据key获取index
                var index = this.hashFunc(key, this.limit);
                var bucket = this.storage[index];
                if (bucket == null) {
                    return null
                }
                for (var i = 0; i < bucket.length; i++) {
                    var tuple = bucket[i];
                    if (tuple[0] == key) {
                        return tuple[1];

                    }
                }
                return null;
            }

            HashTable.prototype.remove = function (key) {
                var index = this.hashFunc(key, this.limit);
                var bucket = this.storage[index];
                if (bucket == null) {
                    return null;
                }
                for (var i = 0; i < bucket.length; i++) {
                    var tuple = bucket[i];
                    if (bucket[0] == key) {
                        bucket.splice(i, 1);
                        this.count--;
                        return tuple[1];

                        if (this.limit > 7 && this.count < this.limit * 0.25) {
                            var newSize = Math.floor(this.limit / 2);
                            var newPrime = this.getPrime(newSize);
                            this.resize(newPrime);
                        }
                    }
                }
                return null;
            }
            HashTable.prototype.isEmpty = function () {
                return this.count == 0;
            }

            HashTable.prototype.size = function () {
                return this.count;
            }

            HashTable.prototype.resize = function (newLimit) {
                var oldStorage = this.storage;
                // 重置所有属性
                this.storage = [];
                this.count = 0;
                this.limit = newLimit;

                for (var i = 0; i < oldStorage.length; i++) {
                    var bucket = oldStorage[i];
                    if (bucket == null) {
                        continue;
                    }
                    for (var j = 0; j < bucket.length; j++) {
                        var tuple = bucket[j];
                        this.put(tuple[0], tuple[1]);
                    }
                }

            }

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

        }

        var ht = new HashTable();
        ht.put('abc', '123');
        ht.put('cba', '456');
        ht.put('bcd', '567');

        alert(ht.get('bcd'));
        // 修改
        ht.put('abc', '111');
        alert(ht.get('abc'));

        ht.remove('abc');
        alert(ht.get('abc'));

 

posted @ 2020-06-19 22:03  bradleydan  阅读(124)  评论(0)    收藏  举报