-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.php
More file actions
66 lines (55 loc) · 1.35 KB
/
Copy pathHash.php
File metadata and controls
66 lines (55 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class HashTable {
private $buckets;
private $size = 10;
public function __construct() {
$this->buckets = new SplFixedArray($this->size)
}
private function hashfunc($key) {
$strlen = strlen($key);
$hashval = 0;
for ($i=0; $i < $strlen ; $i++) {
$hashval += ord($key{$i});
# code...
}
return $hashval % $this->size;
}
public function insert($key, $val) {
$index = $this->hashfunc($key);
//在链表头部插入新的key,value
if(isset($this->buckets[$index])) {
//将原来的key,value放到nextNode变量中
$newNode = new HashNode($key, $value, $this->buckets[$index]);
} else {
$newNode = new HashNode($key,$value,NULL);
}
//将新key,value放到当前节点,
$this->buckets[ $index ] = $newNode;
}
public function find($key) {
$index = $this->hashfunc($key);
$current = $this->buckets[$index];
while(isset($current)) {
if($current -> $key == $key) {
return $current->value;
}
$current = $current->nextNode;
}
return $this->buckets[ $index ];
}
}
class HashNode {
public $key;
public $value;
public $nextNode;
public function __construct($key, $value, $nextNode = NULL) {
$this->key = $key;
$this->value = $value;
$this->nextNode = $nextNode;
}
}
$ht = new HashTable();
$ht->insert('key1','value1');
$ht->insert('key2','value2');
echo $ht->find('key1');
echo $ht->find('key2');