CVS update: php3/functions

From: Date: Sun, 16 May 1999 18:55:08 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-5718@lists.php.net to get a copy of this message
Date: Sunday May 16, 1999 @ 14:55 Author: jim Update of /repository/php3/functions In directory php:/tmp/cvs-serv30713/functions Modified Files: basic_functions.c basic_functions.h Log Message: Add range() and shuffle() functions (with documentation!). (Yes, shuffle() can be done using usort(). I've got a hankering for speed.) Index: php3/functions/basic_functions.c diff -u php3/functions/basic_functions.c:1.261 php3/functions/basic_functions.c:1.262 --- php3/functions/basic_functions.c:1.261 Sat Apr 17 00:21:29 1999 +++ php3/functions/basic_functions.c Sun May 16 14:55:07 1999 @@ -28,7 +28,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.261 1999/04/17 04:21:29 andrey Exp $ */ +/* $Id: basic_functions.c,v 1.262 1999/05/16 18:55:07 jim Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif @@ -117,6 +117,7 @@ {"usort", php3_user_sort, first_arg_force_ref}, {"uasort", php3_auser_sort, first_arg_force_ref}, {"uksort", php3_user_key_sort, first_arg_force_ref}, + {"shuffle", php3_shuffle, first_arg_force_ref}, {"array_walk", php3_array_walk, first_arg_force_ref}, {"sizeof", php3_count, first_arg_allow_ref}, {"count", php3_count, first_arg_allow_ref}, @@ -140,6 +141,7 @@ {"current", array_current, first_arg_force_ref}, {"key", array_current_key, first_arg_force_ref}, {"each", array_each, first_arg_force_ref}, + {"range", php3_range, NULL}, {"gettype", php3_gettype, NULL}, {"settype", php3_settype, first_arg_force_ref}, {"min", php3_min, NULL}, @@ -734,6 +736,47 @@ } /* }}} */ +static int array_data_shuffle(const void *a, const void*b) { + return ( + /* This is just a little messy. */ +#ifdef HAVE_LRAND48 + lrand48() +#else +#ifdef HAVE_RANDOM + random() +#else + rand() +#endif +#endif + % 2) ? 1 : -1; +} + +/* {{{ proto int shuffle(array array_arg) + Randomly shuffle the contents of an array */ +PHP_FUNCTION(shuffle) +{ + pval *array; + TLS_VARS; + + if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &array) == FAILURE) { + WRONG_PARAM_COUNT; + } + if (!(array->type & IS_HASH)) { + php3_error(E_WARNING, "Wrong datatype in shuffle() call"); + return; + } + if (!ParameterPassedByReference(ht,1)) { + php3_error(E_WARNING, "Array not passed by reference in call to shuffle()"); + return; + } + if (_php3_hash_sort(array->value.ht, array_data_shuffle,1) == FAILURE) { + return; + } + RETURN_TRUE; +} +/* }}} */ + + /* the current implementation of count() is a definite example of what * user functions should NOT look like. It's a hack, until we get * unset() to work right in 3.1 @@ -1297,6 +1340,31 @@ break; case HASH_KEY_NON_EXISTANT: return; + } +} +/* }}} */ + +/* {{{ proto array range(int low, int high) + Create an array containing the range of integers from low to high (inclusive) */ +PHP_FUNCTION(range) +{ + pval *plow, *phigh; + int low, high; + if (ARG_COUNT(ht) != 2 || getParameters(ht,2,&plow,&phigh) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long(plow); + convert_to_long(phigh); + low = plow->value.lval; + high = phigh->value.lval; + + /* allocate an array for return */ + if (array_init(return_value) == FAILURE) { + RETURN_FALSE; + } + + for (; low <= high; low++) { + add_next_index_long(return_value, low); } } /* }}} */ Index: php3/functions/basic_functions.h diff -u php3/functions/basic_functions.h:1.41 php3/functions/basic_functions.h:1.42 --- php3/functions/basic_functions.h:1.41 Thu Feb 11 01:23:40 1999 +++ php3/functions/basic_functions.h Sun May 16 14:55:08 1999 @@ -29,7 +29,7 @@ */ -/* $Id: basic_functions.h,v 1.41 1999/02/11 06:23:40 andrey Exp $ */ +/* $Id: basic_functions.h,v 1.42 1999/05/16 18:55:08 jim Exp $ */ #ifndef _BASIC_FUNCTIONS_H #define _BASIC_FUNCTIONS_H @@ -54,6 +54,7 @@ extern void php3_user_sort(INTERNAL_FUNCTION_PARAMETERS); extern void php3_auser_sort(INTERNAL_FUNCTION_PARAMETERS); extern void php3_user_key_sort(INTERNAL_FUNCTION_PARAMETERS); +extern void php3_shuffle(INTERNAL_FUNCTION_PARAMETERS); extern void php3_array_walk(INTERNAL_FUNCTION_PARAMETERS); extern void php3_count(INTERNAL_FUNCTION_PARAMETERS); extern void php3_flush(INTERNAL_FUNCTION_PARAMETERS); @@ -64,6 +65,7 @@ extern void array_reset(INTERNAL_FUNCTION_PARAMETERS); extern void array_current(INTERNAL_FUNCTION_PARAMETERS); extern void array_current_key(INTERNAL_FUNCTION_PARAMETERS); +extern void php3_range(INTERNAL_FUNCTION_PARAMETERS); extern void php3_gettype(INTERNAL_FUNCTION_PARAMETERS); extern void php3_settype(INTERNAL_FUNCTION_PARAMETERS); extern void php3_min(INTERNAL_FUNCTION_PARAMETERS); -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#5718) next »