SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Protected Attributes
SplDoublyLinkedList Class Reference
Inheritance diagram for SplDoublyLinkedList:
Inheritance graph
[legend]
Collaboration diagram for SplDoublyLinkedList:
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 = 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.

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

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

Implements Iterator.

Definition at line 170 of file spldoublylinkedlist.inc.

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

Definition at line 138 of file spldoublylinkedlist.inc.

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

Definition at line 111 of file spldoublylinkedlist.inc.

References count().

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

Here is the call graph for this function:

SplDoublyLinkedList::key ( )
Returns:
current key

Implements Iterator.

Definition at line 163 of file spldoublylinkedlist.inc.

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

Forward to next element.

Implements Iterator.

Definition at line 177 of file spldoublylinkedlist.inc.

References pop(), and 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)
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)
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().

    {
        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 
)

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

    {
        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)

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

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

    {
        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)

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

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

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 145 of file spldoublylinkedlist.inc.

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

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

Reimplemented in SplQueue, and SplStack.

Definition at line 130 of file spldoublylinkedlist.inc.

    {
        $this->_it_mode = $mode;
    }
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 SplQueue\dequeue(), and 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 ( )
Returns:
the element at the beginning of the DLL.

Definition at line 90 of file spldoublylinkedlist.inc.

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

    {
        array_unshift($this->_llist, $data);
        return true;
    }
SplDoublyLinkedList::valid ( )
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

SplDoublyLinkedList::$_it_mode = 0 [protected]

Reimplemented in SplQueue, and SplStack.

Definition at line 25 of file spldoublylinkedlist.inc.

SplDoublyLinkedList::$_it_pos = 0 [protected]

Definition at line 26 of file spldoublylinkedlist.inc.

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

Definition at line 24 of file spldoublylinkedlist.inc.

Iterator mode.

See also:
setIteratorMode

Definition at line 46 of file spldoublylinkedlist.inc.

Iterator mode.

See also:
setIteratorMode

Definition at line 36 of file spldoublylinkedlist.inc.

Iterator mode.

See also:
setIteratorMode

Definition at line 41 of file spldoublylinkedlist.inc.

Iterator mode.

See also:
setIteratorMode

Definition at line 31 of file spldoublylinkedlist.inc.


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