Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: base/linked_list.h

Issue 179060: Add a comment explaining how base::LinkedList differs from std::list, to help... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix an indentation Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 7
8 // Simple LinkedList type. 8 // Simple LinkedList type. (See the Q&A section to understand how this
9 // differs from std::list).
9 // 10 //
10 // To use, start by declaring the class which will be contained in the linked 11 // To use, start by declaring the class which will be contained in the linked
11 // list, as extending LinkNode (this gives it next/previous pointers). 12 // list, as extending LinkNode (this gives it next/previous pointers).
12 // 13 //
13 // class MyNodeType : public LinkNode<MyNodeType> { 14 // class MyNodeType : public LinkNode<MyNodeType> {
14 // ... 15 // ...
15 // }; 16 // };
16 // 17 //
17 // Next, to keep track of the list's head/tail, use a LinkedList instance: 18 // Next, to keep track of the list's head/tail, use a LinkedList instance:
18 // 19 //
(...skipping 21 matching lines...) Expand all
40 // 41 //
41 // Or to iterate the linked list backwards: 42 // Or to iterate the linked list backwards:
42 // 43 //
43 // for (LinkNode<MyNodeType>* node = list.tail(); 44 // for (LinkNode<MyNodeType>* node = list.tail();
44 // node != list.end(); 45 // node != list.end();
45 // node = node->previous()) { 46 // node = node->previous()) {
46 // MyNodeType* value = node->value(); 47 // MyNodeType* value = node->value();
47 // ... 48 // ...
48 // } 49 // }
49 // 50 //
51 // Questions and Answers:
52 //
53 // Q. Should I use std::list or base::LinkedList?
54 //
55 // A. The main reason to use base::LinkedList over std::list is
56 // performance. If you don't care about the performance differences
57 // then use an STL container, as it makes for better code readability.
58 //
59 // Comparing the performance of base::LinkedList<T> to std::list<T*>:
60 //
61 // * Erasing an element of type T* from base::LinkedList<T> is
62 // an O(1) operation. Whereas for std::list<T*> it is O(n).
63 // That is because with std::list<T*> you must obtain an
64 // iterator to the T* element before you can call erase(iterator).
65 //
66 // * Insertion operations with base::LinkedList<T> never require
67 // heap allocations.
68 //
69 // Q. How does base::LinkedList implementation differ from std::list?
70 //
71 // A. Doubly-linked lists are made up of nodes that contain "next" and
72 // "previous" pointers that reference other nodes in the list.
73 //
74 // With base::LinkedList<T>, the type being inserted already reserves
75 // space for the "next" and "previous" pointers (base::LinkNode<T>*).
76 // Whereas with std::list<T> the type can be anything, so the implementation
77 // needs to glue on the "next" and "previous" pointers using
78 // some internal node type.
50 79
51 namespace base { 80 namespace base {
52 81
53 template <typename T> 82 template <typename T>
54 class LinkNode { 83 class LinkNode {
55 public: 84 public:
56 LinkNode() : previous_(0), next_(0) {} 85 LinkNode() : previous_(0), next_(0) {}
57 LinkNode(LinkNode<T>* previous, LinkNode<T>* next) 86 LinkNode(LinkNode<T>* previous, LinkNode<T>* next)
58 : previous_(previous), next_(next) {} 87 : previous_(previous), next_(next) {}
59 88
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 return &root_; 155 return &root_;
127 } 156 }
128 157
129 private: 158 private:
130 LinkNode<T> root_; 159 LinkNode<T> root_;
131 }; 160 };
132 161
133 } // namespace base 162 } // namespace base
134 163
135 #endif // BASE_LINKED_LIST_H_ 164 #endif // BASE_LINKED_LIST_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698