接著上面一節,繼續說php擴展中的數組與哈希表的api,這節主要是說
回調遍歷函數正常遍歷函數析構函數排序、對比、極值函數
Iteration by hash Apply:
對數組進行遍歷,最簡單的是使用一種與php語言中foreach語句功能類似的函數,zend_hash_apply,它接收一個回調函數,並將hashtable的每一個元素傳遞給它。
typedef int (*apply_func_t)(void *pDest TSRMLS_DC);
void zend_hash_apply(HashTable *ht,
apply_func_t apply_func TSRMLS_DC);typedef int (*apply_func_arg_t)(void *pDest,
void *argument TSRMLS_DC);
void zend_hash_apply_with_argument(HashTable *ht,
apply_func_arg_t apply_func, void *data TSRMLS_DC);一個是可以傳遞參數的,還有一個是不傳遞參數,只傳遞哈希表中的值的。對於可傳遞參數的函數而言,在擴展中應用到可能性更大。
回調函數可能有不同的返回值:
int php_sample_print_zval(zval **val TSRMLS_DC)
{
//重新copy一個zval,防止破壞原數據
zval tmpcopy = **val;
zval_copy_ctor(&tmpcopy);
//轉換為字符串
INIT_PZVAL(&tmpcopy);
convert_to_string(&tmpcopy);
//開始輸出
php_printf("The value is: ");
PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy));
php_printf("\n");
//毀屍滅跡
zval_dtor(&tmpcopy);
//返回,繼續遍歷下一個~
return ZEND_HASH_APPLY_KEEP;
}
然後定義循環函數:zend_hash_apply(arrht, php_sample_print_zval TSRMLS_CC); $val) { echo "The value of $key is: $val\n"; }
?>針對這段php代碼的c代碼:
int php_sample_print_zval_and_key(zval **val, int num_args, va_list args, zend_hash_key *hash_key) {
/* 復制zval從而使得原來的內容被保存下來 */
zval tmpcopy = **val;
/* tsrm_ls is needed by output functions */
TSRMLS_FETCH();
zval_copy_ctor(&tmpcopy);
/* Reset refcount & Convert */
INIT_PZVAL(&tmpcopy);
convert_to_string(&tmpcopy);
/* 輸出 */
php_printf("The value of ");
if (hash_key->nKeyLength) {
/* 如果是字符串類型的key */
PHPWRITE(hash_key->arKey, hash_key->nKeyLength);
} else {
/* 如果是數字類型的key */
php_printf("%ld", hash_key->h);
}
php_printf(" is: ");
PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy)); php_printf("\n");
/* Toss out old copy */
zval_dtor(&tmpcopy);
/* continue; */
return ZEND_HASH_APPLY_KEEP;
}1, 'b'=>2, 'c'=>3);
reset($arr);
while (list($key, $val) = each($arr)) {
/* Do something with $key and $val */
}
reset($arr);
$firstkey = key($arr);
$firstval = current($arr);
$bval = next($arr);
$cval = next($arr);
?>每一個相應的函數都會有一個zend版本:
* /* reset() */
void zend_hash_internal_pointer_reset(HashTable *ht);
/* key() */
int zend_hash_get_current_key(HashTable *ht,
char **strIdx, unit *strIdxLen,
ulong *numIdx, zend_bool duplicate);
* /* current() */
int zend_hash_get_current_data(HashTable *ht, void **pData);
* /* next()/each() */
int zend_hash_move_forward(HashTable *ht);
* /* prev() */
int zend_hash_move_backwards(HashTable *ht);
* /* end() */
void zend_hash_internal_pointer_end(HashTable *ht);
* /* Other... */
int zend_hash_get_current_key_type(HashTable *ht);
int zend_hash_has_more_elements(HashTable *ht);
next() prev() end()其實都是找到相應的索引值,再用zend_hash_get_current_data()返回元素值void php_sample_print_var_hash(HashTable *arrht)
{
for(zend_hash_internal_pointer_reset(arrht);
zend_hash_has_more_elements(arrht) == SUCCESS;
zend_hash_move_forward(arrht)) {
char *key;
uint keylen;
ulong idx;
int type;
zval **ppzval, tmpcopy;
type = zend_hash_get_current_key_ex(arrht, &key, &keylen,
&idx, 0, NULL);//獲得返回的key的類型。這個類型可能有三種
if (zend_hash_get_current_data(arrht, (void**)&ppzval) == FAILURE) {//獲得當前索引所指的數據值
/* Should never actually fail
* since the key is known to exist. */
continue;
}
/* 復制zval的值,從而原來的值不會被破壞掉 */
tmpcopy = **ppzval;
zval_copy_ctor(&tmpcopy);
/* 重新設定refcount 並且轉換 */
INIT_PZVAL(&tmpcopy);
convert_to_string(&tmpcopy);
/* 輸出 */
php_printf("The value of ");
if (type == HASH_KEY_IS_STRING) {
/* String Key / Associative */
PHPWRITE(key, keylen);
} else {
/* Numeric Key */
php_printf("%ld", idx);
}
php_printf(" is: ");
PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy));
php_printf("\n");
/* 銷毀原來的副本 */
zval_dtor(&tmpcopy);
}
}來看一下zend_hash_get_current_key_ex返回值的可能性:
Constant Meaningint zend_hash_del(HashTable *ht, char *arKey, uint nKeyLen); int zend_hash_index_del(HashTable *ht, ulong h);返回SUCCESS OR FAILURE
int sample_strvec_handler(int argc, char **argv TSRMLS_DC)
{
HashTable *ht;
/* 為哈希表分配空間 */
ALLOC_HASHTABLE(ht);
/* 初始化哈希表的內部狀態 */
if (zend_hash_init(ht, argc, NULL,
ZVAL_PTR_DTOR, 0) == FAILURE) {
FREE_HASHTABLE(ht);
return FAILURE;
}
/* 把每個字符串變成zval* */
while (argc) {
zval *value;
MAKE_STD_ZVAL(value);
ZVAL_STRING(value, argv[argc], 1);
argv++;
if (zend_hash_next_index_insert(ht, (void**)&value,
sizeof(zval*)) == FAILURE) {
/* 對於分配失敗的情況應該跳掉 */
zval_ptr_dtor(&value);
}
}
/* Do some work */
process_hashtable(ht);
/* 毀壞哈希表
* 釋放所有的分配的空曠 */
zend_hash_destroy(ht);
/* Free the HashTable itself */
FREE_HASHTABLE(ht);
return SUCCESS;
}
int zend_hash_minmax(HashTable *ht, compare_func_t compar,
int flag, void **pData TSRMLS_DC);flag為0就返回最小值,否則就是最大值。int fname_compare(zend_function *a, zend_function *b TSRMLS_DC)
{
return strcasecmp(a->common.function_name, b->common.function_name);
}
void php_sample_funcname_sort(TSRMLS_D)
{
zend_function *fe;
if (zend_hash_minmax(EG(function_table), fname_compare,
0, (void **)&fe) == SUCCESS) {
php_printf("Min function: %s\n", fe->common.function_name);
}
if (zend_hash_minmax(EG(function_table), fname_compare,
1, (void **)&fe) == SUCCESS) {
php_printf("Max function: %s\n", fe->common.function_name);
}
}
typedef void (*sort_func_t)(void **Buckets, size_t numBuckets,
size_t sizBucket, compare_func_t comp TSRMLS_DC);
int zend_hash_sort(HashTable *ht, sort_func_t sort_func,
compare_func_t compare_func, int renumber TSRMLS_DC);
一般就用zend_qsort作為sort_func就夠了。renumber這個參數如果設為1的話,那麼就會拋棄原有的索引鍵值關系,賦予新的數字鍵值。
zend_hash_sort(target_hash, zend_qsort,array_data_compare, 1 TSRMLS_CC);
array_data_compare是一個返回compare_func_t類型數據的函數,它將按照HashTable中zval*值的大小進行排序。