Home Backend Development PHP Tutorial PHP中Array的hash函数兑现

PHP中Array的hash函数兑现

Jun 13, 2016 pm 01:08 PM
case hash lt

PHP中Array的hash函数实现

今天回顾学习了PHP中变量实现的方法,在浏览其源码是发现在PHP中所有的数据类型通过一个union存储。

php语言是弱类型语言,其实现中通过记录变量的类型和值来实现其管理。

?

PHP中使用最多的非Array莫属了,那Array是如何实现的?

在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Array元素的复杂度为O(N),最好则为1.

?

而其计算字符串hash值的方法如下,将源码摘出来以供查备:

ps:对于以下函数,仍有两点不明:

1.? hash = 5381设置的理由?

2.? 这种step=8的循环方式是为了效率么?

?

static inline ulong zend_inline_hash_func(const char *arKey, uint nKeyLength)
{
    register ulong hash = 5381; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此处初始值的设置有什么玄机么?

    /* variant with the hash unrolled eight times */
    for (; nKeyLength >= 8; nKeyLength -= 8) { ? ? ? ? ? ? ? ? ? ? ? ? //这种step=8的方式是为何?
        hash = ((hash 
?
 <div class="clear">
                 
              
              
        
            </div>
Copy after login
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 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)

How to implement Redis Hash operation in php How to implement Redis Hash operation in php May 30, 2023 am 08:58 AM

Hash operation //Assign values ​​to fields in the hash table. Returns 1 on success and 0 on failure. If the hash table does not exist, the table will be created first and then the value will be assigned. If the field already exists, the old value will be overwritten. $ret=$redis->hSet('user','realname','jetwu');//Get the value of the specified field in the hash table. If the hash table does not exist, return false. $ret=$redis->hGet('user','rea

HMD Skyline gets a new color option and official magnetic case HMD Skyline gets a new color option and official magnetic case Aug 23, 2024 am 07:04 AM

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Laravel development: How to generate password hash using Laravel Hash? Laravel development: How to generate password hash using Laravel Hash? Jun 17, 2023 am 10:59 AM

Laravel is currently one of the most popular PHP web frameworks, providing developers with many powerful features and components, among which LaravelHash is one of them. LaravelHash is a PHP library for password hashing that can be used to keep passwords secure and make your application's user data more secure. In this article, we will learn how LaravelHash works and how to use it to hash and verify passwords. Prerequisite knowledge in learning Lara

switch case judgment variable switch case judgment variable Feb 19, 2024 am 08:04 AM

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values ​​​​of variables: #includeintmain(){

请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 Jun 13, 2016 am 10:24 AM

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求

Understand the Hash algorithm and application scenarios in one article Understand the Hash algorithm and application scenarios in one article Apr 13, 2023 am 11:55 AM

1. What is a hashing algorithm? Both hashing and hashing come from the word hash. The former is a transliteration and the latter is a free translation. It is an algorithm that can map a binary value of any length into a fixed-length binary value. The mapped fixed-length binary value is called a hash value. An excellent hash algorithm needs to meet the following requirements: it cannot reversely deduce the original data from the hash value; it is very sensitive to the input data, and a different bit will cause the hash value to be very different; the probability of hash conflict must be Very small; the calculation process of the hash algorithm must be simple and efficient enough, even if the original data is very long, the hash value can be obtained quickly; 2. Usage scenarios of the hash algorithm 2.1 Secure encryption The more common hash encryption algorithms include MD5 ( MD5 Message-Dige

Analysis of common operation examples of Hash, the basic data type of Redis Analysis of common operation examples of Hash, the basic data type of Redis May 31, 2023 am 10:43 AM

Common operations of Redis data type Hash Hash in redis is a mapping table of string type fields and values. Particularly suitable for storing objects, each hash can store more than 4 billion key-value pairs. Children's shoes who are familiar with python can think of it as a dictionary dict. The previous data type storage was k-v, and the hash storage is k-dict, and the dict will have its own k-v. 1. hset assigns values ​​to the fields in the hash table. If the hash table does not exist, create a new hash table and perform the hset operation. If the field already exists in the hash table, the old value will be overwritten. hsetmyhashk1v1 two, h

See all articles