[289] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * PHPExcel |
---|
| 4 | * |
---|
| 5 | * Copyright (c) 2006 - 2014 PHPExcel |
---|
| 6 | * |
---|
| 7 | * This library is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU Lesser General Public |
---|
| 9 | * License as published by the Free Software Foundation; either |
---|
| 10 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This library is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * Lesser General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU Lesser General Public |
---|
| 18 | * License along with this library; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | * |
---|
| 21 | * @category PHPExcel |
---|
| 22 | * @package PHPExcel |
---|
| 23 | * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
---|
| 24 | * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
---|
| 25 | * @version 1.8.0, 2014-03-02 |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * PHPExcel_HashTable |
---|
| 31 | * |
---|
| 32 | * @category PHPExcel |
---|
| 33 | * @package PHPExcel |
---|
| 34 | * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
---|
| 35 | */ |
---|
| 36 | class PHPExcel_HashTable |
---|
| 37 | { |
---|
| 38 | /** |
---|
| 39 | * HashTable elements |
---|
| 40 | * |
---|
| 41 | * @var array |
---|
| 42 | */ |
---|
| 43 | public $_items = array(); |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * HashTable key map |
---|
| 47 | * |
---|
| 48 | * @var array |
---|
| 49 | */ |
---|
| 50 | public $_keyMap = array(); |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Create a new PHPExcel_HashTable |
---|
| 54 | * |
---|
| 55 | * @param PHPExcel_IComparable[] $pSource Optional source array to create HashTable from |
---|
| 56 | * @throws PHPExcel_Exception |
---|
| 57 | */ |
---|
| 58 | public function __construct($pSource = null) |
---|
| 59 | { |
---|
| 60 | if ($pSource !== NULL) { |
---|
| 61 | // Create HashTable |
---|
| 62 | $this->addFromSource($pSource); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Add HashTable items from source |
---|
| 68 | * |
---|
| 69 | * @param PHPExcel_IComparable[] $pSource Source array to create HashTable from |
---|
| 70 | * @throws PHPExcel_Exception |
---|
| 71 | */ |
---|
| 72 | public function addFromSource($pSource = null) { |
---|
| 73 | // Check if an array was passed |
---|
| 74 | if ($pSource == null) { |
---|
| 75 | return; |
---|
| 76 | } else if (!is_array($pSource)) { |
---|
| 77 | throw new PHPExcel_Exception('Invalid array parameter passed.'); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | foreach ($pSource as $item) { |
---|
| 81 | $this->add($item); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Add HashTable item |
---|
| 87 | * |
---|
| 88 | * @param PHPExcel_IComparable $pSource Item to add |
---|
| 89 | * @throws PHPExcel_Exception |
---|
| 90 | */ |
---|
| 91 | public function add(PHPExcel_IComparable $pSource = null) { |
---|
| 92 | $hash = $pSource->getHashCode(); |
---|
| 93 | if (!isset($this->_items[$hash])) { |
---|
| 94 | $this->_items[$hash] = $pSource; |
---|
| 95 | $this->_keyMap[count($this->_items) - 1] = $hash; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Remove HashTable item |
---|
| 101 | * |
---|
| 102 | * @param PHPExcel_IComparable $pSource Item to remove |
---|
| 103 | * @throws PHPExcel_Exception |
---|
| 104 | */ |
---|
| 105 | public function remove(PHPExcel_IComparable $pSource = null) { |
---|
| 106 | $hash = $pSource->getHashCode(); |
---|
| 107 | if (isset($this->_items[$hash])) { |
---|
| 108 | unset($this->_items[$hash]); |
---|
| 109 | |
---|
| 110 | $deleteKey = -1; |
---|
| 111 | foreach ($this->_keyMap as $key => $value) { |
---|
| 112 | if ($deleteKey >= 0) { |
---|
| 113 | $this->_keyMap[$key - 1] = $value; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if ($value == $hash) { |
---|
| 117 | $deleteKey = $key; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | unset($this->_keyMap[count($this->_keyMap) - 1]); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Clear HashTable |
---|
| 126 | * |
---|
| 127 | */ |
---|
| 128 | public function clear() { |
---|
| 129 | $this->_items = array(); |
---|
| 130 | $this->_keyMap = array(); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Count |
---|
| 135 | * |
---|
| 136 | * @return int |
---|
| 137 | */ |
---|
| 138 | public function count() { |
---|
| 139 | return count($this->_items); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Get index for hash code |
---|
| 144 | * |
---|
| 145 | * @param string $pHashCode |
---|
| 146 | * @return int Index |
---|
| 147 | */ |
---|
| 148 | public function getIndexForHashCode($pHashCode = '') { |
---|
| 149 | return array_search($pHashCode, $this->_keyMap); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Get by index |
---|
| 154 | * |
---|
| 155 | * @param int $pIndex |
---|
| 156 | * @return PHPExcel_IComparable |
---|
| 157 | * |
---|
| 158 | */ |
---|
| 159 | public function getByIndex($pIndex = 0) { |
---|
| 160 | if (isset($this->_keyMap[$pIndex])) { |
---|
| 161 | return $this->getByHashCode( $this->_keyMap[$pIndex] ); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | return null; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Get by hashcode |
---|
| 169 | * |
---|
| 170 | * @param string $pHashCode |
---|
| 171 | * @return PHPExcel_IComparable |
---|
| 172 | * |
---|
| 173 | */ |
---|
| 174 | public function getByHashCode($pHashCode = '') { |
---|
| 175 | if (isset($this->_items[$pHashCode])) { |
---|
| 176 | return $this->_items[$pHashCode]; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return null; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * HashTable to array |
---|
| 184 | * |
---|
| 185 | * @return PHPExcel_IComparable[] |
---|
| 186 | */ |
---|
| 187 | public function toArray() { |
---|
| 188 | return $this->_items; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
---|
| 193 | */ |
---|
| 194 | public function __clone() { |
---|
| 195 | $vars = get_object_vars($this); |
---|
| 196 | foreach ($vars as $key => $value) { |
---|
| 197 | if (is_object($value)) { |
---|
| 198 | $this->$key = clone $value; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|