SPL-StandardPHPLibrary
spldoublylinkedlist.inc
Go to the documentation of this file.
1 <?php
23 {
24  protected $_llist = array();
25  protected $_it_mode = 0;
26  protected $_it_pos = 0;
27 
31  const IT_MODE_LIFO = 0x00000002;
32 
36  const IT_MODE_FIFO = 0x00000000;
37 
41  const IT_MODE_KEEP = 0x00000000;
42 
46  const IT_MODE_DELETE = 0x00000001;
47 
51  public function pop()
52  {
53  if (count($this->_llist) == 0) {
54  throw new RuntimeException("Can't pop from an empty datastructure");
55  }
56  return array_pop($this->_llist);
57  }
58 
62  public function shift()
63  {
64  if (count($this->_llist) == 0) {
65  throw new RuntimeException("Can't shift from an empty datastructure");
66  }
67  return array_shift($this->_llist);
68  }
69 
73  public function push($data)
74  {
75  array_push($this->_llist, $data);
76  return true;
77  }
78 
82  public function unshift($data)
83  {
84  array_unshift($this->_llist, $data);
85  return true;
86  }
87 
90  public function top()
91  {
92  return end($this->_llist);
93  }
94 
97  public function bottom()
98  {
99  return reset($this->_llist);
100  }
101 
104  public function count()
105  {
106  return count($this->_llist);
107  }
108 
111  public function isEmpty()
112  {
113  return ($this->count() == 0);
114  }
115 
130  public function setIteratorMode($mode)
131  {
132  $this->_it_mode = $mode;
133  }
134 
138  public function getIteratorMode()
139  {
140  return $this->_it_mode;
141  }
142 
145  public function rewind()
146  {
147  if ($this->_it_mode & self::IT_MODE_LIFO) {
148  $this->_it_pos = count($this->_llist)-1;
149  } else {
150  $this->_it_pos = 0;
151  }
152  }
153 
156  public function valid()
157  {
158  return array_key_exists($this->_it_pos, $this->_llist);
159  }
160 
163  public function key()
164  {
165  return $this->_it_pos;
166  }
167 
170  public function current()
171  {
172  return $this->_llist[$this->_it_pos];
173  }
174 
177  public function next()
178  {
179  if ($this->_it_mode & self::IT_MODE_LIFO) {
180  if ($this->_it_mode & self::IT_MODE_DELETE) {
181  $this->pop();
182  }
183  $this->_it_pos--;
184  } else {
185  if ($this->_it_mode & self::IT_MODE_DELETE) {
186  $this->shift();
187  } else {
188  $this->_it_pos++;
189  }
190  }
191  }
192 
199  public function offsetExists($offset)
200  {
201  if (!is_numeric($offset)) {
202  throw new OutOfRangeException("Offset invalid or out of range");
203  } else {
204  return array_key_exists($offset, $this->_llist);
205  }
206  }
207 
214  public function offsetGet($offset)
215  {
216  if ($this->_it_mode & self::IT_MODE_LIFO) {
217  $realOffset = count($this->_llist)-$offset;
218  } else {
219  $realOffset = $offset;
220  }
221 
222  if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
223  throw new OutOfRangeException("Offset invalid or out of range");
224  } else {
225  return $this->_llist[$realOffset];
226  }
227  }
228 
236  public function offsetSet($offset, $value)
237  {
238  if ($offset === null) {
239  return $this->push($value);
240  }
241 
242  if ($this->_it_mode & self::IT_MODE_LIFO) {
243  $realOffset = count($this->_llist)-$offset;
244  } else {
245  $realOffset = $offset;
246  }
247 
248  if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
249  throw new OutOfRangeException("Offset invalid or out of range");
250  } else {
251  $this->_llist[$realOffset] = $value;
252  }
253  }
254 
261  public function offsetUnset($offset)
262  {
263  if ($this->_it_mode & self::IT_MODE_LIFO) {
264  $realOffset = count($this->_llist)-$offset;
265  } else {
266  $realOffset = $offset;
267  }
268 
269  if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
270  throw new OutOfRangeException("Offset invalid or out of range");
271  } else {
272  array_splice($this->_llist, $realOffset, 1);
273  }
274  }
275 }
276 
277 ?>
Interface to override array access of objects.
Definition: spl.php:486
rewind()
Rewind to top iterator as set in constructor.
const IT_MODE_LIFO
Iterator mode.
offsetUnset($offset)
Unsets the element at a certain offset in the DLL.
unshift($data)
Adds an element to the beginning of the DLL.
Doubly Linked List.
const IT_MODE_FIFO
Iterator mode.
push($data)
Pushes an element to the end of the DLL.
Basic iterator.
Definition: spl.php:549
const IT_MODE_KEEP
Iterator mode.
setIteratorMode($mode)
Changes the iteration mode.
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423
Exception thrown when an illegal index was requested.
Definition: spl.php:415
This Interface allows to hook into the global count() function.
Definition: spl.php:576
next()
Forward to next element.
const IT_MODE_DELETE
Iterator mode.
offsetSet($offset, $value)
Defines the data at a certain offset in the DLL.