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)
24 /** Creates a new swag.
25 * \param offset where the hookup is located in the structure
26 * \see xbt_swag_offset
28 * Usage : xbt_swag_new(&obj.setA-&obj);
30 xbt_swag_t xbt_swag_new(size_t offset)
32 xbt_swag_t swag = xbt_new0(s_xbt_swag_t, 1);
34 swag->offset = offset;
40 * \param swag poor victim
42 * kilkil a swag but not it's content. If you do not understand why
43 * xbt_swag_free should not free its content, don't use swags.
45 void xbt_swag_free(xbt_swag_t swag)
50 /** Creates a new swag.
51 * \param swag the swag to initialize
52 * \param offset where the hookup is located in the structure
53 * \see xbt_swag_offset
55 * Usage : xbt_swag_init(swag,&obj.setA-&obj);
57 void xbt_swag_init(xbt_swag_t swag, size_t offset)
59 swag->offset = offset;
64 * \param obj the objet to insert in the swag
67 * insert \a obj in \a swag
69 void xbt_swag_insert(void *obj, xbt_swag_t swag)
72 if (xbt_swag_belongs(obj, swag))
76 if (swag->head == NULL) {
77 xbt_assert0(!(swag->tail), "Inconsistent swag.");
83 PREV(obj, swag->offset) = swag->tail;
84 NEXT(PREV(obj, swag->offset), swag->offset) = obj;
90 * \param obj the objet to insert in the swag
93 * insert (at the head... you probably had a very good reason to do
94 * that, I hope you know what you're doing) \a obj in \a swag
96 void xbt_swag_insert_at_head(void *obj, xbt_swag_t swag)
99 if (xbt_swag_belongs(obj, swag))
103 if (swag->head == NULL) {
104 xbt_assert0(!(swag->tail), "Inconsistent swag.");
110 NEXT(obj, swag->offset) = swag->head;
111 PREV(NEXT(obj, swag->offset), swag->offset) = obj;
117 * \param obj the objet to insert in the swag
120 * insert (at the tail... you probably had a very good reason to do
121 * that, I hope you know what you're doing) \a obj in \a swag
123 void xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag)
126 if (xbt_swag_belongs(obj, swag))
130 if (swag->head == NULL) {
131 xbt_assert0(!(swag->tail), "Inconsistent swag.");
137 PREV(obj, swag->offset) = swag->tail;
138 NEXT(PREV(obj, swag->offset), swag->offset) = obj;
144 * \param obj the objet to remove from the swag
146 * \return \a obj if it was in the \a swag and NULL otherwise
148 * removes \a obj from \a swag
150 void *xbt_swag_remove(void *obj, xbt_swag_t swag)
152 size_t offset = swag->offset;
154 if ((!obj) || (!swag))
156 if(!xbt_swag_belongs(obj, swag)) /* Trying to remove an object that
157 was not in this swag */
160 if (swag->head == swag->tail) { /* special case */
161 if (swag->head != obj) /* Trying to remove an object that was not in this swag */
165 NEXT(obj, offset) = PREV(obj, offset) = NULL;
166 } else if (obj == swag->head) { /* It's the head */
167 swag->head = NEXT(obj, offset);
168 PREV(swag->head, offset) = NULL;
169 NEXT(obj, offset) = NULL;
170 } else if (obj == swag->tail) { /* It's the tail */
171 swag->tail = PREV(obj, offset);
172 NEXT(swag->tail, offset) = NULL;
173 PREV(obj, offset) = NULL;
174 } else { /* It's in the middle */
175 NEXT(PREV(obj, offset), offset) = NEXT(obj, offset);
176 PREV(NEXT(obj, offset), offset) = PREV(obj, offset);
177 PREV(obj, offset) = NEXT(obj, offset) = NULL;
185 * \return an object from the \a swag
187 void *xbt_swag_extract(xbt_swag_t swag)
189 size_t offset = swag->offset;
192 if ((!swag) || (!(swag->head)))
197 if (swag->head == swag->tail) { /* special case */
198 swag->head = swag->tail = NULL;
199 PREV(obj, offset) = NEXT(obj, offset) = NULL;
201 swag->head = NEXT(obj, offset);
202 PREV(swag->head, offset) = NULL;
203 NEXT(obj, offset) = NULL;
211 * \return the number of objects in \a swag
213 int xbt_swag_size(xbt_swag_t swag)
215 return (swag->count);
219 * \param obj an object
221 * \return 1 if \a obj is in the \a swag and 0 otherwise
223 int xbt_swag_belongs(void *obj, xbt_swag_t swag)
225 return ((NEXT(obj, swag->offset)) || (PREV(obj, swag->offset))
226 || (swag->head == obj));