1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataStructures\Lists; |
4
|
|
|
|
5
|
|
|
use DataStructures\Lists\Interfaces\ListInterface; |
6
|
|
|
|
7
|
|
|
class ArrayList implements ListInterface { |
8
|
|
|
private $data; |
9
|
|
|
private $current; |
10
|
|
|
private $position; |
11
|
|
|
private $size; |
12
|
|
|
|
13
|
|
|
public function __construct() { |
14
|
|
|
$this->data = []; |
15
|
|
|
$this->size = 0; |
16
|
|
|
$this->position = 0; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function insert($index, $data) { |
20
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function clear() { |
24
|
|
|
$this->data = []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function get($index) { |
28
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getAll() { |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function empty() : bool { |
36
|
|
|
return $this->size === 0; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Removes and returns the last node in the list. |
41
|
|
|
* |
42
|
|
|
* @return mixed data in node. |
43
|
|
|
*/ |
44
|
|
|
public function pop() { |
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function delete($index) { |
49
|
|
|
unset($this->data[$index]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the array size. |
54
|
|
|
* |
55
|
|
|
* @return int the length |
56
|
|
|
*/ |
57
|
|
|
public function size() : int { |
58
|
|
|
return $this->size; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns array stored in the data attribute. |
63
|
|
|
* |
64
|
|
|
* @return array data stored in all nodes. |
65
|
|
|
*/ |
66
|
|
|
public function toArray() : array { |
67
|
|
|
return $this->data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Reset the cursor position. |
72
|
|
|
*/ |
73
|
|
|
public function rewind() { |
74
|
|
|
$this->position = 0; |
75
|
|
|
$this->current = $this->data[$this->position]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the current node data. |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function current() { |
84
|
|
|
return $this->current; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Key or index that indicates the cursor position. |
89
|
|
|
* |
90
|
|
|
* @return integer The current position. |
91
|
|
|
*/ |
92
|
|
|
public function key() { |
93
|
|
|
return $this->position; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Move the cursor to the next node and increments the |
98
|
|
|
* position counter. |
99
|
|
|
*/ |
100
|
|
|
public function next() { |
101
|
|
|
++$this->position; |
102
|
|
|
$this->current = $this->data[$this->position]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns if the current pointer position is valid. |
107
|
|
|
* |
108
|
|
|
* @return boolean true if pointer is not last, else false. |
109
|
|
|
*/ |
110
|
|
|
public function valid() { |
111
|
|
|
return $this->position < $this->size; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Binds to count() method. This is equal to make $this->list->size(). |
116
|
|
|
* |
117
|
|
|
* @return integer the list size. 0 if it is empty. |
118
|
|
|
*/ |
119
|
|
|
public function count() { |
120
|
|
|
return $this->size; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function offsetSet($offset, $valor) { |
124
|
|
|
//TODO |
125
|
|
|
if (is_null($offset)) { |
|
|
|
|
126
|
|
|
// $this->contenedor[] = $valor; |
|
|
|
|
127
|
|
|
} else { |
|
|
|
|
128
|
|
|
// $this->contenedor[$offset] = $valor; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function offsetExists($offset) { |
133
|
|
|
//TODO |
134
|
|
|
return false; |
135
|
|
|
// return isset($this->contenedor[$offset]); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function offsetUnset($offset) { |
139
|
|
|
//TODO |
140
|
|
|
// unset($this->contenedor[$offset]); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function offsetGet($offset) { |
144
|
|
|
//TODO |
145
|
|
|
return false; |
146
|
|
|
// return isset($this->contenedor[$offset]) ? $this->contenedor[$offset] : null; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.