Hashtable implementation example in php _ PHP Tutorial

Source: Internet
Author: User
Example of hashtable implementation in php. Anyone who has some knowledge about the php kernel should know that the essence of php is HashTable, which is everywhere in php implementation. Anyone who has some knowledge about the php kernel, including php arrays, global variables, and local variables, should know that the essence of php is HashTable, which is everywhere in php implementations. Including php arrays, global variables, and the scope of local variables. php hashtable is split into four parts:

Hash Function: uses a hash function of time33 to convert the key of a string into a number.
A c array: used to store buckets (buckets)
Two two-way Linked Lists: The first two-way linked list is that each element of the array (bucket) is a two-way linked list, so as to solve the hash conflict; the second two-way linked list is an array that connects each bucket. here, we need to connect the first two-way linked list header to traverse the entire hash table, laruence has a blog about foreach in php. the design here is for foreach ==> in-depth understanding of arrays in PHP (traversal order)
I will not talk about the struct of hashtable and The struct of bucket here, because the following recommendation links almost all talk about it. I don't think I can describe anything better than what I said. the level of each person is different, I will describe it based on my current technical level, so I will only record some of my items

The following are two files implemented by hash in php: zend_hash.c zend_hash.h. These two files implement a bunch of APIs and a bunch of Apis. below is the prototype of the implemented api.

The code is as follows:


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 _ partition (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_insert (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)
Static Bucket * zend_hash_apply_deleter (HashTable * ht, Bucket * 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 TSRMLS_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 TSRMLS_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)
Static 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_backwards_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 renumber TSRMLS_DC)
ZEND_API int zend_hash_compare (HashTable * ht1, 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_pListTail (const HashTable * ht)
Void zend_hash_display (const HashTable * ht)

Bytes. Including php arrays, global variables, and local variables...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.