You're better off changing the name of behavior to check_if_key_equal and
use it as a boolean.
if (check_if_key_equal)
if (!check_if_key_equal)
It'll be much more readable than a magic variable called behavior. If you
want to keep the behavior variable you should probably make the 0 and 1
#define's so that people reading the code understand what the different
behaviors mean.
Congrats on your first patch :)
Andi
At 06:13 PM 9/11/2002 +0000, Andrey Hristov wrote:
>andrey Wed Sep 11 14:13:48 2002 EDT
>
> Modified files:
> /php4/ext/standard array.c basic_functions.c php_array.h
> Log:
> New function added : array_diff_assoc() . Like array_diff() but does
> additional checks on key values. Test script will be added too.
> #My first patch . it feels strange :)
>
>
>Index: php4/ext/standard/array.c
>diff -u php4/ext/standard/array.c:1.187 php4/ext/standard/array.c:1.188
>--- php4/ext/standard/array.c:1.187 Tue Sep 10 18:36:43 2002
>+++ php4/ext/standard/array.c Wed Sep 11 14:13:48 2002
>@@ -21,7 +21,7 @@
> +----------------------------------------------------------------------+
> */
>
>-/* $Id: array.c,v 1.187 2002/09/10 22:36:43 edink Exp $ */
>+/* $Id: array.c,v 1.188 2002/09/11 18:13:48 andrey Exp $ */
>
> #include "php.h"
> #include "php_ini.h"
>@@ -2621,9 +2621,7 @@
> }
> /* }}} */
>
>-/* {{{ proto array array_diff(array arr1, array arr2 [, array ...])
>- Returns the entries of arr1 that have values which are not present in
>any of the others arguments */
>-PHP_FUNCTION(array_diff)
>+static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior
>TSRMLS_DC)
> {
> zval ***args = NULL;
> HashTable *hash;
>@@ -2660,7 +2658,11 @@
> for (p = hash->pListHead; p; p = p->pListNext)
> *list++ = p;
> *list = NULL;
>- zend_qsort((void *) lists[i], hash->nNumOfElements,
>sizeof(Bucket *), array_data_compare TSRMLS_CC);
>+ if (behavior == 0) {
>+ zend_qsort((void *) lists[i],
>hash->nNumOfElements, sizeof(Bucket *), array_data_compare TSRMLS_CC);
>+ } else if (behavior == 1) {
>+ zend_qsort((void *) lists[i],
>hash->nNumOfElements, sizeof(Bucket *), array_key_compare TSRMLS_CC);
>+ }
> }
>
> /* copy the argument array */
>@@ -2672,12 +2674,27 @@
> while (*ptrs[0]) {
> c = 1;
> for (i=1; i<argc; i++) {
>- while (*ptrs[i] && (0 < (c =
>array_data_compare(ptrs[0], ptrs[i] TSRMLS_CC))))
>- ptrs[i]++;
>- if (!c) {
>- if (*ptrs[i])
>+ if (behavior == 0) {
>+ while (*ptrs[i] && (0 < (c =
>array_data_compare(ptrs[0], ptrs[i] TSRMLS_CC))))
> ptrs[i]++;
>- break;
>+ } else if (behavior == 1) {
>+ while (*ptrs[i] && (0 < (c =
>array_key_compare(ptrs[0], ptrs[i] TSRMLS_CC))))
>+ ptrs[i]++;
>+ }
>+ if (!c) {
>+ if (behavior == 0) {
>+ if (*ptrs[i])
>+ ptrs[i]++;
>+ break;
>+ } else if (behavior == 1) {
>+ if (*ptrs[i]) {
>+ if
>(array_data_compare(ptrs[0], ptrs[i] TSRMLS_CC) != 0) {
>+ c = -1;
>+ } else {
>+ break;
>+ }
>+ }
>+ }
> }
> }
> if (!c) {
>@@ -2691,8 +2708,13 @@
>
>zend_hash_index_del(Z_ARRVAL_P(return_value), p->h);
> if (!*++ptrs[0])
> goto out;
>- if (array_data_compare(ptrs[0]-1, ptrs[0]
>TSRMLS_CC))
>+ if (behavior == 0) {
>+ if (array_data_compare(ptrs[0]-1,
>ptrs[0] TSRMLS_CC))
>+ break;
>+ } else if (behavior == 1) {
>+ /* in this case no
>array_key_compare is needed */
> break;
>+ }
> }
> } else {
> /* ptrs[0] in none of the other arguments */
>@@ -2700,8 +2722,13 @@
> for (;;) {
> if (!*++ptrs[0])
> goto out;
>- if (array_data_compare(ptrs[0]-1, ptrs[0]
>TSRMLS_CC))
>+ if (behavior == 0) {
>+ if (array_data_compare(ptrs[0]-1,
>ptrs[0] TSRMLS_CC))
>+ break;
>+ } else if (behavior == 1) {
>+ /* in this case no
>array_key_compare is needed */
> break;
>+ }
> }
> }
> }
>@@ -2714,7 +2741,24 @@
> efree(lists);
> efree(args);
> }
>+
>+/* {{{ proto array array_diff(array arr1, array arr2 [, array ...])
>+ Returns the entries of arr1 that have values which are not present in
>any of the others arguments */
>+PHP_FUNCTION(array_diff)
>+{
>+ php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0 TSRMLS_CC);
>+}
>+/* }}} */
>+
>+/* {{{ proto array array_diff_assoc(array arr1, array arr2 [, array ...])
>+ 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, 1 TSRMLS_CC);
>+}
> /* }}} */
>+
>+
>
> #define MULTISORT_ORDER 0
> #define MULTISORT_TYPE 1
>Index: php4/ext/standard/basic_functions.c
>diff -u php4/ext/standard/basic_functions.c:1.503
>php4/ext/standard/basic_functions.c:1.504
>--- php4/ext/standard/basic_functions.c:1.503 Mon Sep 9 06:12:44 2002
>+++ php4/ext/standard/basic_functions.c Wed Sep 11 14:13:48 2002
>@@ -17,7 +17,7 @@
> +----------------------------------------------------------------------+
> */
>
>-/* $Id: basic_functions.c,v 1.503 2002/09/09 10:12:44 hyanantha Exp $ */
>+/* $Id: basic_functions.c,v 1.504 2002/09/11 18:13:48 andrey Exp $ */
>
> #include "php.h"
> #include "php_streams.h"
>@@ -797,6 +797,7 @@
> PHP_FE(array_unique,
> NULL)
> PHP_FE(array_intersect,
> NULL)
> PHP_FE(array_diff,
> NULL)
>+ PHP_FE(array_diff_assoc,
> NULL)
> PHP_FE(array_sum,
> NULL)
> PHP_FE(array_filter,
> NULL)
> PHP_FE(array_map,
> NULL)
>Index: php4/ext/standard/php_array.h
>diff -u php4/ext/standard/php_array.h:1.37 php4/ext/standard/php_array.h:1.38
>--- php4/ext/standard/php_array.h:1.37 Tue Sep 10 18:36:43 2002
>+++ php4/ext/standard/php_array.h Wed Sep 11 14:13:48 2002
>@@ -19,7 +19,7 @@
> +----------------------------------------------------------------------+
> */
>
>-/* $Id: php_array.h,v 1.37 2002/09/10 22:36:43 edink Exp $ */
>+/* $Id: php_array.h,v 1.38 2002/09/11 18:13:48 andrey Exp $ */
>
> #ifndef PHP_ARRAY_H
> #define PHP_ARRAY_H
>@@ -76,6 +76,7 @@
> PHP_FUNCTION(array_unique);
> PHP_FUNCTION(array_intersect);
> PHP_FUNCTION(array_diff);
>+PHP_FUNCTION(array_diff_assoc);
> PHP_FUNCTION(array_sum);
> PHP_FUNCTION(array_filter);
> PHP_FUNCTION(array_map);
>
>
>
>--
>PHP CVS Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php