feat: add day 3
This commit is contained in:
18
list.h
18
list.h
@@ -18,7 +18,8 @@
|
||||
#endif //ELEMENT_TYPE
|
||||
|
||||
#define LIST_CONCAT(x, y) x ## _ ## y
|
||||
#define LIST_FN(name) LIST_CONCAT(LIST_PREFIX, name)
|
||||
#define LIST_CONCAT1(x, y) LIST_CONCAT(x, y)
|
||||
#define LIST_FN(name) LIST_CONCAT1(LIST_PREFIX, name)
|
||||
|
||||
typedef struct {
|
||||
ELEMENT_TYPE *data;
|
||||
@@ -26,7 +27,7 @@ typedef struct {
|
||||
int length;
|
||||
} LIST_NAME;
|
||||
|
||||
__inline LIST_NAME* LIST_FN(create)(int capacity) {
|
||||
static __inline LIST_NAME* LIST_FN(create)(int capacity) {
|
||||
LIST_NAME *alist = malloc(sizeof (LIST_NAME));
|
||||
alist->data = malloc(sizeof (ELEMENT_TYPE) * capacity);
|
||||
alist->capacity = capacity;
|
||||
@@ -34,7 +35,7 @@ __inline LIST_NAME* LIST_FN(create)(int capacity) {
|
||||
return alist;
|
||||
}
|
||||
|
||||
__inline void LIST_FN(add)(LIST_NAME *alist, ELEMENT_TYPE value) {
|
||||
static __inline void LIST_FN(add)(LIST_NAME *alist, ELEMENT_TYPE value) {
|
||||
if(alist->length >= alist->capacity) {
|
||||
alist->data = realloc(alist->data, alist->capacity * 2);
|
||||
alist->capacity = alist->capacity * 2;
|
||||
@@ -43,7 +44,7 @@ __inline void LIST_FN(add)(LIST_NAME *alist, ELEMENT_TYPE value) {
|
||||
alist->data[alist->length++] = value;
|
||||
}
|
||||
|
||||
__inline void LIST_FN(remove)(LIST_NAME *alist, int index) {
|
||||
static __inline void LIST_FN(remove)(LIST_NAME *alist, int index) {
|
||||
#ifdef LIST_UNORDERED
|
||||
alist->data[index] = alist->data[--alist->length];
|
||||
#else
|
||||
@@ -52,14 +53,7 @@ __inline void LIST_FN(remove)(LIST_NAME *alist, int index) {
|
||||
#endif //LIST_UNORDERED
|
||||
}
|
||||
|
||||
__inline void LIST_FN(free)(LIST_NAME *alist) {
|
||||
static __inline void LIST_FN(free)(LIST_NAME *alist) {
|
||||
free(alist->data);
|
||||
free(alist);
|
||||
}
|
||||
|
||||
#undef LIST_NAME
|
||||
#undef LIST_PREFIX
|
||||
#undef ELEMENT_TYPE
|
||||
#undef LIST_CONCAT
|
||||
#undef LIST_FN
|
||||
#undef LIST_UNORDERED
|
||||
|
||||
Reference in New Issue
Block a user