]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
09346319ab54068c1006b00399601e5a9ca53f24
[bacula/bacula] / bacula / src / lib / htable.h
1 /*
2  *
3  * Written by Kern Sibbald, MMIV
4  *
5  *   Version $Id$
6  */
7 /*
8    Bacula® - The Network Backup Solution
9
10    Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
11
12    The main author of Bacula is Kern Sibbald, with contributions from
13    many others, a complete list can be found in the file AUTHORS.
14    This program is Free Software; you can redistribute it and/or
15    modify it under the terms of version two of the GNU General Public
16    License as published by the Free Software Foundation plus additions
17    that are listed in the file LICENSE.
18
19    This program is distributed in the hope that it will be useful, but
20    WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27    02110-1301, USA.
28
29    Bacula® is a registered trademark of John Walker.
30    The licensor of Bacula is the Free Software Foundation Europe
31    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
32    Switzerland, email:ftf@fsfeurope.org.
33 */
34
35 /* ========================================================================
36  *
37  *   Hash table class -- htable
38  *
39  */
40
41 /*
42  * Loop var through each member of table
43  */
44 #define foreach_htable(var, tbl) \
45         for((*((void **)&(var))=(void *)((tbl)->first())); \
46             (var); \
47             (*((void **)&(var))=(void *)((tbl)->next())))
48
49 struct hlink {
50    void *next;                        /* next hash item */
51    char *key;                         /* key this item */
52    uint32_t hash;                     /* hash for this key */
53 };
54
55 class htable : public SMARTALLOC {
56    hlink **table;                     /* hash table */
57    int loffset;                       /* link offset in item */
58    uint32_t num_items;                /* current number of items */
59    uint32_t max_items;                /* maximum items before growing */
60    uint32_t buckets;                  /* size of hash table */
61    uint32_t hash;                     /* temp storage */
62    uint32_t index;                    /* temp storage */
63    uint32_t mask;                     /* "remainder" mask */
64    uint32_t rshift;                   /* amount to shift down */
65    hlink *walkptr;                    /* table walk pointer */
66    uint32_t walk_index;               /* table walk index */
67    void hash_index(char *key);        /* produce hash key,index */
68    void grow_table();                 /* grow the table */
69 public:
70    htable(void *item, void *link, int tsize = 31);
71    ~htable() { destroy(); }
72    void init(void *item, void *link, int tsize = 31);
73    bool  insert(char *key, void *item);
74    void *lookup(char *key);
75    void *first();                     /* get first item in table */
76    void *next();                      /* get next item in table */
77    void destroy();
78    void stats();                      /* print stats about the table */
79    uint32_t size();                   /* return size of table */
80 };