PHP 8.4.0 RC4 available for testing

SplDoublyLinkedList::setIteratorMode

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SplDoublyLinkedList::setIteratorMode反復処理のモードを設定する

説明

public SplDoublyLinkedList::setIteratorMode(int $mode): int

パラメータ

mode

次の 2 種類の直交するモードを設定できます。

デフォルトのモードは SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLnkedList::IT_MODE_KEEP です。

警告

SplStackSplQueue においては、反復処理の方向は変更できません。 これらのクラスでは、反復処理の方向は常に SplDoublyLinkedList::IT_MODE_FIFO になります。 この値を変更しようとすると、RuntimeException がスローされます。

戻り値

反復処理に影響する、異なるモードとフラグの値を返します。

add a note

User Contributed Notes 2 notes

up
3
gregorypatmore at gmail dot com
12 years ago
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.
up
2
pritiatwork at gmail dot com
13 years ago
Usage of different Iterator Mode

<?php
$doubly
=new SplDoublyLinkedList();
$doubly->push(array('name'=>'Naruto'));
$doubly->push(array('name'=>'Sakura'));
$doubly->push(array('name'=>'Neji'));
$doubly->push(array('name'=>'Sasuke'));
var_dump($doubly);
echo
'<br/> FIFO Traversing<br/>';
$doubly->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP);
$doubly->rewind();
foreach(
$doubly as $key=>$value)
{
echo
'<br/>Traversed:'.$key.' '.$value['name'];
}
echo
'<br/>LIFO Traversing - Keep mode <br/>';
$doubly->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP);
$doubly->rewind();
foreach(
$doubly as $key=>$value)
{
echo
'<br/>Traversed:'.$key.' '.$value['name'];
}

echo
'<br/>LIFO Traversing - Delete mode <br/>';
$doubly->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_DELETE);
$doubly->rewind();

foreach(
$doubly as $key=>$value)
{
if(
$key == 2) break;
echo
'<br/>Traversed:'.$key.' '.$value['name'];
}
var_dump($doubly);
?>

Output:

FIFO Traversing

Traversed:0 Naruto
Traversed:1 Sakura
Traversed:2 Neji
Traversed:3 Sasuke
LIFO Traversing - Keep mode

Traversed:3 Sasuke
Traversed:2 Neji
Traversed:1 Sakura
Traversed:0 Naruto
LIFO Traversing - Delete mode

Traversed:3 Sasuke
object(SplDoublyLinkedList)#1 (2) {
["flags":"SplDoublyLinkedList":private]=>
int(3)
["dllist":"SplDoublyLinkedList":private]=>
array(3) {
[0]=>
array(1) {
["name"]=>
string(6) "Naruto"
}
[1]=>
array(1) {
["name"]=>
string(6) "Sakura"
}
[2]=>
array(1) {
["name"]=>
string(4) "Neji"
}
}
}
To Top