From 5c34847218e8d754447f5cf71ed595bbb412eee7 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 29 Oct 2024 23:30:57 +0100 Subject: more WIP --- backend/List.hpp | 110 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 52 deletions(-) (limited to 'backend/List.hpp') diff --git a/backend/List.hpp b/backend/List.hpp index d6c7738..df075f9 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -1,66 +1,72 @@ #pragma once -#include +#include "List.h" template -struct ListLink { - ListLink * prev; - ListLink * next; - T value; -}; +size_t List::size() { + return this->length; +} -template -class List { -public: - size_t size() { return this->length; } +template +void List::push_back(T el) { + ListLink * link = static_cast *>(malloc(sizeof(ListLink))); + link->prev = this->head; + link->next = nullptr; + link->value = el; + if (this->head != nullptr) this->head->next = link; + if (this->head == nullptr) this->tail = link; + this->head = link; + this->length++; +} - void push_back(T el) { - ListLink * link = static_cast *>(malloc(sizeof(ListLink))); - link->prev = this->head; - link->next = nullptr; - link->value = el; - if (this->head != nullptr) this->head->next = link; - if (this->head == nullptr) this->tail = link; - this->head = link; - this->length++; - } +template +void List::pop_back() { + if (this->head == nullptr) return; // empty list - void pop_back() { - if (this->head == nullptr) return; // empty list + ListLink * secondlast = this->head->prev; + if (secondlast != nullptr) + secondlast->next = nullptr; + free(this->head); + if (this->tail == this->head) + this->tail = nullptr; + this->head = secondlast; - ListLink * secondlast = this->head->prev; - if (secondlast != nullptr) - secondlast->next = nullptr; - free(this->head); - if (this->tail == this->head) - this->tail = nullptr; - this->head = secondlast; + this->length--; +} - this->length--; +template +void List::clear() { + ListLink * link = this->tail; + while (link != nullptr) { + ListLink * next = link->next; + free(link); + link = next; } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; +} - void clear() { - ListLink * link = this->tail; - while (link != nullptr) { - ListLink * next = link->next; - free(link); - link = next; - } - this->head = nullptr; - this->tail = nullptr; - this->length = 0; - } +template +T & List::operator [] (size_t index) { + ListLink * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; +} - T & operator [] (size_t index) { - ListLink * link = this->tail; - for (size_t i = 0; i < index; i++) - link = link->next; - return link->value; - } +template +ListRange List::range() { + return { *this }; +} -private: - ListLink * head = nullptr; - ListLink * tail = nullptr; - size_t length = 0; -}; +template +ListIterator List::begin() { + return this->range().begin(); +} + +template +ListIterator List::end() { + return this->range().end(); +} -- cgit v1.2.3