HashTable.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * PHPExcel_HashTable
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_HashTable
  28. {
  29. /**
  30. * HashTable elements
  31. *
  32. * @var array
  33. */
  34. protected $items = array();
  35. /**
  36. * HashTable key map
  37. *
  38. * @var array
  39. */
  40. protected $keyMap = array();
  41. /**
  42. * Create a new PHPExcel_HashTable
  43. *
  44. * @param PHPExcel_IComparable[] $pSource Optional source array to create HashTable from
  45. * @throws PHPExcel_Exception
  46. */
  47. public function __construct($pSource = null)
  48. {
  49. if ($pSource !== null) {
  50. // Create HashTable
  51. $this->addFromSource($pSource);
  52. }
  53. }
  54. /**
  55. * Add HashTable items from source
  56. *
  57. * @param PHPExcel_IComparable[] $pSource Source array to create HashTable from
  58. * @throws PHPExcel_Exception
  59. */
  60. public function addFromSource($pSource = null)
  61. {
  62. // Check if an array was passed
  63. if ($pSource == null) {
  64. return;
  65. } elseif (!is_array($pSource)) {
  66. throw new PHPExcel_Exception('Invalid array parameter passed.');
  67. }
  68. foreach ($pSource as $item) {
  69. $this->add($item);
  70. }
  71. }
  72. /**
  73. * Add HashTable item
  74. *
  75. * @param PHPExcel_IComparable $pSource Item to add
  76. * @throws PHPExcel_Exception
  77. */
  78. public function add(PHPExcel_IComparable $pSource = null)
  79. {
  80. $hash = $pSource->getHashCode();
  81. if (!isset($this->items[$hash])) {
  82. $this->items[$hash] = $pSource;
  83. $this->keyMap[count($this->items) - 1] = $hash;
  84. }
  85. }
  86. /**
  87. * Remove HashTable item
  88. *
  89. * @param PHPExcel_IComparable $pSource Item to remove
  90. * @throws PHPExcel_Exception
  91. */
  92. public function remove(PHPExcel_IComparable $pSource = null)
  93. {
  94. $hash = $pSource->getHashCode();
  95. if (isset($this->items[$hash])) {
  96. unset($this->items[$hash]);
  97. $deleteKey = -1;
  98. foreach ($this->keyMap as $key => $value) {
  99. if ($deleteKey >= 0) {
  100. $this->keyMap[$key - 1] = $value;
  101. }
  102. if ($value == $hash) {
  103. $deleteKey = $key;
  104. }
  105. }
  106. unset($this->keyMap[count($this->keyMap) - 1]);
  107. }
  108. }
  109. /**
  110. * Clear HashTable
  111. *
  112. */
  113. public function clear()
  114. {
  115. $this->items = array();
  116. $this->keyMap = array();
  117. }
  118. /**
  119. * Count
  120. *
  121. * @return int
  122. */
  123. public function count()
  124. {
  125. return count($this->items);
  126. }
  127. /**
  128. * Get index for hash code
  129. *
  130. * @param string $pHashCode
  131. * @return int Index
  132. */
  133. public function getIndexForHashCode($pHashCode = '')
  134. {
  135. return array_search($pHashCode, $this->keyMap);
  136. }
  137. /**
  138. * Get by index
  139. *
  140. * @param int $pIndex
  141. * @return PHPExcel_IComparable
  142. *
  143. */
  144. public function getByIndex($pIndex = 0)
  145. {
  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. {
  160. if (isset($this->items[$pHashCode])) {
  161. return $this->items[$pHashCode];
  162. }
  163. return null;
  164. }
  165. /**
  166. * HashTable to array
  167. *
  168. * @return PHPExcel_IComparable[]
  169. */
  170. public function toArray()
  171. {
  172. return $this->items;
  173. }
  174. /**
  175. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  176. */
  177. public function __clone()
  178. {
  179. $vars = get_object_vars($this);
  180. foreach ($vars as $key => $value) {
  181. if (is_object($value)) {
  182. $this->$key = clone $value;
  183. }
  184. }
  185. }
  186. }