diff options
-rw-r--r-- | linked_list.h | 12 | ||||
-rw-r--r-- | linked_list.inc.h | 26 | ||||
-rw-r--r-- | macro.h | 2 |
3 files changed, 36 insertions, 4 deletions
diff --git a/linked_list.h b/linked_list.h index 6136d35f..c46304cf 100644 --- a/linked_list.h +++ b/linked_list.h @@ -25,7 +25,9 @@ typedef struct { } LLIST(TYPE); #define FIRST(lst) (lst)->head->after +#define FIRST_V(lst) (lst)->head->after->value #define LAST(lst) (lst)->tail->before +#define LAST_V(lst) (lst)->tail->before->value INIT_F ( LLIST(TYPE) ); @@ -53,16 +55,20 @@ int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new ); int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ); int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); + +LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new); + /* Iterator */ // #define __BEG_LLIST(v, set) v = (set)->head -#define __BEG_LLIST(v, set) v = FIRST(set) +#define __BEG_LLIST(l, set) l = FIRST(set) #define BEG_LLIST(T) LINK(T)* __BEG_LLIST -#define __END_LLIST(var, set) var == (set)->tail +#define __END_LLIST(l, set) l != (set)->tail #define END_LLIST(T) __END_LLIST -#define __NXT_LLIST(var, set) var = var->after +#define __NXT_LLIST(l, set) l = l->after #define NXT_LLIST(T) __NXT_LLIST #endif /* TYPE */ diff --git a/linked_list.inc.h b/linked_list.inc.h index ada0b424..6a0546c7 100644 --- a/linked_list.inc.h +++ b/linked_list.inc.h @@ -17,7 +17,7 @@ INIT_F ( LLIST(TYPE) ) { FREE_F (LINK(TYPE)) { UNLINK(LINK(TYPE))(this); - if (this->value != NULL) FFREE(TYPE, this->value); + if (this->value != NULL) FFREE(TYPE, this->value); return 0; } @@ -132,6 +132,7 @@ int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { LINK(TYPE)* l = FIRST(llist); while (l->after != NULL) { ++size; + l = l->after; } return size; } @@ -140,4 +141,27 @@ int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { return FIRST(llist) == llist->tail; } +LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new) { + if (dest == NULL) return new; + APPEND(LLIST(TYPE))(dest, new); + return dest; +} + +int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { + INFO_F("|llist| = %i", SIZE(LLIST(TYPE))(llist)); +#if 1 + FOR(LLIST(TYPE), link, llist) { + // INFO_F("Freeing link '%p' → '%p'", link, link->value); + INFO("leaking memmory, or something"); + FFREE(LINK(TYPE), link); + // UNLINK(LINK(TYPE))(link); + } +#else + // TODO This is a memmory leak + FIRST(llist) = llist->tail; + LAST(llist) = llist->head; +#endif + return 0; +} + #endif /* TYPE */ @@ -107,5 +107,7 @@ #define PEEK(T) TEMPL(peek , T) #define POP(T) TEMPL(pop , T) #define GET(T) TEMPL(get , T) +#define RESET(T) TEMPL(reset , T) + #endif /* MACRO_H */ |