| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet; |
| 4 |
|
| 5 |
class HashTable |
| 6 |
{ |
| 7 |
/** |
| 8 |
* HashTable elements. |
| 9 |
* |
| 10 |
* @var IComparable[] |
| 11 |
*/ |
| 12 |
protected $items = []; |
| 13 |
|
| 14 |
/** |
| 15 |
* HashTable key map. |
| 16 |
* |
| 17 |
* @var string[] |
| 18 |
*/ |
| 19 |
protected $keyMap = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Create a new \PhpOffice\PhpSpreadsheet\HashTable. |
| 23 |
* |
| 24 |
* @param IComparable[] $pSource Optional source array to create HashTable from |
| 25 |
* |
| 26 |
* @throws Exception |
| 27 |
*/ |
| 28 |
public function __construct($pSource = null) |
| 29 |
{ |
| 30 |
if ($pSource !== null) { |
| 31 |
// Create HashTable |
| 32 |
$this->addFromSource($pSource); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Add HashTable items from source. |
| 38 |
* |
| 39 |
* @param IComparable[] $pSource Source array to create HashTable from |
| 40 |
* |
| 41 |
* @throws Exception |
| 42 |
*/ |
| 43 |
public function addFromSource(array $pSource = null) |
| 44 |
{ |
| 45 |
// Check if an array was passed |
| 46 |
if ($pSource == null) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
foreach ($pSource as $item) { |
| 51 |
$this->add($item); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Add HashTable item. |
| 57 |
* |
| 58 |
* @param IComparable $pSource Item to add |
| 59 |
*/ |
| 60 |
public function add(IComparable $pSource) |
| 61 |
{ |
| 62 |
$hash = $pSource->getHashCode(); |
| 63 |
if (!isset($this->items[$hash])) { |
| 64 |
$this->items[$hash] = $pSource; |
| 65 |
$this->keyMap[count($this->items) - 1] = $hash; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Remove HashTable item. |
| 71 |
* |
| 72 |
* @param IComparable $pSource Item to remove |
| 73 |
*/ |
| 74 |
public function remove(IComparable $pSource) |
| 75 |
{ |
| 76 |
$hash = $pSource->getHashCode(); |
| 77 |
if (isset($this->items[$hash])) { |
| 78 |
unset($this->items[$hash]); |
| 79 |
|
| 80 |
$deleteKey = -1; |
| 81 |
foreach ($this->keyMap as $key => $value) { |
| 82 |
if ($deleteKey >= 0) { |
| 83 |
$this->keyMap[$key - 1] = $value; |
| 84 |
} |
| 85 |
|
| 86 |
if ($value == $hash) { |
| 87 |
$deleteKey = $key; |
| 88 |
} |
| 89 |
} |
| 90 |
unset($this->keyMap[count($this->keyMap) - 1]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Clear HashTable. |
| 96 |
*/ |
| 97 |
public function clear() |
| 98 |
{ |
| 99 |
$this->items = []; |
| 100 |
$this->keyMap = []; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Count. |
| 105 |
* |
| 106 |
* @return int |
| 107 |
*/ |
| 108 |
public function count() |
| 109 |
{ |
| 110 |
return count($this->items); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get index for hash code. |
| 115 |
* |
| 116 |
* @param string $pHashCode |
| 117 |
* |
| 118 |
* @return int Index |
| 119 |
*/ |
| 120 |
public function getIndexForHashCode($pHashCode) |
| 121 |
{ |
| 122 |
return array_search($pHashCode, $this->keyMap); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get by index. |
| 127 |
* |
| 128 |
* @param int $pIndex |
| 129 |
* |
| 130 |
* @return IComparable |
| 131 |
*/ |
| 132 |
public function getByIndex($pIndex) |
| 133 |
{ |
| 134 |
if (isset($this->keyMap[$pIndex])) { |
| 135 |
return $this->getByHashCode($this->keyMap[$pIndex]); |
| 136 |
} |
| 137 |
|
| 138 |
return null; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get by hashcode. |
| 143 |
* |
| 144 |
* @param string $pHashCode |
| 145 |
* |
| 146 |
* @return IComparable |
| 147 |
*/ |
| 148 |
public function getByHashCode($pHashCode) |
| 149 |
{ |
| 150 |
if (isset($this->items[$pHashCode])) { |
| 151 |
return $this->items[$pHashCode]; |
| 152 |
} |
| 153 |
|
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* HashTable to array. |
| 159 |
* |
| 160 |
* @return IComparable[] |
| 161 |
*/ |
| 162 |
public function toArray() |
| 163 |
{ |
| 164 |
return $this->items; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
| 169 |
*/ |
| 170 |
public function __clone() |
| 171 |
{ |
| 172 |
$vars = get_object_vars($this); |
| 173 |
foreach ($vars as $key => $value) { |
| 174 |
if (is_object($value)) { |
| 175 |
$this->$key = clone $value; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|