PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/HashTable.php

https://github.com/jcplat/console-seolan
PHP | 228 lines | 93 code | 23 blank | 112 comment | 19 complexity | f78b81891a6eec03b3330d2acbe5cf92 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  33. }
  34. /** PHPExcel_IComparable */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  36. /**
  37. * PHPExcel_HashTable
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel
  41. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_HashTable
  44. {
  45. /**
  46. * HashTable elements
  47. *
  48. * @var array
  49. */
  50. public $_items = array();
  51. /**
  52. * HashTable key map
  53. *
  54. * @var array
  55. */
  56. public $_keyMap = array();
  57. /**
  58. * Create a new PHPExcel_HashTable
  59. *
  60. * @param PHPExcel_IComparable[] $pSource Optional source array to create HashTable from
  61. * @throws Exception
  62. */
  63. public function __construct($pSource = null)
  64. {
  65. if (!is_null($pSource)) {
  66. // Create HashTable
  67. $this->addFromSource($pSource);
  68. }
  69. }
  70. /**
  71. * Add HashTable items from source
  72. *
  73. * @param PHPExcel_IComparable[] $pSource Source array to create HashTable from
  74. * @throws Exception
  75. */
  76. public function addFromSource($pSource = null) {
  77. // Check if an array was passed
  78. if ($pSource == null) {
  79. return;
  80. } else if (!is_array($pSource)) {
  81. throw new Exception('Invalid array parameter passed.');
  82. }
  83. foreach ($pSource as $item) {
  84. $this->add($item);
  85. }
  86. }
  87. /**
  88. * Add HashTable item
  89. *
  90. * @param PHPExcel_IComparable $pSource Item to add
  91. * @throws Exception
  92. */
  93. public function add(PHPExcel_IComparable $pSource = null) {
  94. // Determine hashcode
  95. $hashCode = null;
  96. $hashIndex = $pSource->getHashIndex();
  97. if ( is_null ( $hashIndex ) ) {
  98. $hashCode = $pSource->getHashCode();
  99. } else if ( isset ( $this->_keyMap[$hashIndex] ) ) {
  100. $hashCode = $this->_keyMap[$hashIndex];
  101. } else {
  102. $hashCode = $pSource->getHashCode();
  103. }
  104. // Add value
  105. if (!isset($this->_items[ $hashCode ])) {
  106. $this->_items[ $hashCode ] = $pSource;
  107. $index = count($this->_items) - 1;
  108. $this->_keyMap[ $index ] = $hashCode;
  109. $pSource->setHashIndex( $index );
  110. } else {
  111. $pSource->setHashIndex( $this->_items[ $hashCode ]->getHashIndex() );
  112. }
  113. }
  114. /**
  115. * Remove HashTable item
  116. *
  117. * @param PHPExcel_IComparable $pSource Item to remove
  118. * @throws Exception
  119. */
  120. public function remove(PHPExcel_IComparable $pSource = null) {
  121. if (isset($this->_items[ $pSource->getHashCode() ])) {
  122. unset($this->_items[ $pSource->getHashCode() ]);
  123. $deleteKey = -1;
  124. foreach ($this->_keyMap as $key => $value) {
  125. if ($deleteKey >= 0) {
  126. $this->_keyMap[$key - 1] = $value;
  127. }
  128. if ($value == $pSource->getHashCode()) {
  129. $deleteKey = $key;
  130. }
  131. }
  132. unset($this->_keyMap[ count($this->_keyMap) - 1 ]);
  133. }
  134. }
  135. /**
  136. * Clear HashTable
  137. *
  138. */
  139. public function clear() {
  140. $this->_items = array();
  141. $this->_keyMap = array();
  142. }
  143. /**
  144. * Count
  145. *
  146. * @return int
  147. */
  148. public function count() {
  149. return count($this->_items);
  150. }
  151. /**
  152. * Get index for hash code
  153. *
  154. * @param string $pHashCode
  155. * @return int Index
  156. */
  157. public function getIndexForHashCode($pHashCode = '') {
  158. return array_search($pHashCode, $this->_keyMap);
  159. }
  160. /**
  161. * Get by index
  162. *
  163. * @param int $pIndex
  164. * @return PHPExcel_IComparable
  165. *
  166. */
  167. public function getByIndex($pIndex = 0) {
  168. if (isset($this->_keyMap[$pIndex])) {
  169. return $this->getByHashCode( $this->_keyMap[$pIndex] );
  170. }
  171. return null;
  172. }
  173. /**
  174. * Get by hashcode
  175. *
  176. * @param string $pHashCode
  177. * @return PHPExcel_IComparable
  178. *
  179. */
  180. public function getByHashCode($pHashCode = '') {
  181. if (isset($this->_items[$pHashCode])) {
  182. return $this->_items[$pHashCode];
  183. }
  184. return null;
  185. }
  186. /**
  187. * HashTable to array
  188. *
  189. * @return PHPExcel_IComparable[]
  190. */
  191. public function toArray() {
  192. return $this->_items;
  193. }
  194. /**
  195. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  196. */
  197. public function __clone() {
  198. $vars = get_object_vars($this);
  199. foreach ($vars as $key => $value) {
  200. if (is_object($value)) {
  201. $this->$key = clone $value;
  202. }
  203. }
  204. }
  205. }