HashTable.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_HashTable
  8. {
  9. /**
  10. * HashTable elements
  11. *
  12. * @var array
  13. */
  14. protected $items = array();
  15. /**
  16. * HashTable key map
  17. *
  18. * @var array
  19. */
  20. protected $keyMap = array();
  21. /**
  22. * Create a new PHPExcel_HashTable
  23. *
  24. * @param PHPExcel_IComparable[] $pSource Optional source array to create HashTable from
  25. * @throws PHPExcel_Exception
  26. */
  27. public function __construct($pSource = null)
  28. {
  29. if ($pSource !== null) {
  30. // Create HashTable
  31. $this->addFromSource($pSource);
  32. }
  33. }
  34. /**
  35. * Add HashTable items from source
  36. *
  37. * @param PHPExcel_IComparable[] $pSource Source array to create HashTable from
  38. * @throws PHPExcel_Exception
  39. */
  40. public function addFromSource($pSource = null)
  41. {
  42. // Check if an array was passed
  43. if ($pSource == null) {
  44. return;
  45. } elseif (!is_array($pSource)) {
  46. throw new PHPExcel_Exception('Invalid array parameter passed.');
  47. }
  48. foreach ($pSource as $item) {
  49. $this->add($item);
  50. }
  51. }
  52. /**
  53. * Add HashTable item
  54. *
  55. * @param PHPExcel_IComparable $pSource Item to add
  56. * @throws PHPExcel_Exception
  57. */
  58. public function add(PHPExcel_IComparable $pSource = null)
  59. {
  60. $hash = $pSource->getHashCode();
  61. if (!isset($this->items[$hash])) {
  62. $this->items[$hash] = $pSource;
  63. $this->keyMap[count($this->items) - 1] = $hash;
  64. }
  65. }
  66. /**
  67. * Remove HashTable item
  68. *
  69. * @param PHPExcel_IComparable $pSource Item to remove
  70. * @throws PHPExcel_Exception
  71. */
  72. public function remove(PHPExcel_IComparable $pSource = null)
  73. {
  74. $hash = $pSource->getHashCode();
  75. if (isset($this->items[$hash])) {
  76. unset($this->items[$hash]);
  77. $deleteKey = -1;
  78. foreach ($this->keyMap as $key => $value) {
  79. if ($deleteKey >= 0) {
  80. $this->keyMap[$key - 1] = $value;
  81. }
  82. if ($value == $hash) {
  83. $deleteKey = $key;
  84. }
  85. }
  86. unset($this->keyMap[count($this->keyMap) - 1]);
  87. }
  88. }
  89. /**
  90. * Clear HashTable
  91. *
  92. */
  93. public function clear()
  94. {
  95. $this->items = array();
  96. $this->keyMap = array();
  97. }
  98. /**
  99. * Count
  100. *
  101. * @return int
  102. */
  103. public function count()
  104. {
  105. return count($this->items);
  106. }
  107. /**
  108. * Get index for hash code
  109. *
  110. * @param string $pHashCode
  111. * @return int Index
  112. */
  113. public function getIndexForHashCode($pHashCode = '')
  114. {
  115. return array_search($pHashCode, $this->keyMap);
  116. }
  117. /**
  118. * Get by index
  119. *
  120. * @param int $pIndex
  121. * @return PHPExcel_IComparable
  122. *
  123. */
  124. public function getByIndex($pIndex = 0)
  125. {
  126. if (isset($this->keyMap[$pIndex])) {
  127. return $this->getByHashCode($this->keyMap[$pIndex]);
  128. }
  129. return null;
  130. }
  131. /**
  132. * Get by hashcode
  133. *
  134. * @param string $pHashCode
  135. * @return PHPExcel_IComparable
  136. *
  137. */
  138. public function getByHashCode($pHashCode = '')
  139. {
  140. if (isset($this->items[$pHashCode])) {
  141. return $this->items[$pHashCode];
  142. }
  143. return null;
  144. }
  145. /**
  146. * HashTable to array
  147. *
  148. * @return PHPExcel_IComparable[]
  149. */
  150. public function toArray()
  151. {
  152. return $this->items;
  153. }
  154. /**
  155. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  156. */
  157. public function __clone()
  158. {
  159. $vars = get_object_vars($this);
  160. foreach ($vars as $key => $value) {
  161. if (is_object($value)) {
  162. $this->$key = clone $value;
  163. }
  164. }
  165. }
  166. }