moved struct timing_env to .c file, added config.h
[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
43   return (lset);
44 }
45
46 /* check whether the lset contains an entry for the given data */
47 int lset_contains (lset_t *lset, void *data)
48 {
49   lset_entry_t *entry = lset->first;
50
51   while (NULL != entry) {
52     if (data == entry->data) {
53       return (TRUE);
54     }
55
56     entry = entry->next;
57   }
58
59   return (FALSE);
60 }
61
62 /* check whether the given lset is empty */
63 int lset_empty (lset_t *lset)
64 {
65   return (NULL == lset->first);
66 }
67
68
69 /* insert the data into the lset (unless there's an entry for it
70    already) */
71 void lset_insert (lset_t *lset, void *data)
72 {
73   if (! lset_contains (lset, data)) {
74     lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
75     entry->data = data;
76     entry->next = lset->first;
77     lset->first = entry;
78
79     if (NULL == lset->last) {
80       lset->last = entry;
81     }
82
83     lset->n_entries ++;
84   }
85 }
86
87 /* insert all entries from src into tgt */
88 void lset_insert_all (lset_t *tgt, lset_t *src)
89 {
90   lset_entry_t *curs = src->first;
91
92   while (NULL != curs) {
93     lset_insert (tgt, curs->data);
94
95     curs = curs->next;
96   }
97 }
98
99 /* append src to tgt. src is deallocated. */
100 void lset_append (lset_t *tgt, lset_t *src)
101 {
102   assert (! tgt->last->next);
103
104   tgt->last->next = src->first;
105   tgt->last = src->last;
106   tgt->n_entries += src->n_entries;
107
108   memset (src, 0x00, sizeof (lset_t));
109   free (src);
110 }
111
112 /* remove the entry for the given data element from the lset. return
113    TRUE iff it was on the list in the first place, FALSE else */
114 int lset_remove (lset_t *lset, void *data)
115 {
116   lset_entry_t *entry = lset->first;
117   lset_entry_t *prev = NULL;
118
119   while (NULL != entry) {
120     if (data == entry->data) {
121       /* ok, dike it out */
122
123       if (NULL == prev) { /* ok, it's lset->first that needs diking */
124         lset->first = entry->next;
125       } else {
126         prev->next = entry->next;
127       }
128
129       memset (entry, 0x00, sizeof (lset_entry_t));
130       free (entry);
131
132       lset->n_entries --;
133
134       return (TRUE);
135     }
136
137     prev = entry;
138     entry = entry->next;
139   }
140
141   return (FALSE);
142 }
143
144 /* prepare the given lset for an iteration. return the first element. */
145 void *lset_first (lset_t *lset)
146 {
147   lset->curs = lset->first;
148
149   if (lset->first) {
150     return (lset->first->data);
151   } else {
152     return (NULL);
153   }
154 }
155
156 /* after calling lset_first, get the next element, if applicable, or
157    NULL */
158 void *lset_next (lset_t *lset)
159 {
160   lset->curs = lset->curs->next;
161
162   if (lset->curs) {
163     return (lset->curs->data);
164   } else {
165     return (NULL);
166   }
167 }
168
169 /* say how many entries there are in the given lset */
170 int lset_n_entries (lset_t *lset)
171 {
172   return (lset->n_entries);
173 }
174
175 /* deallocate the lset and all of its entries */
176 void lset_destroy (lset_t *lset)
177 {
178   lset_entry_t *curs = lset->first;
179
180   while (NULL != curs) {
181     lset_entry_t *tmp = curs->next;
182
183     memset (curs, 0x00, sizeof (lset_entry_t));
184     free (curs);
185
186     curs = tmp;
187   }
188
189   memset (lset, 0x00, sizeof (lset_t));
190   free (lset);
191 }
192
193
194 \f
195 /*
196   $Log$
197   Revision 1.2  2004/12/02 16:17:51  beck
198   fixed config.h include
199
200   Revision 1.1  2004/10/21 11:09:37  liekweg
201   Moved memwalk stuf into irmemwalk
202   Moved lset stuff into lset
203   Moved typalise stuff into typalise
204
205
206  */