|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPWord |
| 4 | + * |
| 5 | + * Copyright (c) 2009 - 2010 PHPWord |
| 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 PHPWord |
| 22 | + * @package PHPWord |
| 23 | + * @copyright Copyright (c) 2009 - 2010 PHPWord (http://www.codeplex.com/PHPWord) |
| 24 | + * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
| 25 | + * @version ##VERSION##, ##DATE## |
| 26 | + */ |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * PHPWord_HashTable |
| 31 | + * |
| 32 | + * @category PHPWord |
| 33 | + * @package PHPWord |
| 34 | + * @copyright Copyright (c) 2009 - 2010 PHPWord (http://www.codeplex.com/PHPWord) |
| 35 | + */ |
| 36 | +class PHPWord_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 PHPWord_HashTable |
| 54 | + * |
| 55 | + * @param PHPWord_IComparable[] $pSource Optional source array to create HashTable from |
| 56 | + * @throws Exception |
| 57 | + */ |
| 58 | + public function __construct($pSource = null) |
| 59 | + { |
| 60 | + if (!is_null($pSource)) { |
| 61 | + // Create HashTable |
| 62 | + $this->addFromSource($pSource); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Add HashTable items from source |
| 68 | + * |
| 69 | + * @param PHPWord_IComparable[] $pSource Source array to create HashTable from |
| 70 | + * @throws 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 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 PHPWord_IComparable $pSource Item to add |
| 89 | + * @throws Exception |
| 90 | + */ |
| 91 | + public function add(PHPWord_IComparable $pSource = null) { |
| 92 | + // Determine hashcode |
| 93 | + $hashCode = null; |
| 94 | + $hashIndex = $pSource->getHashIndex(); |
| 95 | + if ( is_null ( $hashIndex ) ) { |
| 96 | + $hashCode = $pSource->getHashCode(); |
| 97 | + } else if ( isset ( $this->_keyMap[$hashIndex] ) ) { |
| 98 | + $hashCode = $this->_keyMap[$hashIndex]; |
| 99 | + } else { |
| 100 | + $hashCode = $pSource->getHashCode(); |
| 101 | + } |
| 102 | + |
| 103 | + // Add value |
| 104 | + if (!isset($this->_items[ $hashCode ])) { |
| 105 | + $this->_items[ $hashCode ] = $pSource; |
| 106 | + $index = count($this->_items) - 1; |
| 107 | + $this->_keyMap[ $index ] = $hashCode; |
| 108 | + $pSource->setHashIndex( $index ); |
| 109 | + } else { |
| 110 | + $pSource->setHashIndex( $this->_items[ $hashCode ]->getHashIndex() ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Remove HashTable item |
| 116 | + * |
| 117 | + * @param PHPWord_IComparable $pSource Item to remove |
| 118 | + * @throws Exception |
| 119 | + */ |
| 120 | + public function remove(PHPWord_IComparable $pSource = null) { |
| 121 | + if (isset($this->_items[ $pSource->getHashCode() ])) { |
| 122 | + unset($this->_items[ $pSource->getHashCode() ]); |
| 123 | + |
| 124 | + $deleteKey = -1; |
| 125 | + foreach ($this->_keyMap as $key => $value) { |
| 126 | + if ($deleteKey >= 0) { |
| 127 | + $this->_keyMap[$key - 1] = $value; |
| 128 | + } |
| 129 | + |
| 130 | + if ($value == $pSource->getHashCode()) { |
| 131 | + $deleteKey = $key; |
| 132 | + } |
| 133 | + } |
| 134 | + unset($this->_keyMap[ count($this->_keyMap) - 1 ]); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Clear HashTable |
| 140 | + * |
| 141 | + */ |
| 142 | + public function clear() { |
| 143 | + $this->_items = array(); |
| 144 | + $this->_keyMap = array(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Count |
| 149 | + * |
| 150 | + * @return int |
| 151 | + */ |
| 152 | + public function count() { |
| 153 | + return count($this->_items); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get index for hash code |
| 158 | + * |
| 159 | + * @param string $pHashCode |
| 160 | + * @return int Index |
| 161 | + */ |
| 162 | + public function getIndexForHashCode($pHashCode = '') { |
| 163 | + return array_search($pHashCode, $this->_keyMap); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Get by index |
| 168 | + * |
| 169 | + * @param int $pIndex |
| 170 | + * @return PHPWord_IComparable |
| 171 | + * |
| 172 | + */ |
| 173 | + public function getByIndex($pIndex = 0) { |
| 174 | + if (isset($this->_keyMap[$pIndex])) { |
| 175 | + return $this->getByHashCode( $this->_keyMap[$pIndex] ); |
| 176 | + } |
| 177 | + |
| 178 | + return null; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Get by hashcode |
| 183 | + * |
| 184 | + * @param string $pHashCode |
| 185 | + * @return PHPWord_IComparable |
| 186 | + * |
| 187 | + */ |
| 188 | + public function getByHashCode($pHashCode = '') { |
| 189 | + if (isset($this->_items[$pHashCode])) { |
| 190 | + return $this->_items[$pHashCode]; |
| 191 | + } |
| 192 | + |
| 193 | + return null; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * HashTable to array |
| 198 | + * |
| 199 | + * @return PHPWord_IComparable[] |
| 200 | + */ |
| 201 | + public function toArray() { |
| 202 | + return $this->_items; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Implement PHP __clone to create a deep clone, not just a shallow copy. |
| 207 | + */ |
| 208 | + public function __clone() { |
| 209 | + $vars = get_object_vars($this); |
| 210 | + foreach ($vars as $key => $value) { |
| 211 | + if (is_object($value)) { |
| 212 | + $this->$key = clone $value; |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments