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 */
29 # include <string.h> /* need memset */
35 /* create a new lset */
36 lset_t *lset_create (void)
38 lset_t *lset = xmalloc (sizeof (lset_t));
43 /* check whether the lset contains an entry for the given data */
44 int lset_contains (lset_t *lset, void *data)
46 lset_entry_t *entry = lset->first;
48 while (NULL != entry) {
49 if (data == entry->data) {
59 /* check whether the given lset is empty */
60 int lset_empty (lset_t *lset)
62 return (NULL == lset->first);
66 /* insert the data into the lset (unless there's an entry for it
68 void lset_insert (lset_t *lset, void *data)
70 if (! lset_contains (lset, data)) {
71 lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
73 entry->next = lset->first;
76 if (NULL == lset->last) {
84 /* insert all entries from src into tgt */
85 void lset_insert_all (lset_t *tgt, lset_t *src)
87 lset_entry_t *curs = src->first;
89 while (NULL != curs) {
90 lset_insert (tgt, curs->data);
96 /* append src to tgt. src is deallocated. */
97 void lset_append (lset_t *tgt, lset_t *src)
99 assert (! tgt->last->next);
101 tgt->last->next = src->first;
102 tgt->last = src->last;
103 tgt->n_entries += src->n_entries;
105 memset (src, 0x00, sizeof (lset_t));
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)
113 lset_entry_t *entry = lset->first;
114 lset_entry_t *prev = NULL;
116 while (NULL != entry) {
117 if (data == entry->data) {
118 /* ok, dike it out */
120 if (NULL == prev) { /* ok, it's lset->first that needs diking */
121 lset->first = entry->next;
123 prev->next = entry->next;
126 memset (entry, 0x00, sizeof (lset_entry_t));
141 /* prepare the given lset for an iteration. return the first element. */
142 void *lset_first (lset_t *lset)
144 lset->curs = lset->first;
147 return (lset->first->data);
153 /* after calling lset_first, get the next element, if applicable, or
155 void *lset_next (lset_t *lset)
157 lset->curs = lset->curs->next;
160 return (lset->curs->data);
166 /* say how many entries there are in the given lset */
167 int lset_n_entries (lset_t *lset)
169 return (lset->n_entries);
172 /* deallocate the lset and all of its entries */
173 void lset_destroy (lset_t *lset)
175 lset_entry_t *curs = lset->first;
177 while (NULL != curs) {
178 lset_entry_t *tmp = curs->next;
180 memset (curs, 0x00, sizeof (lset_entry_t));
186 memset (lset, 0x00, sizeof (lset_t));
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