diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-19 00:27:43 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-19 00:27:43 +0100 |
commit | aae3b8bfb83abec0f1bb8e4c854c156c03be5ca8 (patch) | |
tree | 305f1287a8cc6a896318a4de5f2b43686e7223b3 /linked_list.h | |
parent | Made to compile as C++. (diff) | |
download | calp-aae3b8bfb83abec0f1bb8e4c854c156c03be5ca8.tar.gz calp-aae3b8bfb83abec0f1bb8e4c854c156c03be5ca8.tar.xz |
Started full rewrite in C++.
Diffstat (limited to 'linked_list.h')
-rw-r--r-- | linked_list.h | 58 |
1 files changed, 26 insertions, 32 deletions
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 <typename T> +struct link { + link<T>* before = nullptr; + link<T>* 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 <typename T> +struct llist { + link<T>* head; + link<T>* tail; + link<T>* 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 */ |