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