你有两个主要问题:
1)您要选择(打开|关闭)哈希表的哈希表范式。
2) Hashtable 可以是一个简单的数组,带有键索引和冲突情况下的数组引用。
3)你必须研究你的哈希密钥生成算法( $hash = ord($string[$i]) + ($hash << 5) - $hash; 就足够了),但你也可以选择 md5/sha . 如果您知道您的密钥空间,也许您可以使用 unix gperf。
这是我的哈希表实现:
<?php
/**
A brief but simple closed hash table class.
Jorge Niedbalski R. <jnr@niedbalski.org>
**/
class HashTable {
public $HashTable = array();
public $HashTableSize;
public function __construct($tablesize)
{
if($tablesize) {
$this->HashTableSize = $tablesize;
} else {
print "Unknown file size\n";
return -1;
}
}
public function __destruct()
{
unset($this->HashTable);
}
public function generate_bucket($string)
{
for($i=0; $i <= strlen($string); $i++) {
$hash = ord($string[$i]) + ($hash << 5) - $hash;
}
print "".$this->HashTableSize."\n";
return($hash%$this->HashTableSize);
}
public function add($string, $associated_array)
{
$bucket = $this->generate_bucket($string);
$tmp_array = array();
$tmp_array['string'] = $string;
$tmp_array['assoc_array'] = $associated_array;
if(!isset($this->HashTable[$bucket])) {
$this->HashTable[$bucket] = $tmp_array;
} else {
if(is_array($this->HashTable[$bucket])) {
array_push($this->HashTable[$bucket], $tmp_array);
} else {
$tmp = $this->HashTable[$bucket];
$this->HashTable[$bucket] = array();
array_push($this->HashTable[$bucket], $tmp);
array_push($this->HashTable[$bucket], $tmp_array);
}
}
}
public function delete($string, $attrname, $attrvalue)
{
$bucket = $this->generate_bucket($string);
if(is_null($this->HashTable[$bucket])) {
return -1;
} else {
if(is_array($this->HashTable[$bucket])) {
for($x = 0; $x <= sizeof($this->HashTable[$bucket]); $x++) {
if(($this->HashTable[$bucket][$x]['string'] == $string) && ($this->HashTable[$bucket][$x]['.$attrname.'] == $attrvalue)) {
unset($this->HashTable[$bucket][$x]);
}
}
} else {
unset($this->HashTable[$bucket][$x]);
}
}
/** everything is OK **/
return 0;
}
public function search($string)
{
$resultArray = array();
$bucket = $this->generate_bucket($string);
if(is_null($this->HashTable[$bucket])) {
return -1;
} else {
if(is_array($this->HashTable[$bucket])) {
for($x = 0; $x <= sizeof($this->HashTable[$bucket]); $x++) {
if(strcmp($this->HashTable[$bucket][$x]['string'], $string) == 0) {
array_push($resultArray,$this->HashTable[$bucket][$x]);
}
}
} else {
array_push($resultArray,$this->HashTable[$bucket]);
}
}
return($resultArray);
}
}
$hash = new HashTable(16);
$arr = array('nombre' => "jorge niedbalski");
$hash->add("astroza", $arr);
$hash->add("astrozas", $arr);
print_r($hash->search("astroza"));
?>