rename type entity into ir_entity
[libfirm] / ir / ana2 / lset.c
1 /* -*- c -*- */
2
3 /*
4  * Project:     libFIRM
5  * File name:   ir/ana2/lset.c
6  * Purpose:     Lists, err, Sets
7  * Author:      Florian
8  * Modified by:
9  * Created:     Mon 18 Oct 2004
10  * CVS-ID:      $Id$
11  * Copyright:   (c) 1999-2004 Universität Karlsruhe
12  * Licence:     This file is protected by GPL -  GNU GENERAL PUBLIC LICENSE.
13  */
14
15
16 # ifdef HAVE_CONFIG_H
17 #  include "config.h"
18 # endif
19
20 # include "lset.h"
21 # include "xmalloc.h"
22
23 # ifndef TRUE
24 #  define TRUE 1
25 #  define FALSE 0
26 # endif /* not defined TRUE */
27
28 # include <assert.h>
29
30 #ifdef HAVE_STRING_H
31 # include <string.h>             /* need memset */
32 #endif
33
34 /*
35   Lists, err, Sets
36  */
37
38 /* create a new lset */
39 lset_t *lset_create (void)
40 {
41   lset_t *lset = xmalloc (sizeof (lset_t));
42   memset (lset, 0x00, sizeof (lset_t));
43
44   return (lset);
45 }
46
47 /* check whether the lset contains an entry for the given data */
48 int lset_contains (lset_t *lset, void *data)
49 {
50   lset_entry_t *entry = lset->first;
51
52   while (NULL != entry) {
53     if (data == entry->data) {
54       return (TRUE);
55     }
56
57     entry = entry->next;
58   }
59
60   return (FALSE);
61 }
62
63 /* check whether the given lset is empty */
64 int lset_empty (lset_t *lset)
65 {
66   return (NULL == lset->first);
67 }
68
69
70 /* insert the data into the lset (unless there's an entry for it
71    already) */
72 void lset_insert (lset_t *lset, void *data)
73 {
74   if (! lset_contains (lset, data)) {
75     lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
76
77     entry->data = data;
78     entry->next = lset->first;
79     lset->first = entry;
80
81     if (NULL == lset->last) {
82       lset->last = entry;
83     }
84
85     lset->n_entries ++;
86   }
87 }
88
89 /* insert all entries from src into tgt */
90 void lset_insert_all (lset_t *tgt, lset_t *src)
91 {
92   lset_entry_t *curs = src->first;
93
94   while (NULL != curs) {
95     lset_insert (tgt, curs->data);
96
97     curs = curs->next;
98   }
99 }
100
101 /* append src to tgt. src is deallocated. */
102 void lset_append (lset_t *tgt, lset_t *src)
103 {
104   assert (! tgt->last->next);
105
106   tgt->last->next = src->first;
107   tgt->last = src->last;
108   tgt->n_entries += src->n_entries;
109
110   memset (src, 0x00, sizeof (lset_t));
111   free (src);
112 }
113
114 /* remove the entry for the given data element from the lset. return
115    TRUE iff it was on the list in the first place, FALSE else */
116 int lset_remove (lset_t *lset, void *data)
117 {
118   lset_entry_t *entry = lset->first;
119   lset_entry_t *prev = NULL;
120
121   while (NULL != entry) {
122     if (data == entry->data) {
123       /* ok, dike it out */
124
125       if (NULL == prev) { /* ok, it's lset->first that needs diking */
126         lset->first = entry->next;
127       } else {
128         prev->next = entry->next;
129       }
130
131       memset (entry, 0x00, sizeof (lset_entry_t));
132       free (entry);
133
134       lset->n_entries --;
135
136       return (TRUE);
137     }
138
139     prev = entry;
140     entry = entry->next;
141   }
142
143   return (FALSE);
144 }
145
146 /* prepare the given lset for an iteration. return the first element. */
147 void *lset_first (lset_t *lset)
148 {
149   lset->curs = lset->first;
150
151   if (lset->first) {
152     return (lset->first->data);
153   } else {
154     return (NULL);
155   }
156 }
157
158 /* after calling lset_first, get the next element, if applicable, or
159    NULL */
160 void *lset_next (lset_t *lset)
161 {
162   lset->curs = lset->curs->next;
163
164   if (lset->curs) {
165     return (lset->curs->data);
166   } else {
167     return (NULL);
168   }
169 }
170
171 /* say how many entries there are in the given lset */
172 int lset_n_entries (lset_t *lset)
173 {
174   return (lset->n_entries);
175 }
176
177 /* deallocate the lset and all of its entries */
178 void lset_destroy (lset_t *lset)
179 {
180   lset_entry_t *curs = lset->first;
181
182   while (NULL != curs) {
183     lset_entry_t *tmp = curs->next;
184
185     memset (curs, 0x00, sizeof (lset_entry_t));
186     free (curs);
187
188     curs = tmp;
189   }
190
191   memset (lset, 0x00, sizeof (lset_t));
192   free (lset);
193 }
194
195
196 \f
197 /*
198   $Log$
199   Revision 1.4  2005/01/14 13:36:10  liekweg
200   fix malloc, fix "initialisation"
201
202   Revision 1.3  2004/12/22 14:43:14  beck
203   made allocations C-like
204
205   Revision 1.2  2004/12/02 16:17:51  beck
206   fixed config.h include
207
208   Revision 1.1  2004/10/21 11:09:37  liekweg
209   Moved memwalk stuf into irmemwalk
210   Moved lset stuff into lset
211   Moved typalise stuff into typalise
212
213
214  */