2 * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
40 checkGList(GList *l) {
42 l = t0malloc(sizeof(GList));
44 tassert( GLIST_LENGTH(l) >= 0 );
46 ( GLIST_LENGTH(l) == 0 && GLIST_HEAD(l) == NULL && GLIST_TAIL(l) == NULL ) ||
47 ( GLIST_LENGTH(l) > 0 && GLIST_HEAD(l) != NULL && GLIST_TAIL(l) != NULL )
55 GListCell *cell = tmalloc(sizeof(GListCell));
61 insertCell(GList *l, GListCell *prev, GListCell *cell) {
65 /* insert into head */
66 if ( GLIST_LENGTH(l) == 0 ) {
67 l->head = l->tail = cell;
68 cell->next = cell->prev = NULL;
75 } else if ( prev == l->tail ) {
76 /* insert into tail */
78 tassert( GLIST_LENGTH(l) > 0 );
84 tassert( GLIST_LENGTH(l) > 0 );
85 cell->next = prev->next;
86 cell->next->prev = cell;
97 GListInsert(GList *l, GListCell *prev, void *ptr) {
98 return insertCell(l, prev, makeCell(ptr));
102 GListPush(GList *l, void *ptr) {
103 return insertCell(l, GLIST_TAIL(l), makeCell(ptr));
107 GListUnshift(GList *l, void *ptr) {
108 return insertCell(l, NULL, makeCell(ptr));
112 GListDelete(GList *l, GListCell *c) {
113 if (GLIST_LENGTH(l) <= 0)
116 if ( l->head == c ) {
117 tassert( c->prev == NULL );
120 l->head->prev = NULL;
123 } else if ( l->tail == c ) {
124 tassert( c->next == NULL );
127 l->tail->next = NULL;
131 c->prev->next = c->next;
132 c->next->prev = c->prev;
135 c->prev = c->next = NULL;
146 if (GLIST_LENGTH(l) > 0) {
155 GListShift(GList *l) {
158 if (GLIST_LENGTH(l) > 0) {
167 GListGet(GList *l, int n) {
179 GListFind(GList *l, void *ptr) {
182 if ( l && l->cmpFunc ) {
184 if ( l->cmpFunc(ptr, c->data) == 0 )
189 if ( ptr == c->data )
197 static GListCellComparator curComparator;
200 compareData( const void *a, const void *b ) {
201 if ( *(void**)a == *(void**)b )
203 return curComparator( *(void**)a, *(void**)b );
207 GListSort(GList *l) {
209 void **arrptr, **array;
211 if (GLIST_LENGTH(l) <= 1)
214 tassert(l->cmpFunc != NULL);
216 arrptr = array = (void**)tmalloc(sizeof(void*)*GLIST_LENGTH(l));;
220 tassert( arrptr - array == GLIST_LENGTH(l) );
222 curComparator = l->cmpFunc;
223 qsort(array, GLIST_LENGTH(l), sizeof(void*), compareData);
235 GListUniq(GList *l) {
240 GListCell *next = c->next;
245 if ( c->data == next->data || ( l->cmpFunc && l->cmpFunc(c->data, next->data)==0 ) ) {
246 GListDelete( l, next );
247 GListFreeCell( l, next );
258 GListFree(GList *l) {
259 GListCell *c = GLIST_HEAD(l), *tmp;
272 GListFreeCell(GList *l, GListCell* c) {
273 if (l && l->freeFunc)
274 l->freeFunc(c->data);
280 GListTruncate(GList *l, int n) {
281 while( GLIST_LENGTH(l) > n ) {
282 GListCell *c = l->tail;
293 GListCopy(GList *l) {
298 n = GListPush(n, c->data);
301 n->cmpFunc = l->cmpFunc;
302 n->freeFunc = l->freeFunc;
309 GListConcat(GList *l1, GList *l2) {
312 tlog(TL_CRIT|TL_EXIT,"Can't concat list itself");
313 if ( GLIST_LENGTH(l1) == 0 )
315 if ( GLIST_LENGTH(l2) == 0 ) {
320 l1->tail->next = l2->head;
321 l2->head->prev = l1->tail;
323 l1->length += l2->length;
325 l2->head = l2->tail = NULL;
333 GListDifference(GList *l1, GList *l2) {
337 if ( l1 && l2 && l1->cmpFunc != l2->cmpFunc )
338 tlog(TL_CRIT|TL_EXIT,"GListDifference: comparators aren't the same");
340 if (GLIST_LENGTH(l2) == 0 )
341 return GListCopy(l1);
344 if ( GListFind(l2, c->data) == NULL )
345 l = GListPush(l, c->data);
352 GListUnion(GList *l1, GList *l2) {
353 GList *l = GListCopy(l1);
356 if ( l1 && l2 && l1->cmpFunc != l2->cmpFunc )
357 tlog(TL_CRIT|TL_EXIT,"GListUnion: comparators aren't the same");
359 if (GLIST_LENGTH(l2) == 0 )
363 if ( GListFind(l1, c->data) == NULL )
364 l = GListPush(l, c->data);
371 GListIntersect(GList *l1, GList *l2) {
372 GList *l = checkGList(NULL);
375 if ( l1 && l2 && l1->cmpFunc != l2->cmpFunc )
376 tlog(TL_CRIT|TL_EXIT,"GListUnion: comparators aren't the same");
378 if ( GLIST_LENGTH(l1) == 0 || GLIST_LENGTH(l2) == 0 )
382 if ( GListFind(l2, c->data) != NULL )
383 l = GListPush(l, c->data);