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
3 // found in the LICENSE file.
4
5 // This file is a copy of Chromium's /src/base/containers/linked_list.h with the following
6 // modifications:
7 // - Added iterators for ranged based iterations
8 // - Added in list check before removing node to prevent segfault, now returns true iff removed
9 // - Added MoveInto functionality for moving list elements to another list
10
11 #ifndef COMMON_LINKED_LIST_H
12 #define COMMON_LINKED_LIST_H
13
14 #include "common/Assert.h"
15
16 // Simple LinkedList type. (See the Q&A section to understand how this
17 // differs from std::list).
18 //
19 // To use, start by declaring the class which will be contained in the linked
20 // list, as extending LinkNode (this gives it next/previous pointers).
21 //
22 // class MyNodeType : public LinkNode<MyNodeType> {
23 // ...
24 // };
25 //
26 // Next, to keep track of the list's head/tail, use a LinkedList instance:
27 //
28 // LinkedList<MyNodeType> list;
29 //
30 // To add elements to the list, use any of LinkedList::Append,
31 // LinkNode::InsertBefore, or LinkNode::InsertAfter:
32 //
33 // LinkNode<MyNodeType>* n1 = ...;
34 // LinkNode<MyNodeType>* n2 = ...;
35 // LinkNode<MyNodeType>* n3 = ...;
36 //
37 // list.Append(n1);
38 // list.Append(n3);
39 // n3->InsertBefore(n3);
40 //
41 // Lastly, to iterate through the linked list forwards:
42 //
43 // for (LinkNode<MyNodeType>* node = list.head();
44 // node != list.end();
45 // node = node->next()) {
46 // MyNodeType* value = node->value();
47 // ...
48 // }
49 //
50 // for (LinkNode<MyNodeType*> node : list) {
51 // MyNodeType* value = node->value();
52 // ...
53 // }
54 //
55 // Or to iterate the linked list backwards:
56 //
57 // for (LinkNode<MyNodeType>* node = list.tail();
58 // node != list.end();
59 // node = node->previous()) {
60 // MyNodeType* value = node->value();
61 // ...
62 // }
63 //
64 // Questions and Answers:
65 //
66 // Q. Should I use std::list or base::LinkedList?
67 //
68 // A. The main reason to use base::LinkedList over std::list is
69 // performance. If you don't care about the performance differences
70 // then use an STL container, as it makes for better code readability.
71 //
72 // Comparing the performance of base::LinkedList<T> to std::list<T*>:
73 //
74 // * Erasing an element of type T* from base::LinkedList<T> is
75 // an O(1) operation. Whereas for std::list<T*> it is O(n).
76 // That is because with std::list<T*> you must obtain an
77 // iterator to the T* element before you can call erase(iterator).
78 //
79 // * Insertion operations with base::LinkedList<T> never require
80 // heap allocations.
81 //
82 // Q. How does base::LinkedList implementation differ from std::list?
83 //
84 // A. Doubly-linked lists are made up of nodes that contain "next" and
85 // "previous" pointers that reference other nodes in the list.
86 //
87 // With base::LinkedList<T>, the type being inserted already reserves
88 // space for the "next" and "previous" pointers (base::LinkNode<T>*).
89 // Whereas with std::list<T> the type can be anything, so the implementation
90 // needs to glue on the "next" and "previous" pointers using
91 // some internal node type.
92
93 // Forward declarations of the types in order for recursive referencing and friending.
94 template <typename T>
95 class LinkNode;
96 template <typename T>
97 class LinkedList;
98
99 template <typename T>
100 class LinkNode {
101 public:
LinkNode()102 LinkNode() : previous_(nullptr), next_(nullptr) {
103 }
LinkNode(LinkNode<T> * previous,LinkNode<T> * next)104 LinkNode(LinkNode<T>* previous, LinkNode<T>* next) : previous_(previous), next_(next) {
105 }
106
LinkNode(LinkNode<T> && rhs)107 LinkNode(LinkNode<T>&& rhs) {
108 next_ = rhs.next_;
109 rhs.next_ = nullptr;
110 previous_ = rhs.previous_;
111 rhs.previous_ = nullptr;
112
113 // If the node belongs to a list, next_ and previous_ are both non-null.
114 // Otherwise, they are both null.
115 if (next_) {
116 next_->previous_ = this;
117 previous_->next_ = this;
118 }
119 }
120
121 // Insert |this| into the linked list, before |e|.
InsertBefore(LinkNode<T> * e)122 void InsertBefore(LinkNode<T>* e) {
123 this->next_ = e;
124 this->previous_ = e->previous_;
125 e->previous_->next_ = this;
126 e->previous_ = this;
127 }
128
129 // Insert |this| into the linked list, after |e|.
InsertAfter(LinkNode<T> * e)130 void InsertAfter(LinkNode<T>* e) {
131 this->next_ = e->next_;
132 this->previous_ = e;
133 e->next_->previous_ = this;
134 e->next_ = this;
135 }
136
137 // Check if |this| is in a list.
IsInList()138 bool IsInList() const {
139 ASSERT((this->previous_ == nullptr) == (this->next_ == nullptr));
140 return this->next_ != nullptr;
141 }
142
143 // Remove |this| from the linked list. Returns true iff removed from a list.
RemoveFromList()144 bool RemoveFromList() {
145 if (!IsInList()) {
146 return false;
147 }
148
149 this->previous_->next_ = this->next_;
150 this->next_->previous_ = this->previous_;
151 // next() and previous() return non-null if and only this node is not in any list.
152 this->next_ = nullptr;
153 this->previous_ = nullptr;
154 return true;
155 }
156
previous()157 LinkNode<T>* previous() const {
158 return previous_;
159 }
160
next()161 LinkNode<T>* next() const {
162 return next_;
163 }
164
165 // Cast from the node-type to the value type.
value()166 const T* value() const {
167 return static_cast<const T*>(this);
168 }
169
value()170 T* value() {
171 return static_cast<T*>(this);
172 }
173
174 private:
175 friend class LinkedList<T>;
176 LinkNode<T>* previous_;
177 LinkNode<T>* next_;
178 };
179
180 template <typename T>
181 class LinkedList {
182 public:
183 // The "root" node is self-referential, and forms the basis of a circular
184 // list (root_.next() will point back to the start of the list,
185 // and root_->previous() wraps around to the end of the list).
LinkedList()186 LinkedList() : root_(&root_, &root_) {
187 }
188
~LinkedList()189 ~LinkedList() {
190 // If any LinkNodes still exist in the LinkedList, there will be outstanding references to
191 // root_ even after it has been freed. We should remove root_ from the list to prevent any
192 // future access.
193 root_.RemoveFromList();
194 }
195
196 // Appends |e| to the end of the linked list.
Append(LinkNode<T> * e)197 void Append(LinkNode<T>* e) {
198 e->InsertBefore(&root_);
199 }
200
201 // Moves all elements (in order) of the list and appends them into |l| leaving the list empty.
MoveInto(LinkedList<T> * l)202 void MoveInto(LinkedList<T>* l) {
203 if (empty()) {
204 return;
205 }
206 l->root_.previous_->next_ = root_.next_;
207 root_.next_->previous_ = l->root_.previous_;
208 l->root_.previous_ = root_.previous_;
209 root_.previous_->next_ = &l->root_;
210
211 root_.next_ = &root_;
212 root_.previous_ = &root_;
213 }
214
head()215 LinkNode<T>* head() const {
216 return root_.next();
217 }
218
tail()219 LinkNode<T>* tail() const {
220 return root_.previous();
221 }
222
end()223 const LinkNode<T>* end() const {
224 return &root_;
225 }
226
empty()227 bool empty() const {
228 return head() == end();
229 }
230
231 private:
232 LinkNode<T> root_;
233 };
234
235 template <typename T>
236 class LinkedListIterator {
237 public:
LinkedListIterator(LinkNode<T> * node)238 LinkedListIterator(LinkNode<T>* node) : current_(node), next_(node->next()) {
239 }
240
241 // We keep an early reference to the next node in the list so that even if the current element
242 // is modified or removed from the list, we have a valid next node.
243 LinkedListIterator<T> const& operator++() {
244 current_ = next_;
245 next_ = current_->next();
246 return *this;
247 }
248
249 bool operator!=(const LinkedListIterator<T>& other) const {
250 return current_ != other.current_;
251 }
252
253 LinkNode<T>* operator*() const {
254 return current_;
255 }
256
257 private:
258 LinkNode<T>* current_;
259 LinkNode<T>* next_;
260 };
261
262 template <typename T>
begin(LinkedList<T> & l)263 LinkedListIterator<T> begin(LinkedList<T>& l) {
264 return LinkedListIterator<T>(l.head());
265 }
266
267 // Free end function does't use LinkedList<T>::end because of it's const nature. Instead we wrap
268 // around from tail.
269 template <typename T>
end(LinkedList<T> & l)270 LinkedListIterator<T> end(LinkedList<T>& l) {
271 return LinkedListIterator<T>(l.tail()->next());
272 }
273
274 #endif // COMMON_LINKED_LIST_H
275