Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
39 / 39
HashTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
10 / 10
20
100.00% covered (success)
100.00%
39 / 39
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 addFromSource
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 add
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
11 / 11
 remove
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
10 / 10
 clear
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 count
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getIndexForHashCode
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
2 / 2
 getByIndex
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getByHashCode
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 toArray
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @copyright   2009-2015 PHPPresentation contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpPresentation;
22
23/**
24 * \PhpOffice\PhpPresentation\HashTable.
25 */
26class HashTable
27{
28    /**
29     * HashTable elements.
30     *
31     * @var array<string, ComparableInterface>
32     */
33    public $items = [];
34
35    /**
36     * HashTable key map.
37     *
38     * @var array<int, string>
39     */
40    public $keyMap = [];
41
42    /**
43     * Create a new \PhpOffice\PhpPresentation\HashTable.
44     *
45     * @param array<int, ComparableInterface> $pSource Optional source array to create HashTable from
46     */
47    public function __construct(array $pSource = [])
48    {
49        $this->addFromSource($pSource);
50    }
51
52    /**
53     * Add HashTable items from source.
54     *
55     * @param array<int, ComparableInterface> $pSource Source array to create HashTable from
56     */
57    public function addFromSource(array $pSource = []): void
58    {
59        foreach ($pSource as $item) {
60            $this->add($item);
61        }
62    }
63
64    /**
65     * Add HashTable item.
66     *
67     * @param ComparableInterface $pSource Item to add
68     */
69    public function add(ComparableInterface $pSource): void
70    {
71        // Determine hashcode
72        $hashIndex = $pSource->getHashIndex();
73        $hashCode = $pSource->getHashCode();
74        if (isset($this->keyMap[$hashIndex])) {
75            $hashCode = $this->keyMap[$hashIndex];
76        }
77
78        // Add value
79        if (!isset($this->items[$hashCode])) {
80            $this->items[$hashCode] = $pSource;
81            $index = count($this->items) - 1;
82            $this->keyMap[$index] = $hashCode;
83            $pSource->setHashIndex($index);
84        } else {
85            $pSource->setHashIndex($this->items[$hashCode]->getHashIndex());
86        }
87    }
88
89    /**
90     * Remove HashTable item.
91     *
92     * @param ComparableInterface $pSource Item to remove
93     */
94    public function remove(ComparableInterface $pSource): void
95    {
96        if (isset($this->items[$pSource->getHashCode()])) {
97            unset($this->items[$pSource->getHashCode()]);
98
99            $deleteKey = -1;
100            foreach ($this->keyMap as $key => $value) {
101                if ($deleteKey >= 0) {
102                    $this->keyMap[$key - 1] = $value;
103                }
104
105                if ($value == $pSource->getHashCode()) {
106                    $deleteKey = $key;
107                }
108            }
109            unset($this->keyMap[count($this->keyMap) - 1]);
110        }
111    }
112
113    /**
114     * Clear HashTable.
115     */
116    public function clear(): void
117    {
118        $this->items = [];
119        $this->keyMap = [];
120    }
121
122    /**
123     * Count.
124     */
125    public function count(): int
126    {
127        return count($this->items);
128    }
129
130    /**
131     * Get index for hash code.
132     *
133     * @return int Index (-1 if not found)
134     */
135    public function getIndexForHashCode(string $pHashCode = ''): int
136    {
137        $index = array_search($pHashCode, $this->keyMap);
138
139        return false === $index ? -1 : $index;
140    }
141
142    /**
143     * Get by index.
144     */
145    public function getByIndex(int $pIndex = 0): ?ComparableInterface
146    {
147        if (isset($this->keyMap[$pIndex])) {
148            return $this->getByHashCode($this->keyMap[$pIndex]);
149        }
150
151        return null;
152    }
153
154    /**
155     * Get by hashcode.
156     */
157    public function getByHashCode(string $pHashCode = ''): ?ComparableInterface
158    {
159        if (isset($this->items[$pHashCode])) {
160            return $this->items[$pHashCode];
161        }
162
163        return null;
164    }
165
166    /**
167     * HashTable to array.
168     *
169     * @return array<ComparableInterface>
170     */
171    public function toArray(): array
172    {
173        return $this->items;
174    }
175}