]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
19July06
[bacula/bacula] / bacula / src / lib / htable.h
1 /*
2  *   Version $Id$
3  */
4 /*
5    Copyright (C) 2003-2005 Kern Sibbald
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as amended with additional clauses defined in the
10    file LICENSE in the main source directory.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
15    the file LICENSE for additional details.
16
17  */
18
19 /* ========================================================================
20  *
21  *   Hash table class -- htable
22  *
23  */
24
25 /*
26  * Loop var through each member of table
27  */
28 #define foreach_htable(var, tbl) \
29         for((*((void **)&(var))=(void *)((tbl)->first())); \
30             (var); \
31             (*((void **)&(var))=(void *)((tbl)->next())))
32
33 struct hlink {
34    void *next;                        /* next hash item */
35    char *key;                         /* key this item */
36    uint32_t hash;                     /* hash for this key */
37 };
38
39 class htable : public SMARTALLOC {
40    hlink **table;                     /* hash table */
41    int loffset;                       /* link offset in item */
42    uint32_t num_items;                /* current number of items */
43    uint32_t max_items;                /* maximum items before growing */
44    uint32_t buckets;                  /* size of hash table */
45    uint32_t hash;                     /* temp storage */
46    uint32_t index;                    /* temp storage */
47    uint32_t mask;                     /* "remainder" mask */
48    uint32_t rshift;                   /* amount to shift down */
49    hlink *walkptr;                    /* table walk pointer */
50    uint32_t walk_index;               /* table walk index */
51    void hash_index(char *key);        /* produce hash key,index */
52    void grow_table();                 /* grow the table */
53 public:
54    htable(void *item, void *link, int tsize = 31);
55    ~htable() { destroy(); }
56    void init(void *item, void *link, int tsize = 31);
57    bool  insert(char *key, void *item);
58    void *lookup(char *key);
59    void *first();                     /* get first item in table */
60    void *next();                      /* get next item in table */
61    void destroy();
62    void stats();                      /* print stats about the table */
63    uint32_t size();                   /* return size of table */
64 };