00001 /*
00002 * File: clist.h
00003 * $Id: clist.h,v 1.1 2002/04/29 09:40:00 alec Exp $
00004 *
00005 * Author: Alec Panoviciu (alecu@email.com)
00006 *
00007 * Comments: C templateized list
00008 *
00009 * Revision history:
00010 *
00011 * $Log: clist.h,v $
00012 * Revision 1.1 2002/04/29 09:40:00 alec
00013 * *** empty log message ***
00014 *
00015 */
00016
00017
00018 /*
00019 Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00020
00021 This program is free software; you can redistribute it and/or modify
00022 it under the terms of the GNU General Public License as published by
00023 the Free Software Foundation; either version 2 of the License, or
00024 (at your option) any later version.
00025
00026 This program is distributed in the hope that it will be useful,
00027 but WITHOUT ANY WARRANTY; without even the implied warranty of
00028 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00029 GNU General Public License for more details.
00030
00031 You should have received a copy of the GNU General Public License
00032 along with this program; if not, write to the Free Software
00033 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00034
00035 */
00036
00037 #ifndef __CLIST_H__
00038 #define __CLIST_H__
00039
00040 #if defined __cplusplus
00041 #define CLINK extern "C"
00042 #else
00043 #define CLINK
00044 #endif
00045
00053 #ifdef CLIST_IMPLEMENT_HERE
00054
00055 #if defined __cplusplus
00056 #include <cstdlib>
00057 #else
00058 #include <stdlib.h>
00059 #endif
00060
00061 /* the full implementation here */
00062
00063 #define DECLARE_LIST(x) typedef struct t_##x##List { \
00064 x* data; \
00065 struct t_##x##List * next; \
00066 } *x##List; \
00067 \
00068 CLINK x##List addTo##x##List (x##List l, x *data) \
00069 { \
00070 x##List cell = (x##List) malloc(sizeof(struct t_##x##List)); \
00071 cell->next = l; \
00072 cell->data = data; \
00073 return cell; \
00074 } \
00075 \
00076 CLINK x##List appendTo##x##List (x##List l, x *data) \
00077 { \
00078 if (!l) { \
00079 x##List res = (x##List) malloc(sizeof(struct t_##x##List)); \
00080 res->next = NULL; \
00081 res->data = data; \
00082 return res; \
00083 } else { \
00084 l->next = appendTo##x##List (l->next, data); \
00085 return l; \
00086 } \
00087 } \
00088 \
00089 CLINK void delete##x##List (x##List l, int deleteData) \
00090 { \
00091 if (!l) return; \
00092 delete##x##List (l->next, deleteData); \
00093 if (deleteData) \
00094 delete##x(l->data); \
00095 free(l); \
00096 } \
00097 \
00098 CLINK x##List join##x##Lists (x##List pre, x##List post) \
00099 { \
00100 x##List i; \
00101 \
00102 if (post) { \
00103 if (!pre) return post; \
00104 for (i = pre; i->next != NULL; i = i->next); \
00105 i->next = post; \
00106 } \
00107 return pre; \
00108 }
00109
00110 #else /*CLIST_IMPLEMENT_HERE*/
00111
00112 /*only prototypes and type decls here*/
00113
00114 #define DECLARE_LIST(x) typedef struct t_##x##List { \
00115 x* data; \
00116 struct t_##x##List * next; \
00117 } *x##List; \
00118 \
00119 CLINK x##List addTo##x##List (x##List l, x *data); \
00120 \
00121 CLINK x##List appendTo##x##List (x##List l, x *data); \
00122 \
00123 CLINK void delete##x##List (x##List l, int deleteData); \
00124 \
00125 CLINK x##List join##x##Lists (x##List pre, x##List post); \
00126
00127 #endif /*CLIST_IMPLEMENT_HERE*/
00128
00129 #endif /*ifndef __CLIST_H__*/
1.2.8.1 written by Dimitri van Heesch,
© 1997-2001