Completed
Push — master ( 43409a...e4ccec )
by Siro Díaz
01:28
created

ArrayList   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 3
dl 0
loc 212
rs 8.8
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 4 1
A __construct() 0 5 1
A delete() 0 11 4
A deleteBeginning() 0 1 1
A deleteAt() 0 1 1
A deleteEnd() 0 1 1
A toArray() 0 3 1
A rewind() 0 4 1
A current() 0 4 1
A key() 0 3 1
A next() 0 3 1
A valid() 0 3 1
A insertAt() 0 1 1
A insertEnd() 0 1 1
A insertBeginning() 0 1 1
A clear() 0 4 1
A get() 0 7 3
A search() 0 1 1
A getLast() 0 7 2
A getAll() 0 5 2
A contains() 0 3 1
A indexOf() 0 3 1
A lastIndexOf() 0 9 3
A remove() 0 18 4
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 DataStructures\Exceptions\NotFoundException;
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
    public $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
     * {@inheritDoc}
39
     */
40
    public function insert($index, $data) {
41
        array_splice($this->data, $index, 0, $data);
42
        $this->size++;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    protected function insertAt($index, $data) {}
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    protected function insertEnd($data) {}
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    protected function insertBeginning($data) {}
59
    
60
    /**
61
     * Removes all the array items.
62
     */
63
    public function clear() {
64
        $this->data = [];
65
        $this->size = 0;
66
    }
67
    
68
     /**
69
     * It execution time is O(1) because arrays in PHP are
70
     * hashtables.
71
     * {@inheritDoc}
72
     */
73
    public function get($index) {
74
        if($index < 0 || $index > $this->size - 1) {
75
            throw new OutOfBoundsException();
76
        }
77
78
        return $this->data[$index];
79
    }
80
81
    protected function search($index) {}
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function getLast() {
87
        if(!$this->empty()) {
88
            return $this->data[$this->size - 1];
89
        }
90
        
91
        return null;
92
    }
93
94
    public function getAll() {
95
        foreach($this->data as $data) {
96
            yield $data;
97
        }
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function contains($data) : bool {
104
        return in_array($data, $this->data);
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function indexOf($data) {
111
        return array_search($data, $this->data, true);
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function lastIndexOf($data) {
118
        for($i = $this->size - 1; $i >= 0; $i--) {
119
            if($this->data[$i] === $data) {
120
                return $i;
121
            }
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function remove($data) {
131
        if($this->empty()) {
132
            throw new NotFoundException();
133
        }
134
        
135
        $i = 0;
136
        while($i < $this->size) {
137
            if($this->data[$i] === $data) {
138
                $aux = $this->data[$i];
139
                array_splice($this->data, $i, 1);
140
                $this->size--;
141
                return $aux;
142
            }
143
            $i++;
144
        }
145
        
146
        throw new NotFoundException();
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
}