mirror of
https://github.com/UberGames/GtkRadiant.git
synced 2024-12-03 00:52:01 +00:00
12b372f89c
git-svn-id: svn://svn.icculus.org/gtkradiant/GtkRadiant@1 8a3a26a2-13c4-0310-b231-cf6edde360e5
101 lines
2 KiB
C
101 lines
2 KiB
C
/*
|
|
======================================================================
|
|
list.c
|
|
|
|
Generic linked list operations.
|
|
|
|
Ernie Wright 17 Sep 00
|
|
====================================================================== */
|
|
|
|
#include "../picointernal.h"
|
|
#include "lwo2.h"
|
|
|
|
|
|
/*
|
|
======================================================================
|
|
lwListFree()
|
|
|
|
Free the items in a list.
|
|
====================================================================== */
|
|
|
|
void lwListFree( void *list, void ( *freeNode )( void * ))
|
|
{
|
|
lwNode *node, *next;
|
|
|
|
node = ( lwNode * ) list;
|
|
while ( node ) {
|
|
next = node->next;
|
|
freeNode( node );
|
|
node = next;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
======================================================================
|
|
lwListAdd()
|
|
|
|
Append a node to a list.
|
|
====================================================================== */
|
|
|
|
void lwListAdd( void **list, void *node )
|
|
{
|
|
lwNode *head, *tail;
|
|
|
|
head = *(( lwNode ** ) list );
|
|
if ( !head ) {
|
|
*list = node;
|
|
return;
|
|
}
|
|
while ( head ) {
|
|
tail = head;
|
|
head = head->next;
|
|
}
|
|
tail->next = ( lwNode * ) node;
|
|
(( lwNode * ) node )->prev = tail;
|
|
}
|
|
|
|
|
|
/*
|
|
======================================================================
|
|
lwListInsert()
|
|
|
|
Insert a node into a list in sorted order.
|
|
====================================================================== */
|
|
|
|
void lwListInsert( void **vlist, void *vitem, int ( *compare )( void *, void * ))
|
|
{
|
|
lwNode **list, *item, *node, *prev;
|
|
|
|
if ( !*vlist ) {
|
|
*vlist = vitem;
|
|
return;
|
|
}
|
|
|
|
list = ( lwNode ** ) vlist;
|
|
item = ( lwNode * ) vitem;
|
|
node = *list;
|
|
prev = NULL;
|
|
|
|
while ( node ) {
|
|
if ( 0 < compare( node, item )) break;
|
|
prev = node;
|
|
node = node->next;
|
|
}
|
|
|
|
if ( !prev ) {
|
|
*list = item;
|
|
node->prev = item;
|
|
item->next = node;
|
|
}
|
|
else if ( !node ) {
|
|
prev->next = item;
|
|
item->prev = prev;
|
|
}
|
|
else {
|
|
item->next = node;
|
|
item->prev = prev;
|
|
prev->next = item;
|
|
node->prev = item;
|
|
}
|
|
}
|