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