]> granicus.if.org Git - php/commitdiff
- Add sorting to ArrayObject/Iterator
authorMarcus Boerger <helly@php.net>
Mon, 3 Oct 2005 12:55:11 +0000 (12:55 +0000)
committerMarcus Boerger <helly@php.net>
Mon, 3 Oct 2005 12:55:11 +0000 (12:55 +0000)
ext/spl/examples/class_tree.php
ext/spl/spl_array.c

index 07741a7dd873b4d72774787529ea376bc10c321e..d10983a11f2e3a021a37114db7e994e3414063d4 100755 (executable)
@@ -5,6 +5,7 @@
  * @ingroup Examples
  * @author  Marcus Boerger
  * @date    2003 - 2005
+ * @version 1.1
  *
  * Usage: php class_tree.php \<class\>
  *
@@ -60,6 +61,7 @@ class SubClasses extends RecursiveArrayIterator
                                }
                        }
                }
+               $this->ksort();
        }
        
        function current()
index cd75a161d2882c6d7ce213fe9b31c513bfa8c915..0d9cc390c60befb0636178fad51cd6d05b896045 100755 (executable)
@@ -1051,6 +1051,35 @@ SPL_METHOD(Array, count)
        RETURN_LONG(count);
 } /* }}} */
 
+static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len)
+{
+       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;
+       
+       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);
+}
+
+#define SPL_ARRAY_METHOD(cname, fname) \
+SPL_METHOD(cname, fname) \
+{ \
+       spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1); \
+}
+
+/* {{{ proto int ArrayObject::asort()
+       proto int ArrayIterator::asort()
+ Sort the entries by values. */
+SPL_ARRAY_METHOD(Array, asort)
+
+/* {{{ proto int ArrayObject::ksort()
+       proto int ArrayIterator::ksort()
+ Sort the entries by key. */
+SPL_ARRAY_METHOD(Array, ksort)
+
 /* {{{ proto mixed|NULL ArrayIterator::current()
    Return current array entry */
 SPL_METHOD(Array, current)
@@ -1254,16 +1283,18 @@ ZEND_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0)
 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)
-       SPL_ME(Array, offsetGet,     arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, offsetSet,     arginfo_array_offsetSet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, offsetUnset,   arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, append,        arginfo_array_append,    ZEND_ACC_PUBLIC)
-       SPL_ME(Array, getArrayCopy,  NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, count,         NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, getFlags,      NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, setFlags,      arginfo_array_setFlags,  ZEND_ACC_PUBLIC)
+       SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, getArrayCopy,     NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, count,            NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, getFlags,         NULL,                           ZEND_ACC_PUBLIC)
+       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)
        /* ArrayObject specific */
        SPL_ME(Array, getIterator,      NULL,                           ZEND_ACC_PUBLIC)
        SPL_ME(Array, exchangeArray,    arginfo_array_exchangeArray,    ZEND_ACC_PUBLIC)
@@ -1273,23 +1304,25 @@ static zend_function_entry spl_funcs_ArrayObject[] = {
 };
 
 static zend_function_entry spl_funcs_ArrayIterator[] = {
-       SPL_ME(Array, __construct,   arginfo_array___construct, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, offsetExists,  arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, offsetGet,     arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, offsetSet,     arginfo_array_offsetSet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, offsetUnset,   arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, append,        arginfo_array_append,    ZEND_ACC_PUBLIC)
-       SPL_ME(Array, getArrayCopy,  NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, count,         NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, getFlags,      NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, setFlags,      arginfo_array_setFlags,  ZEND_ACC_PUBLIC)
+       SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, getArrayCopy,     NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, count,            NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, getFlags,         NULL,                           ZEND_ACC_PUBLIC)
+       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)
        /* ArrayIterator specific */
-       SPL_ME(Array, rewind,        NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, current,       NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, key,           NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, next,          NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, valid,         NULL, ZEND_ACC_PUBLIC)
-       SPL_ME(Array, seek,          arginfo_array_seek,        ZEND_ACC_PUBLIC)
+       SPL_ME(Array, rewind,           NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, current,          NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, key,              NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, next,             NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, valid,            NULL,                           ZEND_ACC_PUBLIC)
+       SPL_ME(Array, seek,             arginfo_array_seek,             ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };