Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

list.c

Go to the documentation of this file.
00001 #include "c.h"
00002 
00003 
00004 static List freenodes;      /* free list nodes */
00005 
00006 /* append - append x to list, return new list */
00007 List append(void *x, List list) {
00008     List new;
00009 
00010     if ((new = freenodes) != NULL)
00011         freenodes = freenodes->link;
00012     else
00013         NEW(new, PERM);
00014     if (list) {
00015         new->link = list->link;
00016         list->link = new;
00017     } else
00018         new->link = new;
00019     new->x = x;
00020     return new;
00021 }
00022 
00023 /* length - # elements in list */
00024 int length(List list) {
00025     int n = 0;
00026 
00027     if (list) {
00028         List lp = list;
00029         do
00030             n++;
00031         while ((lp = lp->link) != list);
00032     }
00033     return n;
00034 }
00035 
00036 /* ltov - convert list to an NULL-terminated vector allocated in arena */
00037 void *ltov(List *list, unsigned arena) {
00038     int i = 0;
00039     void **array = newarray(length(*list) + 1, sizeof array[0], arena);
00040 
00041     if (*list) {
00042         List lp = *list;
00043         do {
00044             lp = lp->link;
00045             array[i++] = lp->x;
00046         } while (lp != *list);
00047 #ifndef PURIFY
00048         lp = (*list)->link;
00049         (*list)->link = freenodes;
00050         freenodes = lp;
00051 #endif
00052     }
00053     *list = NULL;
00054     array[i] = NULL;
00055     return array;
00056 }

Generated on Thu Aug 25 12:38:14 2005 for Quake III Arena by  doxygen 1.3.9.1