Skip to content

Commit b86e926

Browse files
committed
algorithms about HashMap
1 parent f3c30d8 commit b86e926

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Map and HashTable/HashMap.html

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>HashMap</title>
6+
<script type="text/javascript">
7+
function HahsMap(){
8+
9+
var table = [];
10+
11+
var djb2HashCode = function(key){
12+
var hash = 5381;
13+
for(var i = 0; i < key.length; i++){
14+
hash = hash * 33 + key.charCodeAt(i);
15+
}
16+
17+
return hash % 1013;
18+
}
19+
20+
var ValuePair = function(key, value){
21+
this.key = key;
22+
this.vlaue = vlaue;
23+
24+
this.toString = function(){
25+
retu '[' + this.key + '-' + this.value + ']';
26+
}
27+
}
28+
29+
this.put = function(key, value){
30+
var position = djb2HashCode(key);
31+
if(table[position] == undefined){
32+
table[position] = new ValuePair(key, value);
33+
}else{
34+
var index = ++position;
35+
while(table[index] != undefined){
36+
index++;
37+
}
38+
table[index] = new ValuePair(key, value);
39+
}
40+
};
41+
42+
this.get = function(key){
43+
var position = djb2HashCode(key);
44+
if(table[position] !== undefined){
45+
if(table[position].key === key){
46+
return table[position].value;
47+
}else{
48+
var index = ++position;
49+
while(table[index] === undefined || table[position].key !== key){
50+
index++;
51+
}
52+
if (table[index].key === key) {
53+
return table[index].value;
54+
}
55+
}
56+
}
57+
58+
return undefined;
59+
};
60+
61+
this.remove = function(key){
62+
var position = djb2HashCode(key);
63+
if (table[position] !== undefined) {
64+
if (table[position].key === key) {
65+
table[position] = undefined;
66+
return true;
67+
}else{
68+
var index = ++position;
69+
while(table[index] === undefined || table[index].key !== key){
70+
index++;
71+
}
72+
if (table[index].key === key) {
73+
table[index] = undefined;
74+
return true;
75+
}
76+
}
77+
}
78+
79+
return false;
80+
};
81+
}
82+
</script>
83+
</head>
84+
<body>
85+
86+
</body>
87+
</html>

0 commit comments

Comments
 (0)