Completed
Push — develop ( c96e2d...d2f55f )
by Adrien
48:43 queued 44:11
created

HashTable::getIndexForHashCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
class HashTable
6
{
7
    /**
8
     * HashTable elements.
9
     *
10
     * @var IComparable[]
11
     */
12
    protected $items = [];
13
14
    /**
15
     * HashTable key map.
16
     *
17
     * @var string[]
18
     */
19
    protected $keyMap = [];
20
21
    /**
22
     * Create a new \PhpOffice\PhpSpreadsheet\HashTable.
23
     *
24
     * @param IComparable[] $pSource Optional source array to create HashTable from
25
     *
26
     * @throws Exception
27
     */
28 61
    public function __construct($pSource = null)
29
    {
30 61
        if ($pSource !== null) {
31
            // Create HashTable
32
            $this->addFromSource($pSource);
33
        }
34 61
    }
35
36
    /**
37
     * Add HashTable items from source.
38
     *
39
     * @param IComparable[] $pSource Source array to create HashTable from
40
     *
41
     * @throws Exception
42
     */
43 60
    public function addFromSource(array $pSource = null)
44
    {
45
        // Check if an array was passed
46 60
        if ($pSource == null) {
47 60
            return;
48
        }
49
50 60
        foreach ($pSource as $item) {
51 60
            $this->add($item);
52
        }
53 60
    }
54
55
    /**
56
     * Add HashTable item.
57
     *
58
     * @param IComparable $pSource Item to add
59
     */
60 60
    public function add(IComparable $pSource)
61
    {
62 60
        $hash = $pSource->getHashCode();
63 60
        if (!isset($this->items[$hash])) {
64 60
            $this->items[$hash] = $pSource;
65 60
            $this->keyMap[count($this->items) - 1] = $hash;
66
        }
67 60
    }
68
69
    /**
70
     * Remove HashTable item.
71
     *
72
     * @param IComparable $pSource Item to remove
73
     */
74
    public function remove(IComparable $pSource)
75
    {
76
        $hash = $pSource->getHashCode();
77
        if (isset($this->items[$hash])) {
78
            unset($this->items[$hash]);
79
80
            $deleteKey = -1;
81
            foreach ($this->keyMap as $key => $value) {
82
                if ($deleteKey >= 0) {
83
                    $this->keyMap[$key - 1] = $value;
84
                }
85
86
                if ($value == $hash) {
87
                    $deleteKey = $key;
88
                }
89
            }
90
            unset($this->keyMap[count($this->keyMap) - 1]);
91
        }
92
    }
93
94
    /**
95
     * Clear HashTable.
96
     */
97
    public function clear()
98
    {
99
        $this->items = [];
100
        $this->keyMap = [];
101
    }
102
103
    /**
104
     * Count.
105
     *
106
     * @return int
107
     */
108 60
    public function count()
109
    {
110 60
        return count($this->items);
111
    }
112
113
    /**
114
     * Get index for hash code.
115
     *
116
     * @param string $pHashCode
117
     *
118
     * @return int Index
119
     */
120 60
    public function getIndexForHashCode($pHashCode)
121
    {
122 60
        return array_search($pHashCode, $this->keyMap);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($pHashCode, $this->keyMap) also could return the type false|string which is incompatible with the documented return type integer.
Loading history...
123
    }
124
125
    /**
126
     * Get by index.
127
     *
128
     * @param int $pIndex
129
     *
130
     * @return IComparable
131
     */
132 60
    public function getByIndex($pIndex)
133
    {
134 60
        if (isset($this->keyMap[$pIndex])) {
135 60
            return $this->getByHashCode($this->keyMap[$pIndex]);
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * Get by hashcode.
143
     *
144
     * @param string $pHashCode
145
     *
146
     * @return IComparable
147
     */
148 60
    public function getByHashCode($pHashCode)
149
    {
150 60
        if (isset($this->items[$pHashCode])) {
151 60
            return $this->items[$pHashCode];
152
        }
153
154
        return null;
155
    }
156
157
    /**
158
     * HashTable to array.
159
     *
160
     * @return IComparable[]
161
     */
162
    public function toArray()
163
    {
164
        return $this->items;
165
    }
166
167
    /**
168
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
169
     */
170
    public function __clone()
171
    {
172
        $vars = get_object_vars($this);
173
        foreach ($vars as $key => $value) {
174
            if (is_object($value)) {
175
                $this->$key = clone $value;
176
            }
177
        }
178
    }
179
}
180