Re: [PHP4BETA] cvs: /php4 ChangeLog /php4/ext/standard basic_functions.cbasic_functions.h
| From: | Chad | Date: | Wed, 06 Oct 1999 14:43:37 +0000 |
| Subject: | Re: [PHP4BETA] cvs: /php4 ChangeLog /php4/ext/standard basic_functions.cbasic_functions.h | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-5236@lists.php.net to get a copy of this message | ||
I figure there's probably an easier way than cloning the hash_sort
function, but I was in a hurry when I did this :) Here's the diff for
3.0.12 as a point of reference.
diff -u3 old/php3_hash.c new/php3_hash.c
--- old/php3_hash.c Wed Oct 6 11:45:31 1999
+++ new/php3_hash.c Wed Oct 6 12:06:59 1999
@@ -44,6 +44,9 @@
#include <strings.h>
#endif
+#ifndef RAND_MAX
+#define RAND_MAX (1<<15)
+#endif
#define HANDLE_NUMERIC(key,length,func) { \
register char *tmp=key; \
@@ -1106,6 +1109,86 @@
_php3_hash_rehash(ht);
}
return SUCCESS;
+}
+
+#ifdef HAVE_LRAND48
+#define PHP_RAND_MAX 2147483647
+#else
+#define PHP_RAND_MAX RAND_MAX
+#endif
+
+PHPAPI int _php3_hash_shuffle(HashTable *ht, int renumber)
+{
+ Bucket **arTmp;
+ Bucket *p, *t;
+ int i, j, k, s;
+
+ CHECK_INDESTROY(ht);
+
+ if (ht->nNumOfElements <= 1) { /* Doesn't require sorting */
+ return SUCCESS;
+ }
+ arTmp = (Bucket **) pemalloc(ht->nNumOfElements * sizeof(Bucket *),ht->
persistent);
+ if (!arTmp) {
+ return FAILURE;
+ }
+ p = ht->pListHead;
+ i = 0;
+ while (p) {
+ arTmp[i] = p;
+ p = p->pListNext;
+ i++;
+ }
+
+ for (k=0; k<i-1; k++) {
+ s = (k+1) + (int)((double)((i-1)-(k+1)+1) *
+#ifdef HAVE_LRAND48
+ lrand48()
+#else
+#ifdef HAVE_RANDOM
+ random()
+#else
+ rand()
+#endif
+#endif
+ /(PHP_RAND_MAX+1.0));
+ t = arTmp[k];
+ arTmp[k] = arTmp[s];
+ arTmp[s] = t;
+ }
+
+ BLOCK_INTERRUPTIONS;
+ ht->pListHead = arTmp[0];
+ ht->pListTail = NULL;
+ ht->pInternalPointer = ht->pListHead;
+
+ for (j = 0; j < i; j++) {
+ if (ht->pListTail) {
+ ht->pListTail->pListNext = arTmp[j];
+ }
+ arTmp[j]->pListLast = ht->pListTail;
+ arTmp[j]->pListNext = NULL;
+ ht->pListTail = arTmp[j];
+ }
+ pefree(arTmp,ht->persistent);
+ UNBLOCK_INTERRUPTIONS;
+
+ if (renumber) {
+ p = ht->pListHead;
+ i=0;
+ while (p != NULL) {
+ if (p->arKey) {
+ pefree(p->arKey,ht->persistent);
+ }
+ p->arKey = NULL;
+ p->nKeyLength = 0;
+ p->h = i++;
+ p = p->pListNext;
+ }
+ ht->nNextFreeElement = i;
+ _php3_hash_rehash(ht);
+ }
+ return SUCCESS;
}
diff -u3 old/php3_hash.h new/php3_hash.h
--- old/php3_hash.h Wed Oct 6 11:45:31 1999
+++ new/php3_hash.h Wed Oct 6 11:58:38 1999
@@ -142,6 +142,7 @@
extern PHPAPI void _php3_hash_copy(HashTable **target, HashTable
*source, void
(*pCopyConstructor) (void *pData), void *tmp, uint size);
extern PHPAPI void _php3_hash_merge(HashTable *target, HashTable
*source, void
(*pCopyConstructor) (void *pData), void *tmp, uint size);
extern PHPAPI int _php3_hash_sort(HashTable *ht, int (*compar) (const
void *, c
onst void *), int renumber);
+extern PHPAPI int _php3_hash_shuffle(HashTable *ht, int renumber);
extern PHPAPI int _php3_hash_minmax(HashTable *ht, int (*compar) (const
void *,
const void *), int flag, void **pData);
"Thies C. Arntzen" wrote:
>
> chad, please send me your patch...
>
> right now it's not in PHP4 nor in PHP3, right?
>
> tc
>
> On Mon, 4 Oct 1999, Chad Cunningham wrote:
>