Bug #12572: sort() doesn't reindex 1 element arrays at 0

From: Date: Sun, 05 Aug 2001 05:21:24 +0000
Subject: Bug #12572: sort() doesn't reindex 1 element arrays at 0
Groups: php.dev 
Request: Send a blank email to php-dev+get-62121@lists.php.net to get a copy of this message
From:             adam@trachtenberg.com
Operating system: Redhat Linux 7.0
PHP version:      4.0.6
PHP Bug Type:     Arrays related
Bug description:  sort() doesn't reindex 1 element arrays at 0

sort() reindexes arrays of length > 1 at 0, but doesn't do
this for 1 element arrays. The following patch, fixes this
minor inconsistancy. Please review it carefully, my C is
rusty and I don't normally play with these files. It does
build and seem to run correctly for me, with limited testing.

-adam

Here is an example:

$array = array(2 => 'foo', 'bar');
sort($array);
print_r($array);

Array
(
    [0] => bar
    [1] => foo
)

But, 

$array = array(2 => 'foo');
sort($array);
print_r($array);

Array
(
    [2] => foo
)

--- zend_hash.c	Sun Aug  5 01:17:22 2001
+++ /home/adam/zend_hash.c	Sun Aug  5 00:54:21 2001
@@ -1097,6 +1097,13 @@
 	IS_CONSISTENT(ht);
 
 	if (ht->nNumOfElements <= 1) {	/* Doesn't require sorting */
+
	if (ht->nNumOfElements == 1 && renumber) { /* Renumber 1
element arrays to O */
+
		p = ht->pListHead;
+
		p->nKeyLength = 0;
+
		p->h = 0;
+
		ht->nNextFreeElement = 1;
+
		zend_hash_rehash(ht);
+
	}
 
	return SUCCESS;
 	}
 	arTmp = (Bucket **) pemalloc(ht->nNumOfElements *
sizeof(Bucket *), ht->persistent);


-- 
Edit bug report at: http://bugs.php.net/?id=12572&edit=1



Thread (2 messages)

« previous php.dev (#62121) next »