js实现哈希表常用操作

91 阅读1分钟
    //封装哈希表
    function HashTable() {
        //属性
        this.stroage = []; //哈希数组

        this.count = 0;  //元素的个数

        this.limit = 7;  //数组的长度

        //方法

        //哈希函数
        HashTable.prototype.hashFunc = function (str, size) {
            // 1.定义hashCode变量
            let hashCode = 0;

            // 2.霍纳算法, 计算hashCode的值
            // 获得每个字符 的 unicode 编码
            for (let i = 0; i < str.length; i++) {
                hashCode = 37 * hashCode + str.charCodeAt(i);
            }

            // 3 取余操作
            let index = hashCode % size

            return index;

        }
        //插入或修改操作
        HashTable.prototype.put = function (key,value) {

            let index  = this.hashFunc(key,this.limit);
            
            let bucket = this.stroage[index];

            if(!bucket) {
                bucket = [];
                this.stroage[index] = bucket;
            }

            let tuple = []; 
            for (let i = 0; i < bucket.length; i++) {
                tuple = bucket[i];
                if(tuple[0] == key) {
                    tuple[1] = value;
                    return;
                }
            }

            bucket.push([key,value])
            

            this.count += 1;

            if(this.count * 0.75  > this.limit) {
                this.resize(this.limit * 2)
            }
            
        }

        //获取
        HashTable.prototype.get = function (key) {

            let index = this.hashFunc(key,this.limit);

            let bucket = this.stroage[index];

            if (!bucket) {
                return null;
            }
            let tuple = [];
            for (let i = 0; i < bucket.length; i++) {
                tuple = bucket[i];
                
                if(tuple[0] == key) {
                    return tuple[1];
                    
                }
            }

            return null;

        }

        //删除
        HashTable.prototype.remove = function (key) {

            let index = this.hashFunc(key,this.limit);

            let bucket = this.stroage[index];

            if(!bucket) return null;

            let tuple = [];
            for (let i = 0; i < bucket.length; i++) {
                tuple = bucket[i];
                if(tuple[0] == key) {
                    bucket.splice(i,1);
                    this.count -= 1;

                    if(this.count * 0.25 < this.limit) {
                        this.resize(this.limit / 2);
                    }
                    return;
                }
            }

            return null;
        }

        // 扩容
        
        HashTable.prototype.resize = function (newLimit) {

            let oldStroage = this.stroage;

            this.stroage = [];
            this.count = 0;

            
            while (!this.isPrime(newLimit)) {
                newLimit++
            }      
                
            this.limit = newLimit;
                
        
            let bucket = [];
            for (let i = 0; i < oldStroage.length; i++) {
                bucket = oldStroage[i];
                if(!bucket) continue;
                let tuple = [];

                for (let j = 0; j < bucket.length; j++) {
                    tuple = bucket[j];
                    this.put(tuple[0],tuple[1]);
                } 
            }

        }
        //判断是否是一个质数
        HashTable.prototype.isPrime = function(num){
            
            for (let i = 2; i < Math.sqrt(num); i++) {
                
                if( num % i == 0) {
                    return false;
                } 

            }

            return true;

        } 
        

    }


    let hashTable = new HashTable();