Completed
Push — master ( cf78d7...547e17 )
by Siro Díaz
01:35
created

ArrayList::size()   A

Complexity

Conditions 1
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 1
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\Interfaces\ListInterface;
12
use DataStructures\Lists\Traits\CountTrait;
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 implements ListInterface {
24
    use CountTrait;
25
26
    private $data;
27
    private $current;
28
    private $position;
29
    private $size;
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
    public function empty() : bool {
92
        return $this->size === 0;
93
    }
94
    
95
    /**
96
     * Removes and returns the last item in the array.
97
     *
98
     * @return mixed data in node.
99
     */
100
    public function pop() {
101
        return $this->delete(($this->size === 0) ? 0 : $this->size - 1);
102
    }
103
104
    /**
105
     * Adds at the end of the list new node containing
106
     * the data to be stored.
107
     *
108
     * @param mixed $data The data
109
     */
110
    public function push($data) {
111
        $this->data[] = $data;
112
        $this->size++;
113
    }
114
115
    /**
116
     * Adds at the beginning a node in the list.
117
     *
118
     * @param mixed $data
119
     * @return mixed the data stored.
120
     */
121
    public function unshift($data) {
122
        $this->insert(0, $data);
123
    }
124
125
    /**
126
     * Delete a node in the given position and returns it back.
127
     *
128
     * @param integer $index the position.
129
     * @throws OutOfBoundsException if index is negative
130
     *  or is greater than the size of the list.
131
     */
132
    public function delete($index) {
133
        if($this->size === 0 || $index < 0 || $index > $this->size - 1) {
134
            throw new OutOfBoundsException();
135
        }
136
        
137
        $data = $this->data[$index];
138
        array_splice($this->data, $index, 1);
139
        $this->size--;
140
141
        return $data;
142
    }
143
    
144
    /**
145
     * Deletes the first node of the list and returns it.
146
     *
147
     * @return mixed the data.
148
     */
149
    public function shift() {
150
        return $this->delete(0);
151
    }
152
153
    /**
154
     * Returns array stored in the data attribute.
155
     *
156
     * @return array data stored in all nodes.
157
     */
158
    public function toArray() : array {
159
        return $this->data;
160
    }
161
162
    /**
163
     * Reset the cursor position.
164
     */
165
    public function rewind() {
166
        $this->position = 0;
167
        $this->current = $this->data[$this->position];
168
    }
169
170
    /**
171
     * Returns the current node data.
172
     *
173
     * @return mixed
174
     */
175
    public function current() {
176
        return $this->current;
177
    }
178
179
    /**
180
     * Key or index that indicates the cursor position.
181
     *
182
     * @return integer The current position.
183
     */
184
    public function key() {
185
        return $this->position;
186
    }
187
188
    /**
189
     * Move the cursor to the next node and increments the
190
     * position counter.
191
     */
192
    public function next() {
193
        ++$this->position;
194
        $this->current = $this->data[$this->position];
195
    }
196
197
    /**
198
     * Returns if the current pointer position is valid.
199
     *
200
     * @return boolean true if pointer is not last, else false.
201
     */
202
    public function valid() {
203
        return $this->position < $this->size;
204
    }
205
    
206
    public function offsetSet($offset, $value) {
207
        if (is_null($offset)) {
208
            $offset = $this->size;
209
            if($this->size === 0) {
210
                $offset = 0;
211
            }
212
            $this->insert($offset, $value);
213
        } else {
214
            $this->insert($offset, $value);
215
        }
216
    }
217
    
218
    public function offsetExists($offset) {
219
        try {
220
            return $this->get($offset);
221
        } catch(OutOfBoundsException $e) {
222
            return false;
223
        }
224
    }
225
226
    public function offsetUnset($offset) {
227
        $this->delete($offset);
228
    }
229
230
    public function offsetGet($offset) {
231
        return $this->get($offset);
232
    }
233
}