SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Protected Attributes | List of all members
SplDoublyLinkedList Class Reference
Inheritance diagram for SplDoublyLinkedList:
Inheritance graph
Collaboration diagram for SplDoublyLinkedList:
Collaboration graph

Public Member Functions

 bottom ()
 
 count ()
 
 current ()
 
 getIteratorMode ()
 
 isEmpty ()
 
 key ()
 
 next ()
 
 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 
 pop ()
 
 push ($data)
 
 rewind ()
 
 setIteratorMode ($mode)
 
 shift ()
 
 top ()
 
 unshift ($data)
 
 valid ()
 

Public Attributes

const IT_MODE_DELETE = 0x00000001
 
const IT_MODE_FIFO = 0x00000000
 
const IT_MODE_KEEP = 0x00000000
 
const IT_MODE_LIFO = 0x00000002
 

Protected Attributes

 $_it_mode = 0
 
 $_it_pos = 0
 
 $_llist = array()
 

Detailed Description

Doubly Linked List.

Since
PHP 5.3

The SplDoublyLinkedList class provides the main functionalities of a doubly linked list (DLL).

Note
The following userland implementation of Iterator is a bit different from the internal one. Internally, iterators generated by nested foreachs are independent, while they share the same traverse pointer in userland.

Definition at line 22 of file spldoublylinkedlist.inc.

Member Function Documentation

SplDoublyLinkedList::bottom ( )
Returns
the element at the end of the DLL.

Definition at line 97 of file spldoublylinkedlist.inc.

98  {
99  return reset($this->_llist);
100  }
SplDoublyLinkedList::count ( )
Returns
number elements in the DLL.

Implements Countable.

Definition at line 104 of file spldoublylinkedlist.inc.

Referenced by isEmpty(), offsetGet(), offsetSet(), offsetUnset(), pop(), rewind(), and shift().

105  {
106  return count($this->_llist);
107  }
SplDoublyLinkedList::current ( )
Returns
current object

Implements Iterator.

Definition at line 170 of file spldoublylinkedlist.inc.

References $_it_pos.

171  {
172  return $this->_llist[$this->_it_pos];
173  }
SplDoublyLinkedList::getIteratorMode ( )
Returns
the current iteration mode
See Also
setIteratorMode

Definition at line 138 of file spldoublylinkedlist.inc.

References $_it_mode.

139  {
140  return $this->_it_mode;
141  }
SplDoublyLinkedList::isEmpty ( )
Returns
whether the DLL is empty.

Definition at line 111 of file spldoublylinkedlist.inc.

References count().

112  {
113  return ($this->count() == 0);
114  }

Here is the call graph for this function:

SplDoublyLinkedList::key ( )
Returns
current key

Implements Iterator.

Definition at line 163 of file spldoublylinkedlist.inc.

References $_it_pos.

164  {
165  return $this->_it_pos;
166  }
SplDoublyLinkedList::next ( )

Forward to next element.

Implements Iterator.

Definition at line 177 of file spldoublylinkedlist.inc.

References pop(), and shift().

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  }

Here is the call graph for this function:

SplDoublyLinkedList::offsetExists (   $offset)
Returns
whether a certain offset exists in the DLL
Parameters
$offsetThe offset
Exceptions
OutOfRangeExceptionIf the offset is either invalid or out of range.

Implements ArrayAccess.

Definition at line 199 of file spldoublylinkedlist.inc.

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  }
Exception thrown when an illegal index was requested.
Definition: spl.php:415
SplDoublyLinkedList::offsetGet (   $offset)
Returns
the data at a certain offset in the DLL
Parameters
$offsetThe offset
Exceptions
OutOfRangeExceptionIf the offset is either invalid or out of range.

Implements ArrayAccess.

Definition at line 214 of file spldoublylinkedlist.inc.

References count().

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  }
Exception thrown when an illegal index was requested.
Definition: spl.php:415

Here is the call graph for this function:

SplDoublyLinkedList::offsetSet (   $offset,
  $value 
)

Defines the data at a certain offset in the DLL.

Parameters
$offsetThe offset
$valueNew value
Exceptions
OutOfRangeExceptionIf the offset is either invalid or out of range.

Implements ArrayAccess.

Definition at line 236 of file spldoublylinkedlist.inc.

