Skip to content

Commit 58fac14

Browse files
committed
添加防止冲突的hashtable
1 parent 101560c commit 58fac14

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

hashTableLink/HashNode.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* Author: evolution
6+
* Date: 16-7-8
7+
* Time: 下午1:27
8+
*
9+
* license GPL
10+
*/
11+
class HashNode
12+
{
13+
public $key;
14+
public $value;
15+
public $nextNode;
16+
17+
/**
18+
* @param $key
19+
* @param $value
20+
* @param null $nextNode
21+
*/
22+
public function __construct($key, $value, $nextNode=null){
23+
$this->key = $key;
24+
$this->value = $value;
25+
$this->nextNode = $nextNode;
26+
}
27+
}

hashTableLink/HashTable.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* 使用拉链法防止hash冲突的解决方案
5+
*
6+
* Created by PhpStorm.
7+
* Author: evolution
8+
* Date: 16-7-8
9+
* Time: 上午9:33
10+
*
11+
* license GPL
12+
*/
13+
class HashTable
14+
{
15+
//hashtable size
16+
private $size = 100;
17+
private $buckets;
18+
19+
/**
20+
* 初始化hash表
21+
*/
22+
public function __construct(){
23+
$this->buckets = new SplFixedArray($this->size);
24+
}
25+
26+
/**
27+
* hash 函数
28+
*
29+
* @author: jichao.wang <[email protected]>
30+
*
31+
* @param $key
32+
*
33+
* @return int
34+
*/
35+
private function hashFunc($key){
36+
// $len = strlen($key);
37+
// $sum = 0;
38+
// for($i=0; $i<$len; $i++){
39+
// $sum += ord($key[$i]);
40+
// }
41+
42+
$sum = crc32($key);
43+
return $sum % $this->size;
44+
}
45+
46+
/**
47+
* 添加元素到hash表中
48+
*
49+
* @author: jichao.wang <[email protected]>
50+
*
51+
* @param $key
52+
* @param $value
53+
*/
54+
public function add($key, $value){
55+
$index = $this->hashFunc($key);
56+
57+
if(isset($this->buckets[$index])){
58+
$newNode = new HashNode($key, $value, $this->buckets[$index]);
59+
}else{
60+
$newNode = new HashNode($key, $value);
61+
}
62+
$this->buckets[$index] = $newNode;
63+
64+
return true;
65+
}
66+
67+
/**
68+
* 查找元素
69+
* @author: jichao.wang <[email protected]>
70+
*
71+
* @param $key
72+
*/
73+
public function find($key){
74+
$index = $this->hashFunc($key);
75+
if(isset($this->buckets[$index])){
76+
$curNode = $this->buckets[$index];
77+
while($curNode){
78+
if($curNode->key==$key){
79+
return $curNode->value;
80+
}
81+
82+
$curNode = $curNode->nextNode;
83+
}
84+
}
85+
86+
return NULL;
87+
}
88+
89+
/**
90+
* 获取hashTable中的所有元素
91+
*
92+
* @author: jichao.wang <[email protected]>
93+
* @return mixed
94+
*/
95+
public function findAll(){
96+
return $this->buckets;
97+
}
98+
99+
}

hashTableLink/index.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* Author: evolution
5+
* Date: 16-7-8
6+
* Time: 上午10:52
7+
*
8+
* license GPL
9+
*/
10+
11+
include_once "HashTable.php";
12+
include_once "HashNode.php";
13+
$ht = new HashTable();
14+
$ht->add('name','name');
15+
$ht->add('anme','anme');
16+
$ht->add('anem','anem');
17+
$ht->add('name1','lisi');
18+
$ht->add('age',26);
19+
print_r($ht->findAll());
20+
echo $ht->find('name')."\r\n";
21+
echo $ht->find('anme')."\r\n";
22+
echo $ht->find('anem')."\r\n";
23+
echo $ht->find('name1')."\r\n";
24+
echo $ht->find('age')."\r\n";

0 commit comments

Comments
 (0)