note 106843 modified in spldoublylinkedlist.setiteratormode by danbrown

From: Date: Sun, 11 Dec 2011 20:32:34 +0000
Subject: note 106843 modified in spldoublylinkedlist.setiteratormode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-185055@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) ############################# Basically, you should just check the non-zero flags (LIFO(2) and DELETE(1)). Since it's an either/or situation within the pairs, this should be able to help you figure out the separate mode pieces an instance has been set to. example: <?php $l = new SPLDoublyLinkedList(); $l->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE); $mode = $l->getIteratorMode(); $isLIFO = ($mode & SplDoublyLinkedList::IT_MODE_LIFO) == SplDoublyLinkedList::IT_MODE_LIFO; $isDELETE = ($mode & SplDoublyLinkedList::IT_MODE_DELETE) == SplDoublyLinkedList::IT_MODE_DELETE; ?> hope this helps someone. --was-- 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. http://php.net/manual/en/spldoublylinkedlist.setiteratormode.php

« previous php.notes (#185055) next »