javascript hash 函数

//设计哈希函数
//1>将字符串转成比较大的数字:hashCode
//2>将大的数字hashCode压缩到数组范围

    function hashFunc(str,size){
         //1.定义hashCode变量
         var hashCode=0

         //2.霍纳算法,来计算 hashCode的值
         for(var i=0;i<str.length;i++){
            hashCode=37* hashCode + str.charCodeAt(i) //获取编码
         }
         //3.取余状态
         var index=hashCode%size

         return index 
     }

     //测试哈希函数
     alert(hashFunc('abc',7))
     alert(hashFunc('cba',7))
     alert(hashFunc('nba',7))
     alert(hashFunc('mba',7))


 function HashTable(){
         //属性
        this.storage=[]
        this.count=0
        this.limit=7

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

            //2.霍纳算法,来计算 hashCode的值
            for(var i=0;i<str.length;i++){
                hashCode=37* hashCode + str.charCodeAt(i) //获取编码
            }
            //3.取余状态
            var index=hashCode%size

            return index  
        } 

        //插入和修改操作
       HashTable.prototype.put= function (key,value){
            //1。根据key 获取对应的 index
            var index=this.hashFunc(key,this.limit)

            //2.根据 index取出对应的bucket
            var bucket=this.storage[index]

            //3.判断bucket 是否为 null
            if(bucket==null){
                bucket=[]
                this.storage[index]=bucket
            }

            //4.判断是否是修改数据
            for(var i=0;i<bucket.length;i++){
                var tuple=bucket[i]
                if(tuple[0]==key){
                    tuple[1]=value
                    return 
                }
            }

            //5.进行添加操作
            bucket.push([key,value])
            this.count+=1

            //6.判断是否需要扩容操作
            if(this.count>this.limit*0.75){
                this.resize(this.limit*2)
            }
        }

        //获取操作
       HashTable.prototype.get=function(key){
          //1.根据 key获取对应的 index
          var index=this.hashFunc(key,this.limit)

          //2.根据 index获取对应的 bucket
          var bucket=this.storage[index]

          //3.判断bucket是否为 null
          if(bucket===null){
              return null
          }   

          //4.有 bucket,那么进行线性查找
          for(var i=0;i<bucket.length;i++){
              var tuple=bucket[i]
              if(tuple[0]===key){
                  return tuple[1]
              }
          }

          //依然没有找到,那么返回 null
          return null 
       }
       
       //删除操作
       HashTable.prototype.remove=function(key){
           //1.根据 key 获取对应的index
           var index=this.hashFunc(key,this.limit)

           //2.根据index 获取对应的bucket
           var bucket=this.storage[index]

           //3.判断bucket 是否为 null
           if(bucket===null) return null

           //4.有 bucket,那么进行线性查找,并且删除
           for(var i=0;i<bucket.length;i++){
               var tuple=bucket[i]
               if(tuple[0]===key){
                   bucket.splice(i,1)
                   this.count--
                   return tuple[1]

                   // 缩小容量
                   if(this.limit>7&&this.count<this.length*0.25){
                       this.resize(Math.floor(this.limit/2))
                   }
               }
           }

           //5.依然没有找到,return null
           return null
       }

       //其他方法
       //判断 hash 表是否为null
       HashTable.prototype.isEmpty=function(){
           return this.count==0
       }
       
       //获取哈希表中的个数
       HashTable.prototype.size=function(){
           return this.count
       }

       //哈希表的扩容
       HashTable.prototype.resize=function(newLimit){
           //1.保存旧的数组内容
           var oldStorage=this.storage

           //2.重置所有的属性
           this.storage=[]
           this.count=0
           this.limit=newLimit
           
           //3.遍历oldStorage中所有的bucket
           for(var i=0;i<oldStorage.length;i++){
               //3.1取出对应的bucket
               var backet=oldStorage[i]

               //3.2判断 bucket是否为 null
               if(bucket==null) continue

               //3.3bucket 中有数据,取出数据,重新插入
               for(var j=0;j<bucket.length;j++){
                   var tuple=bucket[j]
                   this.put(tuple[0],tuple[1])
               }
           }

       }
    }
 
   
    
    //测试 hansh 表
    var ht=new HashTable()

    //2.插入数据
    ht.put('abc','123')
    ht.put('cba','321')
    ht.put('nba','521')
    ht.put('mba','520')

    //3.获取方法
    alert(ht.get('abc'))

    //4.修改方法
    ht.put('abc','111')
    alert(ht.get('abc'))

    //5.删除方法
    ht.remove('abc')
    alert(ht.get('abc'))

