php中HashTable結構的深入講解任何對php內核有壹定了解的人都應該知道php的本質是HashTable,而在PHP的實現中HashTable無處不在。包括php的數組,什麽全局變量,局部變量的範圍等。,php的哈希表分為四個部分:
哈希函數:time33的哈希函數用於將字符串的密鑰轉換為數字。
C數組:用於存儲桶。
兩個雙向鏈表:第壹個是數組的每個元素(桶)都是雙向鏈表,這樣做是為了解決哈希沖突;第二個雙向鏈表是壹個連接每個桶的數組,這裏要連接第壹個雙向鏈表的鏈表頭,用來遍歷整個哈希表。鳥哥有壹個關於php的foreach的博客,這裏的設計是針對foreach = = & gt深入理解PHP數組(遍歷順序)
我不會在這裏談論hashtable的struct和bucket的struct,因為下面推薦的鏈接幾乎都提到了。我不認為我能比他們描述得更好,說得更好。每個人的水平不壹樣,我就用我現在的技術水平來描述,所以我就只記錄我編的壹些東西。
下面是php中用hash實現的兩個文件:Zend _ hash.czend _ hash.h這兩個文件實現了壹堆API,也衍生了壹堆API。以下是實現的API的原型。
復制代碼代碼如下:
ZEND _ API ulong ZEND _ hash _ func(const char * arKey,uint nKeyLength)
ZEND _ API ulong ZEND _ get _ hash _ value(const char * arKey,uint nKeyLength)
ZEND _ API int _ ZEND _ hash _ init(HashTable * ht,uint nSize,hash_func_t pHashFunction,dtor_func_t pDestructor,ZEND _ bool persistent ZEND _ FILE _ LINE _ DC)
ZEND _ API void ZEND _ hash _ set _ apply _ protection(HashTable * ht,zend_bool bApplyProtection)
ZEND _ API int _ ZEND _ hash _ add _ or _ update(HashTable * ht,const char *arKey,uint nKeyLength,void *pData,uint nDataSize,void **pDest,int flag ZEND_FILE_LINE_DC)
ZEND _ API int _ ZEND _ hash _ quick _ add _ or _ update(HashTable * ht,const char *arKey,uint nKeyLength,ulong h,void *pData,uint nDataSize,void **pDest,int flag ZEND_FILE_LINE_DC)
ZEND _ API int _ ZEND _ hash _ index _ update _ or _ next _(HashTable * ht,ulong h,void *pData,uint nDataSize,void **pDest,int flag ZEND_FILE_LINE_DC)
ZEND _ API int ZEND _ hash _ rehash(HashTable * ht)
static int Zend _ hash _ do _ resize(HashTable * ht)
ZEND _ API int ZEND _ hash _ del _ key _ or _ index(HashTable * ht,const char *arKey,uint nKeyLength,ulong h,int flag)
ZEND _ API void ZEND _ hash _ destroy(HashTable * ht)
ZEND _ API void ZEND _ hash _ clean(HashTable * ht)
靜態桶*zend_hash_apply_r(哈希表*ht,桶*p)
ZEND _ API void ZEND _ hash _ graceful _ destroy(HashTable * ht)
ZEND _ API void ZEND _ hash _ graceful _ reverse _ destroy(HashTable * ht)
ZEND _ API void ZEND _ hash _ apply(HashTable * ht,apply _ func _ t apply _ func TSR mls _ DC)
ZEND _ API void ZEND _ hash _ apply _ with _ argument(HashTable * ht,apply_func_arg_t apply_func,void *argument TSRMLS_DC)
ZEND _ API void ZEND _ hash _ apply _ with _ arguments(HashTable * ht TSR mls _ DC,apply_func_args_t apply_func,int num_args,…)
ZEND _ API void ZEND _ hash _ reverse _ apply(HashTable * ht,apply _ func _ t apply _ func TSRMLS _ DC)
ZEND _ API void ZEND _ hash _ copy(HashTable * target,HashTable *source,copy _ ctor _ func _ t pCopyConstructor,void *tmp,uint size)
ZEND _ API void _ ZEND _ hash _ merge(HashTable * target,HashTable *source,copy _ ctor _ func _ t pCopyConstructor,void *tmp,uint size,int overwrite ZEND_FILE_LINE_DC)
靜態Zend _ bool Zend _ hash _ replace _ checker _ wrapper(HashTable * target,void *source_data,Bucket *p,void *pParam,merge _ checker _ func _ t merge _ checker _ func)
ZEND _ API void ZEND _ hash _ merge _ ex(HashTable * target,HashTable *source,copy _ ctor _ func _ t pCopyConstructor,uint size,merge _ checker _ func _ t pMergeSource,void * pParam)
ZEND _ API int ZEND _ hash _ find(const HashTable * ht,const char *arKey,uint nKeyLength,void **pData)
ZEND _ API int ZEND _ hash _ quick _ find(const HashTable * ht,const char *arKey,uint nKeyLength,ulong h,void **pData)
ZEND _ API int ZEND _ hash _ exists(const HashTable * ht,const char *arKey,uint nKeyLength)
ZEND _ API int ZEND _ hash _ quick _ exists(const HashTable * ht,const char *arKey,uint nKeyLength,ulong h)
ZEND _ API int ZEND _ hash _ index _ find(const HashTable * ht,ulong h,void **pData)
ZEND _ API int ZEND _ hash _ index _ exists(const HashTable * ht,ulong h)
ZEND _ API int ZEND _ hash _ num _ elements(const HashTable * ht)
ZEND _ API int ZEND _ hash _ get _ pointer(const HashTable * ht,HashPointer *ptr)
ZEND _ API int ZEND _ hash _ set _ pointer(HashTable * ht,const HashPointer *ptr)
ZEND _ API void ZEND _ hash _ internal _ pointer _ reset _ ex(HashTable * ht,HashPosition *pos)
ZEND _ API void ZEND _ hash _ internal _ pointer _ end _ ex(HashTable * ht,HashPosition *pos)
ZEND _ API int ZEND _ hash _ move _ forward _ ex(HashTable * ht,HashPosition *pos)
ZEND _ API int ZEND _ hash _ move _ backward _ ex(HashTable * ht,HashPosition *pos)
ZEND _ API int ZEND _ hash _ get _ current _ key _ ex(const HashTable * ht,char **str_index,uint *str_length,ulong *num_index,zend_bool duplicate,HashPosition *pos)
ZEND _ API int ZEND _ hash _ get _ current _ key _ type _ ex(HashTable * ht,HashPosition *pos)
ZEND _ API int ZEND _ hash _ get _ current _ data _ ex(HashTable * ht,void **pData,HashPosition *pos)
ZEND _ API int ZEND _ hash _ update _ current _ key _ ex(HashTable * ht,int key_type,const char *str_index,uint str_length,ulong num_index,int mode,HashPosition *pos)
ZEND _ API int ZEND _ hash _ sort(HashTable * ht,sort_func_t sort_func,compare_func_t compar,int number TSR mls _ DC)
ZEND _ API int ZEND _ hash _ compare(HashTable * ht 1,HashTable *ht2,compare_func_t compar,zend_bool ordered TSRMLS_DC)
ZEND _ API int ZEND _ hash _ minmax(const HashTable * ht,compare_func_t compar,int flag,void **pData TSRMLS_DC)
ZEND _ API ulong ZEND _ hash _ next _ free _ element(const HashTable * ht)
void Zend _ hash _ display _ plist tail(const HashTable * ht)
void Zend _ hash _ display(const HashTable * ht)
;