PageRenderTime 74ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 204 lines | 77 code | 21 blank | 106 comment | 13 complexity | 438dcd64aa31f384515042e320303c5d 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 - 2008 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 - 2008 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_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /**
  30. * PHPExcel_HashTable
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel
  34. * @copyright Copyright (c) 2006 - 2008 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. * HashTable key map
  46. *
  47. * @var array
  48. */
  49. public $_keyMap = array();
  50. /**
  51. * Create a new PHPExcel_HashTable
  52. *
  53. * @param PHPExcel_IComparable[] $pSource Optional source array to create HashTable from
  54. * @throws Exception
  55. */
  56. public function __construct($pSource = null)
  57. {
  58. if (!is_null($pSource)) {
  59. // Create HashTable
  60. $this->addFromSource($pSource);
  61. }
  62. }
  63. /**
  64. * Add HashTable items from source
  65. *
  66. * @param PHPExcel_IComparable[] $pSource Source array to create HashTable from
  67. * @throws Exception
  68. */
  69. public function addFromSource($pSource = null) {
  70. // Check if an array was passed
  71. if ($pSource == null) {
  72. return;
  73. } else if (!is_array($pSource)) {
  74. throw new Exception('Invalid array parameter passed.');
  75. }
  76. foreach ($pSource as $item) {
  77. $this->add($item);
  78. }
  79. }
  80. /**
  81. * Add HashTable item
  82. *
  83. * @param PHPExcel_IComparable $pSource Item to add
  84. * @throws Exception
  85. */
  86. public function add(PHPExcel_IComparable $pSource = null) {
  87. if (!isset($this->_items[ $pSource->getHashCode() ])) {
  88. $this->_items[ $pSource->getHashCode() ] = $pSource;
  89. $this->_keyMap[ count($this->_items) - 1 ] = $pSource->getHashCode();
  90. }
  91. }
  92. /**
  93. * Remove HashTable item
  94. *
  95. * @param PHPExcel_IComparable $pSource Item to remove
  96. * @throws Exception
  97. */
  98. public function remove(PHPExcel_IComparable $pSource = null) {
  99. if (isset($this->_items[ $pSource->getHashCode() ])) {
  100. unset($this->_items[ $pSource->getHashCode() ]);
  101. $deleteKey = -1;
  102. foreach ($this->_keyMap as $key => $value) {
  103. if ($deleteKey >= 0) {
  104. $this->_keyMap[$key - 1] = $value;
  105. }
  106. if ($value == $pSource->getHashCode()) {
  107. $deleteKey = $key;
  108. }
  109. }
  110. unset($this->_keyMap[ count($this->_keyMap) - 1 ]);
  111. }
  112. }
  113. /**
  114. * Clear HashTable
  115. *
  116. */
  117. public function clear() {
  118. $this->_items = array();
  119. $this->_keyMap = array();
  120. }
  121. /**
  122. * Count
  123. *
  124. * @return int
  125. */
  126. public function count() {
  127. return count($this->_items);
  128. }
  129. /**
  130. * Get index for hash code
  131. *
  132. * @param string $pHashCode
  133. * @return int Index
  134. */
  135. public function getIndexForHashCode($pHashCode = '') {
  136. return array_search($pHashCode, $this->_keyMap);
  137. }
  138. /**
  139. * Get by index
  140. *
  141. * @param int $pIndex
  142. * @return PHPExcel_IComparable
  143. *
  144. */
  145. public function getByIndex($pIndex = 0) {
  146. if (isset($this->_keyMap[$pIndex])) {
  147. return $this->getByHashCode( $this->_keyMap[$pIndex] );
  148. }
  149. return null;
  150. }
  151. /**
  152. * Get by hashcode
  153. *
  154. * @param string $pHashCode
  155. * @return PHPExcel_IComparable
  156. *
  157. */
  158. public function getByHashCode($pHashCode = '') {
  159. if (isset($this->_items[$pHashCode])) {
  160. return $this->_items[$pHashCode];
  161. }
  162. return null;
  163. }
  164. /**
  165. * HashTable to array
  166. *
  167. * @return PHPExcel_IComparable[]
  168. */
  169. public function toArray() {
  170. return $this->_items;
  171. }
  172. /**
  173. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  174. */
  175. public function __clone() {
  176. $vars = get_object_vars($this);
  177. foreach ($vars as $key => $value) {
  178. if (is_object($value)) {
  179. $this->$key = clone $value;
  180. }
  181. }
  182. }
  183. }