• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chdemko / php-sorted-collections / 12390158994

18 Dec 2024 09:33AM UTC coverage: 100.0%. Remained the same
12390158994

push

github

chdemko
Fix style in benchmarks

754 of 754 relevant lines covered (100.0%)

113.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/SortedCollection/ReversedMap.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\ReversedMap class
5
 *
6
 * @author    Christophe Demko <chdemko@gmail.com>
7
 * @copyright Copyright (C) 2012-2024 Christophe Demko. All rights reserved.
8
 *
9
 * @license BSD 3-Clause License
10
 *
11
 * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections
12
 */
13

14
// Declare chdemko\SortedCollection namespace
15
namespace chdemko\SortedCollection;
16

17
/**
18
 * Reversed map
19
 *
20
 * @package    SortedCollection
21
 * @subpackage Map
22
 *
23
 * @since 1.0.0
24
 *
25
 * @property-read callable   $comparator  The key comparison function
26
 * @property-read TreeNode   $first       The first element of the map
27
 * @property-read mixed      $firstKey    The first key of the map
28
 * @property-read mixed      $firstValue  The first value of the map
29
 * @property-read TreeNode   $last        The last element of the map
30
 * @property-read mixed      $lastKey     The last key of the map
31
 * @property-read mixed      $lastValue   The last value of the map
32
 * @property-read Iterator   $keys        The keys iterator
33
 * @property-read Iterator   $values      The values iterator
34
 * @property-read integer    $count       The number of elements in the map
35
 * @property-read SortedMap  $map         The underlying map
36
 */
37
class ReversedMap extends AbstractMap
38
{
39
    /**
40
     * @var SortedMap  Internal map
41
     *
42
     * @since 1.0.0
43
     */
44
    private $map;
45

46
    /**
47
     * @var callable  Comparator function
48
     *
49
     * @param mixed $key1 First key
50
     * @param mixed $key2 Second key
51
     *
52
     * @return integer negative if $key1 is lesser than $key2,
53
     *                 0 if $key1 is equal to $key2,
54
     *                 positive if $key1 is greater than $key2
55
     *
56
     * @since 1.0.0
57
     */
58
    private $comparator;
59

60
    /**
61
     * Constructor
62
     *
63
     * @param SortedMap $map Internal map
64
     *
65
     * @since 1.0.0
66
     */
67
    protected function __construct(SortedMap $map)
68
    {
69
        $this->map = $map;
404✔
70
        $this->comparator = function ($key1, $key2) {
404✔
71
            return - call_user_func($this->map->comparator, $key1, $key2);
1✔
72
        };
404✔
73
    }
74

75
    /**
76
     * Create
77
     *
78
     * @param SortedMap $map Internal map
79
     *
80
     * @return ReversedMap A new reversed map
81
     *
82
     * @since 1.0.0
83
     */
84
    public static function create(SortedMap $map)
85
    {
86
        return new static($map);
404✔
87
    }
88

89
    /**
90
     * Magic get method
91
     *
92
     * @param string $property The property
93
     *
94
     * @return mixed The value associated to the property
95
     *
96
     * @since 1.0.0
97
     */
98
    public function __get($property)
99
    {
100
        switch ($property) {
101
            case 'map':
8✔
102
                return $this->map;
1✔
103
            default:
104
                return parent::__get($property);
8✔
105
        }
106
    }
107

108
    /**
109
     * Get the comparator
110
     *
111
     * @return callable The comparator
112
     *
113
     * @since 1.0.0
114
     */
115
    public function comparator()
116
    {
117
        return $this->comparator;
1✔
118
    }
119

120
    /**
121
     * Get the first element
122
     *
123
     * @return mixed The first element
124
     *
125
     * @throws OutOfBoundsException If there is no element
126
     *
127
     * @since 1.0.0
128
     */
129
    public function first()
130
    {
131
        return $this->map->last();
363✔
132
    }
133

134
    /**
135
     * Get the last element
136
     *
137
     * @return mixed The last element
138
     *
139
     * @throws OutOfBoundsException If there is no element
140
     *
141
     * @since 1.0.0
142
     */
143
    public function last()
144
    {
145
        return $this->map->first();
3✔
146
    }
147

148
    /**
149
     * Get the predecessor element
150
     *
151
     * @param TreeNode $element A tree node member of the underlying TreeMap
152
     *
153
     * @return mixed The predecessor element
154
     *
155
     * @throws OutOfBoundsException If there is no predecessor
156
     *
157
     * @since 1.0.0
158
     */
159
    public function predecessor($element)
160
    {
161
        return $this->map->successor($element);
1✔
162
    }
163

164
    /**
165
     * Get the successor element
166
     *
167
     * @param TreeNode $element A tree node member of the underlying TreeMap
168
     *
169
     * @return mixed The successor element
170
     *
171
     * @throws OutOfBoundsException If there is no successor
172
     */
173
    public function successor($element)
174
    {
175
        return $this->map->predecessor($element);
357✔
176
    }
177

178
    /**
179
     * Returns the element whose key is the greatest key lesser than the given key
180
     *
181
     * @param mixed $key The searched key
182
     *
183
     * @return mixed The found element
184
     *
185
     * @throws OutOfBoundsException If there is no lower element
186
     *
187
     * @since 1.0.0
188
     */
189
    public function lower($key)
190
    {
191
        return $this->map->higher($key);
6✔
192
    }
193

194
    /**
195
     * Returns the element whose key is the greatest key lesser than or equal to the given key
196
     *
197
     * @param mixed $key The searched key
198
     *
199
     * @return mixed The found element
200
     *
201
     * @throws OutOfBoundsException If there is no floor element
202
     *
203
     * @since 1.0.0
204
     */
205
    public function floor($key)
206
    {
207
        return $this->map->ceiling($key);
6✔
208
    }
209

210
    /**
211
     * Returns the element whose key is equal to the given key
212
     *
213
     * @param mixed $key The searched key
214
     *
215
     * @return mixed The found element
216
     *
217
     * @throws OutOfBoundsException If there is no such element
218
     *
219
     * @since 1.0.0
220
     */
221
    public function find($key)
222
    {
223
        return $this->map->find($key);
8✔
224
    }
225

226
    /**
227
     * Returns the element whose key is the lowest key greater than or equal to the given key
228
     *
229
     * @param mixed $key The searched key
230
     *
231
     * @return mixed The found element
232
     *
233
     * @throws OutOfBoundsException If there is no ceiling element
234
     *
235
     * @since 1.0.0
236
     */
237
    public function ceiling($key)
238
    {
239
        return $this->map->floor($key);
6✔
240
    }
241

242
    /**
243
     * Returns the element whose key is the lowest key greater than to the given key
244
     *
245
     * @param mixed $key The searched key
246
     *
247
     * @return mixed The found element
248
     *
249
     * @throws OutOfBoundsException If there is no higher element
250
     *
251
     * @since 1.0.0
252
     */
253
    public function higher($key)
254
    {
255
        return $this->map->lower($key);
6✔
256
    }
257

258
    /**
259
     * Serialize the object
260
     *
261
     * @return array Array of values
262
     *
263
     * @since 1.0.0
264
     */
265
    public function jsonSerialize(): array
266
    {
267
        return array('ReversedMap' => $this->map->jsonSerialize());
1✔
268
    }
269

270
    /**
271
     * Count the number of key/value pairs
272
     *
273
     * @return integer
274
     *
275
     * @since 1.0.0
276
     */
277
    public function count(): int
278
    {
279
        return $this->map->count();
1✔
280
    }
281
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc