ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
HashTable.php
Go to the documentation of this file.
1<?php
2
4
9{
15 protected $items = [];
16
22 protected $keyMap = [];
23
29 public function __construct($pSource = null)
30 {
31 if ($pSource !== null) {
32 // Create HashTable
33 $this->addFromSource($pSource);
34 }
35 }
36
42 public function addFromSource(?array $pSource = null): void
43 {
44 // Check if an array was passed
45 if ($pSource == null) {
46 return;
47 }
48
49 foreach ($pSource as $item) {
50 $this->add($item);
51 }
52 }
53
59 public function add(IComparable $pSource): void
60 {
61 $hash = $pSource->getHashCode();
62 if (!isset($this->items[$hash])) {
63 $this->items[$hash] = $pSource;
64 $this->keyMap[count($this->items) - 1] = $hash;
65 }
66 }
67
73 public function remove(IComparable $pSource): void
74 {
75 $hash = $pSource->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
96 public function clear(): void
97 {
98 $this->items = [];
99 $this->keyMap = [];
100 }
101
107 public function count()
108 {
109 return count($this->items);
110 }
111
119 public function getIndexForHashCode($pHashCode)
120 {
121 return array_search($pHashCode, $this->keyMap);
122 }
123
131 public function getByIndex($pIndex)
132 {
133 if (isset($this->keyMap[$pIndex])) {
134 return $this->getByHashCode($this->keyMap[$pIndex]);
135 }
136
137 return null;
138 }
139
147 public function getByHashCode($pHashCode)
148 {
149 if (isset($this->items[$pHashCode])) {
150 return $this->items[$pHashCode];
151 }
152
153 return null;
154 }
155
161 public function toArray()
162 {
163 return $this->items;
164 }
165
169 public function __clone()
170 {
171 $vars = get_object_vars($this);
172 foreach ($vars as $key => $value) {
173 // each member of this class is an array
174 if (is_array($value)) {
175 $array1 = $value;
176 foreach ($array1 as $key1 => $value1) {
177 if (is_object($value1)) {
178 $array1[$key1] = clone $value1;
179 }
180 }
181 $this->$key = $array1;
182 }
183 }
184 }
185}
An exception for terminatinating execution or to throw for unit testing.
@template T of IComparable
Definition: HashTable.php:9
addFromSource(?array $pSource=null)
Add HashTable items from source.
Definition: HashTable.php:42
add(IComparable $pSource)
Add HashTable item.
Definition: HashTable.php:59
getByHashCode($pHashCode)
Get by hashcode.
Definition: HashTable.php:147
__construct($pSource=null)
Create a new \PhpOffice\PhpSpreadsheet\HashTable.
Definition: HashTable.php:29
toArray()
HashTable to array.
Definition: HashTable.php:161
getIndexForHashCode($pHashCode)
Get index for hash code.
Definition: HashTable.php:119
getByIndex($pIndex)
Get by index.
Definition: HashTable.php:131
__clone()
Implement PHP __clone to create a deep clone, not just a shallow copy.
Definition: HashTable.php:169
$key
Definition: croninfo.php:18