代码拉取完成,页面将自动刷新
同步操作将从 XNQC1314/miniftpd服务器 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "hash.h"
#include "common.h"
#include <assert.h>
typedef struct hash_node {
void *key;
void *value;
struct hash_node *prev;
struct hash_node *next;
} hash_node_t;
struct hash {
unsigned int buckets;
hashfunc_t hash_func;
hash_node_t **nodes;
};
hash_node_t** hash_get_bucket(hash_t *hash, void *key);
hash_node_t* hash_get_node_by_key(hash_t *hash, void *key, unsigned int key_size);
hash_t *hash_alloc(unsigned int buckets, hashfunc_t hash_func)
{
hash_t *hash = (hash_t *)malloc(sizeof(hash_t));
//assert(hash != NULL);
hash->buckets = buckets;
hash->hash_func = hash_func;
int size = buckets * sizeof(hash_node_t *);
hash->nodes = (hash_node_t **)malloc(size);
memset(hash->nodes, 0, size);
return hash;
}
void* hash_lookup_entry(hash_t *hash, void* key, unsigned int key_size)
{
hash_node_t *node = hash_get_node_by_key(hash, key, key_size);
if (node == NULL) {
return NULL;
}
return node->value;
}
void hash_add_entry(hash_t *hash, void *key, unsigned int key_size,
void *value, unsigned int value_size)
{
if (hash_lookup_entry(hash, key, key_size)) {
fprintf(stderr, "duplicate hash key\n");
return;
}
hash_node_t *node = malloc(sizeof(hash_node_t));
node->prev = NULL;
node->next = NULL;
node->key = malloc(key_size);
memcpy(node->key, key, key_size);
node->value = malloc(value_size);
memcpy(node->value, value, value_size);
hash_node_t **bucket = hash_get_bucket(hash, key);
if (*bucket == NULL) {
*bucket = node;
} else {
// 将新结点插入到链表头部
node->next = *bucket;
(*bucket)->prev = node;
*bucket = node;
}
}
void hash_free_entry(hash_t *hash, void *key, unsigned int key_size)
{
hash_node_t *node = hash_get_node_by_key(hash, key, key_size);
if (node == NULL) {
return;
}
free(node->key);
free(node->value);
if (node->prev) {
node->prev->next = node->next;
} else {
hash_node_t **bucket = hash_get_bucket(hash, key);
*bucket = node->next;
}
if (node->next)
node->next->prev = node->prev;
free(node);
}
hash_node_t** hash_get_bucket(hash_t *hash, void *key)
{
unsigned int bucket = hash->hash_func(hash->buckets, key);
if (bucket >= hash->buckets) {
fprintf(stderr, "bad bucket lookup\n");
exit(EXIT_FAILURE);
}
return &(hash->nodes[bucket]);
}
hash_node_t* hash_get_node_by_key(hash_t *hash, void *key, unsigned int key_size)
{
hash_node_t **bucket = hash_get_bucket(hash, key);
hash_node_t *node = *bucket;
if (node == NULL) {
return NULL;
}
while (node != NULL && memcmp(node->key, key, key_size) != 0) {
node = node->next;
}
return node;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。