From c72c47f361ed194f7d598258057e1828ffd1ec11 Mon Sep 17 00:00:00 2001 From: Tyler Dauwalder Date: Tue, 9 Sep 2003 06:25:31 +0000 Subject: [PATCH] Switched from to template paramter format. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4603 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../private/kernel/util/SinglyLinkedList.h | 161 ++++++++++-------- 1 file changed, 94 insertions(+), 67 deletions(-) diff --git a/headers/private/kernel/util/SinglyLinkedList.h b/headers/private/kernel/util/SinglyLinkedList.h index ed702d2c54..5a66829473 100644 --- a/headers/private/kernel/util/SinglyLinkedList.h +++ b/headers/private/kernel/util/SinglyLinkedList.h @@ -7,8 +7,68 @@ namespace Strategy { namespace SinglyLinkedList { //! Automatic node strategy (works like STL containers do) + namespace Private { + template + class ListNode + { + public: + typedef Value ValueType; + + ListNode(const ValueType &data) + : data(data) + , next(NULL) + { + } + + ValueType data; + ListNode *next; + }; + } // namespace Private + template class Allocator = MallocFreeAllocator> - class Auto; + class Auto + { + public: + typedef Private::ListNode NodeType; + typedef Value ValueType; + + inline NodeType *Allocate(const ValueType &data) + { + NodeType* result = fAllocator.Allocate(); + fAllocator.Construct(result, data); + return result; + } + + inline void Free(NodeType *node) + { + fAllocator.Destruct(node); + fAllocator.Deallocate(node); + } + + inline ValueType& GetValue(NodeType *node) const { + return node->data; + } + + inline NodeType* GetNext(NodeType *node) const { + return node->next; + } + + inline NodeType* SetNext(NodeType *node, NodeType* next) const { + return node->next = next; + } + + inline NodeType* GetPrevious(NodeType *node) const { + return node->previous; + } + + inline NodeType* SetPrevious(NodeType *node, NodeType* previous) const { + return node->previous = previous; + } + + protected: + Allocator fAllocator; + }; + //! User managed node strategy (user is responsible for node allocation/deallocation) template @@ -19,11 +79,11 @@ namespace Strategy { template class SinglyLinkedListIterator; -template +template > class SinglyLinkedList { public: - typedef SinglyLinkedList Type; + typedef SinglyLinkedList Type; typedef typename NodeStrategy::NodeType NodeType; typedef typename NodeStrategy::ValueType ValueType; @@ -88,10 +148,13 @@ public: typedef SinglyLinkedListIterator IteratorType; typedef typename Parent::NodeType NodeType; + SinglyLinkedListIterator(); SinglyLinkedListIterator(Parent *parent, NodeType *node, NodeType *previous); + SinglyLinkedListIterator(const IteratorType &ref); bool operator==(const IteratorType &ref) const; bool operator!=(const IteratorType &ref) const; + IteratorType &operator=(const IteratorType &ref); inline Reference operator*() const; inline Pointer operator->() const; IteratorType &operator++(); @@ -106,6 +169,14 @@ private: #define _ITERATOR_TEMPLATE_LIST template #define _ITERATOR SinglyLinkedListIterator +_ITERATOR_TEMPLATE_LIST +_ITERATOR::SinglyLinkedListIterator() + : fParent(NULL) + , fNode(NULL) + , fPrevious(NULL) +{ +} + _ITERATOR_TEMPLATE_LIST _ITERATOR::SinglyLinkedListIterator(Parent *parent, NodeType *node, NodeType *previous) : fParent(parent) @@ -114,6 +185,14 @@ _ITERATOR::SinglyLinkedListIterator(Parent *parent, NodeType *node, NodeType *pr { } +_ITERATOR_TEMPLATE_LIST +_ITERATOR::SinglyLinkedListIterator(const IteratorType &ref) + : fParent(ref.fParent) + , fNode(ref.fNode) + , fPrevious(ref.fPrevious) +{ +} + _ITERATOR_TEMPLATE_LIST bool _ITERATOR::operator==(const _ITERATOR &ref) const @@ -128,6 +207,16 @@ _ITERATOR::operator!=(const _ITERATOR &ref) const return !operator==(ref); } +_ITERATOR_TEMPLATE_LIST +_ITERATOR& +_ITERATOR::operator=(const _ITERATOR &ref) +{ + fParent = ref.fParent; + fNode = ref.fNode; + fPrevious = ref.fPrevious; + return *this; +} + _ITERATOR_TEMPLATE_LIST inline Reference @@ -165,8 +254,8 @@ _ITERATOR::operator++(int) { // SinglyLinkedList implementation //-------------------------------------------------------------------------- -#define _SINGLY_LINKED_LIST_TEMPLATE_LIST template -#define _SINGLY_LINKED_LIST SinglyLinkedList +#define _SINGLY_LINKED_LIST_TEMPLATE_LIST template +#define _SINGLY_LINKED_LIST SinglyLinkedList _SINGLY_LINKED_LIST_TEMPLATE_LIST _SINGLY_LINKED_LIST::~SinglyLinkedList() @@ -389,68 +478,6 @@ _SINGLY_LINKED_LIST::Erase(Iterator &pos) namespace Strategy { namespace SinglyLinkedList { - namespace Private { - template - class ListNode - { - public: - typedef Value ValueType; - - ListNode(const ValueType &data) - : data(data) - , next(NULL) - { - } - - ValueType data; - ListNode *next; - }; - } // namespace Private - - template class Allocator> - class Auto - { - public: - typedef Private::ListNode NodeType; - typedef Value ValueType; - - inline NodeType *Allocate(const ValueType &data) - { - NodeType* result = fAllocator.Allocate(); - fAllocator.Construct(result, data); - return result; - } - - inline void Free(NodeType *node) - { - fAllocator.Destruct(node); - fAllocator.Deallocate(node); - } - - inline ValueType& GetValue(NodeType *node) const { - return node->data; - } - - inline NodeType* GetNext(NodeType *node) const { - return node->next; - } - - inline NodeType* SetNext(NodeType *node, NodeType* next) const { - return node->next = next; - } - - inline NodeType* GetPrevious(NodeType *node) const { - return node->previous; - } - - inline NodeType* SetPrevious(NodeType *node, NodeType* previous) const { - return node->previous = previous; - } - - protected: - Allocator fAllocator; - }; - template class User {