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));
46 /* check whether the lset contains an entry for the given data */
47 int lset_contains (lset_t *lset, void *data)
49 lset_entry_t *entry = lset->first;
51 while (NULL != entry) {
52 if (data == entry->data) {
62 /* check whether the given lset is empty */
63 int lset_empty (lset_t *lset)
65 return (NULL == lset->first);
69 /* insert the data into the lset (unless there's an entry for it
71 void lset_insert (lset_t *lset, void *data)
73 if (! lset_contains (lset, data)) {
74 lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
76 entry->next = lset->first;
79 if (NULL == lset->last) {
87 /* insert all entries from src into tgt */
88 void lset_insert_all (lset_t *tgt, lset_t *src)
90 lset_entry_t *curs = src->first;
92 while (NULL != curs) {
93 lset_insert (tgt, curs->data);
99 /* append src to tgt. src is deallocated. */
100 void lset_append (lset_t *tgt, lset_t *src)
102 assert (! tgt->last->next);
104 tgt->last->next = src->first;
105 tgt->last = src->last;
106 tgt->n_entries += src->n_entries;
108 memset (src, 0x00, sizeof (lset_t));
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)
116 lset_entry_t *entry = lset->first;
117 lset_entry_t *prev = NULL;
119 while (NULL != entry) {
120 if (data == entry->data) {
121 /* ok, dike it out */
123 if (NULL == prev) { /* ok, it's lset->first that needs diking */
124 lset->first = entry->next;
126 prev->next = entry->next;
129 memset (entry, 0x00, sizeof (lset_entry_t));
144 /* prepare the given lset for an iteration. return the first element. */
145 void *lset_first (lset_t *lset)
147 lset->curs = lset->first;
150 return (lset->first->data);
156 /* after calling lset_first, get the next element, if applicable, or
158 void *lset_next (lset_t *lset)
160 lset->curs = lset->curs->next;
163 return (lset->curs->data);
169 /* say how many entries there are in the given lset */
170 int lset_n_entries (lset_t *lset)
172 return (lset->n_entries);
175 /* deallocate the lset and all of its entries */
176 void lset_destroy (lset_t *lset)
178 lset_entry_t *curs = lset->first;
180 while (NULL != curs) {
181 lset_entry_t *tmp = curs->next;
183 memset (curs, 0x00, sizeof (lset_entry_t));
189 memset (lset, 0x00, sizeof (lset_t));
197 Revision 1.2 2004/12/02 16:17:51 beck
198 fixed config.h include
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