C , ++ ,
.
-of-T
, .
"" "" ,
. ,
, --T.
, , , @immortal,
.h .c T.
, , -T
, , list_type.inl, list_type.inl
- list_float.h list_float.c -
-float.
-of-int, list-of-list-of-float, list-of-vector-of-list-of-double,
.
.
.
-.
, T = list-of-thingummy
list-of-T. , POD
. -of-thingummy - ,
,
. , ,
. , POD.
, -T ,
T = . , T -POD
- , , -
. C :
-T, -T
, , .
, .
, set-of-T map-of-T1-to-T2,
-T.
-POD- T T1, ,
.
, memcmp() .
, -T
;
API const.
, ,
, , . , macro_kit.h,
:
#ifndef MACRO_KIT_H
#define MACRO_KIT_H
#define _CAT2(x,y) x##y
#define CAT2(x,y) _CAT2(x,y)
#define CAT3(x,y,z) CAT2(x,CAT2(y,z))
#define JOIN2(x,y) CAT3(x,_,y)
#define JOIN3(x,y,z) JOIN2(x,JOIN2(y,z))
#define SPAN(n,T) ((n) * sizeof(T))
#endif
, list_type.inl:
#include "macro_kit.h"
#include <stddef.h>
#ifndef INCLUDE_LIST_TYPE_INL
#error This file should only be included from headers \
that define INCLUDE_LIST_TYPE_INL
#endif
#ifndef LIST_ELEMENT_TYPE
#error Need a definition for LIST_ELEMENT_TYPE
#endif
#define LIST_TYPE JOIN2(list,LIST_ELEMENT_TYPE)
#define LIST_NEW JOIN2(LIST_TYPE,new)
#define LIST_NODE JOIN2(LIST_TYPE,node)
#define LIST_DISPOSE JOIN2(LIST_TYPE,dispose)
#define LIST_COPY_INIT JOIN2(LIST_TYPE,copy_init)
#define LIST_COPY JOIN2(LIST_TYPE,copy)
#define LIST_BEGIN JOIN2(LIST_TYPE,begin)
#define LIST_END JOIN2(LIST_TYPE,end)
#define LIST_SIZE JOIN2(LIST_TYPE,size)
#define LIST_INSERT_BEFORE JOIN3(LIST_TYPE,insert,before)
#define LIST_DELETE_BEFORE JOIN3(LIST_TYPE,delete,before)
#define LIST_PUSH_BACK JOIN3(LIST_TYPE,push,back)
#define LIST_PUSH_FRONT JOIN3(LIST_TYPE,push,front)
#define LIST_POP_BACK JOIN3(LIST_TYPE,pop,back)
#define LIST_POP_FRONT JOIN3(LIST_TYPE,pop,front)
#define LIST_NODE_GET JOIN2(LIST_NODE,get)
#define LIST_NODE_NEXT JOIN2(LIST_NODE,next)
#define LIST_NODE_PREV JOIN2(LIST_NODE,prev)
#define LIST_STRUCT JOIN2(LIST_TYPE,struct)
#define LIST_NODE_STRUCT JOIN2(LIST_NODE,struct)
typedef struct LIST_STRUCT * LIST_TYPE;
typedef struct LIST_NODE_STRUCT * LIST_NODE;
extern LIST_ELEMENT_TYPE * LIST_NODE_GET(LIST_NODE node);
extern LIST_NODE LIST_NODE_NEXT(LIST_NODE node);
extern LIST_NODE LIST_NODE_PREV(LIST_NODE node);
extern LIST_TYPE LIST_NEW(LIST_ELEMENT_TYPE *start, LIST_ELEMENT_TYPE *end);
extern void LIST_DISPOSE(LIST_TYPE * plist);
extern LIST_TYPE * LIST_COPY_INIT(LIST_TYPE *pdest, LIST_TYPE *psrc);
extern LIST_TYPE LIST_COPY(LIST_TYPE src);
extern LIST_NODE LIST_BEGIN(LIST_TYPE list);
extern LIST_NODE LIST_END(LIST_TYPE list);
extern size_t LIST_SIZE(LIST_TYPE list);
#ifdef LIST_IMPLEMENT
struct LIST_NODE_STRUCT {
struct LIST_NODE_STRUCT * _next;
struct LIST_NODE_STRUCT * _prev;
LIST_ELEMENT_TYPE _data[1];
};
struct LIST_STRUCT {
size_t _size;
struct LIST_NODE_STRUCT * _anchor;
};
#undef LIST_IMPLEMENT
#endif
#undef LIST_ELEMENT_TYPE
#undef LIST_ELEMENT_COPY_INITOR
#undef LIST_ELEMENT_DISPOSE
#undef INCLUDE_LIST_TYPE_INL
, list_type.inl - mutliple.
, - , API- - ,
.
, ,
-.
#ifndef LIST_INT_H
#define LIST_INT_H
#define LIST_ELEMENT_TYPE int
#define INCLUDE_LIST_TYPE_INL
#include "list_type.inl"
#endif
, -of-int
:
#ifndef LIST_LIST_INT_H
#define LIST_LIST_INT_H
#define LIST_ELEMENT_TYPE list_int
#define LIST_ELEMENT_COPY_INIT list_int_copy_init
#define LIST_ELEMENT_DISPOSE list_int_dispose
#define INCLUDE_LIST_TYPE_INL
#include "list_type.inl"
#endif
, .
#include "list_int.h"
#include "list_list_int.h"
, LIST_ELEMENT_TYPE ,
list_type.inl always #undefs ,
: . .
LIST_IMPLEMENT. undefined, list_type.inl
, API- ;
. LIST_IMPLEMENT , . ,
, LIST_IMPLEMENT,
API.
, - list_int.c, list_list_int.c,
define LIST_IMPLEMENT. ,
:
#define LIST_IMPLEMENT
#include "list_int.h"
#include "list_int.h"
#define LIST_IMPLEMENT
#include "list_list_int.h"
.
" ":
#include "list_int.h"
#include "list_list_int.h"
int main(void)
{
int idata[10] = {1,2,3,4,5,6,7,8,9,10};
list_int lint = list_int_new(idata,idata + 10);
list_list_int llint = list_list_int_new(&lint,0);
list_int_dispose(&lint);
list_list_int_dispose(&llint);
exit(0);
}
" C", , (!)
, .inl
, . , ,
.h .c jiffy
.
, ++ ,
. ,
, - C .