linked_list.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifndef BASE_CONTAINERS_LINKED_LIST_H_
  5. #define BASE_CONTAINERS_LINKED_LIST_H_
  6. #include "base/check_op.h"
  7. // Simple LinkedList type. (See the Q&A section to understand how this
  8. // differs from std::list).
  9. //
  10. // 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. //
  13. // class MyNodeType : public LinkNode<MyNodeType> {
  14. // ...
  15. // };
  16. //
  17. // Next, to keep track of the list's head/tail, use a LinkedList instance:
  18. //
  19. // LinkedList<MyNodeType> list;
  20. //
  21. // To add elements to the list, use any of LinkedList::Append,
  22. // LinkNode::InsertBefore, or LinkNode::InsertAfter:
  23. //
  24. // LinkNode<MyNodeType>* n1 = ...;
  25. // LinkNode<MyNodeType>* n2 = ...;
  26. // LinkNode<MyNodeType>* n3 = ...;
  27. //
  28. // list.Append(n1);
  29. // list.Append(n3);
  30. // n2->InsertBefore(n3);
  31. //
  32. // Lastly, to iterate through the linked list forwards:
  33. //
  34. // for (LinkNode<MyNodeType>* node = list.head();
  35. // node != list.end();
  36. // node = node->next()) {
  37. // MyNodeType* value = node->value();
  38. // ...
  39. // }
  40. //
  41. // Or to iterate the linked list backwards:
  42. //
  43. // for (LinkNode<MyNodeType>* node = list.tail();
  44. // node != list.end();
  45. // node = node->previous()) {
  46. // MyNodeType* value = node->value();
  47. // ...
  48. // }
  49. //
  50. // Questions and Answers:
  51. //
  52. // Q. Should I use std::list or base::LinkedList?
  53. //
  54. // A. The main reason to use base::LinkedList over std::list is
  55. // performance. If you don't care about the performance differences
  56. // then use an STL container, as it makes for better code readability.
  57. //
  58. // Comparing the performance of base::LinkedList<T> to std::list<T*>:
  59. //
  60. // * Erasing an element of type T* from base::LinkedList<T> is
  61. // an O(1) operation. Whereas for std::list<T*> it is O(n).
  62. // That is because with std::list<T*> you must obtain an
  63. // iterator to the T* element before you can call erase(iterator).
  64. //
  65. // * Insertion operations with base::LinkedList<T> never require
  66. // heap allocations.
  67. //
  68. // Q. How does base::LinkedList implementation differ from std::list?
  69. //
  70. // A. Doubly-linked lists are made up of nodes that contain "next" and
  71. // "previous" pointers that reference other nodes in the list.
  72. //
  73. // With base::LinkedList<T>, the type being inserted already reserves
  74. // space for the "next" and "previous" pointers (base::LinkNode<T>*).
  75. // Whereas with std::list<T> the type can be anything, so the implementation
  76. // needs to glue on the "next" and "previous" pointers using
  77. // some internal node type.
  78. namespace base {
  79. namespace internal {
  80. // Base class for LinkNode<T> type
  81. class BASE_EXPORT LinkNodeBase {
  82. public:
  83. void RemoveFromList();
  84. protected:
  85. LinkNodeBase();
  86. LinkNodeBase(LinkNodeBase* previous, LinkNodeBase* next);
  87. LinkNodeBase(LinkNodeBase&& rhs);
  88. LinkNodeBase(const LinkNodeBase&) = delete;
  89. ~LinkNodeBase() = default;
  90. LinkNodeBase& operator=(const LinkNodeBase&) = delete;
  91. // Calling these with |e| as a different LinkNode type as |this| is
  92. // unsafe. These are protected and only called from LinkNode<T> to
  93. // ensure safety.
  94. void InsertBeforeBase(LinkNodeBase* e);
  95. void InsertAfterBase(LinkNodeBase* e);
  96. LinkNodeBase* previous_base() const { return previous_; }
  97. LinkNodeBase* next_base() const { return next_; }
  98. private:
  99. LinkNodeBase* previous_ = nullptr;
  100. LinkNodeBase* next_ = nullptr;
  101. };
  102. } // namespace internal
  103. template <typename T>
  104. class LinkNode : public internal::LinkNodeBase {
  105. public:
  106. LinkNode() = default;
  107. LinkNode(LinkNode<T>* previous, LinkNode<T>* next)
  108. : internal::LinkNodeBase(previous, next) {}
  109. LinkNode(LinkNode<T>&&) = default;
  110. LinkNode(const LinkNode&) = delete;
  111. LinkNode& operator=(const LinkNode&) = delete;
  112. // Insert |this| into the linked list, before |e|. |this| must not
  113. // already be in a list.
  114. void InsertBefore(LinkNode<T>* e) { InsertBeforeBase(e); }
  115. // Insert |this| into the linked list, after |e|. |this| must not
  116. // already be in a list.
  117. void InsertAfter(LinkNode<T>* e) { InsertAfterBase(e); }
  118. LinkNode<T>* previous() const {
  119. return static_cast<LinkNode<T>*>(previous_base());
  120. }
  121. LinkNode<T>* next() const { return static_cast<LinkNode<T>*>(next_base()); }
  122. // Cast from the node-type to the value type.
  123. const T* value() const {
  124. return static_cast<const T*>(this);
  125. }
  126. T* value() {
  127. return static_cast<T*>(this);
  128. }
  129. };
  130. template <typename T>
  131. class LinkedList {
  132. public:
  133. // The "root" node is self-referential, and forms the basis of a circular
  134. // list (root_.next() will point back to the start of the list,
  135. // and root_->previous() wraps around to the end of the list).
  136. LinkedList() : root_(&root_, &root_) {}
  137. LinkedList(const LinkedList&) = delete;
  138. LinkedList& operator=(const LinkedList&) = delete;
  139. // Appends |e| to the end of the linked list.
  140. void Append(LinkNode<T>* e) {
  141. e->InsertBefore(&root_);
  142. }
  143. LinkNode<T>* head() const {
  144. return root_.next();
  145. }
  146. LinkNode<T>* tail() const {
  147. return root_.previous();
  148. }
  149. const LinkNode<T>* end() const {
  150. return &root_;
  151. }
  152. bool empty() const { return head() == end(); }
  153. private:
  154. LinkNode<T> root_;
  155. };
  156. } // namespace base
  157. #endif // BASE_CONTAINERS_LINKED_LIST_H_