Re: array_intersect_key
| From: | Andi Gutmans | Date: | Wed, 21 Jul 2004 21:25:11 +0000 |
| Subject: | Re: array_intersect_key | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-11449@lists.php.net to get a copy of this message | ||
I suggest to make those #define's bit flags and then change:
} else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY)
to
(behavior & (INTERSECT|INTERSECT_KEY))
Change:
to:#define INTERSECT_NORMAL 0 #define INTERSECT_ASSOC 1 +#define INTERSECT_KEY 2
If he patch is clean it's fine with me. It doesn't look like it'll break anything old. At 07:08 PM 7/21/2004 +0000, Andrey Hristov wrote:#define INTERSECT_NORMAL (1 << 0) #define INTERSECT_ASSOC (1 << 1) +#define INTERSECT_KEY (1 << 2)
Hallo Cristiano, these 2 function will be added to HEAD in few minutes. I see that the patch is kind of trivial and won't hurt anything :) (INTERSECT_KEY is just INTERSECT_ASSOC but no data comparison performed). I will add also array_diff_key() and array_diff_ukey() cheers, andrey P.S. Andi: I think the changes are minimal, so is it possible to merge into 5_0 ? Cristiano Duarte wrote:Andrey Hristov wrote:-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.phpis "make test" showing problems? (i am just currious). I will try to review the patch later today (atm i am sick).Nope. Make test shows the same errors before and after the patch. I decided to attach the patch since it will keep tabs... Cristiano Duarte --------------------------------------------------------------------------- array.c 2004-07-11 18:15:04.000000000 -0300 +++ /home/aluno/php-5.0.0-new/ext/standard/array.c 2004-07-21 11:47:58.000000000 -0300@@ -77,6 +77,7 @@#define DIFF_NORMAL 0 #define DIFF_ASSOC 1 +#define DIFF_KEY 2#define DIFF_COMP_DATA_INTERNAL 0#define DIFF_COMP_DATA_USER 1#define DIFF_COMP_KEY_INTERNAL 0 @@ -84,6 +85,7 @@#define INTERSECT_NORMAL 0 #define INTERSECT_ASSOC 1 +#define INTERSECT_KEY 2#define INTERSECT_COMP_DATA_INTERNAL 0#define INTERSECT_COMP_DATA_USER 1#define INTERSECT_COMP_KEY_INTERNAL 0 @@ -2797,7 +2799,8 @@php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_compare_type is %d. This should never happen. Please report as a bug", data_compare_type); return; } - } else if (behavior == INTERSECT_ASSOC) { + } else if ((behavior == INTERSECT_ASSOC) + ||(behavior == INTERSECT_KEY)) { intersect_key_compare_func = array_key_compare; if (data_compare_type == INTERSECT_COMP_DATA_INTERNAL &&@@ -2910,7 +2913,7 @@*list = NULL; if (behavior == INTERSECT_NORMAL) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), intersect_data_compare_func TSRMLS_CC); - } else if (behavior == INTERSECT_ASSOC) { + } else if ((behavior == INTERSECT_ASSOC) || (behavior == INTERSECT_KEY)) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), intersect_key_compare_func TSRMLS_CC); } }@@ -2926,7 +2929,8 @@/* go through the lists and look for common values */ while (*ptrs[0]) { - if (behavior == INTERSECT_ASSOC + if ((behavior == INTERSECT_ASSOC + || behavior == INTERSECT_KEY) && key_compare_type == INTERSECT_COMP_KEY_USER) {@@ -2938,11 +2942,11 @@while (*ptrs[i] && (0 < (c = intersect_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } - } else if (behavior == INTERSECT_ASSOC) { + } else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY) { while (*ptrs[i] && (0 < (c = intersect_key_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } - if (!c && *ptrs[i]) { /* this means that ptrs[i] is not NULL so we can compare */ + if ((!c && *ptrs[i]) && (behavior == INTERSECT_ASSOC)) { /* this means that ptrs[i] is not NULL so we can compare */ /* and "c==0" is from last operation */ if (data_compare_type == INTERSECT_COMP_DATA_USER) {BG(user_compare_func_name) = args[arr_argc]; @@ -2996,7 +3000,7 @@if (0 <= intersect_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)) { break; } - } else if (behavior == INTERSECT_ASSOC) { + } else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY) { /* no need of looping because indexes are unique */ break; }@@ -3012,7 +3016,7 @@if (intersect_data_compare_func(ptrs[0]-1, ptrs[0] TSRMLS_CC)) { break; } - } else if (behavior == INTERSECT_ASSOC) { + } else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY) { /* no need of looping because indexes are unique */ break; }@@ -3050,7 +3054,27 @@ } /* }}} */ +/* {{{ proto array array_intersect_key(array arr1, array arr2 [, +array ...])+ Returns the entries of arr1 that have keys which are present in all the other arguments. */+PHP_FUNCTION(array_intersect_key) +{+ php_array_intersect(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTERSECT_KEY, + INTERSECT_COMP_DATA_INTERNAL, INTERSECT_COMP_KEY_INTERNAL);+} +/* }}} */ + +/* {{{ proto array array_uintersect_key(array arr1, array arr2 [, array ...], callback data_compare_func) + Returns the entries of arr1 that have keys which are present in all the other arguments. Key is compared by using an user-supplied callback. */ +PHP_FUNCTION(array_uintersect_key) +{+ php_array_intersect(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTERSECT_KEY, + INTERSECT_COMP_DATA_INTERNAL, INTERSECT_COMP_KEY_USER);+} +/* }}} */ + + /* {{{ proto array array_intersect_assoc(array arr1, array arr2 [, array ...])Returns the entries of arr1 that have values which are present in all the other arguments. Keys are used to do more restrictive check */PHP_FUNCTION(array_intersect_assoc) @@ -3151,7 +3175,8 @@php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_compare_type is %d. This should never happen. Please report as a bug", data_compare_type); return; } - } else if (behavior == DIFF_ASSOC) { + } else if ((behavior == DIFF_ASSOC) + || (behavior == DIFF_KEY)) { diff_key_compare_func = array_key_compare; if (data_compare_type == DIFF_COMP_DATA_INTERNAL && @@ -3264,7 +3289,7 @@ *list = NULL; if (behavior == DIFF_NORMAL) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), diff_data_compare_func TSRMLS_CC); - } else if (behavior == DIFF_ASSOC) { + } else if ((behavior == DIFF_ASSOC) || (behavior == DIFF_KEY)) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), diff_key_compare_func TSRMLS_CC); } }@@ -3280,7 +3305,8 @@/* go through the lists and look for values of ptr[0] that are not in the others */ while (*ptrs[0]) { - if (behavior == DIFF_ASSOC + if ((behavior == DIFF_ASSOC + || behavior == DIFF_KEY) && key_compare_type == DIFF_COMP_KEY_USER) {@@ -3292,7 +3318,7 @@while (*ptrs[i] && (0 < (c = diff_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } - } else if (behavior == DIFF_ASSOC) { + } else if ((behavior == DIFF_ASSOC) || (behavior == DIFF_KEY)) { while (*ptrs[i] && (0 < (c = diff_key_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; }@@ -3337,7 +3363,7 @@if (diff_data_compare_func(ptrs[0] - 1, ptrs[0] TSRMLS_CC)) { break; } - } else if (behavior == DIFF_ASSOC) { + } else if ((behavior == DIFF_ASSOC) || (behavior == DIFF_KEY)) { /* in this case no array_key_compare is needed */ break; }@@ -3353,7 +3379,7 @@if (diff_data_compare_func(ptrs[0]-1, ptrs[0] TSRMLS_CC)) { break; } - } else if (behavior == DIFF_ASSOC) { + } else if ((behavior == DIFF_ASSOC) || (behavior == DIFF_KEY)) { /* in this case no array_key_compare is needed */ break; }@@ -3392,6 +3418,23 @@ } /* }}} */ +/* {{{ proto array array_diff_key(array arr1, array arr2 [, array ...]) + Returns the entries of arr1 that have keys which are not present in any of the others arguments */ +PHP_FUNCTION(array_diff_key) +{+ php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_KEY, + DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_INTERNAL);+} +/* }}} */ + +/* {{{ proto array array_udiff_key(array arr1, array arr2 [, array ...], callback key_comp_func) + Returns the entries of arr1 that have keys which are not present in any of the others arguments. Keys are compared by user supplied function. */ +PHP_FUNCTION(array_udiff_key) +{+ php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_KEY, + DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_USER);+} +/* }}} */ /* {{{ 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 */--- basic_functions.c 2004-06-27 18:49:47.000000000 -0300 +++/home/aluno/php-5.0.0-new/ext/standard/basic_functions.c 2004-07-21 11:36:34.000000000 -0300@@ -780,12 +780,16 @@PHP_FE(array_unique, NULL) PHP_FE(array_intersect, NULL) PHP_FE(array_uintersect, NULL) + PHP_FE(array_intersect_key, NULL) + PHP_FE(array_uintersect_key, NULL) PHP_FE(array_intersect_assoc, NULL) PHP_FE(array_uintersect_assoc, NULL) PHP_FE(array_intersect_uassoc, NULL) PHP_FE(array_uintersect_uassoc, NULL) PHP_FE(array_diff, NULL) PHP_FE(array_udiff, NULL) + PHP_FE(array_diff_key, NULL) + PHP_FE(array_udiff_key, NULL) PHP_FE(array_diff_assoc, NULL) PHP_FE(array_udiff_assoc, NULL) PHP_FE(array_diff_uassoc, NULL)--- php_array.h 2004-01-08 15:32:51.000000000 -0200 +++ /home/aluno/php-5.0.0-new/ext/standard/php_array.h 2004-07-21 11:36:54.000000000 -0300 @@ -77,12 +77,16 @@ PHP_FUNCTION(array_unique); PHP_FUNCTION(array_intersect); PHP_FUNCTION(array_uintersect); +PHP_FUNCTION(array_intersect_key); +PHP_FUNCTION(array_uintersect_key); PHP_FUNCTION(array_intersect_assoc); PHP_FUNCTION(array_uintersect_assoc); PHP_FUNCTION(array_intersect_uassoc); PHP_FUNCTION(array_uintersect_uassoc); PHP_FUNCTION(array_diff); PHP_FUNCTION(array_udiff); +PHP_FUNCTION(array_diff_key); +PHP_FUNCTION(array_udiff_key); PHP_FUNCTION(array_diff_assoc); PHP_FUNCTION(array_udiff_assoc); PHP_FUNCTION(array_diff_uassoc);