]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/htable.h
Add new hash table class
[bacula/bacula] / bacula / src / lib / htable.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2000-2003 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 /* ========================================================================
26  * 
27  *   Hash table class -- htable
28  *
29  */
30
31 #define OFFSET(item,link) ((char *)link - (char *)item)
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 {
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);
55    void init(void *item, void *link);
56    bool  insert(char *key, void *item);
57    void *lookup(char *key);
58    void *first();                     /* get first item in table */
59    void *next();                      /* get next item in table */
60    void destroy();
61    void * operator new(size_t);
62    void operator delete(void *);
63 };