From b5c6ae253dc286c86cadc68e854e02e2c99cb69a Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 6 Dec 2022 18:41:35 +0100 Subject: [PATCH] Make LinkedList a lot better --- luna/include/luna/LinkedList.h | 81 ++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/luna/include/luna/LinkedList.h b/luna/include/luna/LinkedList.h index e5e1e279..5e343b3d 100644 --- a/luna/include/luna/LinkedList.h +++ b/luna/include/luna/LinkedList.h @@ -12,10 +12,31 @@ template class DoublyLinkedList; template class DoublyLinkedListNode { + using SelfType = DoublyLinkedListNode; private: - DoublyLinkedListNode* m_next_node; - DoublyLinkedListNode* m_last_node; + SelfType* m_next_node; + SelfType* m_last_node; + + void set_next(SelfType* next) + { + m_next_node = next; + } + + void set_last(SelfType* last) + { + m_last_node = last; + } + + SelfType* get_next() + { + return m_next_node; + } + + SelfType* get_last() + { + return m_last_node; + } void detach_from_list() { @@ -23,7 +44,7 @@ template class DoublyLinkedListNode m_last_node->m_next_node = m_next_node; } - void add_to_list(DoublyLinkedListNode* end_node) + void add_to_list(SelfType* end_node) { end_node->m_next_node = this; this->m_last_node = end_node; @@ -34,14 +55,19 @@ template class DoublyLinkedListNode template class DoublyLinkedList { + using Node = DoublyLinkedListNode; + public: void append(T* ptr) { - DoublyLinkedListNode* node = (DoublyLinkedListNode*)ptr; + Node* const node = extract_node(ptr); if (!m_start_node) m_start_node = node; if (m_end_node) node->add_to_list(m_end_node); else - node->m_next_node = node->m_last_node = nullptr; + { + node->set_next(nullptr); + node->set_last(nullptr); + } m_end_node = node; m_count++; @@ -49,24 +75,24 @@ template class DoublyLinkedList void append_after(T* base, T* ptr) { - DoublyLinkedListNode* new_node = (DoublyLinkedListNode*)ptr; - DoublyLinkedListNode* base_node = (DoublyLinkedListNode*)base; + Node* const new_node = extract_node(ptr); + Node* const base_node = extract_node(base); if (m_end_node == base_node) m_end_node = new_node; - new_node->m_next_node = base_node->m_next_node; - base_node->m_next_node = new_node; - new_node->m_last_node = base_node; + new_node->set_next(base_node->get_next()); + base_node->set_next(new_node); + new_node->set_last(base_node); m_count++; } T* remove(T* ptr) { - DoublyLinkedListNode* node = (DoublyLinkedListNode*)ptr; + Node* const node = extract_node(ptr); - if (node == m_end_node) m_end_node = node->m_last_node; - if (node == m_start_node) m_start_node = node->m_next_node; + if (node == m_end_node) m_end_node = node->get_last(); + if (node == m_start_node) m_start_node = node->get_next(); node->detach_from_list(); @@ -87,40 +113,32 @@ template class DoublyLinkedList Result next(T* item) { - return nonnull_or_error((T*)((DoublyLinkedListNode*)item)->m_next_node); + return nonnull_or_error((T*)extract_node(item)->get_next()); } Result previous(T* item) { - return nonnull_or_error((T*)((DoublyLinkedListNode*)item)->m_last_node); + return nonnull_or_error((T*)extract_node(item)->get_last()); } template void for_each(Callback callback) { - for (DoublyLinkedListNode* node = m_start_node; node; node = node->m_next_node) { callback((T*)node); } + for (Node* node = m_start_node; node; node = node->get_next()) { callback((T*)node); } } template void for_each_reversed(Callback callback) { - for (DoublyLinkedListNode* node = m_end_node; node; node = node->m_last_node) { callback((T*)node); } + for (Node* node = m_end_node; node; node = node->get_last()) { callback((T*)node); } } template void for_each_after(T* start, Callback callback) { - for (DoublyLinkedListNode* node = ((DoublyLinkedListNode*)start)->m_next_node; node; - node = node->m_next_node) - { - callback((T*)node); - } + for (Node* node = extract_node(start)->m_next_node; node; node = node->get_next()) { callback((T*)node); } } template void for_each_before(T* end, Callback callback) { - for (DoublyLinkedListNode* node = ((DoublyLinkedListNode*)end)->m_last_node; node; - node = node->m_last_node) - { - callback((T*)node); - } + for (Node* node = extract_node(end)->m_last_node; node; node = node->get_last()) { callback((T*)node); } } usize count() @@ -129,8 +147,13 @@ template class DoublyLinkedList } private: - DoublyLinkedListNode* m_start_node = nullptr; - DoublyLinkedListNode* m_end_node = nullptr; + Node* m_start_node = nullptr; + Node* m_end_node = nullptr; + + Node* extract_node(T* item) + { + return (Node*)item; + } usize m_count = 0; }; \ No newline at end of file