cvs: php-src /ext/spl php_spl.c spl_iterators.c spl_iterators.h /ext/spl/examples infiniteiterator.inc /ext/spl/internal infiniteiterator.inc
/ext/spl/tests iterator_008.phpt

From: Date: Sun, 31 Oct 2004 19:49:18 +0000
Subject: cvs: php-src /ext/spl php_spl.c spl_iterators.c spl_iterators.h /ext/spl/examples infiniteiterator.inc /ext/spl/internal infiniteiterator.inc
/ext/spl/tests iterator_008.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-29071@lists.php.net to get a copy of this message
helly Sun Oct 31 14:49:18 2004 EDT Added files: /php-src/ext/spl/internal infiniteiterator.inc /php-src/ext/spl/tests iterator_008.phpt Removed files: /php-src/ext/spl/examples infiniteiterator.inc Modified files: /php-src/ext/spl php_spl.c spl_iterators.c spl_iterators.h Log: - Implement InfiniteIterator in C http://cvs.php.net/diff.php/php-src/ext/spl/php_spl.c?r1=1.31&r2=1.32&ty=u Index: php-src/ext/spl/php_spl.c diff -u php-src/ext/spl/php_spl.c:1.31 php-src/ext/spl/php_spl.c:1.32 --- php-src/ext/spl/php_spl.c:1.31 Sun Oct 31 13:43:00 2004 +++ php-src/ext/spl/php_spl.c Sun Oct 31 14:49:15 2004 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_spl.c,v 1.31 2004/10/31 18:43:00 helly Exp $ */ +/* $Id: php_spl.c,v 1.32 2004/10/31 19:49:15 helly Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -167,6 +167,7 @@ SPL_ADD_CLASS(CachingRecursiveIterator, z_list, sub, allow, ce_flags); \ SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \ SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \ + SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \ SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \ SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \ SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \ http://cvs.php.net/diff.php/php-src/ext/spl/spl_iterators.c?r1=1.46&r2=1.47&ty=u Index: php-src/ext/spl/spl_iterators.c diff -u php-src/ext/spl/spl_iterators.c:1.46 php-src/ext/spl/spl_iterators.c:1.47 --- php-src/ext/spl/spl_iterators.c:1.46 Sun Oct 31 14:05:19 2004 +++ php-src/ext/spl/spl_iterators.c Sun Oct 31 14:49:15 2004 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: spl_iterators.c,v 1.46 2004/10/31 19:05:19 helly Exp $ */ +/* $Id: spl_iterators.c,v 1.47 2004/10/31 19:49:15 helly Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" @@ -47,6 +47,7 @@ zend_class_entry *spl_ce_OuterIterator; zend_class_entry *spl_ce_IteratorIterator; zend_class_entry *spl_ce_NoRewindIterator; +zend_class_entry *spl_ce_InfiniteIterator; function_entry spl_funcs_RecursiveIterator[] = { SPL_ABSTRACT_ME(RecursiveIterator, hasChildren, NULL) @@ -1440,7 +1441,37 @@ {NULL, NULL, NULL} }; -zend_object_iterator_funcs spl_norewind_it_iterator_funcs; +/* {{{ proto InfiniteIterator::__construct(Iterator it) + Create an iterator from another iterator */ +SPL_METHOD(InfiniteIterator, __construct) +{ + spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, zend_ce_iterator, DIT_InfiniteIterator); +} /* }}} */ + +/* {{{ proto InfiniteIterator::next() + Prevent a call to inner iterators rewind() (internally the current data will be fetched if valid()) */ +SPL_METHOD(InfiniteIterator, next) +{ + spl_dual_it_object *intern; + + intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC); + + spl_dual_it_next(intern, 1 TSRMLS_CC); + if (spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) { + spl_dual_it_fetch(intern, 0 TSRMLS_CC); + } else { + spl_dual_it_rewind(intern TSRMLS_CC); + if (spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) { + spl_dual_it_fetch(intern, 0 TSRMLS_CC); + } + } + +} /* }}} */ + +static zend_function_entry spl_funcs_InfiniteIterator[] = { + SPL_ME(InfiniteIterator, __construct, arginfo_norewind_it___construct, ZEND_ACC_PUBLIC) + SPL_ME(InfiniteIterator, next, NULL, ZEND_ACC_PUBLIC) +}; /* {{{ array iterator_to_array(IteratorAggregate it) Copy the iterator into an array */ @@ -1575,6 +1606,8 @@ REGISTER_SPL_IMPLEMENTS(IteratorIterator, OuterIterator); REGISTER_SPL_IMPLEMENTS(NoRewindIterator, OuterIterator); + REGISTER_SPL_SUB_CLASS_EX(InfiniteIterator, IteratorIterator, spl_dual_it_new, spl_funcs_InfiniteIterator); + return SUCCESS; } /* }}} */ http://cvs.php.net/diff.php/php-src/ext/spl/spl_iterators.h?r1=1.12&r2=1.13&ty=u Index: php-src/ext/spl/spl_iterators.h diff -u php-src/ext/spl/spl_iterators.h:1.12 php-src/ext/spl/spl_iterators.h:1.13 --- php-src/ext/spl/spl_iterators.h:1.12 Sun Oct 31 13:43:00 2004 +++ php-src/ext/spl/spl_iterators.h Sun Oct 31 14:49:15 2004 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: spl_iterators.h,v 1.12 2004/10/31 18:43:00 helly Exp $ */ +/* $Id: spl_iterators.h,v 1.13 2004/10/31 19:49:15 helly Exp $ */ #ifndef SPL_ITERATORS_H #define SPL_ITERATORS_H @@ -35,6 +35,7 @@ extern zend_class_entry *spl_ce_OuterIterator; extern zend_class_entry *spl_ce_IteratorIterator; extern zend_class_entry *spl_ce_NoRewindIterator; +extern zend_class_entry *spl_ce_InfiniteIterator; PHP_MINIT_FUNCTION(spl_iterators); @@ -48,6 +49,7 @@ DIT_CachingRecursiveIterator, DIT_IteratorIterator, DIT_NoRewindIterator, + DIT_InfiniteIterator, } dual_it_type; enum { http://cvs.php.net/co.php/php-src/ext/spl/internal/infiniteiterator.inc?r=1.1&p=1 Index: php-src/ext/spl/internal/infiniteiterator.inc +++ php-src/ext/spl/internal/infiniteiterator.inc <?php /** @file infiniteiterator.inc * @ingroup SPL * @brief class InfiniteIterator * @author Marcus Boerger * @date 2003 - 2004 * * SPL - Standard PHP Library */ /** @ingroup Examples * @brief An infinite Iterator * @author Marcus Boerger * @version 1.1 * * This Iterator takes another Iterator and infinitvely iterates it by * rewinding it when its end is reached. * * \note Even an InfiniteIterator stops if its inner Iterator is empty. * \verbatim $it = new ArrayIterator(array(1,2,3)); $infinite = new InfiniteIterator($it); $limit = new LimitIterator($infinite, 0, 5); foreach($limit as $val=>$key) { echo "$val=>$key\n"; } \endverbatim */ class InfiniteIterator extends IteratorIterator { /** Move the inner Iterator forward to its next element or rewind it. * @return void */ function next() { $this->getInnerIterator()->next(); if (!$this->getInnerIterator()->valid()) { $this->getInnerIterator()->rewind(); } } } ?> http://cvs.php.net/co.php/php-src/ext/spl/tests/iterator_008.phpt?r=1.1&p=1 Index: php-src/ext/spl/tests/iterator_008.phpt +++ php-src/ext/spl/tests/iterator_008.phpt --TEST-- SPL: InfiniteIterator --SKIPIF-- <?php if (!extension_loaded("spl")) print "skip"; ?> --FILE-- <?php class ArrayIteratorEx extends ArrayIterator { function rewind() { echo __METHOD__ . "\n"; parent::rewind(); } function valid() { echo __METHOD__ . "\n"; return parent::valid(); } function current() { echo __METHOD__ . "\n"; return parent::current(); } function key() { echo __METHOD__ . "\n"; return parent::key(); } function next() { echo __METHOD__ . "\n"; parent::next(); } } $it = new InfiniteIterator(new ArrayIteratorEx(range(0,2))); $pos =0; foreach ($it as $v) { var_dump($v); if ($pos++ > 5) { break; } } ?> ===DONE=== <?php exit(0); ?> --EXPECT-- ArrayIteratorEx::rewind ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(0) ArrayIteratorEx::next ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(1) ArrayIteratorEx::next ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(2) ArrayIteratorEx::next ArrayIteratorEx::valid ArrayIteratorEx::rewind ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(0) ArrayIteratorEx::next ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(1) ArrayIteratorEx::next ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(2) ArrayIteratorEx::next ArrayIteratorEx::valid ArrayIteratorEx::rewind ArrayIteratorEx::valid ArrayIteratorEx::current ArrayIteratorEx::key int(0) ===DONE===

« previous php.cvs (#29071) next »