Re: array_diff() failures

From: Date: Thu, 28 Dec 2000 18:58:40 +0000
Subject: Re: array_diff() failures
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-42465@lists.php.net to get a copy of this message
Hi On Thu, Dec 28, 2000 at 11:42:22AM -0600, Andrei Zmievski wrote: > Stig, > > I've uncovered cases where array_diff() fails to produce the correct > result. I had two identical arrays (unsorted) and I ran array_diff() on > them, expecting to get an empty array back, but array_diff() did produce > some differences. This stems from the fact that you sort the arrays > yourself in the array_diff() and set the comparison function to > SORT_REGULAR. Since the regular comparison function is > non-deterministic, the arrays might be sorted differently depending on > the original order of the elements. I thought I solved this when I made it use array_type_data_compare instead of array_data_compare. I thought SORT_REGULAR was deterministic when the operands were of the same type, I thought that say the strings "99" and "370" were compared as strings with "99" being the bigger, but... What I thought I did, which I personally like better, was to compare strings as strings, numbers as numbers, and numbers < strings. Did you see my mail a few days ago regarding a segfault with array_unique? As Sascha pointed out it segfaults on Solaris since the comparison isn't transitive. As I said there, this results in the non-deterministic behavior you're mentioning now. There I said array_type_data_compare() would fix it, but I see now that I was wrong. It looks like array_data_compare() with SORT_STRING works well. Looks like I can remove array_type_data_compare() again. BTW, in array.c it says: /* Numbers are always smaller than strings int this function as it * anyway doesn't make much sense to compare two different data types. * This keeps it consistant and simple. * * This is not correct any more, if you want this behavior, use * array_type_data_compare(). */ static int array_data_compare(const void *a, const void *b) I added the second paragraph, but I think now that all the comments should be dropped. There are problems with other array functions, for instance asort() and sort(). As i wrote in the array_unique thread I get segfault on Solaris using SORT_REGULAR in some cases. Of course the user can tell it not to use SORT_REGULAR but I don't like the segfaults. Stig

« previous php.dev (#42465) next »