Completed
Push — master ( b84c7d...9126fc )
by Siro Díaz
01:26
created

ArrayList   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 2
dl 0
loc 223
rs 9.3999
c 0
b 0
f 0

21 Methods

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