dmitry          Fri Sep 21 13:11:00 2007 UTC

  Modified files:              (Branch: PHP_5_2)
    /php-src    NEWS 
    /php-src/ext/standard       array.c 
  Log:
  Improved speed of array_diff_key(), array_diff_assoc() and 
array_udiff_assoc().
  
  
http://cvs.php.net/viewvc.cgi/php-src/NEWS?r1=1.2027.2.547.2.959&r2=1.2027.2.547.2.960&diff_format=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.2027.2.547.2.959 php-src/NEWS:1.2027.2.547.2.960
--- php-src/NEWS:1.2027.2.547.2.959     Thu Sep 20 09:32:31 2007
+++ php-src/NEWS        Fri Sep 21 13:10:59 2007
@@ -5,8 +5,9 @@
 - Added optional parameter $provide_object to debug_backtrace(). (Sebastian)
 - Added alpha support for imagefilter() IMG_FILTER_COLORIZE. (Pierre)
 
-- Improved speed of array_intersect_key(), array_intersect_assoc() and
-  array_uintersect_assoc(). (Dmitry)
+- Improved speed of array_intersect_key(), array_intersect_assoc(),
+  array_uintersect_assoc(), array_diff_key(), array_diff_assoc() and
+  array_udiff_assoc(). (Dmitry)
 
 - Fixed regression in glob() when enforcing safe_mode/open_basedir checks on
   paths containing '*'. (Ilia)
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/array.c?r1=1.308.2.21.2.33&r2=1.308.2.21.2.34&diff_format=u
Index: php-src/ext/standard/array.c
diff -u php-src/ext/standard/array.c:1.308.2.21.2.33 
php-src/ext/standard/array.c:1.308.2.21.2.34
--- php-src/ext/standard/array.c:1.308.2.21.2.33        Thu Sep 20 09:22:59 2007
+++ php-src/ext/standard/array.c        Fri Sep 21 13:10:59 2007
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: array.c,v 1.308.2.21.2.33 2007/09/20 09:22:59 dmitry Exp $ */
+/* $Id: array.c,v 1.308.2.21.2.34 2007/09/21 13:10:59 dmitry Exp $ */
 
 #include "php.h"
 #include "php_ini.h"
@@ -78,6 +78,7 @@
 #define DIFF_NORMAL                    1
 #define DIFF_KEY                       2
 #define DIFF_ASSOC                     6
+#define DIFF_COMP_DATA_NONE    -1
 #define DIFF_COMP_DATA_INTERNAL 0
 #define DIFF_COMP_DATA_USER     1
 #define DIFF_COMP_KEY_INTERNAL  0
@@ -3394,6 +3395,81 @@
 }
 /* }}} */
 
+static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int 
data_compare_type) /* {{{ */
+{
+       Bucket *p;
+       int argc, i;
+       zval ***args;
+       int (*diff_data_compare_func)(zval **, zval ** TSRMLS_DC) = NULL;
+       zend_bool ok;
+       zval **data;
+
+       /* Get the argument count */
+       argc = ZEND_NUM_ARGS();
+       /* Allocate arguments array and get the arguments, checking for errors. 
*/
+       args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
+       if (argc < 2 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
+               efree(args);
+               WRONG_PARAM_COUNT;
+       }
+       if (data_compare_type == DIFF_COMP_DATA_USER) {
+               char *callback_name;
+
+               if (argc < 3) {
+                       efree(args);
+                       WRONG_PARAM_COUNT;
+               }
+               argc--;
+               if (!zend_is_callable(*args[argc], 0, &callback_name)) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a 
valid callback %s", callback_name);
+                       efree(callback_name);
+                       efree(args);
+                       return;
+               } 
+               efree(callback_name);
+               diff_data_compare_func = zval_user_compare;
+               BG(user_compare_func_name) = args[argc];
+       } else if (data_compare_type == DIFF_COMP_DATA_INTERNAL) {
+               diff_data_compare_func = zval_compare;
+       }
+
+       array_init(return_value);
+
+       for (p = Z_ARRVAL_PP(args[0])->pListHead; p != NULL; p = p->pListNext) {
+               if (p->nKeyLength == 0) {
+                       ok = 1;
+                       for (i = 1; i < argc; i++) {
+                               if (zend_hash_index_find(Z_ARRVAL_PP(args[i]), 
p->h, (void**)&data) == SUCCESS &&
+                                   (!diff_data_compare_func ||
+                                    diff_data_compare_func((zval**)p->pData, 
data TSRMLS_CC) == 0)) {
+                                       ok = 0;
+                                       break;
+                               }
+                       }
+                       if (ok) {
+                               (*((zval**)p->pData))->refcount++;
+                               
zend_hash_index_update(Z_ARRVAL_P(return_value), p->h, p->pData, sizeof(zval*), 
NULL);
+                       }
+               } else {
+                       ok = 1;
+                       for (i = 1; i < argc; i++) {
+                               if (zend_hash_quick_find(Z_ARRVAL_PP(args[i]), 
p->arKey, p->nKeyLength, p->h, (void**)&data) == SUCCESS &&
+                                   (!diff_data_compare_func ||
+                                    diff_data_compare_func((zval**)p->pData, 
data TSRMLS_CC) == 0)) {
+                                       ok = 0;
+                                       break;
+                               }
+                       }
+                       if (ok) {
+                               (*((zval**)p->pData))->refcount++;
+                               
zend_hash_quick_update(Z_ARRVAL_P(return_value), p->arKey, p->nKeyLength, p->h, 
p->pData, sizeof(zval*), NULL);
+                       }
+               }
+       }
+       efree(args);
+}
+/* }}} */
+
 static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int 
data_compare_type, int key_compare_type) /* {{{ */
 {
        zval ***args = NULL;
@@ -3708,8 +3784,7 @@
    Returns the entries of arr1 that have keys which are not present in any of 
the others arguments. This function is like array_diff() but works on the keys 
instead of the values. The associativity is preserved. */
 PHP_FUNCTION(array_diff_key)
 {
-       php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_KEY,
-                        DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_INTERNAL);
+       php_array_diff_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, 
DIFF_COMP_DATA_NONE);
 }
 /* }}} */
 
@@ -3744,8 +3819,7 @@
    Returns the entries of arr1 that have values which are not present in any 
of the others arguments but do additional checks whether the keys are equal */
 PHP_FUNCTION(array_diff_assoc)
 {
-       php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_ASSOC,
-                        DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_INTERNAL);
+       php_array_diff_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, 
DIFF_COMP_DATA_INTERNAL);
 }
 /* }}} */
 
@@ -3762,8 +3836,7 @@
    Returns the entries of arr1 that have values which are not present in any 
of the others arguments but do additional checks whether the keys are equal. 
Keys are compared by user supplied function. */
 PHP_FUNCTION(array_udiff_assoc)
 {
-       php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_ASSOC,
-                        DIFF_COMP_DATA_USER, DIFF_COMP_KEY_INTERNAL);
+       php_array_diff_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, 
DIFF_COMP_DATA_USER);
 }
 /* }}} */
 

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to