diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 23:30:57 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 23:30:57 +0100 |
commit | 5c34847218e8d754447f5cf71ed595bbb412eee7 (patch) | |
tree | 2872948e6d2f566dd66cb1a9b2a5d9900db4c44f /backend/List.hpp | |
parent | a04cb74fee079e3ee43ae5fae32fc2674409822c (diff) |
more WIP
Diffstat (limited to 'backend/List.hpp')
-rw-r--r-- | backend/List.hpp | 110 |
1 files changed, 58 insertions, 52 deletions
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 <stdlib.h> +#include "List.h" template <typename T> -struct ListLink { - ListLink<T> * prev; - ListLink<T> * next; - T value; -}; +size_t List<T>::size() { + return this->length; +} -template<typename T> -class List { -public: - size_t size() { return this->length; } +template <typename T> +void List<T>::push_back(T el) { + ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); + 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<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); - 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 <typename T> +void List<T>::pop_back() { + if (this->head == nullptr) return; // empty list - void pop_back() { - if (this->head == nullptr) return; // empty list + ListLink<T> * 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<T> * 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 <typename T> +void List<T>::clear() { + ListLink<T> * link = this->tail; + while (link != nullptr) { + ListLink<T> * next = link->next; + free(link); + link = next; } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; +} - void clear() { - ListLink<T> * link = this->tail; - while (link != nullptr) { - ListLink<T> * next = link->next; - free(link); - link = next; - } - this->head = nullptr; - this->tail = nullptr; - this->length = 0; - } +template <typename T> +T & List<T>::operator [] (size_t index) { + ListLink<T> * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; +} - T & operator [] (size_t index) { - ListLink<T> * link = this->tail; - for (size_t i = 0; i < index; i++) - link = link->next; - return link->value; - } +template <typename T> +ListRange<T> List<T>::range() { + return { *this }; +} -private: - ListLink<T> * head = nullptr; - ListLink<T> * tail = nullptr; - size_t length = 0; -}; +template <typename T> +ListIterator<T> List<T>::begin() { + return this->range().begin(); +} + +template <typename T> +ListIterator<T> List<T>::end() { + return this->range().end(); +} |