📄 uthash.h
字号:
/*
Copyright (c) 2003-2006, Troy Hanson http://uthash.sourceforge.net
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h> /* memcmp,strlen */
#ifndef UTHASH_H
#define UTHASH_H
#define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */
#define uthash_bkt_malloc(sz) malloc(sz) /* malloc fcn for UT_hash_bucket's */
#define uthash_bkt_free(ptr) free(ptr) /* free fcn for UT_hash_bucket's */
#define uthash_tbl_malloc(sz) malloc(sz) /* malloc fcn for UT_hash_table */
#define uthash_tbl_free(ptr) free(ptr) /* free fcn for UT_hash_table */
#define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
#define uthash_expand_fyi(tbl) /* can be defined to log expands */
/* initial number of buckets */
#define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */
#define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */
#define HASH_FIND(hh,head,keyptr,keylen_in,out) \
do { \
out=head; \
if (head) { \
(head)->hh.tbl->key = (char*)(keyptr); \
(head)->hh.tbl->keylen = keylen_in; \
HASH_FCN((head)->hh.tbl->key,(head)->hh.tbl->keylen, \
(head)->hh.tbl->num_buckets,(head)->hh.tbl->bkt, \
(head)->hh.tbl->i, (head)->hh.tbl->j,(head)->hh.tbl->k); \
HASH_FIND_IN_BKT(hh, (head)->hh.tbl->buckets[ (head)->hh.tbl->bkt], \
keyptr,keylen_in,out); \
} \
} while (0)
#define HASH_ADD(hh,head,fieldname,keylen_in,add) \
HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add)
#define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
do { \
add->hh.next = NULL; \
add->hh.key = (char*)keyptr; \
add->hh.keylen = keylen_in; \
add->hh.elmt = ; \
if (!(head)) { \
head = add; \
(head)->hh.prev = NULL; \
(head)->hh.tbl = (UT_hash_table*)uthash_tbl_malloc( \
sizeof(UT_hash_table)); \
if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \
(head)->hh.tbl->name = #head; \
(head)->hh.tbl->tail = &(add->hh); \
(head)->hh.tbl->noexpand = 0; \
(head)->hh.tbl->hash_q = 1; \
(head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
(head)->hh.tbl->num_items = 0; \
(head)->hh.tbl->hho = ((long)(&add->hh) - (long)(add)); \
(head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_bkt_malloc( \
HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \
memset((head)->hh.tbl->buckets, 0, \
HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
} else { \
(head)->hh.tbl->tail->next = add; \
add->hh.prev = (head)->hh.tbl->tail->elmt; \
(head)->hh.tbl->tail = &(add->hh); \
} \
(head)->hh.tbl->num_items++; \
add->hh.tbl = (head)->hh.tbl; \
(head)->hh.tbl->key = (char*)keyptr; \
(head)->hh.tbl->keylen = keylen_in; \
HASH_FCN((head)->hh.tbl->key,(head)->hh.tbl->keylen, \
(head)->hh.tbl->num_buckets, \
(head)->hh.tbl->bkt, \
(head)->hh.tbl->i, (head)->hh.tbl->j, (head)->hh.tbl->k ); \
HASH_ADD_TO_BKT(hh,(head)->hh.tbl->buckets[(head)->hh.tbl->bkt],add); \
HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \
HASH_FSCK(head); \
} while(0)
#define HASH_DELETE(hh,head,delptr) \
do { \
if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \
uthash_bkt_free((head)->hh.tbl->buckets ); \
uthash_tbl_free((head)->hh.tbl); \
head = NULL; \
} else { \
if ((delptr) == (head)->hh.tbl->tail->elmt) { \
(head)->hh.tbl->tail = (void*)(((long)((delptr)->hh.prev)) + \
(head)->hh.tbl->hho); \
} \
if ((delptr)->hh.prev) { \
((UT_hash_handle*)(((long)((delptr)->hh.prev)) + \
(head)->hh.tbl->hho))->next = (delptr)->hh.next; \
} else { \
head = (delptr)->hh.next; \
} \
if ((delptr)->hh.next) { \
((UT_hash_handle*)(((long)((delptr)->hh.next)) + \
(head)->hh.tbl->hho))->prev = (delptr)->hh.prev; \
} \
(head)->hh.tbl->key = (char*)((delptr)->hh.key); \
(head)->hh.tbl->keylen = (delptr)->hh.keylen; \
HASH_FCN((head)->hh.tbl->key,(head)->hh.tbl->keylen, \
(head)->hh.tbl->num_buckets,(head)->hh.tbl->bkt, \
(head)->hh.tbl->i,(head)->hh.tbl->j,(head)->hh.tbl->k ); \
HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[(head)->hh.tbl->bkt], \
delptr); \
(head)->hh.tbl->num_items--; \
} \
HASH_FSCK(head); \
} while (0)
/* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */
#define HASH_FIND_STR(head,findstr,out) \
HASH_FIND(hh,head,findstr,strlen(findstr),out)
#define HASH_ADD_STR(head,strfield,add) \
HASH_ADD(hh,head,strfield,strlen(add->strfield),add)
#define HASH_FIND_INT(head,findint,out) \
HASH_FIND(hh,head,findint,sizeof(int),out)
#define HASH_ADD_INT(head,intfield,add) \
HASH_ADD(hh,head,intfield,sizeof(int),add)
#define HASH_DEL(head,delptr) \
HASH_DELETE(hh,head,delptr)
/* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined.
* This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.
* This function misuses fields in UT_hash_table for its bookkeeping variables.
*/
#ifdef HASH_DEBUG
#define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0)
#define HASH_FSCK(head) \
do { \
if (head) { \
(head)->hh.tbl->keylen = 0; /* item counter */ \
for( (head)->hh.tbl->bkt_i = 0; \
(head)->hh.tbl->bkt_i < (head)->hh.tbl->num_buckets; \
(head)->hh.tbl->bkt_i++) \
{ \
(head)->hh.tbl->bkt_ideal = 0; /* bkt item counter */ \
(head)->hh.tbl->hh = \
(head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].hh_head; \
(head)->hh.tbl->key = NULL; /* hh_prev */ \
while ((head)->hh.tbl->hh) { \
if ((head)->hh.tbl->key != \
(char*)((head)->hh.tbl->hh->hh_prev)) { \
HASH_OOPS("invalid hh_prev %x, actual %x\n", \
(head)->hh.tbl->hh->hh_prev, \
(head)->hh.tbl->key ); \
} \
(head)->hh.tbl->bkt_ideal++; \
(head)->hh.tbl->key = (char*)((head)->hh.tbl->hh); \
(head)->hh.tbl->hh = (head)->hh.tbl->hh->hh_next; \
} \
(head)->hh.tbl->keylen += (head)->hh.tbl->bkt_ideal; \
if ((head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].count \
!= (head)->hh.tbl->bkt_ideal) { \
HASH_OOPS("invalid bucket count %d, actual %d\n", \
(head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].count, \
(head)->hh.tbl->bkt_ideal); \
} \
} \
if ((head)->hh.tbl->keylen != (head)->hh.tbl->num_items) { \
HASH_OOPS("invalid hh item count %d, actual %d\n", \
(head)->hh.tbl->num_items, (head)->hh.tbl->keylen ); \
} \
/* traverse hh in app order; check next/prev integrity, count */ \
(head)->hh.tbl->keylen = 0; /* item counter */ \
(head)->hh.tbl->key = NULL; /* app prev */ \
(head)->hh.tbl->hh = &(head)->hh; \
while ((head)->hh.tbl->hh) { \
(head)->hh.tbl->keylen++; \
if ((head)->hh.tbl->key !=(char*)((head)->hh.tbl->hh->prev)) {\
HASH_OOPS("invalid prev %x, actual %x\n", \
(head)->hh.tbl->hh->prev, \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -