Home » Source Code » » uthash.h

uthash.h ( File view )

From:
  • By 2010-08-18
  • View(s):8
  • Download(s):0
  • Point(s): 1
			/*
Copyright (c) 2003-2009, Troy D. 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.

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 */
#include <stddef.h> /* ptrdiff_t */

#ifndef UTHASH_H
#define UTHASH_H 

#define UTHASH_VERSION 1.5

/* 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,out)                                   \
do {
                                                                           \
  unsigned _hf_bkt,_hf_hashv;                                                  \
  out=TYPEOF(out)head;                                                         \
  if (head) {
                                                                  \
     HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \
     HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ],  \
                      keyptr,keylen,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 {
                                                                           \
 unsigned _ha_bkt;                                                             \
 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;                                                 \
 HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets,                       \
         (add)->hh.hashv, _ha_bkt);                                            \
 HASH_ADD_TO_BKT(hh,(head)->hh.tbl->buckets[_ha_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 _hd_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 {
                                                                           \
    unsigned _hd_bkt;                                                          \
    struct UT_hash_handle *_hd_hh_del;                                         \
    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 {
                                                                   \
        _hd_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 (_hd_hh_del->next) {
                                                \
            ((UT_hash_handle*)((char*)_hd_hh_del->next +                       \
                    (head)->hh.tbl->hho))->prev =                              \
                    _hd_hh_del->prev;                                          \
        
}                                                                      \
        HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
        HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_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.
 */
#ifdef HASH_DEBUG
#define HASH_OOPS(...) do {
 fprin
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
ChangeLog.html11.58 kB19-02-09 19:47
banner.png20.00 kB20-07-08 13:29
banner.svg18.14 kB20-07-08 13:29
grad_blue.png290.00 B05-06-06 14:31
grad_blue.svg3.02 kB05-06-06 14:31
rss.png689.00 B13-02-09 16:52
uthash-mini.png8.44 kB13-02-09 17:14
uthash.png21.01 kB13-02-09 17:14
index.html3.21 kB19-02-09 19:47
license.html2.19 kB13-02-09 16:52
styles.css1.80 kB13-02-09 16:52
tdh-quirks.css613.00 B13-02-09 16:52
tdh.css4.53 kB13-02-09 16:52
toc.css673.00 B01-10-06 11:58
userguide.html60.85 kB19-02-09 19:47
userguide.pdf249.72 kB19-02-09 19:47
Makefile678.00 B13-02-09 16:52
NOTES1.88 kB13-02-09 16:52
userguide.pdf249.72 kB19-02-09 19:47
ChangeLog.txt3.52 kB19-02-09 19:47
donate.txt435.00 B20-07-08 17:13
sflogo.txt434.00 B15-07-08 13:40
toc.txt2.42 kB25-07-08 13:43
topnav.txt540.00 B13-02-09 16:52
userguide.txt44.11 kB19-02-09 18:19
LICENSE1.11 kB13-02-09 16:52
README805.00 B19-02-09 18:19
uthash.h38.66 kB19-02-09 18:19
do_tests367.00 B12-08-08 11:41
do_tests.cygwin437.00 B12-08-08 11:41
do_tests.mingw293.00 B28-07-08 05:57
emit_keys.c984.00 B17-02-09 14:51
example.c2.97 kB09-09-08 18:21
keystat.c4.75 kB18-02-09 21:18
keystats1.07 kB21-07-08 04:19
Makefile1.41 kB19-02-09 18:41
README2.55 kB09-09-08 18:21
simkeys.pl791.00 B24-01-07 12:25
test1.ans176.00 B05-06-06 14:31
test1.c671.00 B17-02-09 14:51
test10.ans131.00 B14-07-08 05:14
test10.c1.28 kB17-02-09 14:51
test11.ans346.00 B20-11-06 15:03
test11.c1.13 kB17-02-09 14:51
test11.dat346.00 B05-06-06 14:31
test12.ans344.00 B05-06-06 14:31
test12.c976.00 B17-02-09 14:51
test13.ans120.00 B05-06-06 14:31
test13.c1,003.00 B17-02-09 14:51
test14.ans113.00 B14-07-08 05:14
test14.c1.09 kB17-02-09 14:51
test14.dat8.00 kB05-06-06 14:31
test15.ans16.00 B05-06-06 14:31
test15.c743.00 B17-02-09 14:51
test16.ans35.00 B27-06-06 15:20
test16.c1.44 kB18-02-09 21:18
test17.ans2.54 kB22-11-06 11:25
test17.c1.53 kB17-02-09 14:51
test18.ans316.00 B18-07-08 13:12
test18.c842.00 B17-02-09 14:51
test19.ans11.73 kB20-07-08 17:11
test19.c1.31 kB17-02-09 14:51
test2.ans133.00 B05-06-06 14:31
test2.c777.00 B17-02-09 14:51
test20.ans6.00 B22-07-08 10:41
test20.c760.00 B17-02-09 14:51
test21.ans13.00 B22-07-08 10:41
test21.c497.00 B17-02-09 14:51
test22.ans7.00 B24-07-08 11:34
test22.c1.67 kB18-02-09 21:18
test23.ans79.00 B24-07-08 11:34
test23.c980.00 B17-02-09 14:51
test24.ans23.00 B09-09-08 18:21
test24.c589.00 B17-02-09 14:51
test3.ans88.00 B05-06-06 14:31
test3.c939.00 B17-02-09 14:51
test4.ans176.00 B05-06-06 14:31
test4.c774.00 B17-02-09 14:51
test5.ans133.00 B05-06-06 14:31
test5.c912.00 B17-02-09 14:51
test6.ans56.00 B05-06-06 14:31
test6.c1.59 kB17-02-09 14:51
test7.ans182.00 B14-07-08 05:14
test7.c1.77 kB17-02-09 14:51
test8.ans361.00 B05-06-06 14:31
test8.c906.00 B17-02-09 14:51
test9.ans11.59 kB14-07-08 05:14
test9.c1.61 kB17-02-09 14:51
do_tests422.00 B17-02-09 14:04
Makefile631.00 B19-02-09 18:41
README120.00 B19-02-09 18:19
test1.c2.84 kB17-02-09 14:04
test2.c1.49 kB19-02-09 18:19
<img>0.00 B11-04-09 21:25
<html>0.00 B11-04-09 21:25
<pdf>0.00 B11-04-09 21:25
<txt>0.00 B11-04-09 21:25
<threads>0.00 B11-04-09 21:25
<doc>0.00 B11-04-09 21:25
<src>0.00 B11-04-09 21:25
<tests>0.00 B11-04-09 21:25
<uthash-1.5>0.00 B11-04-09 21:25
<uthash-1.5>0.00 B11-04-09 21:25
...
Sponsored links

uthash.h (459.71 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D