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

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

#
PHP | 209 lines | 82 code | 21 blank | 106 comment | 14 complexity | e37e40aeed9801b0e07ebeba96556f1c 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 - 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 ##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 - 2009 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. $hashCode = $pSource->getHashCode();
  88. if (!isset($this->_items[ $hashCode ])) {
  89. $this->_items[ $hashCode ] = $pSource;
  90. $index = count($this->_items) - 1;
  91. $this->_keyMap[ $index ] = $hashCode;
  92. $pSource->setHashIndex( $index );
  93. } else {
  94. $pSource->setHashIndex( $this->_items[ $hashCode ]->getHashIndex() );
  95. }
  96. }
  97. /**
  98. * Remove HashTable item
  99. *
  100. * @param PHPExcel_IComparable $pSource Item to remove
  101. * @throws Exception
  102. */
  103. public function remove(PHPExcel_IComparable $pSource = null) {
  104. if (isset($this->_items[ $pSource->getHashCode() ])) {
  105. unset($this->_items[ $pSource->getHashCode() ]);
  106. $deleteKey = -1;
  107. foreach ($this->_keyMap as $key => $value) {
  108. if ($deleteKey >= 0) {
  109. $this->_keyMap[$key - 1] = $value;
  110. }
  111. if ($value == $pSource->getHashCode()) {
  112. $deleteKey = $key;
  113. }
  114. }
  115. unset($this->_keyMap[ count($this->_keyMap) - 1 ]);
  116. }
  117. }
  118. /**
  119. * Clear HashTable
  120. *
  121. */
  122. public function clear() {
  123. $this->_items = array();
  124. $this->_keyMap = array();
  125. }
  126. /**
  127. * Count
  128. *
  129. * @return int
  130. */
  131. public function count() {
  132. return count($this->_items);
  133. }
  134. /**
  135. * Get index for hash code
  136. *
  137. * @param string $pHashCode
  138. * @return int Index
  139. */
  140. public function getIndexForHashCode($pHashCode = '') {
  141. return array_search($pHashCode, $this->_keyMap);
  142. }
  143. /**
  144. * Get by index
  145. *
  146. * @param int $pIndex
  147. * @return PHPExcel_IComparable
  148. *
  149. */
  150. public function getByIndex($pIndex = 0) {
  151. if (isset($this->_keyMap[$pIndex])) {
  152. return $this->getByHashCode( $this->_keyMap[$pIndex] );
  153. }
  154. return null;
  155. }
  156. /**
  157. * Get by hashcode
  158. *
  159. * @param string $pHashCode
  160. * @return PHPExcel_IComparable
  161. *
  162. */
  163. public function getByHashCode($pHashCode = '') {
  164. if (isset($this->_items[$pHashCode])) {
  165. return $this->_items[$pHashCode];
  166. }
  167. return null;
  168. }
  169. /**
  170. * HashTable to array
  171. *
  172. * @return PHPExcel_IComparable[]
  173. */
  174. public function toArray() {
  175. return $this->_items;
  176. }
  177. /**
  178. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  179. */
  180. public function __clone() {
  181. $vars = get_object_vars($this);
  182. foreach ($vars as $key => $value) {
  183. if (is_object($value)) {
  184. $this->$key = clone $value;
  185. }
  186. }
  187. }
  188. }