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

Public Member Functions

 bottom ()
 
 count ()
 
 current ()
 
 dequeue ()
 
 enqueue ($data)
 
 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 = parent::IT_MODE_FIFO
 
 $_it_pos = 0
 
 $_llist = array()
 

Detailed Description

Implementation of a Queue through a DoublyLinkedList.

As SplQueue extends SplDoublyLinkedList, unshift() and pop() are still available even though they don't make much sense for a queue. For convenience, two aliases are available:

Since
PHP 5.3

The SplQueue class provides the main functionalities of a queue implemented using a doubly linked list (DLL).

Definition at line 25 of file splqueue.inc.

Member Function Documentation

SplDoublyLinkedList::bottom ( )
inherited
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 ( )
inherited
SplDoublyLinkedList::current ( )
inherited
Returns
current object

Implements Iterator.

Definition at line 170 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\$_it_pos.

171  {
172  return $this->_llist[$this->_it_pos];
173  }
SplQueue::dequeue ( )
Returns
the first element of the queue.
Note
dequeue is an alias of push()
See Also
splDoublyLinkedList::push()

Definition at line 55 of file splqueue.inc.

56  {
57  return parent::shift();
58  }
SplQueue::enqueue (   $data)

Pushes an element at the end of the queue.

Parameters
$datavariable to add to the queue.
Note
enqueue is an alias of shift()
See Also
splDoublyLinkedList::shift()

Definition at line 65 of file splqueue.inc.

66  {
67  return parent::push($data);
68  }
SplDoublyLinkedList::getIteratorMode ( )
inherited
Returns
the current iteration mode
See Also
setIteratorMode

Definition at line 138 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\$_it_mode.

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

Definition at line 111 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\count().

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

Here is the call graph for this function:

SplDoublyLinkedList::key ( )
inherited
Returns
current key

Implements Iterator.

Definition at line 163 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\$_it_pos.

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

Forward to next element.

Implements Iterator.

Definition at line 177 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\pop(), and SplDoublyLinkedList\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)
inherited
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)
inherited
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 SplDoublyLinkedList\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 
)
inherited

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 SplDoublyLinkedList\count(), and SplDoublyLinkedList\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)
inherited

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 SplDoublyLinkedList\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 ( )
inherited
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 SplDoublyLinkedList\count().

Referenced by SplDoublyLinkedList\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)
inherited

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 SplDoublyLinkedList\offsetSet().

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

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 145 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\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:

SplQueue::setIteratorMode (   $mode)

Changes the iteration mode.

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

  • 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_LIFO | SplDoublyLnkedList::IT_MODE_KEEP

Note
The iteration's direction is not modifiable for queue instances
Parameters
$modeNew mode of iteration
Exceptions
RuntimeExceptionIf the new mode affects the iteration's direction.

Definition at line 42 of file splqueue.inc.

43  {
44  if ($mode & parent::IT_MODE_LIFO === parent::IT_MODE_LIFO) {
45  throw new RuntimeException("Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen");
46  }
47 
48  $this->_it_mode = $mode;
49  }
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423
SplDoublyLinkedList::shift ( )
inherited
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 SplDoublyLinkedList\count().

Referenced by SplDoublyLinkedList\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 ( )
inherited
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)
inherited

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 ( )
inherited
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

SplQueue::$_it_mode = parent::IT_MODE_FIFO
protected

Definition at line 27 of file splqueue.inc.

SplDoublyLinkedList::$_it_pos = 0
protectedinherited

Definition at line 26 of file spldoublylinkedlist.inc.

Referenced by SplDoublyLinkedList\current(), and SplDoublyLinkedList\key().

SplDoublyLinkedList::$_llist = array()
protectedinherited

Definition at line 24 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_DELETE = 0x00000001
inherited

Iterator mode.

See Also
setIteratorMode

Definition at line 46 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_FIFO = 0x00000000
inherited

Iterator mode.

See Also
setIteratorMode

Definition at line 36 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_KEEP = 0x00000000
inherited

Iterator mode.

See Also
setIteratorMode

Definition at line 41 of file spldoublylinkedlist.inc.

const SplDoublyLinkedList::IT_MODE_LIFO = 0x00000002
inherited

Iterator mode.

See Also
setIteratorMode

Definition at line 31 of file spldoublylinkedlist.inc.


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