From: Scott MacVicar Date: Wed, 17 Jun 2009 12:57:38 +0000 (+0000) Subject: Add SplDoublyLinkedList::prev(), no point in having a DLL that only goes one way X-Git-Tag: php-5.4.0alpha1~191^2~3297 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=88994ce758c4a777920bbcad3775a0c3bae188ca;p=php Add SplDoublyLinkedList::prev(), no point in having a DLL that only goes one way --- diff --git a/ext/spl/spl_dllist.c b/ext/spl/spl_dllist.c index dde49a8755..6783e24148 100644 --- a/ext/spl/spl_dllist.c +++ b/ext/spl/spl_dllist.c @@ -350,6 +350,7 @@ static void spl_dllist_object_free_storage(void *object TSRMLS_DC) /* {{{ */ } spl_ptr_llist_destroy(intern->llist TSRMLS_CC); + SPL_LLIST_CHECK_DELREF(intern->traverse_pointer); zval_ptr_dtor(&intern->retval); efree(object); @@ -1033,6 +1034,16 @@ SPL_METHOD(SplDoublyLinkedList, key) } /* }}} */ +/* {{{ proto void SplDoublyLinkedList::prev() U + Move to next entry */ +SPL_METHOD(SplDoublyLinkedList, prev) +{ + spl_dllist_object *intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC); + + spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO TSRMLS_CC); +} +/* }}} */ + /* {{{ proto void SplDoublyLinkedList::next() U Move to next entry */ SPL_METHOD(SplDoublyLinkedList, next) @@ -1161,6 +1172,7 @@ static const zend_function_entry spl_funcs_SplDoublyLinkedList[] = { SPL_ME(SplDoublyLinkedList, current, NULL, ZEND_ACC_PUBLIC) SPL_ME(SplDoublyLinkedList, key, NULL, ZEND_ACC_PUBLIC) SPL_ME(SplDoublyLinkedList, next, NULL, ZEND_ACC_PUBLIC) + SPL_ME(SplDoublyLinkedList, prev, NULL, ZEND_ACC_PUBLIC) SPL_ME(SplDoublyLinkedList, valid, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; diff --git a/ext/spl/tests/dllist_010.phpt b/ext/spl/tests/dllist_010.phpt new file mode 100644 index 0000000000..685f735d6b --- /dev/null +++ b/ext/spl/tests/dllist_010.phpt @@ -0,0 +1,27 @@ +--TEST-- +SPL: DoublyLinkedList: prev +--FILE-- +push(1); +$dll->push(2); +$dll->push(3); +$dll->push(4); + + +$dll->rewind(); +echo $dll->current()."\n"; +$dll->next(); +$dll->next(); +echo $dll->current()."\n"; +$dll->prev(); +echo $dll->current()."\n"; + +?> +===DONE=== + +--EXPECTF-- +1 +3 +2 +===DONE=== diff --git a/ext/spl/tests/dllist_memleak.phpt b/ext/spl/tests/dllist_memleak.phpt new file mode 100644 index 0000000000..9bae68bf2e --- /dev/null +++ b/ext/spl/tests/dllist_memleak.phpt @@ -0,0 +1,24 @@ +--TEST-- +SPL: DoublyLinkedList: memory leak when iterator pointer isn't at the last element +--FILE-- +push(1); +$dll->push(2); +$dll->push(3); +$dll->push(4); + + +$dll->rewind(); +echo $dll->current()."\n"; +$dll->next(); +$dll->next(); +echo $dll->current()."\n"; + +?> +===DONE=== + +--EXPECTF-- +1 +3 +===DONE===