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_LINKED_LIST_H_ | 5 #ifndef BASE_LINKED_LIST_H_ |
6 #define BASE_LINKED_LIST_H_ | 6 #define BASE_LINKED_LIST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 // Simple LinkedList type. (See the Q&A section to understand how this | 9 // Simple LinkedList type. (See the Q&A section to understand how this |
10 // differs from std::list). | 10 // differs from std::list). |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 | 119 |
120 // Cast from the node-type to the value type. | 120 // Cast from the node-type to the value type. |
121 const T* value() const { | 121 const T* value() const { |
122 return static_cast<const T*>(this); | 122 return static_cast<const T*>(this); |
123 } | 123 } |
124 | 124 |
125 T* value() { | 125 T* value() { |
126 return static_cast<T*>(this); | 126 return static_cast<T*>(this); |
127 } | 127 } |
128 | 128 |
129 // Work around bug reported upstream: | |
130 // http://llvm.org/bugs/show_bug.cgi?id=7974 | |
131 void set(LinkNode<T>* prev, LinkNode<T>* next) { | |
132 previous_ = prev; next_ = next; | |
hans
2010/09/20 08:11:49
Nit: two statements on a single row like this is q
| |
133 } | |
134 | |
129 private: | 135 private: |
130 LinkNode<T>* previous_; | 136 LinkNode<T>* previous_; |
131 LinkNode<T>* next_; | 137 LinkNode<T>* next_; |
132 }; | 138 }; |
133 | 139 |
134 template <typename T> | 140 template <typename T> |
135 class LinkedList { | 141 class LinkedList { |
136 public: | 142 public: |
137 // The "root" node is self-referential, and forms the basis of a circular | 143 // The "root" node is self-referential, and forms the basis of a circular |
138 // list (root_.next() will point back to the start of the list, | 144 // list (root_.next() will point back to the start of the list, |
139 // and root_->previous() wraps around to the end of the list). | 145 // and root_->previous() wraps around to the end of the list). |
140 LinkedList() : root_(&root_, &root_) {} | 146 LinkedList() { root_.set(&root_, &root_); } |
141 | 147 |
142 // Appends |e| to the end of the linked list. | 148 // Appends |e| to the end of the linked list. |
143 void Append(LinkNode<T>* e) { | 149 void Append(LinkNode<T>* e) { |
144 e->InsertBefore(&root_); | 150 e->InsertBefore(&root_); |
145 } | 151 } |
146 | 152 |
147 LinkNode<T>* head() const { | 153 LinkNode<T>* head() const { |
148 return root_.next(); | 154 return root_.next(); |
149 } | 155 } |
150 | 156 |
151 LinkNode<T>* tail() const { | 157 LinkNode<T>* tail() const { |
152 return root_.previous(); | 158 return root_.previous(); |
153 } | 159 } |
154 | 160 |
155 const LinkNode<T>* end() const { | 161 const LinkNode<T>* end() const { |
156 return &root_; | 162 return &root_; |
157 } | 163 } |
158 | 164 |
159 private: | 165 private: |
160 LinkNode<T> root_; | 166 LinkNode<T> root_; |
161 }; | 167 }; |
162 | 168 |
163 } // namespace base | 169 } // namespace base |
164 | 170 |
165 #endif // BASE_LINKED_LIST_H_ | 171 #endif // BASE_LINKED_LIST_H_ |
OLD | NEW |