Chromium Code Reviews| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 this->next_ = NULL; | |
| 112 this->previous_ = NULL; | |
|
tyoshino (SeeGerritForStatus)
2014/05/22 06:04:01
use 0 or NULL for both initializer list and here?
Adam Rice
2014/05/22 06:49:07
Okay, I changed the initialisers to use NULL for s
| |
| 109 } | 113 } |
| 110 | 114 |
| 111 LinkNode<T>* previous() const { | 115 LinkNode<T>* previous() const { |
| 112 return previous_; | 116 return previous_; |
| 113 } | 117 } |
| 114 | 118 |
| 115 LinkNode<T>* next() const { | 119 LinkNode<T>* next() const { |
| 116 return next_; | 120 return next_; |
| 117 } | 121 } |
| 118 | 122 |
| 119 // Cast from the node-type to the value type. | 123 // Cast from the node-type to the value type. |
| 120 const T* value() const { | 124 const T* value() const { |
| 121 return static_cast<const T*>(this); | 125 return static_cast<const T*>(this); |
| 122 } | 126 } |
| 123 | 127 |
| 124 T* value() { | 128 T* value() { |
| 125 return static_cast<T*>(this); | 129 return static_cast<T*>(this); |
| 126 } | 130 } |
| 127 | 131 |
| 128 private: | 132 private: |
| 129 LinkNode<T>* previous_; | 133 LinkNode<T>* previous_; |
| 130 LinkNode<T>* next_; | 134 LinkNode<T>* next_; |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(LinkNode); | |
| 131 }; | 137 }; |
| 132 | 138 |
| 133 template <typename T> | 139 template <typename T> |
| 134 class LinkedList { | 140 class LinkedList { |
| 135 public: | 141 public: |
| 136 // The "root" node is self-referential, and forms the basis of a circular | 142 // 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, | 143 // list (root_.next() will point back to the start of the list, |
| 138 // and root_->previous() wraps around to the end of the list). | 144 // and root_->previous() wraps around to the end of the list). |
| 139 LinkedList() : root_(&root_, &root_) {} | 145 LinkedList() : root_(&root_, &root_) {} |
| 140 | 146 |
| 141 // Appends |e| to the end of the linked list. | 147 // Appends |e| to the end of the linked list. |
| 142 void Append(LinkNode<T>* e) { | 148 void Append(LinkNode<T>* e) { |
| 143 e->InsertBefore(&root_); | 149 e->InsertBefore(&root_); |
| 144 } | 150 } |
| 145 | 151 |
| 146 LinkNode<T>* head() const { | 152 LinkNode<T>* head() const { |
| 147 return root_.next(); | 153 return root_.next(); |
| 148 } | 154 } |
| 149 | 155 |
| 150 LinkNode<T>* tail() const { | 156 LinkNode<T>* tail() const { |
| 151 return root_.previous(); | 157 return root_.previous(); |
| 152 } | 158 } |
| 153 | 159 |
| 154 const LinkNode<T>* end() const { | 160 const LinkNode<T>* end() const { |
| 155 return &root_; | 161 return &root_; |
| 156 } | 162 } |
| 157 | 163 |
| 164 bool empty() const { return head() == end(); } | |
| 165 | |
| 158 private: | 166 private: |
| 159 LinkNode<T> root_; | 167 LinkNode<T> root_; |
| 168 | |
| 169 DISALLOW_COPY_AND_ASSIGN(LinkedList); | |
| 160 }; | 170 }; |
| 161 | 171 |
| 162 } // namespace base | 172 } // namespace base |
| 163 | 173 |
| 164 #endif // BASE_CONTAINERS_LINKED_LIST_H_ | 174 #endif // BASE_CONTAINERS_LINKED_LIST_H_ |
| OLD | NEW |