aa661ef587474bfbd4643d6786142365bd422457.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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. /**
  28. * PHPExcel_HashTable
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_HashTable
  35. {
  36. /**
  37. * HashTable elements
  38. *
  39. * @var array
  40. */
  41. public $_items = array();
  42. /**
  43. * HashTable key map
  44. *
  45. * @var array
  46. */
  47. public $_keyMap = array();
  48. /**
  49. * Create a new PHPExcel_HashTable
  50. *
  51. * @param PHPExcel_IComparable[] $pSource Optional source array to create HashTable from
  52. * @throws PHPExcel_Exception
  53. */
  54. public function __construct($pSource = null)
  55. {
  56. if ($pSource !== NULL) {
  57. // Create HashTable
  58. $this->addFromSource($pSource);
  59. }
  60. }
  61. /**
  62. * Add HashTable items from source
  63. *
  64. * @param PHPExcel_IComparable[] $pSource Source array to create HashTable from
  65. * @throws PHPExcel_Exception
  66. */
  67. public function addFromSource($pSource = null) {
  68. // Check if an array was passed
  69. if ($pSource == null) {
  70. return;
  71. } else if (!is_array($pSource)) {
  72. throw new PHPExcel_Exception('Invalid array parameter passed.');
  73. }
  74. foreach ($pSource as $item) {
  75. $this->add($item);
  76. }
  77. }
  78. /**
  79. * Add HashTable item
  80. *
  81. * @param PHPExcel_IComparable $pSource Item to add
  82. * @throws PHPExcel_Exception
  83. */
  84. public function add(PHPExcel_IComparable $pSource = null) {
  85. $hash = $pSource->getHashCode();
  86. if (!isset($this->_items[$hash])) {
  87. $this->_items[$hash] = $pSource;
  88. $this->_keyMap[count($this->_items) - 1] = $hash;
  89. }
  90. }
  91. /**
  92. * Remove HashTable item
  93. *
  94. * @param PHPExcel_IComparable $pSource Item to remove
  95. * @throws PHPExcel_Exception
  96. */
  97. public function remove(PHPExcel_IComparable $pSource = null) {
  98. $hash = $pSource->getHashCode();
  99. if (isset($this->_items[$hash])) {
  100. unset($this->_items[$hash]);
  101. $deleteKey = -1;
  102. foreach ($this->_keyMap as $key => $value) {
  103. if ($deleteKey >= 0) {
  104. $this->_keyMap[$key - 1] = $value;
  105. }
  106. if ($value == $hash) {
  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. }