Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 $items = [];
16
 
17
    /**
18
     * HashTable key map.
19
     *
20
     * @var array<int, string>
21
     */
22
    protected $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($source = null)
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
     * @return int
106
     */
107
    public function count()
108
    {
109
        return count($this->items);
110
    }
111
 
112
    /**
113
     * Get index for hash code.
114
     *
115
     * @return false|int Index
116
     */
117
    public function getIndexForHashCode(string $hashCode)
118
    {
119
        // Scrutinizer thinks the following could return string. It is wrong.
120
        return array_search($hashCode, $this->keyMap, true);
121
    }
122
 
123
    /**
124
     * Get by index.
125
     *
126
     * @return null|T
127
     */
128
    public function getByIndex(int $index)
129
    {
130
        if (isset($this->keyMap[$index])) {
131
            return $this->getByHashCode($this->keyMap[$index]);
132
        }
133
 
134
        return null;
135
    }
136
 
137
    /**
138
     * Get by hashcode.
139
     *
140
     * @return null|T
141
     */
142
    public function getByHashCode(string $hashCode)
143
    {
144
        if (isset($this->items[$hashCode])) {
145
            return $this->items[$hashCode];
146
        }
147
 
148
        return null;
149
    }
150
 
151
    /**
152
     * HashTable to array.
153
     *
154
     * @return T[]
155
     */
156
    public function toArray()
157
    {
158
        return $this->items;
159
    }
160
 
161
    /**
162
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
163
     */
164
    public function __clone()
165
    {
166
        $vars = get_object_vars($this);
167
        foreach ($vars as $key => $value) {
168
            // each member of this class is an array
169
            if (is_array($value)) {
170
                $array1 = $value;
171
                foreach ($array1 as $key1 => $value1) {
172
                    if (is_object($value1)) {
173
                        $array1[$key1] = clone $value1;
174
                    }
175
                }
176
                $this->$key = $array1;
177
            }
178
        }
179
    }
180
}