<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>哈希表的封装</title>
</head>
<body>
<script>
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
}
function HashTable(){
this.storage=[]
this.count=0
this.limit=7*2
//方法
HashTable.prototype.put=function(key,value){
//根据key获取对应的index
var index=this.hashFunc(key,this.limit)
//根据index取出对应的bucket
var bucket=this.storage[index]
//、
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
}
}
</script>
</body>
</html>