1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ianriizky\CodingInterview\DataSet; |
4
|
|
|
|
5
|
|
|
use SplFixedArray; |
6
|
|
|
|
7
|
|
|
class DataSet |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Current size of the collection. |
11
|
|
|
* |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected int $size = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Collection of the data set. |
18
|
|
|
* |
19
|
|
|
* @var \SplFixedArray |
20
|
|
|
*/ |
21
|
|
|
protected SplFixedArray $collection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new instance class. |
25
|
|
|
* |
26
|
|
|
* @param int $initialSize |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct(int $initialSize = 10) |
30
|
|
|
{ |
31
|
5 |
|
$this->collection = new SplFixedArray($initialSize); |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add a value to the collection. |
36
|
|
|
* |
37
|
|
|
* @param mixed $value |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
5 |
|
public function add($value): bool |
41
|
|
|
{ |
42
|
5 |
|
if ($this->contains($value)) { |
43
|
2 |
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
$this->ensureSize(); |
47
|
|
|
|
48
|
5 |
|
$this->collection[$this->size] = $value; |
49
|
5 |
|
$this->size++; |
50
|
|
|
|
51
|
5 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Determine whether the given value is exists on the collection or not. |
56
|
|
|
* |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
5 |
|
public function contains($value): bool |
61
|
|
|
{ |
62
|
5 |
|
foreach ($this->collection as $item) { |
63
|
5 |
|
if ($value === $item) { |
64
|
4 |
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return size of collection. |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
3 |
|
public function size(): int |
77
|
|
|
{ |
78
|
3 |
|
return $this->size; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return collection of the data set. |
83
|
|
|
* |
84
|
|
|
* @return \SplFixedArray |
85
|
|
|
*/ |
86
|
|
|
public function collection(): SplFixedArray |
87
|
|
|
{ |
88
|
|
|
return $this->collection; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Remove a value from the collection. |
93
|
|
|
* |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
1 |
|
public function remove($value): bool |
98
|
|
|
{ |
99
|
1 |
|
if (!$this->contains($value)) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$removedIndex = $this->indexOf($value); |
104
|
|
|
|
105
|
1 |
|
for ($index = $removedIndex; $index <= $this->size; $index++) { |
106
|
1 |
|
$this->collection[$index] = $this->collection[$index + 1]; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$this->size--; |
110
|
|
|
|
111
|
1 |
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return index of given value. |
116
|
|
|
* |
117
|
|
|
* @param mixed $value |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
1 |
|
protected function indexOf($value): int |
121
|
|
|
{ |
122
|
1 |
|
for ($index = 0; $index < $this->collection->count(); $index++) { |
123
|
1 |
|
if ($value === $this->collection[$index]) { |
124
|
1 |
|
return $index; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return -1; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Ensure that the data set size is enough to contain the collection. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
5 |
|
protected function ensureSize(): void |
137
|
|
|
{ |
138
|
5 |
|
if ($this->size < $this->collection->count()) { |
139
|
5 |
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$tempCollection = new SplFixedArray($this->size + 1); |
143
|
|
|
|
144
|
|
|
for ($index = 0; $index < $this->collection->count(); $index++) { |
145
|
|
|
$tempCollection[$index] = $this->collection[$index]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->collection = $tempCollection; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|