cvs: php-src /ext/spl spl.php spl_array.c /ext/spl/examples class_tree.php
| From: | Marcus Boerger | Date: | Mon, 03 Oct 2005 13:34:02 +0000 |
| Subject: | cvs: php-src /ext/spl spl.php spl_array.c /ext/spl/examples class_tree.php | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-34257@lists.php.net to get a copy of this message | ||
helly Mon Oct 3 09:34:02 2005 EDT
Modified files:
/php-src/ext/spl spl.php spl_array.c
/php-src/ext/spl/examples class_tree.php
Log:
- Add more sorting funcs to ArrayObject/Iterator
http://cvs.php.net/diff.php/php-src/ext/spl/spl.php?r1=1.58&r2=1.59&ty=u Index: php-src/ext/spl/spl.php diff -u php-src/ext/spl/spl.php:1.58 php-src/ext/spl/spl.php:1.59 --- php-src/ext/spl/spl.php:1.58 Sat Sep 24 19:18:14 2005 +++ php-src/ext/spl/spl.php Mon Oct 3 09:34:00 2005 @@ -502,7 +502,7 @@ /** @ingroup SPL * @brief An Array wrapper * @since PHP 5.0 - * @version 1.1 + * @version 1.2 * * This array wrapper allows to recursively iterate over Arrays and public * Object properties. @@ -540,6 +540,30 @@ */ function getFlags(); + /** Sort the entries by values. + */ + function asort(); + + /** Sort the entries by key. + */ + function ksort(); + + /** Sort the entries by values using user defined function. + */ + function uasort(mixed cmp_function); + + /** Sort the entries by key using user defined function. + */ + function uksort(mixed cmp_function); + + /** Sort the entries by values using "natural order" algorithm. + */ + function natsort(); + + /** Sort the entries by values using case insensitive "natural order" algorithm. + */ + function natcasesort(); + /** * @param $array new array or object */ @@ -598,7 +622,7 @@ /** @ingroup SPL * @brief An Array iterator * @since PHP 5.0 - * @version 1.1 + * @version 1.2 * * This iterator allows to unset and modify values and keys while iterating * over Arrays and Objects. @@ -632,12 +656,36 @@ * 1 set: array indices can be accessed as properties in read/write */ function setFlags($flags); - + /** * @ return current flags */ function getFlags(); + /** Sort the entries by values. + */ + function asort(); + + /** Sort the entries by key. + */ + function ksort(); + + /** Sort the entries by values using user defined function. + */ + function uasort(mixed cmp_function); + + /** Sort the entries by key using user defined function. + */ + function uksort(mixed cmp_function); + + /** Sort the entries by values using "natural order" algorithm. + */ + function natsort(); + + /** Sort the entries by values using case insensitive "natural order" algorithm. + */ + function natcasesort(); + /** @param $index offset to inspect * @return whetehr offset $index esists */ http://cvs.php.net/diff.php/php-src/ext/spl/spl_array.c?r1=1.82&r2=1.83&ty=u Index: php-src/ext/spl/spl_array.c diff -u php-src/ext/spl/spl_array.c:1.82 php-src/ext/spl/spl_array.c:1.83 --- php-src/ext/spl/spl_array.c:1.82 Mon Oct 3 08:55:08 2005 +++ php-src/ext/spl/spl_array.c Mon Oct 3 09:34:00 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: spl_array.c,v 1.82 2005/10/03 12:55:08 helly Exp $ */ +/* $Id: spl_array.c,v 1.83 2005/10/03 13:34:00 helly Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" @@ -1051,34 +1051,62 @@ RETURN_LONG(count); } /* }}} */ -static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len) +static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(getThis() TSRMLS_CC); HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); - zval tmp; + zval tmp, **arg; INIT_PZVAL(&tmp); Z_TYPE(tmp) = IS_ARRAY; Z_ARRVAL(tmp) = aht; - zend_call_method(NULL, NULL, NULL, fname, fname_len, &return_value, 1, &tmp, NULL TSRMLS_CC); + if (use_arg) { + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { + zend_throw_exception(U_CLASS_ENTRY(spl_ce_BadMethodCallException), "Function expects exactly one argument", 0 TSRMLS_CC); + return; + } + zend_call_method(NULL, NULL, NULL, fname, fname_len, &return_value, 2, &tmp, *arg TSRMLS_CC); + } else { + zend_call_method(NULL, NULL, NULL, fname, fname_len, &return_value, 1, &tmp, NULL TSRMLS_CC); + } } -#define SPL_ARRAY_METHOD(cname, fname) \ +#define SPL_ARRAY_METHOD(cname, fname, use_arg) \ SPL_METHOD(cname, fname) \ { \ - spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1); \ + spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \ } /* {{{ proto int ArrayObject::asort() proto int ArrayIterator::asort() Sort the entries by values. */ -SPL_ARRAY_METHOD(Array, asort) +SPL_ARRAY_METHOD(Array, asort, 0) /* {{{ proto int ArrayObject::ksort() proto int ArrayIterator::ksort() Sort the entries by key. */ -SPL_ARRAY_METHOD(Array, ksort) +SPL_ARRAY_METHOD(Array, ksort, 0) + +/* {{{ proto int ArrayObject::uasort(callback cmp_function) + proto int ArrayIterator::uasort(callback cmp_function) + Sort the entries by values user defined function. */ +SPL_ARRAY_METHOD(Array, uasort, 1) + +/* {{{ proto int ArrayObject::uksort(callback cmp_function) + proto int ArrayIterator::uksort(callback cmp_function) + Sort the entries by key using user defined function. */ +SPL_ARRAY_METHOD(Array, uksort, 1) + +/* {{{ proto int ArrayObject::natsort() + proto int ArrayIterator::natsort() + Sort the entries by values using "natural order" algorithm. */ +SPL_ARRAY_METHOD(Array, natsort, 0) + +/* {{{ proto int ArrayObject::natcasesort() + proto int ArrayIterator::natcasesort() + Sort the entries by key using case insensitive "natural order" algorithm. */ +SPL_ARRAY_METHOD(Array, natcasesort, 0) /* {{{ proto mixed|NULL ArrayIterator::current() Return current array entry */ @@ -1282,6 +1310,11 @@ ZEND_ARG_INFO(0, iteratorClass) ZEND_END_ARG_INFO(); +static +ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0) + ZEND_ARG_INFO(0, cmp_function ) +ZEND_END_ARG_INFO(); + static zend_function_entry spl_funcs_ArrayObject[] = { SPL_ME(Array, __construct, arginfo_array___construct, ZEND_ACC_PUBLIC) SPL_ME(Array, offsetExists, arginfo_array_offsetGet, ZEND_ACC_PUBLIC) @@ -1295,6 +1328,10 @@ SPL_ME(Array, setFlags, arginfo_array_setFlags, ZEND_ACC_PUBLIC) SPL_ME(Array, asort, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, ksort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, uasort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, uksort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, natsort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, natcasesort, NULL, ZEND_ACC_PUBLIC) /* ArrayObject specific */ SPL_ME(Array, getIterator, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, exchangeArray, arginfo_array_exchangeArray, ZEND_ACC_PUBLIC) @@ -1316,6 +1353,10 @@ SPL_ME(Array, setFlags, arginfo_array_setFlags, ZEND_ACC_PUBLIC) SPL_ME(Array, asort, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, ksort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, uasort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, uksort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, natsort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, natcasesort, NULL, ZEND_ACC_PUBLIC) /* ArrayIterator specific */ SPL_ME(Array, rewind, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, current, NULL, ZEND_ACC_PUBLIC) http://cvs.php.net/diff.php/php-src/ext/spl/examples/class_tree.php?r1=1.3&r2=1.4&ty=u Index: php-src/ext/spl/examples/class_tree.php diff -u php-src/ext/spl/examples/class_tree.php:1.3 php-src/ext/spl/examples/class_tree.php:1.4 --- php-src/ext/spl/examples/class_tree.php:1.3 Mon Oct 3 08:55:11 2005 +++ php-src/ext/spl/examples/class_tree.php Mon Oct 3 09:34:01 2005 @@ -61,7 +61,7 @@ } } } - $this->ksort(); + $this->uksort('strnatcasecmp'); } function current()
http://cvs.php.net/diff.php/php-src/ext/spl/spl.php?r1=1.58&r2=1.59&ty=u Index: php-src/ext/spl/spl.php diff -u php-src/ext/spl/spl.php:1.58 php-src/ext/spl/spl.php:1.59 --- php-src/ext/spl/spl.php:1.58 Sat Sep 24 19:18:14 2005 +++ php-src/ext/spl/spl.php Mon Oct 3 09:34:00 2005 @@ -502,7 +502,7 @@ /** @ingroup SPL * @brief An Array wrapper * @since PHP 5.0 - * @version 1.1 + * @version 1.2 * * This array wrapper allows to recursively iterate over Arrays and public * Object properties. @@ -540,6 +540,30 @@ */ function getFlags(); + /** Sort the entries by values. + */ + function asort(); + + /** Sort the entries by key. + */ + function ksort(); + + /** Sort the entries by values using user defined function. + */ + function uasort(mixed cmp_function); + + /** Sort the entries by key using user defined function. + */ + function uksort(mixed cmp_function); + + /** Sort the entries by values using "natural order" algorithm. + */ + function natsort(); + + /** Sort the entries by values using case insensitive "natural order" algorithm. + */ + function natcasesort(); + /** * @param $array new array or object */ @@ -598,7 +622,7 @@ /** @ingroup SPL * @brief An Array iterator * @since PHP 5.0 - * @version 1.1 + * @version 1.2 * * This iterator allows to unset and modify values and keys while iterating * over Arrays and Objects. @@ -632,12 +656,36 @@ * 1 set: array indices can be accessed as properties in read/write */ function setFlags($flags); - + /** * @ return current flags */ function getFlags(); + /** Sort the entries by values. + */ + function asort(); + + /** Sort the entries by key. + */ + function ksort(); + + /** Sort the entries by values using user defined function. + */ + function uasort(mixed cmp_function); + + /** Sort the entries by key using user defined function. + */ + function uksort(mixed cmp_function); + + /** Sort the entries by values using "natural order" algorithm. + */ + function natsort(); + + /** Sort the entries by values using case insensitive "natural order" algorithm. + */ + function natcasesort(); + /** @param $index offset to inspect * @return whetehr offset $index esists */ http://cvs.php.net/diff.php/php-src/ext/spl/spl_array.c?r1=1.82&r2=1.83&ty=u Index: php-src/ext/spl/spl_array.c diff -u php-src/ext/spl/spl_array.c:1.82 php-src/ext/spl/spl_array.c:1.83 --- php-src/ext/spl/spl_array.c:1.82 Mon Oct 3 08:55:08 2005 +++ php-src/ext/spl/spl_array.c Mon Oct 3 09:34:00 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: spl_array.c,v 1.82 2005/10/03 12:55:08 helly Exp $ */ +/* $Id: spl_array.c,v 1.83 2005/10/03 13:34:00 helly Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" @@ -1051,34 +1051,62 @@ RETURN_LONG(count); } /* }}} */ -static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len) +static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(getThis() TSRMLS_CC); HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); - zval tmp; + zval tmp, **arg; INIT_PZVAL(&tmp); Z_TYPE(tmp) = IS_ARRAY; Z_ARRVAL(tmp) = aht; - zend_call_method(NULL, NULL, NULL, fname, fname_len, &return_value, 1, &tmp, NULL TSRMLS_CC); + if (use_arg) { + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { + zend_throw_exception(U_CLASS_ENTRY(spl_ce_BadMethodCallException), "Function expects exactly one argument", 0 TSRMLS_CC); + return; + } + zend_call_method(NULL, NULL, NULL, fname, fname_len, &return_value, 2, &tmp, *arg TSRMLS_CC); + } else { + zend_call_method(NULL, NULL, NULL, fname, fname_len, &return_value, 1, &tmp, NULL TSRMLS_CC); + } } -#define SPL_ARRAY_METHOD(cname, fname) \ +#define SPL_ARRAY_METHOD(cname, fname, use_arg) \ SPL_METHOD(cname, fname) \ { \ - spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1); \ + spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \ } /* {{{ proto int ArrayObject::asort() proto int ArrayIterator::asort() Sort the entries by values. */ -SPL_ARRAY_METHOD(Array, asort) +SPL_ARRAY_METHOD(Array, asort, 0) /* {{{ proto int ArrayObject::ksort() proto int ArrayIterator::ksort() Sort the entries by key. */ -SPL_ARRAY_METHOD(Array, ksort) +SPL_ARRAY_METHOD(Array, ksort, 0) + +/* {{{ proto int ArrayObject::uasort(callback cmp_function) + proto int ArrayIterator::uasort(callback cmp_function) + Sort the entries by values user defined function. */ +SPL_ARRAY_METHOD(Array, uasort, 1) + +/* {{{ proto int ArrayObject::uksort(callback cmp_function) + proto int ArrayIterator::uksort(callback cmp_function) + Sort the entries by key using user defined function. */ +SPL_ARRAY_METHOD(Array, uksort, 1) + +/* {{{ proto int ArrayObject::natsort() + proto int ArrayIterator::natsort() + Sort the entries by values using "natural order" algorithm. */ +SPL_ARRAY_METHOD(Array, natsort, 0) + +/* {{{ proto int ArrayObject::natcasesort() + proto int ArrayIterator::natcasesort() + Sort the entries by key using case insensitive "natural order" algorithm. */ +SPL_ARRAY_METHOD(Array, natcasesort, 0) /* {{{ proto mixed|NULL ArrayIterator::current() Return current array entry */ @@ -1282,6 +1310,11 @@ ZEND_ARG_INFO(0, iteratorClass) ZEND_END_ARG_INFO(); +static +ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0) + ZEND_ARG_INFO(0, cmp_function ) +ZEND_END_ARG_INFO(); + static zend_function_entry spl_funcs_ArrayObject[] = { SPL_ME(Array, __construct, arginfo_array___construct, ZEND_ACC_PUBLIC) SPL_ME(Array, offsetExists, arginfo_array_offsetGet, ZEND_ACC_PUBLIC) @@ -1295,6 +1328,10 @@ SPL_ME(Array, setFlags, arginfo_array_setFlags, ZEND_ACC_PUBLIC) SPL_ME(Array, asort, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, ksort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, uasort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, uksort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, natsort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, natcasesort, NULL, ZEND_ACC_PUBLIC) /* ArrayObject specific */ SPL_ME(Array, getIterator, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, exchangeArray, arginfo_array_exchangeArray, ZEND_ACC_PUBLIC) @@ -1316,6 +1353,10 @@ SPL_ME(Array, setFlags, arginfo_array_setFlags, ZEND_ACC_PUBLIC) SPL_ME(Array, asort, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, ksort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, uasort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, uksort, arginfo_array_uXsort, ZEND_ACC_PUBLIC) + SPL_ME(Array, natsort, NULL, ZEND_ACC_PUBLIC) + SPL_ME(Array, natcasesort, NULL, ZEND_ACC_PUBLIC) /* ArrayIterator specific */ SPL_ME(Array, rewind, NULL, ZEND_ACC_PUBLIC) SPL_ME(Array, current, NULL, ZEND_ACC_PUBLIC) http://cvs.php.net/diff.php/php-src/ext/spl/examples/class_tree.php?r1=1.3&r2=1.4&ty=u Index: php-src/ext/spl/examples/class_tree.php diff -u php-src/ext/spl/examples/class_tree.php:1.3 php-src/ext/spl/examples/class_tree.php:1.4 --- php-src/ext/spl/examples/class_tree.php:1.3 Mon Oct 3 08:55:11 2005 +++ php-src/ext/spl/examples/class_tree.php Mon Oct 3 09:34:01 2005 @@ -61,7 +61,7 @@ } } } - $this->ksort(); + $this->uksort('strnatcasecmp'); } function current()