SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Protected Attributes
SplStack Class Reference
Inheritance diagram for SplStack:
Inheritance graph
[legend]
Collaboration diagram for SplStack:
Collaboration graph
[legend]

List of all members.

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 = parent::IT_MODE_LIFO
 $_it_pos = 0
 $_llist = array()

Detailed Description

Implementation of a stack through a DoublyLinkedList.

As SplStack extends SplDoublyLinkedList, shift() and unshift() are still available even though they don't make much sense for a stack.

Since:
PHP 5.3

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

Definition at line 21 of file splstack.inc.


Member Function Documentation

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

Definition at line 97 of file spldoublylinkedlist.inc.

    {
        return reset($this->_llist);
    }
SplDoublyLinkedList::count ( ) [inherited]
SplDoublyLinkedList::current ( ) [inherited]
Returns:
current object

Implements Iterator.

Definition at line 170 of file spldoublylinkedlist.inc.

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

Definition at line 138 of file spldoublylinkedlist.inc.

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

Definition at line 111 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\count().

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

Here is the call graph for this function:

SplDoublyLinkedList::key ( ) [inherited]
Returns:
current key

Implements Iterator.

Definition at line 163 of file spldoublylinkedlist.inc.

    {
        return $this->_it_pos;
    }
SplDoublyLinkedList::next ( ) [inherited]

Forward to next element.

Implements Iterator.

Definition at line 177 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\pop(), and SplDoublyLinkedList\shift().

    {
        if ($this->_it_mode & self::IT_MODE_LIFO) {
            if ($this->_it_mode & self::IT_MODE_DELETE) {
                $this->pop();
            }
            $this->_it_pos--;
        } else {
            if ($this->_it_mode & self::IT_MODE_DELETE) {
                $this->shift();
            } else {
                $this->_it_pos++;
            }
        }
    }

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.

    {
        if (!is_numeric($offset)) {
            throw new OutOfRangeException("Offset invalid or out of range");
        } else {
            return array_key_exists($offset, $this->_llist);
        }
    }
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().

    {
        if ($this->_it_mode & self::IT_MODE_LIFO) {
            $realOffset = count($this->_llist)-$offset;
        } else {
            $realOffset = $offset;
        }

        if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
            throw new OutOfRangeException("Offset invalid or out of range");
        } else {
            return $this->_llist[$realOffset];
        }
    }

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

    {
        if ($offset === null) {
            return $this->push($value);
        }

        if ($this->_it_mode & self::IT_MODE_LIFO) {
            $realOffset = count($this->_llist)-$offset;
        } else {
            $realOffset = $offset;
        }

        if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
            throw new OutOfRangeException("Offset invalid or out of range");
        } else {
            $this->_llist[$realOffset] = $value;
        }
    }

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

    {
        if ($this->_it_mode & self::IT_MODE_LIFO) {
            $realOffset = count($this->_llist)-$offset;
        } else {
            $realOffset = $offset;
        }

        if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
            throw new OutOfRangeException("Offset invalid or out of range");
        } else {
            array_splice($this->_llist, $realOffset, 1);
        }
    }

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

    {
        if (count($this->_llist) == 0) {
            throw new RuntimeException("Can't pop from an empty datastructure");
        }
        return array_pop($this->_llist);
    }

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 SplQueue\enqueue(), and SplDoublyLinkedList\offsetSet().

    {
        array_push($this->_llist, $data);
        return true;
    }
SplDoublyLinkedList::rewind ( ) [inherited]

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 145 of file spldoublylinkedlist.inc.

References SplDoublyLinkedList\count().

    {
        if ($this->_it_mode & self::IT_MODE_LIFO) {
            $this->_it_pos = count($this->_llist)-1;
        } else {
            $this->_it_pos = 0;
        }
    }

Here is the call graph for this function:

SplStack::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 stack instances
Parameters:
$modeNew mode of iteration
Exceptions:
RuntimeExceptionIf the new mode affects the iteration's direction.

Reimplemented from SplDoublyLinkedList.

Definition at line 38 of file splstack.inc.

    {
        if ($mode & parent::IT_MODE_LIFO !== parent::IT_MODE_LIFO) {
            throw new RuntimeException("Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen");
        }

        $this->_it_mode = $mode;
    }
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 SplQueue\dequeue(), and SplDoublyLinkedList\next().

    {
        if (count($this->_llist) == 0) {
            throw new RuntimeException("Can't shift from an empty datastructure");
        }
        return array_shift($this->_llist);
    }

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.

    {
        return end($this->_llist);
    }
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.

    {
        array_unshift($this->_llist, $data);
        return true;
    }
SplDoublyLinkedList::valid ( ) [inherited]
Returns:
whether iterator is valid

Implements Iterator.

Definition at line 156 of file spldoublylinkedlist.inc.

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

Member Data Documentation

SplStack::$_it_mode = parent::IT_MODE_LIFO [protected]

Reimplemented from SplDoublyLinkedList.

Definition at line 23 of file splstack.inc.

SplDoublyLinkedList::$_it_pos = 0 [protected, inherited]

Definition at line 26 of file spldoublylinkedlist.inc.

SplDoublyLinkedList::$_llist = array() [protected, inherited]

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: