* Minor tuning
[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 #include <stdlib.h>
32 #define GC_malloc malloc
33 #define GC_free free
34 #define GC_realloc realloc
35 #define GC_malloc_atomic malloc
36
37 #else
38
39 #include "./gc/gc.h" 
40
41 #endif
42
43 #ifndef ALLOC
44
45 #define  ALLOC(x,sz) if (!(x = GC_malloc(sz)))                          \
46          {                                                           \
47             fprintf(stderr,"out of virtual memory %s , %d",__FILE__,__LINE__);\
48             exit (1);                                                \
49          }
50
51 #define  ALLOC_ATOMIC(x,sz) if (!(x = GC_malloc_atomic(sz)))                          \
52          {                                                           \
53             fprintf(stderr,"out of virtual memory %s , %d",__FILE__,__LINE__);\
54             exit (1);                                                \
55          }
56
57
58
59 #endif
60
61
62 /* hashtable item */
63 typedef struct hashtItem
64 {
65     int key ;
66     void *item ;
67     struct hashtItem *next ;
68 } hashtItem ;
69
70 /* hashtable */
71 typedef struct hTab
72 {
73     int size  ;             /* max number of items */
74     int minKey;             /* minimum key value   */
75     int maxKey ;            /* maximum key value */
76     hashtItem **table ;     /* the actual table  */
77     int currKey  ;          /* used for iteration */
78     hashtItem *currItem ;   /* current item within the list */
79     int nItems ;
80 } hTab ;
81
82
83
84 enum {
85     DELETE_CHAIN = 1,
86     DELETE_ITEM };
87
88
89 /*-----------------------------------------------------------------*/
90 /*           Forward   definition    for   functions               */
91 /*-----------------------------------------------------------------*/
92
93 /* hashtable related functions */
94 hTab        *newHashTable      (int);
95 void         hTabAddItem (hTab **, int , void * );
96 void         hTabDeleteItem (hTab **, int  ,
97                                   void *, int  ,
98                                   int (*compareFunc)(void *,void *));
99 int          hTabIsInTable (hTab *, int , void * , 
100                                  int (*compareFunc)(void *,void *));
101 void         *hTabFirstItem (hTab *, int *);
102 void         *hTabNextItem (hTab *, int *);
103 hTab         *hTabFromTable (hTab *);
104 int          isHtabsEqual (hTab *,hTab *, int (*compareFunc)(void *,void *));
105 hashtItem    *hTabSearch  (hTab *, int );
106 void         *hTabItemWithKey(hTab *,int );
107 void         hTabAddItemIfNotP(hTab **,int, void *);
108 void         hTabDeleteAll(hTab *);
109 void        *hTabFirstItemWK (hTab *htab, int wk);
110 void        *hTabNextItemWK (hTab *htab );
111 void         hTabClearAll (hTab *htab);
112
113
114 #endif