This repository was archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.php
More file actions
148 lines (132 loc) · 3 KB
/
HashTable.php
File metadata and controls
148 lines (132 loc) · 3 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* HashTable
*
* An iterator, using a SHA1 hash as key.
* This iterator is able to use non-scalar values as key.
*/
class HashTable extends IterableCountable
{
protected $binaryKey;
/**
* @param boolean $binaryKey Store the key as binary (TRUE) or hexadecimal (FALSE) string
*/
public function __construct( $binaryKey = true )
{
$this->binaryKey = $binaryKey;
}
/**
* Returns a SHA1 hash in binary representation.
*
* Stored as PHP string with 20 chars. This matches
* 40 hex-values/bytes, which is the default return
* length of the hash('sha1') function.
* Use a BINARY(20) to store that key in a database.
*
* Numeric strings as key would be equal to their respective PHP data types.
* Empty keys are (probably) duplicates (this depends on their JSON encoded representation).
*
* @param string|array $key single string or array of keys
* @return string
*/
protected function getHash( $key )
{
if( !is_scalar( $key ) )
{
$key = json_encode( $key );
}
return hash( 'sha1', $key, $this->binaryKey );
}
/**
* Return the un-hashed key when passing in the hash.
*
* @param string $hash
* @return mixed
*/
public function getKey( $hash )
{
return isset( $this->data[$hash]['key'] ) ? $this->data[$hash]['key'] : null;
}
/**
* @param string|array $key
* @param mixed $value
* @return boolean
*/
public function set( $key, $value )
{
$hash = $this->getHash( $key );
$this->data[$hash] = array( 'key' => $key, 'value' => $value );
return $this;
}
/**
* @param string|array $key
* @return mixed|null
*/
public function get( $key )
{
$hash = $this->getHash( $key );
return isset( $this->data[$hash] ) ? $this->data[$hash]['value'] : null;
}
/**
* @param string|array $key
* @return boolean
*/
public function has( $key )
{
return isset( $this->data[$this->getHash( $key )] );
}
/**
* @param string|array $key
* @return boolean
*/
public function remove( $key )
{
unset( $this->data[$this->getHash( $key )] );
return $this;
}
/**
* @return mixed
*/
public function current()
{
$value = current( $this->data );
return $value['value'];
}
/**
* @return mixed
*/
public function key()
{
return key( $this->data );
}
/* helper methods */
/**
* Returns the HashTable as an array.
* If any value is an HashTable too, it will get inserted to
* the output array recursively.
*
* @param boolean $recursive default: TRUE
* @return array
*/
public function toArray( $recursive = true )
{
$output = array();
foreach( $this as $hash => $value )
{
if( $recursive == true && $value instanceof HashTable)
{
$value = $value->toArray();
}
$tmp = array(
'hash_bin' => $this->binaryKey ? $hash : pack( 'H*' , $hash ),
/* PHP 5.4: */
// 'hash_bin' => $this->binaryKey ? $hash : hex2bin( $hash ),
'hash_hex' => $this->binaryKey ? bin2hex( $hash ) : $hash,
'key' => $this->getKey( $hash ),
'value' => $value
);
$output[$hash] = $tmp;
}
return $output;
}
}