References count(), and push().

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  }
push($data)
Pushes an element to the end of the DLL.
Exception thrown when an illegal index was requested.
Definition: spl.php:415

Here is the call graph for this function:

SplDoublyLinkedList::offsetUnset (   $offset)

Unsets the element at a certain offset in the DLL.

Parameters
$offsetThe offset
Exceptions
OutOfRangeExceptionIf the offset is either invalid or out of range.

Implements ArrayAccess.

Definition at line 261 of file spldoublylinkedlist.inc.

References count().

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  }
Exception thrown when an illegal index was requested.
Definition: spl.php:415

Here is the call graph for this function:

SplDoublyLinkedList::pop ( )
Returns
the element popped from the end of the DLL.
Exceptions
RuntimeExceptionIf the datastructure is empty.

Definition at line 51 of file spldoublylinkedlist.inc.

References count().

Referenced by next().

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  }
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423

Here is the call graph for this function:

SplDoublyLinkedList::push (   $data)

Pushes an element to the end of the DLL.

Parameters
$datavariable to add to the DLL.

Definition at line 73 of file spldoublylinkedlist.inc.

Referenced by offsetSet().

74  {
75  array_push($this->_llist, $data);
76  return true;
77  }
SplDoublyLinkedList::rewind ( )

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 145 of file spldoublylinkedlist.inc.

References count().

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  }

Here is the call graph for this function:

SplDoublyLinkedList::setIteratorMode (   $mode)

Changes the iteration mode.

There are two orthogonal sets of modes that can be set:

  • The direction of the iteration (either one or the other)
    • SplDoublyLnkedList::IT_MODE_LIFO (Stack style)
    • SplDoublyLnkedList::IT_MODE_FIFO (Queue style)
  • The behavior of the iterator (either one or the other)
    • SplDoublyLnkedList::IT_MODE_DELETE (Elements are deleted by the iterator)
    • SplDoublyLnkedList::IT_MODE_KEEP (Elements are traversed by the iterator)

The default mode is 0 : SplDoublyLnkedList::IT_MODE_FIFO | SplDoublyLnkedList::IT_MODE_KEEP

Parameters
$modenew mode of iteration

Definition at line 130 of file spldoublylinkedlist.inc.

131  {
132  $this->_it_mode = $mode;
133  }
SplDoublyLinkedList::shift ( )
Returns
the element shifted from the beginning of the DLL.
Exceptions
RuntimeExceptionIf the datastructure is empty.

Definition at line 62 of file spldoublylinkedlist.inc.

References count().

Referenced by next().

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  }
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423

Here is the call graph for this function:

SplDoublyLinkedList::top ( )
Returns
the element at the beginning of the DLL.

Definition at line 90 of file spldoublylinkedlist.inc.

91  {
92  return end($this->_llist);
93  }
SplDoublyLinkedList::unshift (   $data)

Adds an element to the beginning of the DLL.

Parameters
$datavariable to add to the DLL.

Definition at line 82 of file spldoublylinkedlist.inc.

83  {
84  array_unshift($this->_llist, $data);
85  return true;
86  }
SplDoublyLinkedList::valid ( )
Returns
whether iterator is valid

Implements Iterator.

Definition at line 156 of file spldoublylinkedlist.inc.

157  {
158  return array_key_exists($this->_it_pos, $this->_llist);
159  }

Member Data Documentation

SplDoublyLinkedList::$_it_mode = 0
protected

Definition at line 25 of file spldoublylinkedlist.inc.

Referenced by getIteratorMode().

SplDoublyLinkedList::$_it_pos = 0
protected

Definition at line 26 of file spldoublylinkedlist.inc.

Referenced by current(), and key().

SplDoublyLinkedList::$_llist = array()
protected

Definition at line 24 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_DELETE = 0x00000001

Iterator mode.

See Also
setIteratorMode

Definition at line 46 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_FIFO = 0x00000000

Iterator mode.

See Also
setIteratorMode

Definition at line 36 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_KEEP = 0x00000000

Iterator mode.

See Also
setIteratorMode

Definition at line 41 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_LIFO = 0x00000002

Iterator mode.

See Also
setIteratorMode

Definition at line 31 of file spldoublylinkedlist.inc.


The documentation for this class was generated from the following file: