很多站长朋友们都不太清楚php实现hash函数,今天小编就来给大家整理php实现hash函数,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP 函数hash_hmac()怎么用 2、 深入PHP中的HashTable结构详解 3、 PHP中用hash实现的数组 4、 PHP 数组的底层实现 PHP 函数hash_hmac()怎么用hash_hmac — 使用 HMAC 方法生成带有密钥的哈希值
string hash_hmac(string $algo, string $data, string $key[, bool $raw_output = false])
参数:
algo:要使用的哈希算法名称,例如:"md5","sha256","haval160,4" 等。
data:要进行哈希运算的消息。
key:使用 HMAC 生成信息摘要时所使用的密钥。
raw_output:设置为 TRUE 输出原始二进制数据, 设置为 FALSE 输出小写 16 进制字符串。
返回值:
如果 raw_output 设置为 TRUE, 则返回原始二进制数据表示的信息摘要,否则返回 16 进制小写字符串格式表示的信息摘要。
如果 algo 参数指定的不是受支持的算法,返回 FALSE。
深入PHP中的HashTable结构详解深入PHP中的HashTable结构详解
深入PHP中的HashTable结构详解
对php内核有一定了解的人应该都知道php的精髓就是HashTable,HashTable在php的实现中无处不在。包括php的数组、什么全局变量、局部变量的作用域等等,php的hashtable拆开来说就是四部分:
hash函数:用的是time33的散列函数,将一个字符串的key转换成一个数字
一个C数组:用来储存桶(buckets)的
两个双向的链表:第一个双向链表是数组的每个元素(桶bucket)是一个双向链表,这样做是为了解决hash冲突;第二个双向链表是数组将每一个桶(bucket)连接起来,这里要连接的也就是第一个双向链表的链表头,这样做是为了遍历整个hash表用的,鸟哥有篇blog是讲php的foreach的,这里这样设计就是给foreach用的==>《深入理解PHP之数组(遍历顺序)》
我这里不再说hashtable的struct和bucket的`struct了,因为下面的推荐链接几乎都讲了,我不觉得我能描述和说的比他们好,每个人的水平不一样,我就以我现在的技术水平来描述,所以我就只把我整理的一些东西记录一下
下面是php中hash实现的两个文件:zend_hash.c zend_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)
static Bucket *zend_hash_apply_r(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)
;
PHP中用hash实现的数组PHP中使用最多的非Array莫属了,那Array是如何实现的?在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Array元素的复杂度为O(N),最好则为1.
而其计算字符串hash值的方法如下,将源码摘出来以供查备:
复制代码
代码如下:
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
<<
5)
+
hash)
+
*arKey++;
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
//比直接*33要快
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
}
switch
(nKeyLength)
{
case
7:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
/*
fallthrough...
*/
//此处是将剩余的字符hash
case
6:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
/*
fallthrough...
*/
case
5:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
/*
fallthrough...
*/
case
4:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
/*
fallthrough...
*/
case
3:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
/*
fallthrough...
*/
case
2:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
/*
fallthrough...
*/
case
1:
hash
=
((hash
<<
5)
+
hash)
+
*arKey++;
break;
case
0:
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
return
hash;//返回hash值
}
ps:对于以下函数,仍有两点不明:
hash
=
5381设置的理由?
这种step=8的循环方式是为了效率么?
PHP 数组的底层实现PHP 数组的底层主要是通过 HashTable 实现,HashTable 通过映射函数或者散列函数将 String Key 转换成一个普通的数字下标,然后再将 Value 值存储到下标对应的数组元素中
HashTable 主要包含两部分:1.存储元素的数组 2.散列函数或者映射函数
随机访问
如果我们指定一个 Key=>Value 的映射关系,Key 是一个 String 类型的,则先通过 Time 33 算法将 String 转换成一个 Int 整型,然后再通过 PHP 里面特定的散列算法映射成 Bucket 数组中的一个下标,将 Value 值存储到对应的下标元素中,当我们通过 Key 访问数组元素时,只需要再通过相同的算法计算出对应的 Key,就能实现随机访问数组元素
顺序访问
存储在 HashTable 中的数组是无序的,但是 PHP 中的数组是有序的,为了实现 HashTable 的有序性,PHP 引入了一个中间映射表,该表是一个大小和 Bucket 数组相同的数组,数组中存放的是整形数据,主要用于存放元素实际存储的 Value 的下标值,当引入中间映射表之后,Bucket 中的数据是有序的,而中间映射表中的数据是无序的,当我们顺序访问的时候只需要遍历 Bucket 中的数据即可
Hash 冲突
PHP 解决 Hash 冲突采用的是链地址法,将出现冲突的 Bucket 串成链表,这样通过中间映射表映射出来的就不再是一个元素而是一个链表,通过散列函数定位到对应的 Bucket 链表时,需要遍历链表,逐个对比 key 值,直至找出对应的目标值
PHP 实现扩容
1.当删除的元素所占比例超出阈值的时候,则需要移除已经被逻辑删除的 Bucket,将后面的 Bucket 补位到前面,因为 Bucket 的下标发生了变动,所以需要更新每元素在中间映射表中实际存储的下标值
2.当没有超出阈值的时候,PHP 会申请一个大小是原来两倍的新数组,并将旧数组中的数据复制到新数组中,因为数组长度发生了变化,所以 key->value 的映射关系需要重新计算,这个就是重建索引
关于php实现hash函数的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php实现hash函数 php哈希表的详细内容...