| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef DoublyLinkedList_h | 26 #ifndef DoublyLinkedList_h |
| 27 #define DoublyLinkedList_h | 27 #define DoublyLinkedList_h |
| 28 | 28 |
| 29 #include "wtf/Allocator.h" | 29 #include "wtf/Allocator.h" |
| 30 | 30 |
| 31 namespace WTF { | 31 namespace WTF { |
| 32 | 32 |
| 33 // This class allows nodes to share code without dictating data member layout. | 33 // This class allows nodes to share code without dictating data member layout. |
| 34 template<typename T> class DoublyLinkedListNode { | 34 template <typename T> |
| 35 public: | 35 class DoublyLinkedListNode { |
| 36 DoublyLinkedListNode(); | 36 public: |
| 37 DoublyLinkedListNode(); |
| 37 | 38 |
| 38 void setPrev(T*); | 39 void setPrev(T*); |
| 39 void setNext(T*); | 40 void setNext(T*); |
| 40 | 41 |
| 41 T* prev() const; | 42 T* prev() const; |
| 42 T* next() const; | 43 T* next() const; |
| 43 }; | 44 }; |
| 44 | 45 |
| 45 template<typename T> inline DoublyLinkedListNode<T>::DoublyLinkedListNode() | 46 template <typename T> |
| 46 { | 47 inline DoublyLinkedListNode<T>::DoublyLinkedListNode() { |
| 47 setPrev(0); | 48 setPrev(0); |
| 48 setNext(0); | 49 setNext(0); |
| 49 } | 50 } |
| 50 | 51 |
| 51 template<typename T> inline void DoublyLinkedListNode<T>::setPrev(T* prev) | 52 template <typename T> |
| 52 { | 53 inline void DoublyLinkedListNode<T>::setPrev(T* prev) { |
| 53 static_cast<T*>(this)->m_prev = prev; | 54 static_cast<T*>(this)->m_prev = prev; |
| 54 } | 55 } |
| 55 | 56 |
| 56 template<typename T> inline void DoublyLinkedListNode<T>::setNext(T* next) | 57 template <typename T> |
| 57 { | 58 inline void DoublyLinkedListNode<T>::setNext(T* next) { |
| 58 static_cast<T*>(this)->m_next = next; | 59 static_cast<T*>(this)->m_next = next; |
| 59 } | 60 } |
| 60 | 61 |
| 61 template<typename T> inline T* DoublyLinkedListNode<T>::prev() const | 62 template <typename T> |
| 62 { | 63 inline T* DoublyLinkedListNode<T>::prev() const { |
| 63 return static_cast<const T*>(this)->m_prev; | 64 return static_cast<const T*>(this)->m_prev; |
| 64 } | 65 } |
| 65 | 66 |
| 66 template<typename T> inline T* DoublyLinkedListNode<T>::next() const | 67 template <typename T> |
| 67 { | 68 inline T* DoublyLinkedListNode<T>::next() const { |
| 68 return static_cast<const T*>(this)->m_next; | 69 return static_cast<const T*>(this)->m_next; |
| 69 } | 70 } |
| 70 | 71 |
| 71 template<typename T> class DoublyLinkedList { | 72 template <typename T> |
| 72 USING_FAST_MALLOC(DoublyLinkedList); | 73 class DoublyLinkedList { |
| 73 public: | 74 USING_FAST_MALLOC(DoublyLinkedList); |
| 74 DoublyLinkedList(); | |
| 75 | 75 |
| 76 bool isEmpty() const; | 76 public: |
| 77 size_t size() const; // This is O(n). | 77 DoublyLinkedList(); |
| 78 void clear(); | |
| 79 | 78 |
| 80 T* head() const; | 79 bool isEmpty() const; |
| 81 T* removeHead(); | 80 size_t size() const; // This is O(n). |
| 81 void clear(); |
| 82 | 82 |
| 83 T* tail() const; | 83 T* head() const; |
| 84 T* removeHead(); |
| 84 | 85 |
| 85 void push(T*); | 86 T* tail() const; |
| 86 void append(T*); | |
| 87 void remove(T*); | |
| 88 | 87 |
| 89 private: | 88 void push(T*); |
| 90 T* m_head; | 89 void append(T*); |
| 91 T* m_tail; | 90 void remove(T*); |
| 91 |
| 92 private: |
| 93 T* m_head; |
| 94 T* m_tail; |
| 92 }; | 95 }; |
| 93 | 96 |
| 94 template<typename T> inline DoublyLinkedList<T>::DoublyLinkedList() | 97 template <typename T> |
| 95 : m_head(0) | 98 inline DoublyLinkedList<T>::DoublyLinkedList() : m_head(0), m_tail(0) {} |
| 96 , m_tail(0) | 99 |
| 97 { | 100 template <typename T> |
| 101 inline bool DoublyLinkedList<T>::isEmpty() const { |
| 102 return !m_head; |
| 98 } | 103 } |
| 99 | 104 |
| 100 template<typename T> inline bool DoublyLinkedList<T>::isEmpty() const | 105 template <typename T> |
| 101 { | 106 inline size_t DoublyLinkedList<T>::size() const { |
| 102 return !m_head; | 107 size_t size = 0; |
| 108 for (T* node = m_head; node; node = node->next()) |
| 109 ++size; |
| 110 return size; |
| 103 } | 111 } |
| 104 | 112 |
| 105 template<typename T> inline size_t DoublyLinkedList<T>::size() const | 113 template <typename T> |
| 106 { | 114 inline void DoublyLinkedList<T>::clear() { |
| 107 size_t size = 0; | 115 m_head = 0; |
| 108 for (T* node = m_head; node; node = node->next()) | 116 m_tail = 0; |
| 109 ++size; | |
| 110 return size; | |
| 111 } | 117 } |
| 112 | 118 |
| 113 template<typename T> inline void DoublyLinkedList<T>::clear() | 119 template <typename T> |
| 114 { | 120 inline T* DoublyLinkedList<T>::head() const { |
| 115 m_head = 0; | 121 return m_head; |
| 116 m_tail = 0; | |
| 117 } | 122 } |
| 118 | 123 |
| 119 template<typename T> inline T* DoublyLinkedList<T>::head() const | 124 template <typename T> |
| 120 { | 125 inline T* DoublyLinkedList<T>::tail() const { |
| 121 return m_head; | 126 return m_tail; |
| 122 } | 127 } |
| 123 | 128 |
| 124 template<typename T> inline T* DoublyLinkedList<T>::tail() const | 129 template <typename T> |
| 125 { | 130 inline void DoublyLinkedList<T>::push(T* node) { |
| 126 return m_tail; | 131 if (!m_head) { |
| 132 ASSERT(!m_tail); |
| 133 m_head = node; |
| 134 m_tail = node; |
| 135 node->setPrev(0); |
| 136 node->setNext(0); |
| 137 return; |
| 138 } |
| 139 |
| 140 ASSERT(m_tail); |
| 141 m_head->setPrev(node); |
| 142 node->setNext(m_head); |
| 143 node->setPrev(0); |
| 144 m_head = node; |
| 127 } | 145 } |
| 128 | 146 |
| 129 template<typename T> inline void DoublyLinkedList<T>::push(T* node) | 147 template <typename T> |
| 130 { | 148 inline void DoublyLinkedList<T>::append(T* node) { |
| 131 if (!m_head) { | 149 if (!m_tail) { |
| 132 ASSERT(!m_tail); | 150 ASSERT(!m_head); |
| 133 m_head = node; | 151 m_head = node; |
| 134 m_tail = node; | 152 m_tail = node; |
| 135 node->setPrev(0); | 153 node->setPrev(0); |
| 136 node->setNext(0); | 154 node->setNext(0); |
| 137 return; | 155 return; |
| 138 } | 156 } |
| 139 | 157 |
| 140 ASSERT(m_tail); | 158 ASSERT(m_head); |
| 141 m_head->setPrev(node); | 159 m_tail->setNext(node); |
| 142 node->setNext(m_head); | 160 node->setPrev(m_tail); |
| 143 node->setPrev(0); | 161 node->setNext(0); |
| 144 m_head = node; | 162 m_tail = node; |
| 145 } | 163 } |
| 146 | 164 |
| 147 template<typename T> inline void DoublyLinkedList<T>::append(T* node) | 165 template <typename T> |
| 148 { | 166 inline void DoublyLinkedList<T>::remove(T* node) { |
| 149 if (!m_tail) { | 167 if (node->prev()) { |
| 150 ASSERT(!m_head); | 168 ASSERT(node != m_head); |
| 151 m_head = node; | 169 node->prev()->setNext(node->next()); |
| 152 m_tail = node; | 170 } else { |
| 153 node->setPrev(0); | 171 ASSERT(node == m_head); |
| 154 node->setNext(0); | 172 m_head = node->next(); |
| 155 return; | 173 } |
| 156 } | |
| 157 | 174 |
| 158 ASSERT(m_head); | 175 if (node->next()) { |
| 159 m_tail->setNext(node); | 176 ASSERT(node != m_tail); |
| 160 node->setPrev(m_tail); | 177 node->next()->setPrev(node->prev()); |
| 161 node->setNext(0); | 178 } else { |
| 162 m_tail = node; | 179 ASSERT(node == m_tail); |
| 180 m_tail = node->prev(); |
| 181 } |
| 163 } | 182 } |
| 164 | 183 |
| 165 template<typename T> inline void DoublyLinkedList<T>::remove(T* node) | 184 template <typename T> |
| 166 { | 185 inline T* DoublyLinkedList<T>::removeHead() { |
| 167 if (node->prev()) { | 186 T* node = head(); |
| 168 ASSERT(node != m_head); | 187 if (node) |
| 169 node->prev()->setNext(node->next()); | 188 remove(node); |
| 170 } else { | 189 return node; |
| 171 ASSERT(node == m_head); | |
| 172 m_head = node->next(); | |
| 173 } | |
| 174 | |
| 175 if (node->next()) { | |
| 176 ASSERT(node != m_tail); | |
| 177 node->next()->setPrev(node->prev()); | |
| 178 } else { | |
| 179 ASSERT(node == m_tail); | |
| 180 m_tail = node->prev(); | |
| 181 } | |
| 182 } | 190 } |
| 183 | 191 |
| 184 template<typename T> inline T* DoublyLinkedList<T>::removeHead() | 192 } // namespace WTF |
| 185 { | |
| 186 T* node = head(); | |
| 187 if (node) | |
| 188 remove(node); | |
| 189 return node; | |
| 190 } | |
| 191 | |
| 192 } // namespace WTF | |
| 193 | 193 |
| 194 using WTF::DoublyLinkedListNode; | 194 using WTF::DoublyLinkedListNode; |
| 195 using WTF::DoublyLinkedList; | 195 using WTF::DoublyLinkedList; |
| 196 | 196 |
| 197 #endif | 197 #endif |
| OLD | NEW |