PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / HashTable.php

HashTable.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/HashTable.php

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