Initial revision
[fw/sdcc] / src / SDCChasht.h
1 /*-----------------------------------------------------------------
2     SDCChast.h - contains support routines for hashtables/sets .
3
4     Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
5
6     This program is free software; you can redistribute it and/or modify it
7     under the terms of the GNU General Public License as published by the
8     Free Software Foundation; either version 2, or (at your option) any
9     later version.
10     
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15     
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     
20     In other words, you are welcome to use, share and improve this program.
21     You are forbidden to forbid anyone else to use, share and improve
22     what you give them.   Help stamp out software-hoarding!  
23 -------------------------------------------------------------------------*/
24
25
26 #ifndef SDCCHASHT_H
27 #define SDCCHASHT_H
28
29 #ifdef _NO_GC
30
31 #define GC_malloc malloc
32 #define GC_free free
33 #define GC_realloc realloc
34 #define GC_malloc_atomic malloc
35
36 #else
37
38 #include "./gc/gc.h" 
39
40 #endif
41
42 #ifndef ALLOC
43
44 #define  ALLOC(x,sz) if (!(x = GC_malloc(sz)))                          \
45          {                                                           \
46             fprintf(stderr,"out of virtual memory %s , %d",__FILE__,__LINE__);\
47             exit (1);                                                \
48          }
49
50 #define  ALLOC_ATOMIC(x,sz) if (!(x = GC_malloc_atomic(sz)))                          \
51          {                                                           \
52             fprintf(stderr,"out of virtual memory %s , %d",__FILE__,__LINE__);\
53             exit (1);                                                \
54          }
55
56
57
58 #endif
59
60
61 /* hashtable item */
62 typedef struct hashtItem
63 {
64     int key ;
65     void *item ;
66     struct hashtItem *next ;
67 } hashtItem ;
68
69 /* hashtable */
70 typedef struct hTab
71 {
72     int size  ;             /* max number of items */
73     int minKey;             /* minimum key value   */
74     int maxKey ;            /* maximum key value */
75     hashtItem **table ;     /* the actual table  */
76     int currKey  ;          /* used for iteration */
77     hashtItem *currItem ;   /* current item within the list */
78     int nItems ;
79 } hTab ;
80
81
82
83 enum {
84     DELETE_CHAIN = 1,
85     DELETE_ITEM };
86
87
88 /*-----------------------------------------------------------------*/
89 /*           Forward   definition    for   functions               */
90 /*-----------------------------------------------------------------*/
91
92 /* hashtable related functions */
93 hTab        *newHashTable      (int);
94 void         hTabAddItem (hTab **, int , void * );
95 void         hTabDeleteItem (hTab **, int  ,
96                                   void *, int  ,
97                                   int (*compareFunc)(void *,void *));
98 int          hTabIsInTable (hTab *, int , void * , 
99                                  int (*compareFunc)(void *,void *));
100 void         *hTabFirstItem (hTab *, int *);
101 void         *hTabNextItem (hTab *, int *);
102 hTab         *hTabFromTable (hTab *);
103 int          isHtabsEqual (hTab *,hTab *, int (*compareFunc)(void *,void *));
104 hashtItem    *hTabSearch  (hTab *, int );
105 void         *hTabItemWithKey(hTab *,int );
106 void         hTabAddItemIfNotP(hTab **,int, void *);
107 void         hTabDeleteAll(hTab *);
108 void        *hTabFirstItemWK (hTab *htab, int wk);
109 void        *hTabNextItemWK (hTab *htab );
110 void         hTabClearAll (hTab *htab);
111
112
113 #endif