📄 uthash.h
字号:
/*Copyright (c) 2003-2008, Troy D. Hanson http://uthash.sourceforge.netAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, 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 "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITEDTO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNEROR 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, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include <string.h> /* memcmp,strlen */#include <stddef.h> /* ptrdiff_t */#ifndef UTHASH_H#define UTHASH_H /* C++ requires extra stringent casting */#if defined __cplusplus#define TYPEOF(x) (typeof(x))#else#define TYPEOF(x)#endif#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_INITIAL_NUM_BUCKETS_LOG2 5 /* lg2 of initial number of buckets */#define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches *//* calculate the element whose hash handle address is hhe */#define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)hhp) - (tbl)->hho))#define HASH_FIND(hh,head,keyptr,keylen_in,out) \do { \ out=TYPEOF(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->hash_scratch, (head)->hh.tbl->bkt, \ (head)->hh.tbl->i, (head)->hh.tbl->j,(head)->hh.tbl->k); \ HASH_FIND_IN_BKT((head)->hh.tbl, 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; \ 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"); } \ memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \ (head)->hh.tbl->tail = &(add->hh); \ (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ (head)->hh.tbl->hho = (char*)(&add->hh) - (char*)(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 = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ (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, \ (add)->hh.hashv, \ (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(hh,head); \} while(0)#define HASH_TO_BKT( hashv, num_bkts, bkt ) bkt = ((hashv) & ((num_bkts) - 1))/* delete "delptr" from the hash table. * "the usual" patch-up process for the app-order doubly-linked-list. * The use of tbl->hh_del below deserves special explanation. * These used to be expressed using (delptr) but that led to a bug * if someone used the same symbol for the head and deletee, like * HASH_DELETE(hh,users,users); * We want that to work, but by changing the head (users) below * we were forfeiting our ability to further refer to the deletee (users) * in the patch-up process. Solution: use scratch space in the table to * copy the deletee pointer, then the latter references are via that * scratch pointer rather than through the repointed (users) symbol. */#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 { \ (head)->hh.tbl->hh_del = &((delptr)->hh); \ if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \ (head)->hh.tbl->tail = \ (UT_hash_handle*)((char*)((delptr)->hh.prev) + \ (head)->hh.tbl->hho); \ } \ if ((delptr)->hh.prev) { \ ((UT_hash_handle*)((char*)((delptr)->hh.prev) + \ (head)->hh.tbl->hho))->next = (delptr)->hh.next; \ } else { \ head = TYPEOF(head)((delptr)->hh.next); \ } \ if ((head)->hh.tbl->hh_del->next) { \ ((UT_hash_handle*)((char*)(head)->hh.tbl->hh_del->next + \ (head)->hh.tbl->hho))->prev = \ (head)->hh.tbl->hh_del->prev; \ } \ HASH_TO_BKT( (head)->hh.tbl->hh_del->hashv, \ (head)->hh.tbl->num_buckets, \ (head)->hh.tbl->bkt); \ HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[(head)->hh.tbl->bkt], \ (head)->hh.tbl->hh_del); \ (head)->hh.tbl->num_items--; \ } \ HASH_FSCK(hh,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(hh,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->ideal_chain_maxlen = 0; /*bkt item counter*/ \ (head)->hh.tbl->thh = \ (head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].hh_head; \ (head)->hh.tbl->key = NULL; /* hh_prev */ \ while ((head)->hh.tbl->thh) { \ if ((head)->hh.tbl->key != \ (char*)((head)->hh.tbl->thh->hh_prev)) { \ HASH_OOPS("invalid hh_prev %x, actual %x\n", \ (head)->hh.tbl->thh->hh_prev, \ (head)->hh.tbl->key ); \ } \ (head)->hh.tbl->ideal_chain_maxlen++; \ (head)->hh.tbl->key = (char*)((head)->hh.tbl->thh); \ (head)->hh.tbl->thh = (head)->hh.tbl->thh->hh_next; \ } \ (head)->hh.tbl->keylen += \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -