Home Backend Development PHP Tutorial PHP extension development-use of arrays and introduction to HashTable_PHP tutorial

PHP extension development-use of arrays and introduction to HashTable_PHP tutorial

Jul 14, 2016 am 10:08 AM
hashtable php one time use exist develop us Expand array of Introduction

1 array

In this section we will talk about PHP arrays. In PHP, arrays are implemented using HashTable. In this section, we first introduce HashTable in detail, and then talk about how to use HastTable
1.1 Variable length structure
The so-called variable length structure is actually a special usage of our C language structure, and there is nothing novel about it. Let's first look at a common definition method of variable length structures.
typedef struct bucket {
int n;
char key[30];
char value[1];
} Bucket;
We have defined a structure Bucket, and we hope to use this structure to store students' personal profiles. The key is used to store the student's name, and the value is used to store the student's profile. You may be curious, our value declares a length of 1. How much information can be stored in 1 char?
In fact, for variable-length structures, the variables we are using cannot be directly defined, such as: Bucket bucket; if you use it this way, the value will definitely not store much information. For variable-length structures, we need to first declare a pointer to the variable-length structure when using it, and then allocate function space through the malloc function. We can malloc as much as the length of the space we need to use. The general usage methods are as follows:
Bucket* pBucket;
pBucket = malloc(sizeof(Bucket) + n * sizeof(char));
Where n is the length of value you want to use. If used in this way, will the string pointed to by value become longer soon?
1.2 Introduction to Hashtable
Let’s take a look at the definition of HashTable first
struct _hashtable;
typedef struct bucket {
ulong h;//Use
when the element is a numeric index
uint nKeyLength;//When using string index, this variable represents the length of the index, and the index (string) is stored in the last element aKey
void *pData;//Used to point to the saved data. If the saved data is a pointer, pDataPtr points to this data, and pData points to pDataPtr
void *pDataPtr;
struct bucket *pListNext; //Previous element
struct bucket *pListLast; //Next element
struct bucket *pNext; //Pointer to the next bucket
struct bucket *pLast; //Pointer to the previous bucket
char arKey[1]; //Must be placed at the end, mainly to achieve variable length structure
} Bucket;
typedef struct _hashtable {
uint nTableSize; //The size of the hash table
uint nTableMask; //Numerically equal to nTableSize- 1
uint nNumOfElements; //Records the number of records saved in the current HashTable
ulong nNextFreeElement; //Point to the next free Bucket
Bucket *pInternalPointer; //This variable is used for array inversion
Bucket *pListHead; //Point to the head of the Bucket
Bucket *pListTail; //Point to the tail of the Bucket
Bucket **arBuckets;
dtor_func_t pDestructor; //Function pointer, automatically called when adding, deleting, modifying, and checking the array, used for certain cleaning operations
zend_bool persistent; // Is it persistent?
unsigned char nApplyCount;
zend_bool bApplyProtection; //Works with nApplyCount to prevent infinite recursion during array traversal
#if ZEND_DEBUG
int inconsistent;
#endif
} HashTable;
I hope everyone can take a good look at the above definitions. There are some things that I will not understand when I explain them. It is better for everyone to look at the code and it will be clearer. PHP's array is actually a doubly linked list with a head node, where HashTable is the head and Bucket stores specific node information.
1.3 HashTable internal function analysis
1.3.1 Macro HASH_PROTECT_RECURSION
#defineHASH_PROTECT_RECURSION (ht)
if ((ht)->bApplyProtection) {                                                                                                                       
if ((ht)->nApplyCount++ >= 3){
zend_error(E_ERROR, "Nestinglevel too deep - recursive dependency?");
                                                                          
}
This macro is mainly used to prevent circular references.
1.3.2 Macro ZEND_HASH_IF_FULL_DO_RESIZE
#defineZEND_HASH_IF_FULL_DO_RESIZE(ht)                                                                          
if ((ht)->nNumOfElements >(ht)->nTableSize) {
zend_hash_do_resize(ht);
}
The function of this macro is to check whether the number of elements in the current HashTable is greater than the total size of the HashTable. If the number is greater than the size of the HashTable, then we will reallocate the space. Let’s take a look at zend_hash_do_resize
static int zend_hash_do_resize(HashTable *ht)
{
Bucket **t;
IS_CONSISTENT(ht);
if ((ht->nTableSize << 1) > 0) { /* Let's double the table size */
t = (Bucket**) perealloc_recoverable(ht->arBuckets,
(ht->nTableSize << 1) * sizeof(Bucket *), ht->persistent);
if (t) {
HANDLE_BLOCK_INTERRUPTIONS();
ht->arBuckets = t;
ht->nTableSize = (ht->nTableSize << 1);
ht->nTableMask = ht->nTableSize - 1;
zend_hash_rehash(ht);
HANDLE_UNBLOCK_INTERRUPTIONS();
return SUCCESS;
}
return FAILURE;
}
return SUCCESS;
}
From the above code we can see that when HashTable allocates space, the newly allocated space is equal to twice the original space.
1.3.3 Function _zend_hash_init
This function is used to initialize HashTable. Let’s take a look at the code first:
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)
{
uint i = 3; //The default size of HashTable is 2 raised to the third power
Bucket **tmp;
SET_INCONSISTENT(HT_OK);
if (nSize >= 0x80000000) {
ht->nTableSize = 0x80000000;
} else {
while ((1U << i) < nSize) {
i++;
}
ht->nTableSize = 1 << i;
}
ht->nTableMask = ht->nTableSize- 1;
ht->pDestructor = pDestructor;
ht->arBuckets = NULL;
ht->pListHead = NULL;
ht->pListTail = NULL;
ht->nNumOfElements = 0;
ht->nNextFreeElement = 0;
ht->pInternalPointer = NULL;
ht->persistent = persistent;
ht->nApplyCount = 0;
ht->bApplyProtection = 1;
/* Uses ecalloc() so that 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;
}
It can be seen that the size of HashTable is initialized to the nth power of 2. In addition, we see that there are two memory methods, one is calloc and the other is ecalloc_rel. We have discussed these two memory allocation methods in detail. If you are interested, you can check it out yourself.
1.3.4 Function_zend_hash_add_or_update
This function adds or modifies element information to HashTable
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)
{
ulong h;
uint nIndex;
Bucket *p;
IS_CONSISTENT(ht);
if (nKeyLength <= 0) {
#if ZEND_DEBUG
ZEND_PUTS("zend_hash_update: Can't put inempty keyn");
#endif
return FAILURE;
}
h = zend_inline_hash_func(arKey, nKeyLength);
nIndex = h & ht->nTableMask;
p = ht->arBuckets[nIndex];
while (p != NULL) {
if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
if (!memcmp(p->arKey, arKey, nKeyLength)) {
              if (flag & HASH_ADD) {
                    return FAILURE;
        }
HANDLE_BLOCK_INTERRUPTIONS();
#if ZEND_DEBUG
if (p->pData == pData) {
ZEND_PUTS("Fatal error in zend_hash_update:p->pData == pDatan");
HANDLE_UNBLOCK_INTERRUPTIONS();
                    return FAILURE;
        }
#endif
              if (ht->pDestructor) {
              ht->pDestructor(p->pData);
        }
UPDATE_DATA(ht, p, pData, nDataSize);
            if (pDest) {
*pDest = p->pData;
        }
HANDLE_UNBLOCK_INTERRUPTIONS();
return SUCCESS;
      }
}
p = p->pNext;
}
p = (Bucket *) pemalloc(sizeof(Bucket) - 1 + nKeyLength, ht->persistent);
if (!p) {
return FAILURE;
}
memcpy(p->arKey, arKey, nKeyLength);
p->nKeyLength = nKeyLength;
INIT_DATA(ht, p, pData, nDataSize);
p->h = h;
CONNECT_TO_BUCKET_DLLIST(p, ht->arBuckets[nIndex]);
if (pDest) {
*pDest = p->pData;
}
HANDLE_BLOCK_INTERRUPTIONS();
CONNECT_TO_GLOBAL_DLLIST(p, ht);
ht->arBuckets[nIndex] = p;
HANDLE_UNBLOCK_INTERRUPTIONS();
ht->nNumOfElements++;
ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
return SUCCESS;
}
1.3.5 Macro CONNECT_TO_BUCKET_DLLIST
#define CONNECT_TO_BUCKET_DLLIST(element, list_head)
(element)->pNext= (list_head);
(element)->pLast= NULL;
if((element)->pNext) {                                                                            
(element)->pNext->pLast =(element);
}
This macro adds the bucket to the bucket list
1.3.6 Other functions or macro definitions
We just briefly introduce HashTable. If you want to understand HashTable in detail, it is recommended that you take a look at the source code of php. The code for HashTable is in Zend/zend_hash.h and Zend/zend_hash.c.
zend_hash_add_empty_element adds an empty element to the function
zend_hash_del_key_or_index deletes elements based on index
zend_hash_reverse_apply Reverse traverse HashTable
zend_hash_copy copy
_zend_hash_merge merge
zend_hash_find String index search
zend_hash_index_find Numeric index method search
zend_hash_quick_find encapsulation of the above two functions
zend_hash_exists Whether the index exists
zend_hash_index_exists Whether the index exists
zend_hash_quick_exists encapsulation of the above two methods
1.4 Commonly used HashTable functions in C extension
Although HashTable looks a bit complicated, it is very convenient to use. We can use the following functions to initialize and assign values ​​to HashTable.
Enrollment numbers of local colleges and universities in 2005
PHP syntax
C syntax
Meaning
$arr = array()
array_init(arr);
Initialize array
$arr[] = NULL;
add_next_index_null(arr);
$arr[] = 42;
add_next_index_long(arr, 42);
$arr[] = true;
add_next_index_bool(arr, 1);
$arr[] = 3.14;
add_next_index_double(3.14);
$arr[] = ‘foo’;
add_next_index_string(arr, “foo”, 1);
1 means string copy
$arr[] = $myvar;
add_next_index_zval(arr, myvar);
$arr[0] = NULL;
add_index_null(arr, 0);
$arr[1] = 42;
add_index_long(arr, 1, 42);
$arr[2] = true;
add_index_bool(arr, 2, 1);
$arr[3] = 3.14;
add_index_double(arr, 3, 3,14);
$arr[4] = ‘foo’;
add_index_string(arr, 4, “foo”, 1);
$arr[5] = $myvar;
add_index_zval(arr, 5, myvar);
$arr[“abc”] = NULL;
add_assoc_null(arr, “abc”);
$arr["def"] = 711;
add_assoc_long(arr, “def”, 711);
$arr[“ghi”] = true;
add_assoc_bool(arr, ghi”, 1);
$arr[“jkl”] = 1.44;
add_assoc_double(arr, “jkl”, 1.44);
$arr[“mno”] = ‘baz’;
add_assoc_string(arr, “mno”, “baz”, 1);
$arr[‘pqr’] = $myvar;
add_assoc_zval(arr, “pqr”, myvar);
1.5 Tasks and Experiments
Having said all that, let’s experiment.
Task: Return an array. The data in the array is as follows:
Array
(
[0] => for test
[42] => 123
[for test. for test.] => 1
[array] => Array
(
[0] => 3.34
)
)
Code implementation:
PHP_FUNCTION(test)
{
zval* t;
array_init(return_value);
add_next_index_string(return_value, "for test", 1);
add_index_long(return_value, 42, 123);
add_assoc_double(return_value, "for test. for test.", 1.0);
ALLOC_INIT_ZVAL(t);
array_init(t);
add_next_index_double(t, 3.34);
add_assoc_zval(return_value, "array", t);
}
It’s very simple, remember return_value?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477779.htmlTechArticle1 Array In this section we will talk about php arrays. In php, arrays are implemented using HashTable. In this section, we first introduce HashTable in detail, and then talk about how to use HastTable 1.1...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles