From c38f43094036b83b7a575e723dd09f3873e0b585 Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Sat, 21 Mar 2026 22:54:26 -0600 Subject: [PATCH] Added correct type restrains, concepts are really cool, not more horrible template metaprogramming, concepts make it mre clear, so sad that the industry will catch up to it until like 2038 or something --- include/linkedlist.h | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/include/linkedlist.h b/include/linkedlist.h index bf9f6cf..24dbf58 100644 --- a/include/linkedlist.h +++ b/include/linkedlist.h @@ -1,11 +1,25 @@ #ifndef LINKEDLIST_H #define LINKEDLIST_H +#include #include #include +#include #include #include +template +concept HasEqual = requires (T a, T b) { + {a == b} -> std::convertible_to; +}; + +template +concept LinkedListSupported = + std::formattable && + std::equality_comparable && + HasEqual; + + /** * @brief Codigos de error para operaciones de LinkedList */ @@ -23,7 +37,7 @@ enum class LinkedListErr { * * @tparam T Tipo de dato almacenado en el nodo */ -template +template class Node { public: T data; /**< Dato almacenado en el nodo */ @@ -38,7 +52,7 @@ public: * * @tparam T Tipo de dato almacenado en la lista */ -template +template class LinkedList { private: size_t size; /**< Numero de elementos en la lista */ @@ -146,14 +160,14 @@ public: size_t len(); }; -template +template LinkedList::LinkedList() { size = 0; head = nullptr; tail = nullptr; } -template +template LinkedList::~LinkedList() { Node *current = this->head; @@ -164,7 +178,7 @@ LinkedList::~LinkedList() { } } -template +template std::expected LinkedList::get(size_t index) { if (index >= this->size) { return std::unexpected(LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS); @@ -182,7 +196,7 @@ std::expected LinkedList::get(size_t index) { return current->data; } -template +template LinkedListErr LinkedList::set(size_t index, const T& val) { if (index >= this->size) { return LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS; @@ -201,7 +215,7 @@ LinkedListErr LinkedList::set(size_t index, const T& val) { return LinkedListErr::LINKEDLIST_OK; } -template +template LinkedListErr LinkedList::insert(size_t index, const T& value) { if (this->is_empty()) { return this->append(value); @@ -241,7 +255,7 @@ LinkedListErr LinkedList::insert(size_t index, const T& value) { return LinkedListErr::LINKEDLIST_OK; } -template +template LinkedListErr LinkedList::append(const T& value) { Node *new_node = nullptr; @@ -267,7 +281,7 @@ LinkedListErr LinkedList::append(const T& value) { return LinkedListErr::LINKEDLIST_OK; } -template +template LinkedListErr LinkedList::prepend(const T& value) { Node *new_node = nullptr; @@ -293,7 +307,7 @@ LinkedListErr LinkedList::prepend(const T& value) { return LinkedListErr::LINKEDLIST_OK; } -template +template std::expected LinkedList::pop() { if (this->size == 0) { return std::unexpected(LinkedListErr::LINKEDLIST_EMPTY); @@ -322,7 +336,7 @@ std::expected LinkedList::pop() { return return_val; } -template +template LinkedListErr LinkedList::remove(size_t index) { if (index >= this->size) { return LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS; @@ -356,7 +370,7 @@ LinkedListErr LinkedList::remove(size_t index) { } -template +template std::expected LinkedList::find(const T& value) { Node *current = this->head; size_t count = 0; @@ -372,7 +386,7 @@ std::expected LinkedList::find(const T& value) { return std::unexpected(LinkedListErr::LINKEDLIST_NOT_FOUND); } -template +template LinkedListErr LinkedList::print() { if (this->size == 0) { return LinkedListErr::LINKEDLIST_OK; @@ -392,7 +406,7 @@ LinkedListErr LinkedList::print() { return LinkedListErr::LINKEDLIST_OK; } -template +template size_t LinkedList::len() { return this->size; }