Completed
Push — master ( 46289f...c51b27 )
by Siro Díaz
01:28
created

ArrayList::pop()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 0
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
     * Add a new node in the specified index.
38
     *
39
     * @param integer $index the position.
40
     * @param mixed $data the data to be stored.
41
     */
42
    public function insert($index, $data) {
43
        array_splice($this->data, $index, 0, $data);
44
        $this->size++;
45
    }
46
    
47
    /**
48
     * Removes all the array items.
49
     */
50
    public function clear() {
51
        $this->data = [];
52
        $this->size = 0;
53
    }
54
    
55
     /**
56
     * Returns the array in the especified index.
57
     *
58
     * @param integer $index Index that must be greater than 0
59
     *  and lower than the list size.
60
     * @return mixed The data stored in the given index
61
     * @throws OutOfBoundsException if index is out bounds.
62
     */
63
    public function get($index) {
64
        if($index < 0 || $index > $this->size - 1) {
65
            throw new OutOfBoundsException();
66
        }
67
68
        return $this->data[$index];
69
    }
70
71
    /**
72
     * Returns the last node with O(1).
73
     *
74
     * @return mixed null if the array is empty.
75
     */
76
    public function getLast() {
77
        if(!$this->empty()) {
78
            return $this->data[$this->size - 1];
79
        }
80
        
81
        return null;
82
    }
83
84
    public function getAll() {
85
        foreach($this->data as $data) {
86
            yield $data;
87
        }
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function contains($data) : bool {
94
        return in_array($data, $this->data);
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function indexOf($data) {
101
        return array_search($data, $this->data, true);
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function lastIndexOf($data) {
108
        for($i = $this->size - 1; $i >= 0; $i--) {
109
            if($this->data[$i] === $data) {
110
                return $i;
111
            }
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120
    public function remove($data) {
121
        $i = 0;
122
        
123
        while($i < $this->size) {
124
            if($this->data[$i] === $data) {
125
                $aux = $this->data[$i];
126
                array_splice($this->data, $i, 1);
127
                
128
                return $aux;
129
            }
130
            $i++;
131
        }
132
        return null;
133
    }
134
135
    /**
136
     * Delete a node in the given position and returns it back.
137
     *
138
     * @param integer $index the position.
139
     * @throws OutOfBoundsException if index is negative
140
     *  or is greater than the size of the list.
141
     */
142
    public function delete($index) {
143
        if($this->size === 0 || $index < 0 || $index > $this->size - 1) {
144
            throw new OutOfBoundsException();
145
        }
146
        
147
        $data = $this->data[$index];
148
        array_splice($this->data, $index, 1);
149
        $this->size--;
150
151
        return $data;
152
    }
153
154
    /**
155
     * Returns array stored in the data attribute.
156
     *
157
     * @return array data stored in all nodes.
158
     */
159
    public function toArray() : array {
160
        return $this->data;
161
    }
162
163
    /**
164
     * Reset the cursor position.
165
     */
166
    public function rewind() {
167
        $this->position = 0;
168
        $this->current = $this->data[$this->position];
169
    }
170
171
    /**
172
     * Returns the current node data.
173
     *
174
     * @return mixed
175
     */
176
    public function current() {
177
        return $this->current;
178
    }
179
180
    /**
181
     * Key or index that indicates the cursor position.
182
     *
183
     * @return integer The current position.
184
     */
185
    public function key() {
186
        return $this->position;
187
    }
188
189
    /**
190
     * Move the cursor to the next node and increments the
191
     * position counter.
192
     */
193
    public function next() {
194
        ++$this->position;
195
        $this->current = $this->data[$this->position];
196
    }
197
198
    /**
199
     * Returns if the current pointer position is valid.
200
     *
201
     * @return boolean true if pointer is not last, else false.
202
     */
203
    public function valid() {
204
        return $this->position < $this->size;
205
    }
206
}