//封装一个函数:判断传入的函数是否是质数
     //特点:只能被 1和自己整数,不能被 2到n-1之间的数字整除
     function isPrime(num){
        for(var i=2;i<num;i++){
            if(num%i==0) return false
        }
        return true
     }

     function isPrime2(){
         //1.获取 num的平方根
         var temp=parseInt(Math.sqrt(num))
         //2.循环判断
         for(var i=0;i<temp;i++){
           if(num%i==0) return false
         }
         return true
     }
     //验证函数
     alert(isPrime(3))
     alert(isPrime(11))
     alert(isPrime(123))
     alert(isPrime(41))


 function HashTable(){
         //属性
        this.storage=[]
        this.count=0
        this.limit=7

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

            //2.霍纳算法,来计算 hashCode的值
            for(var i=0;i<str.length;i++){
                hashCode=37* hashCode + str.charCodeAt(i) //获取编码
            }
            //3.取余状态
            var index=hashCode%size

            return index  
        } 

        //插入和修改操作
       HashTable.prototype.put= function (key,value){
            //1。根据key 获取对应的 index
            var index=this.hashFunc(key,this.limit)

            //2.根据 index取出对应的bucket
            var bucket=this.storage[index]

            //3.判断bucket 是否为 null
            if(bucket==null){
                bucket=[]
                this.storage[index]=bucket
            }

            //4.判断是否是修改数据
            for(var i=0;i<bucket.length;i++){
                var tuple=bucket[i]
                if(tuple[0]==key){
                    tuple[1]=value
                    return 
                }
            }

            //5.进行添加操作
            bucket.push([key,value])
            this.count+=1

            //6.判断是否需要扩容操作
            if(this.count>this.limit*0.75){
                var newSize=this.limit*2
                var newPrime=this.getPrime(newPrime)
                this.resize(this.limit*2)
            }
        }

        //获取操作
       HashTable.prototype.get=function(key){
          //1.根据 key获取对应的 index
          var index=this.hashFunc(key,this.limit)

          //2.根据 index获取对应的 bucket
          var bucket=this.storage[index]

          //3.判断bucket是否为 null
          if(bucket===null){
              return null
          }   

          //4.有 bucket,那么进行线性查找
          for(var i=0;i<bucket.length;i++){
              var tuple=bucket[i]
              if(tuple[0]===key){
                  return tuple[1]
              }
          }

          //依然没有找到,那么返回 null
          return null 
       }
       
       //删除操作
       HashTable.prototype.remove=function(key){
           //1.根据 key 获取对应的index
           var index=this.hashFunc(key,this.limit)

           //2.根据index 获取对应的bucket
           var bucket=this.storage[index]

           //3.判断bucket 是否为 null
           if(bucket===null) return null

           //4.有 bucket,那么进行线性查找,并且删除
           for(var i=0;i<bucket.length;i++){
               var tuple=bucket[i]
               if(tuple[0]===key){
                   bucket.splice(i,1)
                   this.count--
                   return tuple[1]

                   // 缩小容量
                   if(this.limit>7&&this.count<this.length*0.25){
                       var newSize=Math.floor(this.limit /2)
                       var newPrime=this.getPrime(newSize)
                       this.resize(newPrime)
                   }
               }
           }

           //5.依然没有找到,return null
           return null
       }

       //其他方法
       //判断 hash 表是否为null
       HashTable.prototype.isEmpty=function(){
           return this.count==0
       }
       
       //获取哈希表中的个数
       HashTable.prototype.size=function(){
           return this.count
       }

       //哈希表的扩容
       HashTable.prototype.resize=function(newLimit){
           //1.保存旧的数组内容
           var oldStorage=this.storage

           //2.重置所有的属性
           this.storage=[]
           this.count=0
           this.limit=newLimit
           
           //3.遍历oldStorage中所有的bucket
           for(var i=0;i<oldStorage.length;i++){
               //3.1取出对应的bucket
               var backet=oldStorage[i]

               //3.2判断 bucket是否为 null
               if(bucket==null) continue

               //3.3bucket 中有数据,取出数据,重新插入
               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))
         //2.循环判断
         for(var i=0;i<temp;i++){
           if(num%i==0) return false
         }
         return true
       }

       //获取质数的方法
       HashTable.prototype.getPrime=function(num){
           while(!this.isPrime(num)){
               num++
           }
           return num
       }
    }

  

posted @ 2020-11-01 14:55  石shi  阅读(3127)  评论(0编辑  收藏  举报