From aae3b8bfb83abec0f1bb8e4c854c156c03be5ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 19 Feb 2019 00:27:43 +0100 Subject: Started full rewrite in C++. --- linked_list.h | 58 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) (limited to 'linked_list.h') diff --git a/linked_list.h b/linked_list.h index a17bd797..7d11ca81 100644 --- a/linked_list.h +++ b/linked_list.h @@ -1,54 +1,48 @@ #ifndef LINKED_LIST_H #define LINKED_LIST_H -#include "macro.h" -#define LLIST(T) TEMPL(llist, T) -#define LINK(T) TEMPL(llist_link, T) +template +struct link { + link* before = nullptr; + link* after = nullptr; + T* value; -#define UNLINK(T) TEMPL(unlink, T) + link (); + link (T* val) : value(val) { } + ~link (); -#endif /* LINKED_LIST_H */ -#ifdef TYPE - -typedef struct LINK(TYPE) { - struct LINK(TYPE)* before; - struct LINK(TYPE)* after; - TYPE* value; -} LINK(TYPE); + void unlink (); +}; -#define L(link) (link)->value +// #define L(link) (link)->value -typedef struct { - LINK(TYPE)* head; - LINK(TYPE)* tail; - LINK(TYPE)* cur; +template +struct llist { + link* head; + link* tail; + link* cur; int length; -} LLIST(TYPE); + + llist (); + + void push ( T& ); + T& peek (); + T& pop (); + + llist& operator += (llist& other); +}; #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) ); - /* * 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) ); - -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 DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); @@ -85,4 +79,4 @@ FMT_F(LLIST(TYPE)); // #define __NXT_LLIST(T, l, set) l = L(__INTER(l) = __INTER(l)->after) #define NXT_LLIST(T) __NXT_LLIST -#endif /* TYPE */ +#endif /* LINKED_LIST_H */ -- cgit v1.2.3