HashTable.php 3.7 KB

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