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
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function insert($index, $data) { |
19
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function clear() { |
23
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function get($index) { |
27
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getAll() { |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function empty() : bool { |
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function delete($index) { |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function size() : int { |
43
|
|
|
return 0; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function toArray() : array { |
47
|
|
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Reset the cursor position. |
52
|
|
|
*/ |
53
|
|
|
public function rewind() { |
54
|
|
|
$this->position = 0; |
55
|
|
|
$this->current = &$this->head; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the current node data. |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function current() { |
64
|
|
|
return $this->current->data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Key or index that indicates the cursor position. |
69
|
|
|
* |
70
|
|
|
* @return integer The current position. |
71
|
|
|
*/ |
72
|
|
|
public function key() { |
73
|
|
|
return $this->position; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Move the cursor to the next node and increments the |
78
|
|
|
* position counter. |
79
|
|
|
*/ |
80
|
|
|
public function next() { |
81
|
|
|
++$this->position; |
82
|
|
|
$this->current = $this->current->next; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns if the current pointer position is valid. |
87
|
|
|
* |
88
|
|
|
* @return boolean true if pointer is not last, else false. |
89
|
|
|
*/ |
90
|
|
|
public function valid() { |
91
|
|
|
return $this->position < $this->size; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Binds to count() method. This is equal to make $this->list->size(). |
96
|
|
|
* |
97
|
|
|
* @return integer the list size. 0 if it is empty. |
98
|
|
|
*/ |
99
|
|
|
public function count() { |
100
|
|
|
return $this->size; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function offsetSet($offset, $valor) { |
104
|
|
|
//TODO |
105
|
|
|
if (is_null($offset)) { |
|
|
|
|
106
|
|
|
// $this->contenedor[] = $valor; |
|
|
|
|
107
|
|
|
} else { |
|
|
|
|
108
|
|
|
// $this->contenedor[$offset] = $valor; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function offsetExists($offset) { |
113
|
|
|
//TODO |
114
|
|
|
return false; |
115
|
|
|
// return isset($this->contenedor[$offset]); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function offsetUnset($offset) { |
119
|
|
|
//TODO |
120
|
|
|
// unset($this->contenedor[$offset]); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function offsetGet($offset) { |
124
|
|
|
//TODO |
125
|
|
|
return false; |
126
|
|
|
// return isset($this->contenedor[$offset]) ? $this->contenedor[$offset] : null; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: