From: Andrei Zmievski Date: Mon, 10 Jun 2002 02:28:32 +0000 (+0000) Subject: Fix bug #7045: shuffle() now provides consistent distribution of values X-Git-Tag: php5_5_0~102 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7f4c12b0061305d54f7c200fe3b4d76c193655ad;p=php Fix bug #7045: shuffle() now provides consistent distribution of values in the array. --- diff --git a/NEWS b/NEWS index 8065f745a3..c54bcd3735 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP 4 NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2002, Version 4.3.0 +- Fixed shuffle() to provide equal distribution of values. (Andrei) - Added --with-mysql-sock configure option which can be used to override the unix socket location. (e.g. NFS compiles, etc.) (imajes) - Fixed is_a() to properly work on extension registered classes. (Andrei) diff --git a/ext/standard/array.c b/ext/standard/array.c index 77544a2c13..79dc8778fe 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1435,24 +1435,68 @@ PHP_FUNCTION(range) /* }}} */ -static int array_data_shuffle(const void *a, const void *b TSRMLS_DC) -{ - return (php_rand(TSRMLS_C) % 2) ? 1 : -1; -} - - /* {{{ proto bool shuffle(array array_arg) Randomly shuffle the contents of an array */ PHP_FUNCTION(shuffle) { zval *array; + Bucket **elems, *temp; + HashTable *hash; + int j, n_elems, cur_elem = 0, rnd_idx, n_left; + TSRMLS_FETCH(); if (zend_parse_parameters(1 TSRMLS_CC, "a", &array) == FAILURE) { RETURN_FALSE; } - if (zend_hash_sort(Z_ARRVAL_PP(&array), (sort_func_t)php_mergesort, array_data_shuffle, 1 TSRMLS_CC) == FAILURE) { - RETURN_FALSE; + + n_elems = zend_hash_num_elements(Z_ARRVAL_P(array)); + if (n_elems <= 1) { + RETURN_TRUE; } + + elems = (Bucket **)emalloc(n_elems * sizeof(Bucket *)); + hash = Z_ARRVAL_P(array); + n_left = n_elems; + + for (j = 0, temp = hash->pListHead; temp; temp = temp->pListNext) + elems[j++] = temp; + while (--n_left) { + rnd_idx = php_rand(TSRMLS_C); + RAND_RANGE(rnd_idx, cur_elem, n_left, PHP_RAND_MAX); + if (rnd_idx != cur_elem) { + temp = elems[cur_elem]; + elems[cur_elem] = elems[rnd_idx]; + elems[rnd_idx] = temp; + } + cur_elem++; + } + + HANDLE_BLOCK_INTERRUPTIONS(); + hash->pListHead = elems[0]; + hash->pListTail = NULL; + hash->pInternalPointer = hash->pListHead; + + for (j = 0; j < n_elems; j++) { + if (hash->pListTail) { + hash->pListTail->pListNext = elems[j]; + } + elems[j]->pListLast = hash->pListTail; + elems[j]->pListNext = NULL; + hash->pListTail = elems[j]; + } + temp = hash->pListHead; + j = 0; + while (temp != NULL) { + temp->nKeyLength = 0; + temp->h = j++; + temp = temp->pListNext; + } + hash->nNextFreeElement = n_elems; + zend_hash_rehash(hash); + HANDLE_UNBLOCK_INTERRUPTIONS(); + + efree(elems); + RETURN_TRUE; } /* }}} */