php 文件hash,PHP HASH算法实现代码分享
/*
* 头文件
* 说明:此实现不保存对象,只保存对象的引用
*/
#ifndef _HASH_H
#define _HASH_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct tagHASHBUCKET HASHBUCKET, *LPHASHBUCKET;
typedef struct tagHASHTABLE HASHTABLE, *LPHASHTABLE;
struct tagHASHBUCKET
{
unsigned long h;
unsigned int key_length;
void *data;
LPHASHBUCKET next;
LPHASHBUCKET previous;
LPHASHBUCKET conflict_next;
LPHASHBUCKET conflict_previous;
char key[1];
};
typedef unsigned long (*hash_func_t)(char *, unsigned int);
struct tagHASHTABLE
{
unsigned int table_size;
unsigned int size_index;
unsigned int elements;
hash_func_t hash;
LPHASHBUCKET p;
LPHASHBUCKET head;
LPHASHBUCKET tail;
LPHASHBUCKET *buckets;
};
extern int hash_create(LPHASHTABLE, unsigned int, hash_func_t);
extern int hash_entry(LPHASHTABLE, char *, unsigned int, void *);
extern int hash_find(LPHASHTABLE, char *, unsigned int, void **);
extern int hash_update(LPHASHTABLE, char *, unsigned int, void *);
extern int hash_remove(LPHASHTABLE, char *, unsigned int);
extern int hash_destroy(LPHASHTABLE);
#ifdef __cplusplus
};
#endif
#endif
/*
* HASH实现
*/
#include
#include
#include "main.h"
static unsigned int size_table[] =
{5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987, 262237, 524521, 1048793, 2097397, 41941
03, 8388857, 16777447, 33554201, 67108961, 134217487, 268435697, 536870683, 1073741621, 2147483399};
#define COUNTS_OF_SIZE_TABLE (sizeof(size_table) / sizeof(size_table[0]))
static unsigned long
hashpjw(char *key, unsigned int key_length)
{
unsigned long h, g;
char *p;
h = 0;
p = key + key_length;
while (key < p)
{
h = (h << 4) + *key++;
if ((g = (h & 0xF0000000)))
{
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
static int
hash_do_rehash(LPHASHTABLE pht)
{
LPHASHBUCKET p;
unsigned int index;
memset(pht->buckets, 0, sizeof(LPHASHBUCKET) * size_table[pht->size_index]);
for (p = pht->head; p; p = p->next)
{
index = p->h % pht->table_size;
p->conflict_next = 0;
p->conflict_previous = pht->buckets[index];
if (p->conflict_previous)
{
p->conflict_previous->conflict_next = p;
}
pht->buckets[index] = p;
}
return 0;
}
static int
hash_do_resize(LPHASHTABLE pht)
{
LPHASHBUCKET *pp;
if (pht->size_index < (unsigned int)COUNTS_OF_SIZE_TABLE - 1)
{
pp = (LPHASHBUCKET *)realloc(pht->buckets, size_table[pht->size_index + 1] * sizeof(LPHASHBUCKET));
if (pp)
{
pht->buckets = pp;
pht->size_index++;
pht->table_size = size_table[pht->size_index];
hash_do_rehash(pht);
return 0;
}
return -1;
}
return 0;
}
int
hash_create(LPHASHTABLE pht, unsigned int size, hash_func_t hash)
{
int i;
for (i = 0; i < COUNTS_OF_SIZE_TABLE; ++i)
{
if (size <= size_table[i])
{
size = size_table[i];
pht->size_index = i;
break;
}
}
if (i == COUNTS_OF_SIZE_TABLE)
{
size = size_table[COUNTS_OF_SIZE_TABLE - 1];
pht->size_index = COUNTS_OF_SIZE_TABLE - 1;
}
pht->buckets = (LPHASHBUCKET *)calloc(size, sizeof(LPHASHBUCKET));
if (!pht->buckets)
{
return -1;
}
pht->hash = hash? hash: hashpjw;
pht->elements = 0;
pht->head = 0;
pht->p = 0;
pht->tail = 0;
pht->table_size = size;
return 0;
}
int
hash_entry(LPHASHTABLE pht, char *key, unsigned int key_length, void *data)
{
unsigned long h;
unsigned int index;
LPHASHBUCKET p;
h = pht->hash(key, key_length);
index = h % pht->table_size;
for (p = pht->buckets[index]; p; p = p->conflict_previous)
{
if (p->h == h && p->key_length == key_length)
{
if (!memcmp(p->key, key, key_length))
{
return -1;
}
}
}
p = (LPHASHBUCKET)malloc(sizeof(HASHBUCKET) - 1 + key_length);
if (!p)
{
return -1;
}
memcpy(p->key, key, key_length);
p->key_length = key_length;
p->h = h;
p->data = data;
p->conflict_next = 0;
p->conflict_previous = pht->buckets[index];
if (p->conflict_previous)
{
p->conflict_previous->conflict_next = p;
}
p->previous = pht->tail;
p->next = 0;
pht->tail = p;
if (p->previous)
{
p->previous->next = p;
}
if (!pht->head)
{
pht->head = p;
}
pht->buckets[index] = p;
++pht->elements;
if (pht->elements > pht->table_size)
{
hash_do_resize(pht);
}
return 0;
}
int
hash_find(LPHASHTABLE pht, char *key, unsigned int key_length, void **data)
{
unsigned long h;
unsigned int index;
LPHASHBUCKET p;
h = pht->hash(key, key_length);
index = h % pht->table_size;
for (p = pht->buckets[index]; p; p = p->conflict_previous)
{
if (p->h == h && p->key_length == key_length && !memcmp(p->key, key, key_length))
{
*data = p->data;
return 0;
}
}
return -1;
}
int
hash_remove(LPHASHTABLE pht, char *key, unsigned int key_length)
{
unsigned long h;
unsigned int index;
LPHASHBUCKET p;
h = pht->hash(key, key_length);
index = h % pht->table_size;
for (p = pht->buckets[index]; p; p = p->conflict_previous)
{
if (p->h == h && p->key_length == key_length && !memcmp(p->key, key, key_length))
{
if (p->conflict_previous)
{
p->conflict_previous->conflict_next = p->conflict_next;
}
if (p->conflict_next)
{
p->conflict_next->conflict_previous = p->conflict_previous;
}
if (p->previous)
{
p->previous->next = p->next;
}
if (p->next)
{
p->next->previous = p->previous;
}
if (pht->buckets[index] == p)
{
pht->buckets[index] = p->conflict_previous;
}
if (pht->head == p)
{
pht->head = p->next;
}
if (pht->tail == p)
{
pht->tail = p->previous;
}
--pht->elements;
free(p);
return 0;
}
}
return -1;
}
int
hash_update(LPHASHTABLE pht, char *key, unsigned int key_length, void *data)
{
unsigned long h;
unsigned int index;
LPHASHBUCKET p;
h = pht->hash(key, key_length);
index = h % pht->table_size;
for (p = pht->buckets[index]; p; p = p->conflict_previous)
{
if (p->h == h && p->key_length == key_length && !memcmp(p->key, key, key_length))
{
p->data = data;
return 0;
}
}
return -1;
}
int
hash_destroy(LPHASHTABLE pht)
{
LPHASHBUCKET p, q;
p = pht->head;
while (p)
{
q = p;
p = p->next;
free(q);
}
free(pht->buckets);
return 0;
}
php 文件hash,PHP HASH算法实现代码分享相关推荐
- linux脚本实现复制,Shell脚本实现复制文件到多台服务器的代码分享
在多机集群环境中,经常面临修改配置文件后拷贝到多台服务器的情况,传统的执行scp比较麻烦,所以写了以下shell脚本,可以将指定文件拷贝到多台机器. 使用方法请参见HELP部分代码. #!/bin/b ...
- c语言快速排序算法代码,c语言快速排序算法示例代码分享
#include #include #include #define RANDOM(i) (rand()%i) #define N 9 //设置数组长度 //分区操作 int Partition ...
- hash算法_到底什么是Hash?Hash算法的原理和实际应用讲解
提到hash,相信大多数同学都不会陌生,之前很火现在也依旧很火的技术区块链背后的底层原理之一就是hash,下面就从hash算法的原理和实际应用等几个角度,对hash算法进行一个讲解. 1.什么是Has ...
- hash地址_hash 算法原理及应用漫谈
本文作者:jeffhe,腾讯 IEG 开发工程师 提到hash,相信大多数同学都不会陌生,之前很火现在也依旧很火的技术区块链背后的底层原理之一就是hash,下面就从hash算法的原理和实际应用等几个角 ...
- 安全系列之——主流Hash散列算法介绍和使用
其他文章: 安全系列之--手写JAVA加密.解密 安全系列之--数据传输的完整性.私密性.源认证.不可否认性 安全系列之--主流Hash散列算法介绍和使用 安全系列之--RSA的公钥私钥有多少人能分的 ...
- 求锤得锤,你要的一致性 hash 来了! | 附代码
来源 | BigData之路 头图 | 视觉中国 前言 最近总有人问我一致性hash的事情,求锤得锤,我们今天就来聊聊看.前两篇我们分别介绍了两类哈希分片的方法:hash取模和虚拟桶. hash取模法 ...
- vue打包静态文件名称不加hash值和不修改文件夹结构
背景 使用vue脚手架开发前端项目引用了大量的图片,导致每次打包后都有好几百兆的大小,即使改动不涉及一些静态的图片和音频,打包的结果依然会修改静态文件名.如果只更新js文件等,会报引用无效的错误.这是 ...
- 安全系列之——主流 Hash 散列算法介绍和使用
这里填写标题 1. 安全系列之--主流 Hash 散列算法介绍和使用 1.1. Hash 散列算法介绍 1.2. Hash 散列算法的特征 1.3. 散列算法的使用 1.3.1. 文件传输 1.3.2 ...
- 密码学之Hash散列算法
前言 在第一篇文章中已经有介绍区块链技术概念,我们知道区块链主要是由共识算法机制.p2p网络.密码学这几个核心技术组成,前面几篇文章讲了共识算法,p2p网络,这次我们谈一下密码学,密码学是区块链系统的 ...
最新文章
- 基于区域的CNN(R-CNN)
- 【自动化__持续集成】___java___static
- C# 发邮件 服务器响应为: 5.7.0 Must issue a STARTTLS command first
- 如何用架构师思维解读区块链技术?
- 基础练习 十六进制转八进制 c语言
- 易语言怎么判断文件是否一样_怎么判断专利代理人或专利代理机构是否靠谱?...
- 权限设计表结构超详细_模具设计丨问题点检表、全套标准件结构分布,值得转发...
- ARM联合创始人:若被英伟达收购 将是一场灾难
- 【LeetCode】231. Power of Two
- Linux目录详细说明
- android中一些常用的VIEW动作类型
- 【亲测有效】快速简单解决apple Ipad无法配对apple pencil问题
- 用计算机播放cd,电脑上怎么放光盘_播放CD的方法步骤
- 论文阅读:MPViT : Multi-Path Vision Transformer for Dense Prediction
- 如何将手机中Word文档转换成PDF
- Reality Shares联合纳斯达克,将推出区块链经济指数
- Unity实时GI与烘焙GI
- vue 全局刷新与局部刷新
- 经济-一文看懂经济学进化史:经济学的四次革命
- 什么叫h5项目_对移动端h5项目的一点总结
热门文章
- python实现决策树算法sklearn_GitHub - cbyonder/lihang_algorithms: 用python和sklearn两种方法实现李航《统计学习方法》中的算法...
- MATLAB 循环结构解析
- 电子商务c语言实训报告,中南民族大学电子商务C语言实验报告.doc
- 步步深入MySQL:架构-gt;查询执行流程-gt;SQL解析顺序!
- JAVA并发,线程异常捕获
- 第六章—条件语句和循环
- [JQuery代码]超酷鼠标滑过背景高亮效果
- 利用Office Chart 制作柱图(一个柱子)
- Visual Studio 剪切板新特性
- java 二维数据结构_JAVA描述算法和数据结构(01):稀疏数组和二维数组转换