add license info to ana2
[libfirm] / ir / ana2 / lset.c
1 /* -*- c -*- */
2
3 /*
4  * Copyrigth (C) 1995-2007 University of Karlsruhe.  All right reserved.
5  *
6  * This file is part of libFirm.
7  *
8  * This file may be distributed and/or modified under the terms of the
9  * GNU General Public License version 2 as published by the Free Software
10  * Foundation and appearing in the file LICENSE.GPL included in the
11  * packaging of this file.
12  *
13  * Licensees holding valid libFirm Professional Edition licenses may use
14  * this file in accordance with the libFirm Commercial License.
15  * Agreement provided with the Software.
16  *
17  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE.
20  */
21
22 /**
23  * @file
24  * @brief    Lists, err, Sets
25  * @author   Florian
26  * @date     Mon 18 Oct 2004
27  * @version  $Id$
28  */
29 # ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 # endif
32
33 # include "lset.h"
34 # include "xmalloc.h"
35
36 # ifndef TRUE
37 #  define TRUE 1
38 #  define FALSE 0
39 # endif /* not defined TRUE */
40
41 # include <assert.h>
42
43 #ifdef HAVE_STRING_H
44 # include <string.h>             /* need memset */
45 #endif
46
47 /*
48   Lists, err, Sets
49  */
50
51 /* create a new lset */
52 lset_t *lset_create (void)
53 {
54   lset_t *lset = xmalloc (sizeof (lset_t));
55   memset (lset, 0x00, sizeof (lset_t));
56
57   return (lset);
58 }
59
60 /* check whether the lset contains an entry for the given data */
61 int lset_contains (lset_t *lset, void *data)
62 {
63   lset_entry_t *entry = lset->first;
64
65   while (NULL != entry) {
66     if (data == entry->data) {
67       return (TRUE);
68     }
69
70     entry = entry->next;
71   }
72
73   return (FALSE);
74 }
75
76 /* check whether the given lset is empty */
77 int lset_empty (lset_t *lset)
78 {
79   return (NULL == lset->first);
80 }
81
82
83 /* insert the data into the lset (unless there's an entry for it
84    already) */
85 void lset_insert (lset_t *lset, void *data)
86 {
87   if (! lset_contains (lset, data)) {
88     lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
89
90     entry->data = data;
91     entry->next = lset->first;
92     lset->first = entry;
93
94     if (NULL == lset->last) {
95       lset->last = entry;
96     }
97
98     lset->n_entries ++;
99   }
100 }
101
102 /* insert all entries from src into tgt */
103 void lset_insert_all (lset_t *tgt, lset_t *src)
104 {
105   lset_entry_t *curs = src->first;
106
107   while (NULL != curs) {
108     lset_insert (tgt, curs->data);
109
110     curs = curs->next;
111   }
112 }
113
114 /* append src to tgt. src is deallocated. */
115 void lset_append (lset_t *tgt, lset_t *src)
116 {
117   assert (! tgt->last->next);
118
119   tgt->last->next = src->first;
120   tgt->last = src->last;
121   tgt->n_entries += src->n_entries;
122
123   memset (src, 0x00, sizeof (lset_t));
124   free (src);
125 }
126
127 /* remove the entry for the given data element from the lset. return
128    TRUE iff it was on the list in the first place, FALSE else */
129 int lset_remove (lset_t *lset, void *data)
130 {
131   lset_entry_t *entry = lset->first;
132   lset_entry_t *prev = NULL;
133
134   while (NULL != entry) {
135     if (data == entry->data) {
136       /* ok, dike it out */
137
138       if (NULL == prev) { /* ok, it's lset->first that needs diking */
139         lset->first = entry->next;
140       } else {
141         prev->next = entry->next;
142       }
143
144       memset (entry, 0x00, sizeof (lset_entry_t));
145       free (entry);
146
147       lset->n_entries --;
148
149       return (TRUE);
150     }
151
152     prev = entry;
153     entry = entry->next;
154   }
155
156   return (FALSE);
157 }
158
159 /* prepare the given lset for an iteration. return the first element. */
160 void *lset_first (lset_t *lset)
161 {
162   lset->curs = lset->first;
163
164   if (lset->first) {
165     return (lset->first->data);
166   } else {
167     return (NULL);
168   }
169 }
170
171 /* after calling lset_first, get the next element, if applicable, or
172    NULL */
173 void *lset_next (lset_t *lset)
174 {
175   lset->curs = lset->curs->next;
176
177   if (lset->curs) {
178     return (lset->curs->data);
179   } else {
180     return (NULL);
181   }
182 }
183
184 /* say how many entries there are in the given lset */
185 int lset_n_entries (lset_t *lset)
186 {
187   return (lset->n_entries);
188 }
189
190 /* deallocate the lset and all of its entries */
191 void lset_destroy (lset_t *lset)
192 {
193   lset_entry_t *curs = lset->first;
194
195   while (NULL != curs) {
196     lset_entry_t *tmp = curs->next;
197
198     memset (curs, 0x00, sizeof (lset_entry_t));
199     free (curs);
200
201     curs = tmp;
202   }
203
204   memset (lset, 0x00, sizeof (lset_t));
205   free (lset);
206 }
207
208
209 \f
210 /*
211   $Log$
212   Revision 1.4  2005/01/14 13:36:10  liekweg
213   fix malloc, fix "initialisation"
214
215   Revision 1.3  2004/12/22 14:43:14  beck
216   made allocations C-like
217
218   Revision 1.2  2004/12/02 16:17:51  beck
219   fixed config.h include
220
221   Revision 1.1  2004/10/21 11:09:37  liekweg
222   Moved memwalk stuf into irmemwalk
223   Moved lset stuff into lset
224   Moved typalise stuff into typalise
225
226
227  */