ilist_node.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_UTIL_ILIST_NODE_H_
  15. #define SOURCE_UTIL_ILIST_NODE_H_
  16. #include <cassert>
  17. namespace spvtools {
  18. namespace utils {
  19. template <class NodeType>
  20. class IntrusiveList;
  21. // IntrusiveNodeBase is the base class for nodes in an IntrusiveList.
  22. // See the comments in ilist.h on how to use the class.
  23. template <class NodeType>
  24. class IntrusiveNodeBase {
  25. public:
  26. // Creates a new node that is not in a list.
  27. inline IntrusiveNodeBase();
  28. inline IntrusiveNodeBase(const IntrusiveNodeBase&);
  29. inline IntrusiveNodeBase& operator=(const IntrusiveNodeBase&);
  30. inline IntrusiveNodeBase(IntrusiveNodeBase&& that);
  31. // Destroys a node. It is an error to destroy a node that is part of a
  32. // list, unless it is a sentinel.
  33. virtual ~IntrusiveNodeBase();
  34. IntrusiveNodeBase& operator=(IntrusiveNodeBase&& that);
  35. // Returns true if |this| is in a list.
  36. inline bool IsInAList() const;
  37. // Returns the node that comes after the given node in the list, if one
  38. // exists. If the given node is not in a list or is at the end of the list,
  39. // the return value is nullptr.
  40. inline NodeType* NextNode() const;
  41. // Returns the node that comes before the given node in the list, if one
  42. // exists. If the given node is not in a list or is at the start of the
  43. // list, the return value is nullptr.
  44. inline NodeType* PreviousNode() const;
  45. // Inserts the given node immediately before |pos| in the list.
  46. // If the given node is already in a list, it will first be removed
  47. // from that list.
  48. //
  49. // It is assumed that the given node is of type NodeType. It is an error if
  50. // |pos| is not already in a list.
  51. inline void InsertBefore(NodeType* pos);
  52. // Inserts the given node immediately after |pos| in the list.
  53. // If the given node is already in a list, it will first be removed
  54. // from that list.
  55. //
  56. // It is assumed that the given node is of type NodeType. It is an error if
  57. // |pos| is not already in a list, or if |pos| is equal to |this|.
  58. inline void InsertAfter(NodeType* pos);
  59. // Removes the given node from the list. It is assumed that the node is
  60. // in a list. Note that this does not free any storage related to the node,
  61. // it becomes the caller's responsibility to free the storage.
  62. inline void RemoveFromList();
  63. protected:
  64. // Replaces |this| with |target|. |this| is a sentinel if and only if
  65. // |target| is also a sentinel.
  66. //
  67. // If neither node is a sentinel, |target| takes
  68. // the place of |this|. It is assumed that |target| is not in a list.
  69. //
  70. // If both are sentinels, then it will cause all of the
  71. // nodes in the list containing |this| to be moved to the list containing
  72. // |target|. In this case, it is assumed that |target| is an empty list.
  73. //
  74. // No storage will be deleted.
  75. void ReplaceWith(NodeType* target);
  76. // Returns true if |this| is the sentinel node of an empty list.
  77. bool IsEmptyList();
  78. // The pointers to the next and previous nodes in the list.
  79. // If the current node is not part of a list, then |next_node_| and
  80. // |previous_node_| are equal to |nullptr|.
  81. NodeType* next_node_;
  82. NodeType* previous_node_;
  83. // Only true for the sentinel node stored in the list itself.
  84. bool is_sentinel_;
  85. friend IntrusiveList<NodeType>;
  86. };
  87. // Implementation of IntrusiveNodeBase
  88. template <class NodeType>
  89. inline IntrusiveNodeBase<NodeType>::IntrusiveNodeBase()
  90. : next_node_(nullptr), previous_node_(nullptr), is_sentinel_(false) {}
  91. template <class NodeType>
  92. inline IntrusiveNodeBase<NodeType>::IntrusiveNodeBase(
  93. const IntrusiveNodeBase&) {
  94. next_node_ = nullptr;
  95. previous_node_ = nullptr;
  96. is_sentinel_ = false;
  97. }
  98. template <class NodeType>
  99. inline IntrusiveNodeBase<NodeType>& IntrusiveNodeBase<NodeType>::operator=(
  100. const IntrusiveNodeBase&) {
  101. assert(!is_sentinel_);
  102. if (IsInAList()) {
  103. RemoveFromList();
  104. }
  105. return *this;
  106. }
  107. template <class NodeType>
  108. inline IntrusiveNodeBase<NodeType>::IntrusiveNodeBase(IntrusiveNodeBase&& that)
  109. : next_node_(nullptr),
  110. previous_node_(nullptr),
  111. is_sentinel_(that.is_sentinel_) {
  112. if (is_sentinel_) {
  113. next_node_ = this;
  114. previous_node_ = this;
  115. }
  116. that.ReplaceWith(this);
  117. }
  118. template <class NodeType>
  119. IntrusiveNodeBase<NodeType>::~IntrusiveNodeBase() {
  120. assert(is_sentinel_ || !IsInAList());
  121. }
  122. template <class NodeType>
  123. IntrusiveNodeBase<NodeType>& IntrusiveNodeBase<NodeType>::operator=(
  124. IntrusiveNodeBase&& that) {
  125. that.ReplaceWith(this);
  126. return *this;
  127. }
  128. template <class NodeType>
  129. inline bool IntrusiveNodeBase<NodeType>::IsInAList() const {
  130. return next_node_ != nullptr;
  131. }
  132. template <class NodeType>
  133. inline NodeType* IntrusiveNodeBase<NodeType>::NextNode() const {
  134. if (!next_node_->is_sentinel_) return next_node_;
  135. return nullptr;
  136. }
  137. template <class NodeType>
  138. inline NodeType* IntrusiveNodeBase<NodeType>::PreviousNode() const {
  139. if (!previous_node_->is_sentinel_) return previous_node_;
  140. return nullptr;
  141. }
  142. template <class NodeType>
  143. inline void IntrusiveNodeBase<NodeType>::InsertBefore(NodeType* pos) {
  144. assert(!this->is_sentinel_ && "Sentinel nodes cannot be moved around.");
  145. assert(pos->IsInAList() && "Pos should already be in a list.");
  146. if (this->IsInAList()) this->RemoveFromList();
  147. this->next_node_ = pos;
  148. this->previous_node_ = pos->previous_node_;
  149. pos->previous_node_ = static_cast<NodeType*>(this);
  150. this->previous_node_->next_node_ = static_cast<NodeType*>(this);
  151. }
  152. template <class NodeType>
  153. inline void IntrusiveNodeBase<NodeType>::InsertAfter(NodeType* pos) {
  154. assert(!this->is_sentinel_ && "Sentinel nodes cannot be moved around.");
  155. assert(pos->IsInAList() && "Pos should already be in a list.");
  156. assert(this != pos && "Can't insert a node after itself.");
  157. if (this->IsInAList()) {
  158. this->RemoveFromList();
  159. }
  160. this->previous_node_ = pos;
  161. this->next_node_ = pos->next_node_;
  162. pos->next_node_ = static_cast<NodeType*>(this);
  163. this->next_node_->previous_node_ = static_cast<NodeType*>(this);
  164. }
  165. template <class NodeType>
  166. inline void IntrusiveNodeBase<NodeType>::RemoveFromList() {
  167. assert(!this->is_sentinel_ && "Sentinel nodes cannot be moved around.");
  168. assert(this->IsInAList() &&
  169. "Cannot remove a node from a list if it is not in a list.");
  170. this->next_node_->previous_node_ = this->previous_node_;
  171. this->previous_node_->next_node_ = this->next_node_;
  172. this->next_node_ = nullptr;
  173. this->previous_node_ = nullptr;
  174. }
  175. template <class NodeType>
  176. void IntrusiveNodeBase<NodeType>::ReplaceWith(NodeType* target) {
  177. if (this->is_sentinel_) {
  178. assert(target->IsEmptyList() &&
  179. "If target is not an empty list, the nodes in that list would not "
  180. "be linked to a sentinel.");
  181. } else {
  182. assert(IsInAList() && "The node being replaced must be in a list.");
  183. assert(!target->is_sentinel_ &&
  184. "Cannot turn a sentinel node into one that is not.");
  185. }
  186. if (!this->IsEmptyList()) {
  187. // Link target into the same position that |this| was in.
  188. target->next_node_ = this->next_node_;
  189. target->previous_node_ = this->previous_node_;
  190. target->next_node_->previous_node_ = target;
  191. target->previous_node_->next_node_ = target;
  192. // Reset |this| to itself default value.
  193. if (!this->is_sentinel_) {
  194. // Reset |this| so that it is not in a list.
  195. this->next_node_ = nullptr;
  196. this->previous_node_ = nullptr;
  197. } else {
  198. // Set |this| so that it is the head of an empty list.
  199. // We cannot treat sentinel nodes like others because it is invalid for
  200. // a sentinel node to not be in a list.
  201. this->next_node_ = static_cast<NodeType*>(this);
  202. this->previous_node_ = static_cast<NodeType*>(this);
  203. }
  204. } else {
  205. // If |this| points to itself, it must be a sentinel node with an empty
  206. // list. Reset |this| so that it is the head of an empty list. We want
  207. // |target| to be the same. The asserts above guarantee that.
  208. }
  209. }
  210. template <class NodeType>
  211. bool IntrusiveNodeBase<NodeType>::IsEmptyList() {
  212. if (next_node_ == this) {
  213. assert(is_sentinel_ &&
  214. "None sentinel nodes should never point to themselves.");
  215. assert(previous_node_ == this &&
  216. "Inconsistency with the previous and next nodes.");
  217. return true;
  218. }
  219. return false;
  220. }
  221. } // namespace utils
  222. } // namespace spvtools
  223. #endif // SOURCE_UTIL_ILIST_NODE_H_