note 106843 added to spldoublylinkedlist.setiteratormode
| From: | gregorypatmore at gmail dot com | Date: | Sun, 11 Dec 2011 19:15:01 +0000 |
| Subject: | note 106843 added to spldoublylinkedlist.setiteratormode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185041@lists.php.net to get a copy of this message | ||
Despite the seeming unrelated-ness between the FIFO/LIFO and the KEEP/DELETE option pairs, in
respect to the behavior of setIteratorMode they are in some way linked. Meaning, a second call to
setIteratorMode will erase any previous settings even if they are from the other pair. This,
coupled with the default settings for the object (SplDoublyLinkedList::IT_MODE_FIFO |
SplDoublyLinkedList::IT_MODE_KEEP), means you have to be careful or you can run into trouble
Consider the following example:
<?php
$l = new SPLDoublyLinkedList();
$l->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE);
$l->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO);
$mode = $l->getIteratorMode();
var_dump("MODE: $mode");
var_dump("MODE CHECKS");
var_dump(($mode & SplDoublyLinkedList::IT_MODE_LIFO) == SplDoublyLinkedList::IT_MODE_LIFO);
var_dump(($mode & SplDoublyLinkedList::IT_MODE_FIFO) == SplDoublyLinkedList::IT_MODE_FIFO);
var_dump(($mode & SplDoublyLinkedList::IT_MODE_DELETE) == SplDoublyLinkedList::IT_MODE_DELETE);
var_dump(($mode & SplDoublyLinkedList::IT_MODE_KEEP) == SplDoublyLinkedList::IT_MODE_KEEP);
$l->push('A');
$l->push('B');
$l->push('C');
$l->push('D');
$l->rewind();
var_dump("Traversing");
var_dump($l->isEmpty());
var_dump($l->count());
var_dump($l->current());
$l->next();
var_dump($l->count());
var_dump($l->current());
$l->next();
var_dump($l->count());
var_dump($l->current());
$l->next();
var_dump($l->count());
var_dump($l->current());
$l->next();
var_dump($l->count());
var_dump($l->isEmpty());
?>
Which outputs the following:
#############################
string(7) "MODE: 2"
string(11) "MODE CHECKS"
bool(true) #LIFO (ok)
bool(true) #FIFO (umm...wait a minute)
bool(false)#DELETE (houston... wtf)
bool(true) #KEEP (ok, where's the camera hidden)
string(10) "Traversing"
bool(false)
int(4)
string(1) "D"
int(4)
string(1) "C"
int(4)
string(1) "B"
int(4)
string(1) "A"
int(4)
bool(false)
#############################
hope this helps someone.
----
Server IP: 69.147.83.197
Probable Submitter: 108.50.197.183
----
Manual Page -- http://www.php.net/manual/en/spldoublylinkedlist.setiteratormode.php
Edit -- https://master.php.net/note/edit/106843
Del: integrated -- https://master.php.net/note/delete/106843/integrated
Del: useless -- https://master.php.net/note/delete/106843/useless
Del: bad code -- https://master.php.net/note/delete/106843/bad+code
Del: spam -- https://master.php.net/note/delete/106843/spam
Del: non-english -- https://master.php.net/note/delete/106843/non-english
Del: in docs -- https://master.php.net/note/delete/106843/in+docs
Del: other reasons-- https://master.php.net/note/delete/106843
Reject -- https://master.php.net/note/reject/106843
Search -- https://master.php.net/manage/user-notes.php