Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace 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
     */
113
    public function getIndexForHashCode(string $hashCode): false|int
114
    {
115
        return array_search($hashCode, $this->keyMap, true);
116
    }
117
 
118
    /**
119
     * Get by index.
120
     *
121
     * @return null|T
122
     */
123
    public function getByIndex(int $index): ?IComparable
124
    {
125
        if (isset($this->keyMap[$index])) {
126
            return $this->getByHashCode($this->keyMap[$index]);
127
        }
128
 
129
        return null;
130
    }
131
 
132
    /**
133
     * Get by hashcode.
134
     *
135
     * @return null|T
136
     */
137
    public function getByHashCode(string $hashCode): ?IComparable
138
    {
139
        if (isset($this->items[$hashCode])) {
140
            return $this->items[$hashCode];
141
        }
142
 
143
        return null;
144
    }
145
 
146
    /**
147
     * HashTable to array.
148
     *
149
     * @return T[]
150
     */
151
    public function toArray(): array
152
    {
153
        return $this->items;
154
    }
155
 
156
    /**
157
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
158
     */
159
    public function __clone()
160
    {
161
        $vars = get_object_vars($this);
162
        foreach ($vars as $key => $value) {
163
            // each member of this class is an array
164
            if (is_array($value)) {
165
                $array1 = $value;
166
                foreach ($array1 as $key1 => $value1) {
167
                    if (is_object($value1)) {
168
                        $array1[$key1] = clone $value1;
169
                    }
170
                }
171
                $this->$key = $array1;
172
            }
173
        }
174
    }
175
}