5 * File name: ir/ana2/lset.c
6 * Purpose: Lists, err, Sets
9 * Created: Mon 18 Oct 2004
11 * Copyright: (c) 1999-2004 Universität Karlsruhe
12 * Licence: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 # endif /* not defined TRUE */
31 # include <string.h> /* need memset */
38 /* create a new lset */
39 lset_t *lset_create (void)
41 lset_t *lset = xmalloc (sizeof (lset_t));
42 memset (lset, 0x00, sizeof (lset_t));
47 /* check whether the lset contains an entry for the given data */
48 int lset_contains (lset_t *lset, void *data)
50 lset_entry_t *entry = lset->first;
52 while (NULL != entry) {
53 if (data == entry->data) {
63 /* check whether the given lset is empty */
64 int lset_empty (lset_t *lset)
66 return (NULL == lset->first);
70 /* insert the data into the lset (unless there's an entry for it
72 void lset_insert (lset_t *lset, void *data)
74 if (! lset_contains (lset, data)) {
75 lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
78 entry->next = lset->first;
81 if (NULL == lset->last) {
89 /* insert all entries from src into tgt */
90 void lset_insert_all (lset_t *tgt, lset_t *src)
92 lset_entry_t *curs = src->first;
94 while (NULL != curs) {
95 lset_insert (tgt, curs->data);
101 /* append src to tgt. src is deallocated. */
102 void lset_append (lset_t *tgt, lset_t *src)
104 assert (! tgt->last->next);
106 tgt->last->next = src->first;
107 tgt->last = src->last;
108 tgt->n_entries += src->n_entries;
110 memset (src, 0x00, sizeof (lset_t));
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)
118 lset_entry_t *entry = lset->first;
119 lset_entry_t *prev = NULL;
121 while (NULL != entry) {
122 if (data == entry->data) {
123 /* ok, dike it out */
125 if (NULL == prev) { /* ok, it's lset->first that needs diking */
126 lset->first = entry->next;
128 prev->next = entry->next;
131 memset (entry, 0x00, sizeof (lset_entry_t));
146 /* prepare the given lset for an iteration. return the first element. */
147 void *lset_first (lset_t *lset)
149 lset->curs = lset->first;
152 return (lset->first->data);
158 /* after calling lset_first, get the next element, if applicable, or
160 void *lset_next (lset_t *lset)
162 lset->curs = lset->curs->next;
165 return (lset->curs->data);
171 /* say how many entries there are in the given lset */
172 int lset_n_entries (lset_t *lset)
174 return (lset->n_entries);
177 /* deallocate the lset and all of its entries */
178 void lset_destroy (lset_t *lset)
180 lset_entry_t *curs = lset->first;
182 while (NULL != curs) {
183 lset_entry_t *tmp = curs->next;
185 memset (curs, 0x00, sizeof (lset_entry_t));
191 memset (lset, 0x00, sizeof (lset_t));
199 Revision 1.4 2005/01/14 13:36:10 liekweg
200 fix malloc, fix "initialisation"
202 Revision 1.3 2004/12/22 14:43:14 beck
203 made allocations C-like
205 Revision 1.2 2004/12/02 16:17:51 beck
206 fixed config.h include
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