PageRenderTime 98ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.7.2/Classes/PHPExcel/HashTable.php

#
PHP | 212 lines | 80 code | 22 blank | 110 comment | 14 complexity | 88d9d4b4ff7b782cd349cb3a274f68ed MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  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 - 2010 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. if (!isset($this->_items[ $pSource->getHashCode() ])) {
  95. $this->_items[ $pSource->getHashCode() ] = $pSource;
  96. $this->_keyMap[ count($this->_items) - 1 ] = $pSource->getHashCode();
  97. }
  98. }
  99. /**
  100. * Remove HashTable item
  101. *
  102. * @param PHPExcel_IComparable $pSource Item to remove
  103. * @throws Exception
  104. */
  105. public function remove(PHPExcel_IComparable $pSource = null) {
  106. if (isset($this->_items[ $pSource->getHashCode() ])) {
  107. unset($this->_items[ $pSource->getHashCode() ]);
  108. $deleteKey = -1;
  109. foreach ($this->_keyMap as $key => $value) {
  110. if ($deleteKey >= 0) {
  111. $this->_keyMap[$key - 1] = $value;
  112. }
  113. if ($value == $pSource->getHashCode()) {
  114. $deleteKey = $key;
  115. }
  116. }
  117. unset($this->_keyMap[ count($this->_keyMap) - 1 ]);
  118. }
  119. }
  120. /**
  121. * Clear HashTable
  122. *
  123. */
  124. public function clear() {
  125. $this->_items = array();
  126. $this->_keyMap = array();
  127. }
  128. /**
  129. * Count
  130. *
  131. * @return int
  132. */
  133. public function count() {
  134. return count($this->_items);
  135. }
  136. /**
  137. * Get index for hash code
  138. *
  139. * @param string $pHashCode
  140. * @return int Index
  141. */
  142. public function getIndexForHashCode($pHashCode = '') {
  143. return array_search($pHashCode, $this->_keyMap);
  144. }
  145. /**
  146. * Get by index
  147. *
  148. * @param int $pIndex
  149. * @return PHPExcel_IComparable
  150. *
  151. */
  152. public function getByIndex($pIndex = 0) {
  153. if (isset($this->_keyMap[$pIndex])) {
  154. return $this->getByHashCode( $this->_keyMap[$pIndex] );
  155. }
  156. return null;
  157. }
  158. /**
  159. * Get by hashcode
  160. *
  161. * @param string $pHashCode
  162. * @return PHPExcel_IComparable
  163. *
  164. */
  165. public function getByHashCode($pHashCode = '') {
  166. if (isset($this->_items[$pHashCode])) {
  167. return $this->_items[$pHashCode];
  168. }
  169. return null;
  170. }
  171. /**
  172. * HashTable to array
  173. *
  174. * @return PHPExcel_IComparable[]
  175. */
  176. public function toArray() {
  177. return $this->_items;
  178. }
  179. /**
  180. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  181. */
  182. public function __clone() {
  183. $vars = get_object_vars($this);
  184. foreach ($vars as $key => $value) {
  185. if (is_object($value)) {
  186. $this->$key = clone $value;
  187. }
  188. }
  189. }
  190. }