From 329e3092542067f3a99fda328a8118e8d024e054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 21 Feb 2019 19:57:31 +0100 Subject: Build, but doesn't link. --- linked_list.inc.h | 187 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 105 insertions(+), 82 deletions(-) (limited to 'linked_list.inc.h') diff --git a/linked_list.inc.h b/linked_list.inc.h index 8ae720ba..e20c6339 100644 --- a/linked_list.inc.h +++ b/linked_list.inc.h @@ -1,107 +1,129 @@ -#ifndef TYPE -#error "Set TYPE before including self file" -#else - -INIT_F ( LLIST(TYPE) ) { - self->length = 0; - NEW(LINK(TYPE), head); - NEW(LINK(TYPE), tail); - self->head = head; - self->tail = tail; +// #ifndef TYPE +// #error "Set TYPE before including this file" +// #else + +// INIT_F ( LLIST(TYPE) ) { +template +llist::llist () { + this->length = 0; + head = new llink; + tail = new llink; + this->head = head; + this->tail = tail; head->after = tail; tail->before = head; - self->cur = head; - return 0; + this->cur = head; + // return 0; } -FREE_F (LINK(TYPE)) { - UNLINK(LINK(TYPE))(self); +// FREE_F (LINK(TYPE)) { +template +llink::~llink () { + this->unlink(); - if (self->value != NULL) FFREE(TYPE, self->value); - return 0; + // if (this->value != NULL) delete this->value; } -FREE_F( LLIST(TYPE) ) { - LINK(TYPE) *n, *next; - n = self->head; +// FREE_F( LLIST(TYPE) ) { +template +llist::~llist () { + llink *n, *next; + n = this->head; while ( n != NULL ) { next = n->after; - FFREE(LINK(TYPE), n); + delete n; + // FFREE(LINK(TYPE), n); n = next; } - self->length = -1; - - return 0; + this->length = -1; } +/* INIT_F ( LINK(TYPE) ) { - self->before = NULL; - self->after = NULL; - self->value = NULL; + this->before = NULL; + this->after = NULL; + this->value = NULL; return 0; } +*/ -INIT_F ( LINK(TYPE), TYPE* val ) { - self->before = NULL; - self->after = NULL; - self->value = val; - return 0; +// INIT_F ( LINK(TYPE), TYPE* val ) { +template +llink::llink (T* val) { + this->before = NULL; + this->after = NULL; + this->value = val; } -int UNLINK(LINK(TYPE)) ( LINK(TYPE)* self ) { - if (self->before != NULL) self->before->after = self->after; - if (self->after != NULL) self->after->before = self->before; +// int UNLINK(LINK(TYPE)) ( LINK(TYPE)* this ) { +template +int llink::unlink () { + if (this->before != NULL) this->before->after = this->after; + if (this->after != NULL) this->after->before = this->before; return 0; } -int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) { - NEW(LINK(TYPE), link, val); +// int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) { +template +int llist::push (T* val) { + // NEW(LINK(TYPE), link, val); + auto link = new llink(val); - link->after = FIRST(lst); - FIRST(lst) = link; + link->after = FIRST(this); + FIRST(this) = link; link->after->before = link; - link->before = lst->head; + link->before = this->head; - ++lst->length; + ++this->length; // TODO do I want to change that? - lst->cur = link; + this->cur = link; return 0; } -TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) { - if (EMPTY(LLIST(TYPE))(lst)) return NULL; +// TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) { +template +T* llist::peek () { + if (this->empty()) return NULL; + // if (EMPTY(LLIST(TYPE))(lst)) return NULL; - return FIRST(lst)->value; + return FIRST(this)->value; } -TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) { - if (EMPTY(LLIST(TYPE))(lst)) return NULL; +// TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) { +template +T* llist::pop () { + // if (EMPTY(LLIST(TYPE))(lst)) return NULL; + if (this->empty()) return NULL; - LINK(TYPE)* frst = FIRST(lst); - UNLINK(LINK(TYPE))(frst); + // LINK(TYPE)* frst = FIRST(lst); + //UNLINK(LINK(TYPE))(frst); + auto frst = FIRST(this); + frst->unlink(); - TYPE* retval = frst->value; - --lst->length; - free (frst); + T* retval = frst->value; + --this->length; + delete frst; return retval; } -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { - LINK(TYPE)* n = FIRST(src); +// int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { +template +llist::llist (llist* other) { + llink* n = FIRST(other); while (n->after != NULL) { - NEW(TYPE, cpy); - DEEP_COPY(TYPE)(cpy, n->value); - PUSH(LLIST(TYPE)) ( dest, cpy ); + // NEW(TYPE, cpy); + T* cpy = new T(n->value); + // DEEP_COPY(TYPE)(cpy, n->value); + //PUSH(LLIST(TYPE)) ( dest, cpy ); + this->push (cpy); n = n->after; } - - return 0; } /* @@ -110,60 +132,60 @@ int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { * destroys new__ in the process, but keeps the elements. * make sure to free(new__) after. */ -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new__ ) { +template +int llist::append ( llist* other ) { /* Link end of dest onto start of new__. */ - LAST(dest)->after = FIRST(new__); - FIRST(new__)->before = LAST(dest); + LAST(this)->after = FIRST(other); + FIRST(other)->before = LAST(this); /* Free the two now not needed end links. */ - free(new__->head); - free(dest->tail); + // free(new__->head); + // free(dest->tail); + delete this->tail; + delete other->head; /* Update dest with new__ tail ptr. */ - dest->tail = new__->tail; + this->tail = other->tail; - dest->length += new__->length; + this->length += other->length; return 0; } -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - return llist->length; -} - -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - return FIRST(llist) == llist->tail; -} - -LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new__) { +template +llist* resolve (llist* dest, llist* new__) { if (dest == NULL) return new__; - APPEND(LLIST(TYPE))(dest, new__); + dest.append(new__); return dest; } -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { +// int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { +template +int llist::reset () { - LINK(TYPE) *link = FIRST(llist), *next; + llink *link = FIRST(this), *next; /* * Manual looping rather than itterators since we destroyed the * loop variable. */ - while (link != llist->tail) { + while (link != this->tail) { next = link->after; - FFREE(LINK(TYPE), link); + // FFREE(LINK(TYPE), link); + delete link; link = next; } - llist->cur = llist->head; + this->cur = this->head; return 0; } +#if 0 FMT_F(LLIST(TYPE)) { int seek = 0; fmtf("("); - FOR(LLIST, TYPE, v, self) { + FOR(LLIST, TYPE, v, this) { seek += FMT(TYPE)(v, buf + seek); fmtf(" "); } @@ -171,5 +193,6 @@ FMT_F(LLIST(TYPE)) { return seek; } +#endif -#endif /* TYPE */ +// #endif /* TYPE */ -- cgit v1.2.3