3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /* Warning, this module is done to be efficient and performs tons of
9 cast and dirty things. So avoid using it unless you really know
10 what you are doing. */
12 /* This type should be added to a type that is to be used in such a swag */
14 #include "xbt/sysdep.h"
15 #include "xbt/error.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(swag,xbt,"Swag : O(1) set library");
20 #define PREV(obj,offset) xbt_swag_getPrev(obj,offset)
21 #define NEXT(obj,offset) xbt_swag_getNext(obj,offset)
28 /** Creates a new swag.
29 * \param offset where the hookup is located in the structure
30 * \see xbt_swag_offset
32 * Usage : xbt_swag_new(&obj.setA-&obj);
34 xbt_swag_t xbt_swag_new(size_t offset)
36 xbt_swag_t swag = xbt_new0(s_xbt_swag_t, 1);
38 swag->offset = offset;
44 * \param swag poor victim
46 * kilkil a swag but not it's content. If you do not understand why
47 * xbt_swag_free should not free its content, don't use swags.
49 void xbt_swag_free(xbt_swag_t swag)
54 /** Creates a new swag.
55 * \param swag the swag to initialize
56 * \param offset where the hookup is located in the structure
57 * \see xbt_swag_offset
59 * Usage : xbt_swag_init(swag,&obj.setA-&obj);
61 void xbt_swag_init(xbt_swag_t swag, size_t offset)
63 swag->offset = offset;
68 * \param obj the objet to insert in the swag
71 * insert \a obj in \a swag
73 void xbt_swag_insert(void *obj, xbt_swag_t swag)
76 if (xbt_swag_belongs(obj, swag))
80 if (swag->head == NULL) {
81 xbt_assert0(!(swag->tail), "Inconsistent swag.");
87 PREV(obj, swag->offset) = swag->tail;
88 NEXT(PREV(obj, swag->offset), swag->offset) = obj;
94 * \param obj the objet to insert in the swag
97 * insert (at the head... you probably had a very good reason to do
98 * that, I hope you know what you're doing) \a obj in \a swag
100 void xbt_swag_insert_at_head(void *obj, xbt_swag_t swag)
103 if (xbt_swag_belongs(obj, swag))
107 if (swag->head == NULL) {
108 xbt_assert0(!(swag->tail), "Inconsistent swag.");
114 NEXT(obj, swag->offset) = swag->head;
115 PREV(NEXT(obj, swag->offset), swag->offset) = obj;
121 * \param obj the objet to insert in the swag
124 * insert (at the tail... you probably had a very good reason to do
125 * that, I hope you know what you're doing) \a obj in \a swag
127 void xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag)
130 if (xbt_swag_belongs(obj, swag))
134 if (swag->head == NULL) {
135 xbt_assert0(!(swag->tail), "Inconsistent swag.");
141 PREV(obj, swag->offset) = swag->tail;
142 NEXT(PREV(obj, swag->offset), swag->offset) = obj;
148 * \param obj the objet to remove from the swag
150 * \return \a obj if it was in the \a swag and NULL otherwise
152 * removes \a obj from \a swag
154 void *xbt_swag_remove(void *obj, xbt_swag_t swag)
156 size_t offset = swag->offset;
158 if ((!obj) || (!swag))
160 if(!xbt_swag_belongs(obj, swag)) /* Trying to remove an object that
161 was not in this swag */
164 if (swag->head == swag->tail) { /* special case */
165 if (swag->head != obj) /* Trying to remove an object that was not in this swag */
169 NEXT(obj, offset) = PREV(obj, offset) = NULL;
170 } else if (obj == swag->head) { /* It's the head */
171 swag->head = NEXT(obj, offset);
172 PREV(swag->head, offset) = NULL;
173 NEXT(obj, offset) = NULL;
174 } else if (obj == swag->tail) { /* It's the tail */
175 swag->tail = PREV(obj, offset);
176 NEXT(swag->tail, offset) = NULL;
177 PREV(obj, offset) = NULL;
178 } else { /* It's in the middle */
179 NEXT(PREV(obj, offset), offset) = NEXT(obj, offset);
180 PREV(NEXT(obj, offset), offset) = PREV(obj, offset);
181 PREV(obj, offset) = NEXT(obj, offset) = NULL;
189 * \return an object from the \a swag
191 void *xbt_swag_extract(xbt_swag_t swag)
193 size_t offset = swag->offset;
196 if ((!swag) || (!(swag->head)))
201 if (swag->head == swag->tail) { /* special case */
202 swag->head = swag->tail = NULL;
203 PREV(obj, offset) = NEXT(obj, offset) = NULL;
205 swag->head = NEXT(obj, offset);
206 PREV(swag->head, offset) = NULL;
207 NEXT(obj, offset) = NULL;
215 * \return the number of objects in \a swag
217 int xbt_swag_size(xbt_swag_t swag)
219 return (swag->count);
223 * \param obj an object
225 * \return 1 if \a obj is in the \a swag and 0 otherwise
227 int xbt_swag_belongs(void *obj, xbt_swag_t swag)
229 return ((NEXT(obj, swag->offset)) || (PREV(obj, swag->offset))
230 || (swag->head == obj));