Completed
Push — master ( c6925e...1b130f )
by Siro Díaz
01:40
created

ArrayList::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists;
10
11
use DataStructures\Lists\Traits\{CountTrait, ArrayAccessTrait};
12
use DataStructures\Lists\ListAbstract;
13
use OutOfBoundsException;
14
15
/**
16
 * ArrayList
17
 *
18
 * This class is an implementation of a list based in native arrays.
19
 * The access time is, in general, O(1) since all in PHP is a hash table.
20
 *
21
 * @author Siro Diaz Palazon <[email protected]>
22
 */
23
class ArrayList extends ListAbstract {
24
    use ArrayAccessTrait;
25
26
    private $data;
27
    private $current;
28
    private $position;
29
30
    public function __construct() {
31
        $this->data = [];
32
        $this->size = 0;
33
        $this->position = 0;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function insert($index, $data) {
40
        array_splice($this->data, $index, 0, $data);
41
        $this->size++;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    protected function insertAt($index, $data) {}
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    protected function insertEnd($data) {}
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    protected function insertBeginning($data) {}
58
    
59
    /**
60
     * Removes all the array items.
61
     */
62
    public function clear() {
63
        $this->data = [];
64
        $this->size = 0;
65
    }
66
    
67
     /**
68
     * Returns the array in the especified index.
69
     *
70
     * @param integer $index Index that must be greater than 0
71
     *  and lower than the list size.
72
     * @return mixed The data stored in the given index
73
     * @throws OutOfBoundsException if index is out bounds.
74
     */
75
    public function get($index) {
76
        if($index < 0 || $index > $this->size - 1) {
77
            throw new OutOfBoundsException();
78
        }
79
80
        return $this->data[$index];
81
    }
82
83
    protected function search($index) {}
84
85
    /**
86
     * Returns the last node with O(1).
87
     *
88
     * @return mixed null if the array is empty.
89
     */
90
    public function getLast() {
91
        if(!$this->empty()) {
92
            return $this->data[$this->size - 1];
93
        }
94
        
95
        return null;
96
    }
97
98
    public function getAll() {
99
        foreach($this->data as $data) {
100
            yield $data;
101
        }
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function contains($data) : bool {
108
        return in_array($data, $this->data);
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function indexOf($data) {
115
        return array_search($data, $this->data, true);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function lastIndexOf($data) {
122
        for($i = $this->size - 1; $i >= 0; $i--) {
123
            if($this->data[$i] === $data) {
124
                return $i;
125
            }
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function remove($data) {
135
        $i = 0;
136
        
137
        while($i < $this->size) {
138
            if($this->data[$i] === $data) {
139
                $aux = $this->data[$i];
140
                array_splice($this->data, $i, 1);
141
                
142
                return $aux;
143
            }
144
            $i++;
145
        }
146
        return null;
147
    }
148
149
    /**
150
     * Delete a node in the given position and returns it back.
151
     *
152
     * @param integer $index the position.
153
     * @throws OutOfBoundsException if index is negative
154
     *  or is greater than the size of the list.
155
     */
156
    public function delete($index) {
157
        if($this->size === 0 || $index < 0 || $index > $this->size - 1) {
158
            throw new OutOfBoundsException();
159
        }
160
        
161
        $data = $this->data[$index];
162
        array_splice($this->data, $index, 1);
163
        $this->size--;
164
165
        return $data;
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    protected function deleteBeginning() {}
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    protected function deleteAt($index) {}
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    protected function deleteEnd() {}
182
183
    /**
184
     * Returns array stored in the data attribute.
185
     *
186
     * @return array data stored in all nodes.
187
     */
188
    public function toArray() : array {
189
        return $this->data;
190
    }
191
192
    /**
193
     * Reset the cursor position.
194
     */
195
    public function rewind() {
196
        $this->position = 0;
197
        $this->current = $this->data[$this->position];
198
    }
199
200
    /**
201
     * Returns the current node data.
202
     *
203
     * @return mixed
204
     */
205
    public function current() {
206
        $this->current = $this->data[$this->position];
207
        return $this->current;
208
    }
209
210
    /**
211
     * Key or index that indicates the cursor position.
212
     *
213
     * @return integer The current position.
214
     */
215
    public function key() {
216
        return $this->position;
217
    }
218
219
    /**
220
     * Move the cursor to the next node and increments the
221
     * position counter.
222
     */
223
    public function next() {
224
        ++$this->position;
225
    }
226
227
    /**
228
     * Returns if the current pointer position is valid.
229
     *
230
     * @return boolean true if pointer is not last, else false.
231
     */
232
    public function valid() {
233
        return isset($this->data[$this->position]);
234
    }
235
}