This article is reproduced from: http://segmentfault.com/blog/tree/1190000000718519
Hashtable is a very important data structure for PHP. Many of the internal implementations of PHP (variable scope, function table, class properties, methods, arrays) are implemented through Hashtable. Recently learned about the implementation of the PHP bottom Hashtable.
The implementation of PHP's underlying Hashtable has two very important structures: hashtable and bucket.
First of all, Hashtable structure:
The underlying implementation code for the Hashtable is as follows:
typedef struct _hashtable{
UINT ntablesize; The size of the hash bucket, with a minimum of 8
UINT ntablemask; NTableSize-1, optimization of index values
Total number of uint nnumofelements//Bucket
ULONG nnextfreeelement//next digit index position
Bucket *pinternalpointer//Current traversal pointer (foreach faster reason)
Bucket *plisthead//whole hashtable head pointer
Bucket *plisttail//tail pointer of whole Hashtable
Bucket **argbuckets//buceket array, for storing data
Doctor_func_t the callback function when the element is Pdestructor//deleted for the release of the resource
Zend_bool Persistent//bucket memory allocation, true use the system's allocation function, false use PHP's memory allocation function
unsigned char napplycount//mark the number of times the current hash bucket is recursive
Zend_bool bapplyprotection
#If zend_debug
int Inconsistent
#EndIf
}hashtable
The proposal does not understand the hash data structure of the classmate first simple to understand the hash structure.
Briefly describe the initialization of Hashtable in PHP:
The code is as follows:
Zend_api int _zend_hash_init (HashTable *ht, uint nsize, hash_func_t phashfunction, dtor_func_t pdestructor, Zend_bool per Sistent zend_file_line_dc)
{
UINT i = 3;
//...
if (Nsize >= 0x80000000) {
/* Prevent overflow * *
Ht->ntablesize = 0x80000000;
} Else {
while ((1U << i) < nsize) {
i++;
}
Ht->ntablesize = 1 << i;
}
// ...
Ht->ntablemask = ht->ntablesize-1;
/* Uses Ecalloc () so, bucket* = = NULL */
if (persistent) {
TMP = (Bucket * *) calloc (ht->ntablesize, sizeof (Bucket *));
if (!tmp) {
return Failure;
}
Ht->arbuckets = tmp;
} Else {
TMP = (Bucket * *) Ecalloc_rel (ht->ntablesize, sizeof (Bucket *));
if (TMP) {
Ht->arbuckets = tmp;
}
}
return SUCCESS;
}
The first thing to determine is whether the hashtable size you want to initialize is larger than the maximum size the system can use. The following is a processing of the size of the tablesize. Change the user's custom size to the size you want. For example, if the user-defined Hashtable size is 6, the initialization will turn 6 to 8, and if the user-defined size is 11, the initialized hashtable size is 16.
Here is a simple judgment to decide whether to allocate memory according to the C language's own allocated memory function, or to allocate memory according to the memory allocation function that PHP encapsulates.
Let's talk about the structure of bucket.
typedef struct bucket{
ulong H; After the value of the key index, the number key does not do Kash
UINT nkeylength; The length of the key
void *pdata;
void *pdataptr; Pointer data, pointing to real data
struct bucket * plistnext; The next element of the entire hash table
struct bucket *plistlast; The last element of the entire hash table
struct bucket *pnext; Inside the bucket, the next element
struct bucket *plast; The last element in the bucket
Char arkey[1];
}bucket
Here with a very fire on the Network Diagram description (map original address was not found, did not do source description):
The following is a reference to the insert description inside the tipi:
Reference Address: tipi
As in the lower-left corner of the diagram, suppose you inserted the Bucket1,bucket2,bucket3 three elements in turn:
1, when inserting the BUCKET1, the hash table is empty, after the hash is positioned to the slot with index 1. At this point the 1 slots are only one element Bucket1. Where Bucket1 's pdata or pdataptr point to the data stored by BUCKET1. Because there is no link relationship at this time. Pnext, plast,plistnext,plistlast pointers are empty. The first element pointer of the entire hash table and the last element pointer are also saved in the Hashtable structure, where the Hashtable plisthead and Plisttail pointers point to Bucket1.
2, insert Bucket2, because Bucket2 key and Bucket1 key conflict, at this time will Bucket2 placed in the front of the double linked list. Since Bucket2 is inserted and placed at the front end of the list, the Bucket2.pnext points to Bucket1, which is inserted after Bucket2. Bucket1.plistnext points to Bucket2, then Bucket2 is the last element of the hash table, which is hashtable.plisttail pointing to Bucket2. \3, insert Bucket3, the key does not hash to slot 1, then bucket2.plistnext points to Bucket3, because the Bucket3 is inserted after. At the same time hashtable.plisttail to point Bucket3.
Simply put, the bucket structure of the hash table maintains the order in which the elements are inserted in the hash tables, and the Hashtable structure maintains the head and tail of the entire Hashtable. The relationship between budgets is always maintained during the operation of the hash table.