diff options
Diffstat (limited to 'linked_list.h')
-rw-r--r-- | linked_list.h | 104 |
1 files changed, 62 insertions, 42 deletions
diff --git a/linked_list.h b/linked_list.h index a17bd797..d87aa8b0 100644 --- a/linked_list.h +++ b/linked_list.h @@ -3,69 +3,87 @@ #include "macro.h" -#define LLIST(T) TEMPL(llist, T) -#define LINK(T) TEMPL(llist_link, T) +// #define LLIST(T) TEMPL(llist, T) +// #define LINK(T) TEMPL(llist_link, T) +// +// #define UNLINK(T) TEMPL(unlink, T) -#define UNLINK(T) TEMPL(unlink, T) +// #endif /* LINKED_LIST_H */ +// #ifdef TYPE -#endif /* LINKED_LIST_H */ -#ifdef 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 + +template <class T> struct llink { + struct llink<T>* before; + struct llink<T>* after; + T* value; + + llink (T* val); + llink () : llink (NULL) { }; + ~llink (); -typedef struct LINK(TYPE) { - struct LINK(TYPE)* before; - struct LINK(TYPE)* after; - TYPE* value; -} LINK(TYPE); + int unlink (); +}; #define L(link) (link)->value -typedef struct { - LINK(TYPE)* head; - LINK(TYPE)* tail; - LINK(TYPE)* cur; +template <class T> struct llist { + llink<T>* head; + llink<T>* tail; + llink<T>* cur; int length; -} 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 + llist (); + llist (llist<T>* other); + ~llist (); + + int push (T*); + T* peek (); + T* pop (); -INIT_F ( LLIST(TYPE) ); + int size () { return this->length; } + int empty () { return FIRST(this) == this->tail; } + int append (llist<T>* other); + + /* + * Resets a linked list by removing all it's objects. + * FREE's all elements stored in the list. + */ + int reset (); + +}; + + +// INIT_F ( LLIST(TYPE) ); /* * NOTE freeing a linked list alsa FFREE's all its contents. * TODO some form of shared pointer to ensure nothing is free'd twice * would be a good idea. */ -FREE_F ( LLIST(TYPE) ); - -INIT_F ( LINK(TYPE) ); -INIT_F ( LINK(TYPE), TYPE* val ); -FREE_F ( LINK(TYPE) ); +// FREE_F ( LLIST(TYPE) ); +// +// INIT_F ( LINK(TYPE) ); +// INIT_F ( LINK(TYPE), TYPE* val ); +// FREE_F ( LINK(TYPE) ); -int UNLINK(LINK(TYPE)) ( LINK(TYPE)* ); +// int UNLINK(LINK(TYPE)) ( LINK(TYPE)* ); -int PUSH(LLIST(TYPE)) ( LLIST(TYPE)*, TYPE* ); -TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* ); -TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* ); +// int PUSH(LLIST(TYPE)) ( LLIST(TYPE)*, TYPE* ); +// TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* ); +// TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* ); -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); +// // int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new_ ); -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ); -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +// LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); +template <class T> +llist<T>* resolve (llist<T>* dest, llist<T>* new__); -/* - * Resets a linked list by removing all it's objects. - * FREE's all elements stored in the list. - */ -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); - -LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); - -FMT_F(LLIST(TYPE)); +// FMT_F(LLIST(TYPE)); /* Iterator */ @@ -85,4 +103,6 @@ FMT_F(LLIST(TYPE)); // #define __NXT_LLIST(T, l, set) l = L(__INTER(l) = __INTER(l)->after) #define NXT_LLIST(T) __NXT_LLIST +#include "linked_list.inc.h" + #endif /* TYPE */ |