| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_CONTAINERS_LINKED_LIST_H_ | 5 #ifndef BASE_CONTAINERS_LINKED_LIST_H_ |
| 6 #define BASE_CONTAINERS_LINKED_LIST_H_ | 6 #define BASE_CONTAINERS_LINKED_LIST_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" |
| 9 |
| 8 // Simple LinkedList type. (See the Q&A section to understand how this | 10 // Simple LinkedList type. (See the Q&A section to understand how this |
| 9 // differs from std::list). | 11 // differs from std::list). |
| 10 // | 12 // |
| 11 // To use, start by declaring the class which will be contained in the linked | 13 // To use, start by declaring the class which will be contained in the linked |
| 12 // list, as extending LinkNode (this gives it next/previous pointers). | 14 // list, as extending LinkNode (this gives it next/previous pointers). |
| 13 // | 15 // |
| 14 // class MyNodeType : public LinkNode<MyNodeType> { | 16 // class MyNodeType : public LinkNode<MyNodeType> { |
| 15 // ... | 17 // ... |
| 16 // }; | 18 // }; |
| 17 // | 19 // |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // space for the "next" and "previous" pointers (base::LinkNode<T>*). | 77 // space for the "next" and "previous" pointers (base::LinkNode<T>*). |
| 76 // Whereas with std::list<T> the type can be anything, so the implementation | 78 // Whereas with std::list<T> the type can be anything, so the implementation |
| 77 // needs to glue on the "next" and "previous" pointers using | 79 // needs to glue on the "next" and "previous" pointers using |
| 78 // some internal node type. | 80 // some internal node type. |
| 79 | 81 |
| 80 namespace base { | 82 namespace base { |
| 81 | 83 |
| 82 template <typename T> | 84 template <typename T> |
| 83 class LinkNode { | 85 class LinkNode { |
| 84 public: | 86 public: |
| 85 LinkNode() : previous_(0), next_(0) {} | 87 LinkNode() : previous_(NULL), next_(NULL) {} |
| 86 LinkNode(LinkNode<T>* previous, LinkNode<T>* next) | 88 LinkNode(LinkNode<T>* previous, LinkNode<T>* next) |
| 87 : previous_(previous), next_(next) {} | 89 : previous_(previous), next_(next) {} |
| 88 | 90 |
| 89 // Insert |this| into the linked list, before |e|. | 91 // Insert |this| into the linked list, before |e|. |
| 90 void InsertBefore(LinkNode<T>* e) { | 92 void InsertBefore(LinkNode<T>* e) { |
| 91 this->next_ = e; | 93 this->next_ = e; |
| 92 this->previous_ = e->previous_; | 94 this->previous_ = e->previous_; |
| 93 e->previous_->next_ = this; | 95 e->previous_->next_ = this; |
| 94 e->previous_ = this; | 96 e->previous_ = this; |
| 95 } | 97 } |
| 96 | 98 |
| 97 // Insert |this| into the linked list, after |e|. | 99 // Insert |this| into the linked list, after |e|. |
| 98 void InsertAfter(LinkNode<T>* e) { | 100 void InsertAfter(LinkNode<T>* e) { |
| 99 this->next_ = e->next_; | 101 this->next_ = e->next_; |
| 100 this->previous_ = e; | 102 this->previous_ = e; |
| 101 e->next_->previous_ = this; | 103 e->next_->previous_ = this; |
| 102 e->next_ = this; | 104 e->next_ = this; |
| 103 } | 105 } |
| 104 | 106 |
| 105 // Remove |this| from the linked list. | 107 // Remove |this| from the linked list. |
| 106 void RemoveFromList() { | 108 void RemoveFromList() { |
| 107 this->previous_->next_ = this->next_; | 109 this->previous_->next_ = this->next_; |
| 108 this->next_->previous_ = this->previous_; | 110 this->next_->previous_ = this->previous_; |
| 111 // next() and previous() return non-NULL if and only this node is not in any |
| 112 // list. |
| 113 this->next_ = NULL; |
| 114 this->previous_ = NULL; |
| 109 } | 115 } |
| 110 | 116 |
| 111 LinkNode<T>* previous() const { | 117 LinkNode<T>* previous() const { |
| 112 return previous_; | 118 return previous_; |
| 113 } | 119 } |
| 114 | 120 |
| 115 LinkNode<T>* next() const { | 121 LinkNode<T>* next() const { |
| 116 return next_; | 122 return next_; |
| 117 } | 123 } |
| 118 | 124 |
| 119 // Cast from the node-type to the value type. | 125 // Cast from the node-type to the value type. |
| 120 const T* value() const { | 126 const T* value() const { |
| 121 return static_cast<const T*>(this); | 127 return static_cast<const T*>(this); |
| 122 } | 128 } |
| 123 | 129 |
| 124 T* value() { | 130 T* value() { |
| 125 return static_cast<T*>(this); | 131 return static_cast<T*>(this); |
| 126 } | 132 } |
| 127 | 133 |
| 128 private: | 134 private: |
| 129 LinkNode<T>* previous_; | 135 LinkNode<T>* previous_; |
| 130 LinkNode<T>* next_; | 136 LinkNode<T>* next_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(LinkNode); |
| 131 }; | 139 }; |
| 132 | 140 |
| 133 template <typename T> | 141 template <typename T> |
| 134 class LinkedList { | 142 class LinkedList { |
| 135 public: | 143 public: |
| 136 // The "root" node is self-referential, and forms the basis of a circular | 144 // The "root" node is self-referential, and forms the basis of a circular |
| 137 // list (root_.next() will point back to the start of the list, | 145 // list (root_.next() will point back to the start of the list, |
| 138 // and root_->previous() wraps around to the end of the list). | 146 // and root_->previous() wraps around to the end of the list). |
| 139 LinkedList() : root_(&root_, &root_) {} | 147 LinkedList() : root_(&root_, &root_) {} |
| 140 | 148 |
| 141 // Appends |e| to the end of the linked list. | 149 // Appends |e| to the end of the linked list. |
| 142 void Append(LinkNode<T>* e) { | 150 void Append(LinkNode<T>* e) { |
| 143 e->InsertBefore(&root_); | 151 e->InsertBefore(&root_); |
| 144 } | 152 } |
| 145 | 153 |
| 146 LinkNode<T>* head() const { | 154 LinkNode<T>* head() const { |
| 147 return root_.next(); | 155 return root_.next(); |
| 148 } | 156 } |
| 149 | 157 |
| 150 LinkNode<T>* tail() const { | 158 LinkNode<T>* tail() const { |
| 151 return root_.previous(); | 159 return root_.previous(); |
| 152 } | 160 } |
| 153 | 161 |
| 154 const LinkNode<T>* end() const { | 162 const LinkNode<T>* end() const { |
| 155 return &root_; | 163 return &root_; |
| 156 } | 164 } |
| 157 | 165 |
| 166 bool empty() const { return head() == end(); } |
| 167 |
| 158 private: | 168 private: |
| 159 LinkNode<T> root_; | 169 LinkNode<T> root_; |
| 170 |
| 171 DISALLOW_COPY_AND_ASSIGN(LinkedList); |
| 160 }; | 172 }; |
| 161 | 173 |
| 162 } // namespace base | 174 } // namespace base |
| 163 | 175 |
| 164 #endif // BASE_CONTAINERS_LINKED_LIST_H_ | 176 #endif // BASE_CONTAINERS_LINKED_LIST_H_ |
| OLD | NEW |