1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SOURCE_UTIL_ILIST_H_
#define SOURCE_UTIL_ILIST_H_
#include <cassert>
#include <memory>
#include <type_traits>
#include <vector>
#include "source/util/ilist_node.h"
namespace spvtools {
namespace utils {
// An IntrusiveList is a generic implementation of a doubly-linked list. The
// intended convention for using this container is:
//
// class Node : public IntrusiveNodeBase<Node> {
// // Note that "Node", the class being defined is the template.
// // Must have a default constructor accessible to List.
// // Add whatever data is needed in the node
// };
//
// using List = IntrusiveList<Node>;
//
// You can also inherit from IntrusiveList instead of a typedef if you want to
// add more functionality.
//
// The condition on the template for IntrusiveNodeBase is there to add some type
// checking to the container. The compiler will still allow inserting elements
// of type IntrusiveNodeBase<Node>, but that would be an error. This assumption
// allows NextNode and PreviousNode to return pointers to Node, and casting will
// not be required by the user.
template <class NodeType>
class IntrusiveList {
public:
static_assert(
std::is_base_of<IntrusiveNodeBase<NodeType>, NodeType>::value,
"The type from the node must be derived from IntrusiveNodeBase, with "
"itself in the template.");
// Creates an empty list.
inline IntrusiveList();
// Moves the contents of the given list to the list being constructed.
IntrusiveList(IntrusiveList&&);
// Destorys the list. Note that the elements of the list will not be deleted,
// but they will be removed from the list.
virtual ~IntrusiveList();
// Moves all of the elements in the list on the RHS to the list on the LHS.
IntrusiveList& operator=(IntrusiveList&&);
// Basetype for iterators so an IntrusiveList can be traversed like STL
// containers.
template <class T>
class iterator_template {
public:
iterator_template(const iterator_template& i) : node_(i.node_) {}
iterator_template& operator++() {
node_ = node_->next_node_;
return *this;
}
iterator_template& operator--() {
node_ = node_->previous_node_;
return *this;
}
iterator_template& operator=(const iterator_template& i) {
node_ = i.node_;
return *this;
}
T& operator*() const { return *node_; }
T* operator->() const { return node_; }
friend inline bool operator==(const iterator_template& lhs,
const iterator_template& rhs) {
return lhs.node_ == rhs.node_;
}
friend inline bool operator!=(const iterator_template& lhs,
const iterator_template& rhs) {
return !(lhs == rhs);
}
// Moves the nodes in |list| to the list that |this| points to. The
// positions of the nodes will be immediately before the element pointed to
// by the iterator. The return value will be an iterator pointing to the
// first of the newly inserted elements.
iterator_template MoveBefore(IntrusiveList* list) {
if (list->empty()) return *this;
NodeType* first_node = list->sentinel_.next_node_;
NodeType* last_node = list->sentinel_.previous_node_;
this->node_->previous_node_->next_node_ = first_node;
first_node->previous_node_ = this->node_->previous_node_;
last_node->next_node_ = this->node_;
this->node_->previous_node_ = last_node;
list->sentinel_.next_node_ = &list->sentinel_;
list->sentinel_.previous_node_ = &list->sentinel_;
return iterator(first_node);
}
// Define standard iterator types needs so this class can be
// used with <algorithms>.
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = size_t;
protected:
iterator_template() = delete;
inline iterator_template(T* node) { node_ = node; }
T* node_;
friend IntrusiveList;
};
using iterator = iterator_template<NodeType>;
using const_iterator = iterator_template<const NodeType>;
// Various types of iterators for the start (begin) and one past the end (end)
// of the list.
//
// Decrementing |end()| iterator will give and iterator pointing to the last
// element in the list, if one exists.
//
// Incrementing |end()| iterator will give |begin()|.
//
// Decrementing |begin()| will give |end()|.
//
// TODO: Not marking these functions as noexcept because Visual Studio 2013
// does not support it. When we no longer care about that compiler, we should
// mark these as noexcept.
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
const_iterator cbegin() const;
const_iterator cend() const;
// Appends |node| to the end of the list. If |node| is already in a list, it
// will be removed from that list first.
void push_back(NodeType* node);
// Returns true if the list is empty.
bool empty() const;
// Makes the current list empty.
inline void clear();
// Returns references to the first or last element in the list. It is an
// error to call these functions on an empty list.
NodeType& front();
NodeType& back();
const NodeType& front() const;
const NodeType& back() const;
// Transfers [|first|, |last|) from |other| into the list at |where|.
//
// If |other| is |this|, no change is made.
void Splice(iterator where, IntrusiveList<NodeType>* other, iterator first,
iterator last);
protected:
// Doing a deep copy of the list does not make sense if the list does not own
// the data. It is not clear who will own the newly created data. Making
// copies illegal for that reason.
IntrusiveList(const IntrusiveList&) = delete;
IntrusiveList& operator=(const IntrusiveList&) = delete;
// This function will assert if it finds the list containing |node| is not in
// a valid state.
static void Check(NodeType* node);
// A special node used to represent both the start and end of the list,
// without being part of the list.
NodeType sentinel_;
};
// Implementation of IntrusiveList
template <class NodeType>
inline IntrusiveList<NodeType>::IntrusiveList() : sentinel_() {
sentinel_.next_node_ = &sentinel_;
sentinel_.previous_node_ = &sentinel_;
sentinel_.is_sentinel_ = true;
}
template <class NodeType>
IntrusiveList<NodeType>::IntrusiveList(IntrusiveList&& list) : sentinel_() {
sentinel_.next_node_ = &sentinel_;
sentinel_.previous_node_ = &sentinel_;
sentinel_.is_sentinel_ = true;
list.sentinel_.ReplaceWith(&sentinel_);
}
template <class NodeType>
IntrusiveList<NodeType>::~IntrusiveList() {
clear();
}
template <class NodeType>
IntrusiveList<NodeType>& IntrusiveList<NodeType>::operator=(
IntrusiveList<NodeType>&& list) {
list.sentinel_.ReplaceWith(&sentinel_);
return *this;
}
template <class NodeType>
inline typename IntrusiveList<NodeType>::iterator
IntrusiveList<NodeType>::begin() {
return iterator(sentinel_.next_node_);
}
template <class NodeType>
inline typename IntrusiveList<NodeType>::iterator
IntrusiveList<NodeType>::end() {
return iterator(&sentinel_);
}
template <class NodeType>
inline typename IntrusiveList<NodeType>::const_iterator
IntrusiveList<NodeType>::begin() const {
return const_iterator(sentinel_.next_node_);
}
template <class NodeType>
inline typename IntrusiveList<NodeType>::const_iterator
IntrusiveList<NodeType>::end() const {
return const_iterator(&sentinel_);
}
template <class NodeType>
inline typename IntrusiveList<NodeType>::const_iterator
IntrusiveList<NodeType>::cbegin() const {
return const_iterator(sentinel_.next_node_);
}
template <class NodeType>
inline typename IntrusiveList<NodeType>::const_iterator
IntrusiveList<NodeType>::cend() const {
return const_iterator(&sentinel_);
}
template <class NodeType>
void IntrusiveList<NodeType>::push_back(NodeType* node) {
node->InsertBefore(&sentinel_);
}
template <class NodeType>
bool IntrusiveList<NodeType>::empty() const {
return sentinel_.NextNode() == nullptr;
}
template <class NodeType>
void IntrusiveList<NodeType>::clear() {
while (!empty()) {
front().RemoveFromList();
}
}
template <class NodeType>
NodeType& IntrusiveList<NodeType>::front() {
NodeType* node = sentinel_.NextNode();
assert(node != nullptr && "Can't get the front of an empty list.");
return *node;
}
template <class NodeType>
NodeType& IntrusiveList<NodeType>::back() {
NodeType* node = sentinel_.PreviousNode();
assert(node != nullptr && "Can't get the back of an empty list.");
return *node;
}
template <class NodeType>
const NodeType& IntrusiveList<NodeType>::front() const {
NodeType* node = sentinel_.NextNode();
assert(node != nullptr && "Can't get the front of an empty list.");
return *node;
}
template <class NodeType>
const NodeType& IntrusiveList<NodeType>::back() const {
NodeType* node = sentinel_.PreviousNode();
assert(node != nullptr && "Can't get the back of an empty list.");
return *node;
}
template <class NodeType>
void IntrusiveList<NodeType>::Splice(iterator where,
IntrusiveList<NodeType>* other,
iterator first, iterator last) {
if (first == last) return;
if (other == this) return;
NodeType* first_prev = first.node_->previous_node_;
NodeType* where_next = where.node_->next_node_;
// Attach first.
where.node_->next_node_ = first.node_;
first.node_->previous_node_ = where.node_;
// Attach last.
where_next->previous_node_ = last.node_->previous_node_;
last.node_->previous_node_->next_node_ = where_next;
// Fixup other.
first_prev->next_node_ = last.node_;
last.node_->previous_node_ = first_prev;
}
template <class NodeType>
void IntrusiveList<NodeType>::Check(NodeType* start) {
int sentinel_count = 0;
NodeType* p = start;
do {
assert(p != nullptr);
assert(p->next_node_->previous_node_ == p);
assert(p->previous_node_->next_node_ == p);
if (p->is_sentinel_) sentinel_count++;
p = p->next_node_;
} while (p != start);
assert(sentinel_count == 1 && "List should have exactly 1 sentinel node.");
p = start;
do {
assert(p != nullptr);
assert(p->previous_node_->next_node_ == p);
assert(p->next_node_->previous_node_ == p);
if (p->is_sentinel_) sentinel_count++;
p = p->previous_node_;
} while (p != start);
}
} // namespace utils
} // namespace spvtools
#endif // SOURCE_UTIL_ILIST_H_
|