|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Smoren\ArrayView\Structs; |
|
6
|
|
|
|
|
7
|
|
|
use Smoren\ArrayView\Util; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represents a normalized slice definition with start, end, and step values. |
|
11
|
|
|
* |
|
12
|
|
|
* @implements \IteratorAggregate<int, int> |
|
13
|
|
|
*/ |
|
14
|
|
|
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Creates a new NormalizedSlice instance with optional start, end, and step values. |
|
18
|
|
|
* |
|
19
|
|
|
* @param int|null $start The start index of the slice range. |
|
20
|
|
|
* @param int|null $end The end index of the slice range. |
|
21
|
|
|
* @param int|null $step The step size for selecting elements in the slice range. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(int $start = null, int $end = null, int $step = null) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($start, $end, $step); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Getter for the start index of the normalized slice. |
|
30
|
|
|
* |
|
31
|
|
|
* @return int |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getStart(): int |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var int */ |
|
36
|
|
|
return $this->start; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Getter for the stop index of the normalized slice. |
|
41
|
|
|
* |
|
42
|
|
|
* @return int |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getEnd(): int |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var int */ |
|
47
|
|
|
return $this->end; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Getter for the step of the normalized slice. |
|
52
|
|
|
* |
|
53
|
|
|
* @return int |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getStep(): int |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var int */ |
|
58
|
|
|
return $this->step; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return size of the slice range. |
|
63
|
|
|
* |
|
64
|
|
|
* @return int Size of the slice range. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function count(): int |
|
67
|
|
|
{ |
|
68
|
|
|
return intval(ceil(abs((($this->end - $this->start) / $this->step)))); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Converts the provided index to the actual index based on the normalized slice parameters. |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $i The index to convert. |
|
75
|
|
|
* |
|
76
|
|
|
* @return int The converted index value. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function convertIndex(int $i): int |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->start + Util::normalizeIndex($i, \count($this), false) * $this->step; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return iterator to iterate slice range. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Generator<int, int> |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getIterator(): \Generator |
|
89
|
|
|
{ |
|
90
|
|
|
for ($i = 0; $i < \count($this); ++$i) { |
|
|
|
|
|
|
91
|
|
|
yield $this->convertIndex($i); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|