forked from ashriths/cfg-team3
/
hash.php
114 lines (93 loc) · 3.97 KB
/
hash.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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("astrozas"));
?>