#27042 [Csd->Opn]: SPL: SeekableIterator seek() broken

From: Date: Tue, 27 Jan 2004 01:42:29 +0000
Subject: #27042 [Csd->Opn]: SPL: SeekableIterator seek() broken
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-53935@lists.php.net to get a copy of this message
 ID:               27042
 User updated by:  adam at trachtenberg dot com
 Reported By:      adam at trachtenberg dot com
-Status:           Closed
+Status:           Open
 Bug Type:         Zend Engine 2 problem
 Operating System: *
 PHP Version:      5CVS-2004-01-26 (dev)
 Assigned To:      helly
 New Comment:

Thanks for the speedy bug fix. However, I found another 
problem when you seek() to an amount other than -1. In 
those cases, you only get 1 result. For example, using 
the same code as before, but with "3" instead of "-1":

$a = array(1, 2, 3, 4, 5);
foreach (new LimitIterator(new 
NumericArrayIterator($a), 0, 3) as $v)
{
        print "$v\n";
}
foreach (new LimitIterator(new 
SeekableNumericArrayIterator($a), 0, 3)
as $v) {
        print "$v\n";
}


1
2
3
1


Previous Comments:
------------------------------------------------------------------------

[2004-01-26 17:29:18] helly@php.net

This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.



------------------------------------------------------------------------

[2004-01-26 02:16:51] adam at trachtenberg dot com

Description:
------------
When a class implements SeekableIterator and provides a 
seek() method, SPL does not seek correctly. When SPL is 
forced to emulate seek() by advancing step-by-step, it 
works correctly. (See NumericArrayIterator vs 
SeekableNumericArrayIterator.)

At first, I thought spl_limit_it_seek() needed to call:

if (spl_dual_it_has_more(intern TSRMLS_CC) == SUCCESS) {
	spl_dual_it_fetch(intern, 1 TSRMLS_CC);
}

Even for instances of SeekableIterator, but that only 
worked with the offset was 0.

Reproduce code:
---------------
class NumericArrayIterator implements Iterator {
	protected $a;
	protected $i;
	
	public function __construct($a) {
		$this->a = $a;
	}

	public function rewind() {
		$this->i = 0;
	}

	public function hasMore() {
		return $this->i < count($this->a);
	}

	public function key() {
		return $this->i;
	}

	public function current() {
		return $this->a[$this->i];
	}

	public function next() {
		$this->i++;
	}
}

class SeekableNumericArrayIterator extends NumericArrayIterator
implements SeekableIterator {
	public function seek($index) {
		if ($index < count($this->a)) {
			$this->i = $index;
		}
	}
}

$a = array(1, 2, 3, 4, 5);
foreach (new LimitIterator(new NumericArrayIterator($a), 0, -1) as $v)
{
	print "$v\n";
}
foreach (new LimitIterator(new SeekableNumericArrayIterator($a), 0, -1)
as $v) {
	print "$v\n";
}

Expected result:
----------------
1
2
3
4
5
1
2
3
4
5

Actual result:
--------------
1
2
3
4
5


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=27042&edit=1


Thread (5 messages)

« previous php.bugs (#53935) next »