PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/wi16-graphs/Classes/PHPExcel/HashTable.php

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