splqueue.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /** @file splqueue.inc
  3. * @ingroup SPL
  4. * @brief class SplQueue
  5. * @author Etienne Kneuss
  6. * @date 2008 - 2009
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /** @ingroup SPL
  11. * @brief Implementation of a Queue through a DoublyLinkedList. As SplQueue
  12. * extends SplDoublyLinkedList, unshift() and pop() are still available
  13. * even though they don't make much sense for a queue. For convenience,
  14. * two aliases are available:
  15. * - enqueue() is an alias of push()
  16. * - dequeue() is an alias of shift()
  17. *
  18. * @since PHP 5.3
  19. *
  20. * The SplQueue class provides the main functionalities of a
  21. * queue implemented using a doubly linked list (DLL).
  22. */
  23. class SplQueue extends SplDoublyLinkedList
  24. {
  25. protected $_it_mode = parent::IT_MODE_FIFO;
  26. /** Changes the iteration mode. There are two orthogonal sets of modes that
  27. * can be set:
  28. *
  29. * - The behavior of the iterator (either one or the other)
  30. * - SplDoublyLnkedList::IT_MODE_DELETE (Elements are deleted by the iterator)
  31. * - SplDoublyLnkedList::IT_MODE_KEEP (Elements are traversed by the iterator)
  32. *
  33. * The default mode is 0 : SplDoublyLnkedList::IT_MODE_LIFO | SplDoublyLnkedList::IT_MODE_KEEP
  34. *
  35. * @note The iteration's direction is not modifiable for queue instances
  36. * @param $mode New mode of iteration
  37. * @throw RuntimeException If the new mode affects the iteration's direction.
  38. */
  39. public function setIteratorMode($mode)
  40. {
  41. if ($mode & parent::IT_MODE_LIFO === parent::IT_MODE_LIFO) {
  42. throw new RuntimeException("Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen");
  43. }
  44. $this->_it_mode = $mode;
  45. }
  46. /** @return the first element of the queue.
  47. * @note dequeue is an alias of push()
  48. * @see splDoublyLinkedList::push()
  49. */
  50. public function dequeue()
  51. {
  52. return parent::shift();
  53. }
  54. /** Pushes an element at the end of the queue.
  55. * @param $data variable to add to the queue.
  56. * @note enqueue is an alias of shift()
  57. * @see splDoublyLinkedList::shift()
  58. */
  59. public function enqueue($data)
  60. {
  61. return parent::push($data);
  62. }
  63. }
  64. ?>