From: Marcus Boerger Date: Mon, 3 Oct 2005 13:34:01 +0000 (+0000) Subject: - Add more sorting funcs to ArrayObject/Iterator X-Git-Tag: RELEASE_0_9_0~28 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=94274df4e059b64b2062ab3adb78750e0d35397c;p=php - Add more sorting funcs to ArrayObject/Iterator --- diff --git a/ext/spl/examples/class_tree.php b/ext/spl/examples/class_tree.php index d10983a11f..b57eba373e 100755 --- a/ext/spl/examples/class_tree.php +++ b/ext/spl/examples/class_tree.php @@ -61,7 +61,7 @@ class SubClasses extends RecursiveArrayIterator } } } - $this->ksort(); + $this->uksort('strnatcasecmp'); } function current() diff --git a/ext/spl/spl.php b/ext/spl/spl.php index 836d4eaf62..ef219de88f 100755 --- a/ext/spl/spl.php +++ b/ext/spl/spl.php @@ -502,7 +502,7 @@ interface Serializable /** @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 @@ class ArrayObject implements IteratorAggregate, ArrayAccess, Countable */ 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 @@ class ArrayObject implements IteratorAggregate, ArrayAccess, Countable /** @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 @@ class ArrayIterator implements SeekableIterator, ArrayAccess, Countable * 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 */ diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 0d9cc390c6..adb83ccea4 100755 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -1051,34 +1051,62 @@ SPL_METHOD(Array, count) 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_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0) 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 @@ static zend_function_entry spl_funcs_ArrayObject[] = { 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 @@ static zend_function_entry spl_funcs_ArrayIterator[] = { 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)