Digging into the PHP 5.3 SplDoublyLinkedList (part 2)

After our first entry, I wanted to take some time to show some example usage of the SplDoublyLinkedList since the documentation is very minimal at the present time.

Lets start with following code:

$dll = new SplDoublyLinkedList();
echo "IS EMPTY ON EMPTY\n";
var_dump($dll->isEmpty());

echo "OFFSET EXISTS ON EMPTY\n";
var_dump($dll->offsetExists(0));

Here, we are instantiating the SplDoublyLinkedList class, and testing the isEmpty and offsetExists methods. The output of the above code is:


IS EMPTY ON EMPTY
bool(true)
OFFSET EXISTS ON EMPTY
bool(false)

We can see the the isEmpty method returns true because we haven’t populated any items in our SplDoublyLinkedList. In addition to this, we can see that the offsetExists method is returning false for the same reason (because its empty).

Now, lets populate our list with some simple data and run these methods again:

$dll->push(2);
$dll->push(3);
$dll->push(4);
echo "IS EMPTY NOT EMPTY\n";
var_dump($dll->isEmpty());

echo "OFFSET EXISTS NOT EMPTY\n";
var_dump($dll->offsetExists(0));

The output of the above code is:


IS EMPTY NOT EMPTY
bool(false)
OFFSET EXISTS NOT EMPTY
bool(true)

Now, that we have some data in our list we can see the positive results from the isEmpty and offsetExists methods.

Using the same list that we just populated, lets try out some of the other simplier methods:

echo "BOTTOM: " . $dll->bottom() . "\n";
echo "TOP: " . $dll->top() . "\n";
echo "COUNT: " . $dll->count() . "\n";

The resulting output of the above code:

BOTTOM: 2
TOP: 4
COUNT: 3

The bottom method returns the first item in our list, while the top method returns the last item in our list. The count method is obvious, so there is no need to go into that any further.

Now, lets do some iteration:

foreach ($dll as $k => $v) {
    echo "$k=>$v\n";
}
$dll->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP);
foreach ($dll as $k => $v) {
    echo "$k=>$v\n";
}

Both of the above code blocks result in the same output:

0=>2
1=>3
2=>4

This reason that both of these are identical, is because the default iterator mode options are SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP. Nothing really new here, so lets explore some of the iterator options:

$dll->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO);
foreach ($dll as $k => $v) {
    echo "$k=>$v\n";
}

Above, we are using the IT_MODE_LIFO flag which lets us traverse our list in reverse… so the output is:


2=>4
1=>3
0=>2

The IT_MODE_LIFO option can be really convenient, especially if it means we can avoid the use of array_reverse function with arrays.

The last flag I’m going to cover is IT_MODE_DELETE, which deletes the items from the list as we iterate through it.

Here is an example using the IT_MODE_DELETE:

echo "COUNT BEFORE: " . $dll->count() . "\n";
$dll->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE);
foreach ($dll as $k => $v) {
    echo "$k=>$v\n";
}
echo "COUNT AFTER: " . $dll->count() . "\n";

Above, we echo out the count of the items in our list. After echoing out the count we iterate through our list then echo out the count one last time.

The above code results in the output:

COUNT BEFORE: 3
0=>2
0=>3
0=>4
COUNT AFTER: 0

Just as we expect, the list is empty after the iteration.

Now, lets explore some iteration using a while statement.

$dll = new SplDoublyLinkedList();

$dll->push(2);
$dll->push(3);
$dll->push(4);

$dll->rewind();

while ($tmp = $dll->current()) {
  echo "ITEM: $tmp\n";
  $dll->next();
}

One important note on the above code is that if the rewind method is removed, the current method returns a false value so no iteration is done.

I don’t feel it necessary to detail the remaining methods of SplDoublyLinkedList because they seem self-explanatory to me… correct me if I’m wrong.

~ by ityndall on October 28, 2010.

7 Responses to “Digging into the PHP 5.3 SplDoublyLinkedList (part 2)”

  1. hi!This was a really wonderful website!
    I come from itlay, I was fortunate to come cross your subject in baidu
    Also I obtain much in your theme really thanks very much i will come again

  2. Sweet site, I hadn’t noticed blog.ianty.com before in my searches!

  3. It is remarkable, it is the valuable information

  4. Your phrase, simply charm

  5. Happy New Yearhttp://nfksghjskf.com/ – , everyone! 🙂

  6. What excellent words

  7. Every nice explaination. Thank you very much. I am just starting out with spl and the iteration_modes really puzzled me. Ty once again

Leave a Reply