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
This commit is contained in:
@@ -1,11 +1,25 @@
|
|||||||
#ifndef LINKEDLIST_H
|
#ifndef LINKEDLIST_H
|
||||||
#define LINKEDLIST_H
|
#define LINKEDLIST_H
|
||||||
|
|
||||||
|
#include <concepts>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
|
#include <format>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept HasEqual = requires (T a, T b) {
|
||||||
|
{a == b} -> std::convertible_to<bool>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept LinkedListSupported =
|
||||||
|
std::formattable<T, char> &&
|
||||||
|
std::equality_comparable<T> &&
|
||||||
|
HasEqual<T>;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Codigos de error para operaciones de LinkedList
|
* @brief Codigos de error para operaciones de LinkedList
|
||||||
*/
|
*/
|
||||||
@@ -23,7 +37,7 @@ enum class LinkedListErr {
|
|||||||
*
|
*
|
||||||
* @tparam T Tipo de dato almacenado en el nodo
|
* @tparam T Tipo de dato almacenado en el nodo
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<LinkedListSupported T>
|
||||||
class Node {
|
class Node {
|
||||||
public:
|
public:
|
||||||
T data; /**< Dato almacenado en el nodo */
|
T data; /**< Dato almacenado en el nodo */
|
||||||
@@ -38,7 +52,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @tparam T Tipo de dato almacenado en la lista
|
* @tparam T Tipo de dato almacenado en la lista
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<LinkedListSupported T>
|
||||||
class LinkedList {
|
class LinkedList {
|
||||||
private:
|
private:
|
||||||
size_t size; /**< Numero de elementos en la lista */
|
size_t size; /**< Numero de elementos en la lista */
|
||||||
@@ -146,14 +160,14 @@ public:
|
|||||||
size_t len();
|
size_t len();
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedList<T>::LinkedList() {
|
LinkedList<T>::LinkedList() {
|
||||||
size = 0;
|
size = 0;
|
||||||
head = nullptr;
|
head = nullptr;
|
||||||
tail = nullptr;
|
tail = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedList<T>::~LinkedList() {
|
LinkedList<T>::~LinkedList() {
|
||||||
Node<T> *current = this->head;
|
Node<T> *current = this->head;
|
||||||
|
|
||||||
@@ -164,7 +178,7 @@ LinkedList<T>::~LinkedList() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
std::expected<T, LinkedListErr> LinkedList<T>::get(size_t index) {
|
std::expected<T, LinkedListErr> LinkedList<T>::get(size_t index) {
|
||||||
if (index >= this->size) {
|
if (index >= this->size) {
|
||||||
return std::unexpected(LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS);
|
return std::unexpected(LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS);
|
||||||
@@ -182,7 +196,7 @@ std::expected<T, LinkedListErr> LinkedList<T>::get(size_t index) {
|
|||||||
return current->data;
|
return current->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedListErr LinkedList<T>::set(size_t index, const T& val) {
|
LinkedListErr LinkedList<T>::set(size_t index, const T& val) {
|
||||||
if (index >= this->size) {
|
if (index >= this->size) {
|
||||||
return LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS;
|
return LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS;
|
||||||
@@ -201,7 +215,7 @@ LinkedListErr LinkedList<T>::set(size_t index, const T& val) {
|
|||||||
return LinkedListErr::LINKEDLIST_OK;
|
return LinkedListErr::LINKEDLIST_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedListErr LinkedList<T>::insert(size_t index, const T& value) {
|
LinkedListErr LinkedList<T>::insert(size_t index, const T& value) {
|
||||||
if (this->is_empty()) {
|
if (this->is_empty()) {
|
||||||
return this->append(value);
|
return this->append(value);
|
||||||
@@ -241,7 +255,7 @@ LinkedListErr LinkedList<T>::insert(size_t index, const T& value) {
|
|||||||
return LinkedListErr::LINKEDLIST_OK;
|
return LinkedListErr::LINKEDLIST_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedListErr LinkedList<T>::append(const T& value) {
|
LinkedListErr LinkedList<T>::append(const T& value) {
|
||||||
Node<T> *new_node = nullptr;
|
Node<T> *new_node = nullptr;
|
||||||
|
|
||||||
@@ -267,7 +281,7 @@ LinkedListErr LinkedList<T>::append(const T& value) {
|
|||||||
return LinkedListErr::LINKEDLIST_OK;
|
return LinkedListErr::LINKEDLIST_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedListErr LinkedList<T>::prepend(const T& value) {
|
LinkedListErr LinkedList<T>::prepend(const T& value) {
|
||||||
Node<T> *new_node = nullptr;
|
Node<T> *new_node = nullptr;
|
||||||
|
|
||||||
@@ -293,7 +307,7 @@ LinkedListErr LinkedList<T>::prepend(const T& value) {
|
|||||||
return LinkedListErr::LINKEDLIST_OK;
|
return LinkedListErr::LINKEDLIST_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
std::expected<T, LinkedListErr> LinkedList<T>::pop() {
|
std::expected<T, LinkedListErr> LinkedList<T>::pop() {
|
||||||
if (this->size == 0) {
|
if (this->size == 0) {
|
||||||
return std::unexpected(LinkedListErr::LINKEDLIST_EMPTY);
|
return std::unexpected(LinkedListErr::LINKEDLIST_EMPTY);
|
||||||
@@ -322,7 +336,7 @@ std::expected<T, LinkedListErr> LinkedList<T>::pop() {
|
|||||||
return return_val;
|
return return_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedListErr LinkedList<T>::remove(size_t index) {
|
LinkedListErr LinkedList<T>::remove(size_t index) {
|
||||||
if (index >= this->size) {
|
if (index >= this->size) {
|
||||||
return LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS;
|
return LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS;
|
||||||
@@ -356,7 +370,7 @@ LinkedListErr LinkedList<T>::remove(size_t index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
std::expected<size_t, LinkedListErr> LinkedList<T>::find(const T& value) {
|
std::expected<size_t, LinkedListErr> LinkedList<T>::find(const T& value) {
|
||||||
Node<T> *current = this->head;
|
Node<T> *current = this->head;
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
@@ -372,7 +386,7 @@ std::expected<size_t, LinkedListErr> LinkedList<T>::find(const T& value) {
|
|||||||
return std::unexpected(LinkedListErr::LINKEDLIST_NOT_FOUND);
|
return std::unexpected(LinkedListErr::LINKEDLIST_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
LinkedListErr LinkedList<T>::print() {
|
LinkedListErr LinkedList<T>::print() {
|
||||||
if (this->size == 0) {
|
if (this->size == 0) {
|
||||||
return LinkedListErr::LINKEDLIST_OK;
|
return LinkedListErr::LINKEDLIST_OK;
|
||||||
@@ -392,7 +406,7 @@ LinkedListErr LinkedList<T>::print() {
|
|||||||
return LinkedListErr::LINKEDLIST_OK;
|
return LinkedListErr::LINKEDLIST_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <LinkedListSupported T>
|
||||||
size_t LinkedList<T>::len() {
|
size_t LinkedList<T>::len() {
|
||||||
return this->size;
|
return this->size;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user