4 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
6 * This file is part of libFirm.
8 * This file may be distributed and/or modified under the terms of the
9 * GNU General Public License version 2 as published by the Free Software
10 * Foundation and appearing in the file LICENSE.GPL included in the
11 * packaging of this file.
13 * Licensees holding valid libFirm Professional Edition licenses may use
14 * this file in accordance with the libFirm Commercial License.
15 * Agreement provided with the Software.
17 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * @brief Lists, err, Sets
26 * @date Mon 18 Oct 2004
39 # endif /* not defined TRUE */
44 # include <string.h> /* need memset */
51 /* create a new lset */
52 lset_t *lset_create (void)
54 lset_t *lset = xmalloc (sizeof (lset_t));
55 memset (lset, 0x00, sizeof (lset_t));
60 /* check whether the lset contains an entry for the given data */
61 int lset_contains (lset_t *lset, void *data)
63 lset_entry_t *entry = lset->first;
65 while (NULL != entry) {
66 if (data == entry->data) {
76 /* check whether the given lset is empty */
77 int lset_empty (lset_t *lset)
79 return (NULL == lset->first);
83 /* insert the data into the lset (unless there's an entry for it
85 void lset_insert (lset_t *lset, void *data)
87 if (! lset_contains (lset, data)) {
88 lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
91 entry->next = lset->first;
94 if (NULL == lset->last) {
102 /* insert all entries from src into tgt */
103 void lset_insert_all (lset_t *tgt, lset_t *src)
105 lset_entry_t *curs = src->first;
107 while (NULL != curs) {
108 lset_insert (tgt, curs->data);
114 /* append src to tgt. src is deallocated. */
115 void lset_append (lset_t *tgt, lset_t *src)
117 assert (! tgt->last->next);
119 tgt->last->next = src->first;
120 tgt->last = src->last;
121 tgt->n_entries += src->n_entries;
123 memset (src, 0x00, sizeof (lset_t));
127 /* remove the entry for the given data element from the lset. return
128 TRUE iff it was on the list in the first place, FALSE else */
129 int lset_remove (lset_t *lset, void *data)
131 lset_entry_t *entry = lset->first;
132 lset_entry_t *prev = NULL;
134 while (NULL != entry) {
135 if (data == entry->data) {
136 /* ok, dike it out */
138 if (NULL == prev) { /* ok, it's lset->first that needs diking */
139 lset->first = entry->next;
141 prev->next = entry->next;
144 memset (entry, 0x00, sizeof (lset_entry_t));
159 /* prepare the given lset for an iteration. return the first element. */
160 void *lset_first (lset_t *lset)
162 lset->curs = lset->first;
165 return (lset->first->data);
171 /* after calling lset_first, get the next element, if applicable, or
173 void *lset_next (lset_t *lset)
175 lset->curs = lset->curs->next;
178 return (lset->curs->data);
184 /* say how many entries there are in the given lset */
185 int lset_n_entries (lset_t *lset)
187 return (lset->n_entries);
190 /* deallocate the lset and all of its entries */
191 void lset_destroy (lset_t *lset)
193 lset_entry_t *curs = lset->first;
195 while (NULL != curs) {
196 lset_entry_t *tmp = curs->next;
198 memset (curs, 0x00, sizeof (lset_entry_t));
204 memset (lset, 0x00, sizeof (lset_t));
212 Revision 1.4 2005/01/14 13:36:10 liekweg
213 fix malloc, fix "initialisation"
215 Revision 1.3 2004/12/22 14:43:14 beck
216 made allocations C-like
218 Revision 1.2 2004/12/02 16:17:51 beck
219 fixed config.h include
221 Revision 1.1 2004/10/21 11:09:37 liekweg
222 Moved memwalk stuf into irmemwalk
223 Moved lset stuff into lset
224 Moved typalise stuff into typalise