From 5755d152cae3d62a3db1d90be68cca3a918948c1 Mon Sep 17 00:00:00 2001 From: Etienne Kneuss Date: Fri, 16 Jan 2009 22:20:53 +0000 Subject: [PATCH] MFH: Implement SplObjectStorage::addAll/removeAll --- NEWS | 1 + ext/spl/spl_observer.c | 60 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index e750b38d3e..22cba76635 100644 --- a/NEWS +++ b/NEWS @@ -38,6 +38,7 @@ PHP NEWS maximum if required. (Scott) - Added ICU support to SQLite3 when using the bundled version. (Scott) - Added pixelation support in imagefilter(). (Takeshi Abe, Kalle) +- Added SplObjectStorage::addAll/removeAll. (Etienne) - Re-added socket_create_pair() for Windows in sockets extension. (Kalle) diff --git a/ext/spl/spl_observer.c b/ext/spl/spl_observer.c index 5b0eb71b69..926d88d0e1 100755 --- a/ext/spl/spl_observer.c +++ b/ext/spl/spl_observer.c @@ -13,6 +13,7 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Marcus Boerger | + | Etienne Kneuss | +----------------------------------------------------------------------+ */ @@ -237,7 +238,6 @@ spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern } } /* }}} */ - void spl_object_storage_attach(spl_SplObjectStorage *intern, zval *obj, zval *inf TSRMLS_DC) /* {{{ */ { spl_SplObjectStorageElement *pelement, element; @@ -332,6 +332,62 @@ SPL_METHOD(SplObjectStorage, offsetGet) } } /* }}} */ +/* {{{ proto bool SplObjectStorage::addAll(SplObjectStorage $os) + Add all elements contained in $os */ +SPL_METHOD(SplObjectStorage, addAll) +{ + zval *obj; + spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC); + spl_SplObjectStorage *other; + spl_SplObjectStorageElement *element; + HashPosition pos; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { + return; + } + + other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC); + + zend_hash_internal_pointer_reset_ex(&other->storage, &pos); + while (zend_hash_get_current_data_ex(&other->storage, (void **)&element, &pos) == SUCCESS) { + spl_object_storage_attach(intern, element->obj, element->inf TSRMLS_CC); + zend_hash_move_forward_ex(&other->storage, &pos); + } + + zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); + intern->index = 0; + + RETURN_LONG(zend_hash_num_elements(&intern->storage)); +} /* }}} */ + +/* {{{ proto bool SplObjectStorage::removeAll(SplObjectStorage $os) + Remove all elements contained in $os */ +SPL_METHOD(SplObjectStorage, removeAll) +{ + zval *obj; + spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC); + spl_SplObjectStorage *other; + spl_SplObjectStorageElement *element; + HashPosition pos; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { + return; + } + + other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC); + + zend_hash_internal_pointer_reset_ex(&other->storage, &pos); + while (zend_hash_get_current_data_ex(&other->storage, (void **)&element, &pos) == SUCCESS) { + spl_object_storage_detach(intern, element->obj TSRMLS_CC); + zend_hash_move_forward_ex(&other->storage, &pos); + } + + zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); + intern->index = 0; + + RETURN_LONG(zend_hash_num_elements(&intern->storage)); +} /* }}} */ + /* {{{ proto bool SplObjectStorage::contains($obj) Determine whethe an object is contained in the storage */ SPL_METHOD(SplObjectStorage, contains) @@ -618,6 +674,8 @@ static const zend_function_entry spl_funcs_SplObjectStorage[] = { SPL_ME(SplObjectStorage, attach, arginfo_attach, 0) SPL_ME(SplObjectStorage, detach, arginfo_Object, 0) SPL_ME(SplObjectStorage, contains, arginfo_Object, 0) + SPL_ME(SplObjectStorage, addAll, arginfo_Object, 0) + SPL_ME(SplObjectStorage, removeAll, arginfo_Object, 0) SPL_ME(SplObjectStorage, getInfo, NULL, 0) SPL_ME(SplObjectStorage, setInfo, arginfo_setInfo, 0) /* Countable */ -- 2.50.1