2016-08-06 22:23:01 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
2020-04-23 15:25:11 +01:00
|
|
|
* SPDX-License-Identifier: 0BSD
|
2016-08-06 22:23:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-09-03 21:48:03 +01:00
|
|
|
#include <cstddef>
|
2016-08-06 22:23:01 +01:00
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
2016-08-18 15:02:36 +01:00
|
|
|
#include <type_traits>
|
2016-08-06 22:23:01 +01:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::Common {
|
2016-08-06 22:23:01 +01:00
|
|
|
|
|
|
|
template <typename T> class IntrusiveList;
|
|
|
|
template <typename T> class IntrusiveListIterator;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class IntrusiveListNode {
|
|
|
|
public:
|
2016-08-26 20:38:59 +01:00
|
|
|
bool IsSentinel() const {
|
|
|
|
return is_sentinel;
|
2016-08-06 22:23:01 +01:00
|
|
|
}
|
2016-08-17 04:50:17 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
protected:
|
|
|
|
IntrusiveListNode* next = nullptr;
|
|
|
|
IntrusiveListNode* prev = nullptr;
|
|
|
|
bool is_sentinel = false;
|
|
|
|
|
2016-08-06 22:23:01 +01:00
|
|
|
friend class IntrusiveList<T>;
|
|
|
|
friend class IntrusiveListIterator<T>;
|
2016-08-18 15:02:36 +01:00
|
|
|
friend class IntrusiveListIterator<const T>;
|
2016-08-26 20:38:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class IntrusiveListSentinel final : public IntrusiveListNode<T>
|
|
|
|
{
|
|
|
|
using IntrusiveListNode<T>::next;
|
|
|
|
using IntrusiveListNode<T>::prev;
|
|
|
|
using IntrusiveListNode<T>::is_sentinel;
|
2016-08-18 15:02:36 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
public:
|
|
|
|
IntrusiveListSentinel() {
|
|
|
|
next = this;
|
|
|
|
prev = this;
|
|
|
|
is_sentinel = true;
|
|
|
|
}
|
2016-08-06 22:23:01 +01:00
|
|
|
};
|
|
|
|
|
2016-08-21 11:04:00 +01:00
|
|
|
template <typename T>
|
|
|
|
class IntrusiveListIterator {
|
|
|
|
public:
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = T;
|
|
|
|
using pointer = value_type*;
|
|
|
|
using const_pointer = const value_type*;
|
|
|
|
using reference = value_type&;
|
|
|
|
using const_reference = const value_type&;
|
|
|
|
|
|
|
|
// If value_type is const, we want "const IntrusiveListNode<value_type>", not "const IntrusiveListNode<const value_type>"
|
|
|
|
using node_type = std::conditional_t<std::is_const<value_type>::value,
|
|
|
|
const IntrusiveListNode<std::remove_const_t<value_type>>,
|
|
|
|
IntrusiveListNode<value_type>>;
|
|
|
|
using node_pointer = node_type*;
|
|
|
|
using node_reference = node_type&;
|
|
|
|
|
|
|
|
IntrusiveListIterator() = default;
|
|
|
|
IntrusiveListIterator(const IntrusiveListIterator& other) = default;
|
|
|
|
IntrusiveListIterator& operator=(const IntrusiveListIterator& other) = default;
|
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
explicit IntrusiveListIterator(node_pointer list_node) : node(list_node) {
|
|
|
|
}
|
|
|
|
explicit IntrusiveListIterator(pointer data) : node(data) {
|
|
|
|
}
|
|
|
|
explicit IntrusiveListIterator(reference data) : node(&data) {
|
2016-08-21 11:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveListIterator& operator++() {
|
2016-08-26 20:38:59 +01:00
|
|
|
node = node->next;
|
2016-08-21 11:04:00 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
IntrusiveListIterator& operator--() {
|
2016-08-26 20:38:59 +01:00
|
|
|
node = node->prev;
|
2016-08-21 11:04:00 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2016-08-26 18:57:38 +01:00
|
|
|
IntrusiveListIterator operator++(int) {
|
|
|
|
IntrusiveListIterator it(*this);
|
|
|
|
++*this;
|
|
|
|
return it;
|
|
|
|
}
|
2016-08-21 11:04:00 +01:00
|
|
|
IntrusiveListIterator operator--(int) {
|
|
|
|
IntrusiveListIterator it(*this);
|
2016-08-26 18:57:38 +01:00
|
|
|
--*this;
|
2016-08-21 11:04:00 +01:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const IntrusiveListIterator& other) const {
|
|
|
|
return node == other.node;
|
|
|
|
}
|
|
|
|
bool operator!=(const IntrusiveListIterator& other) const {
|
2016-08-26 20:38:59 +01:00
|
|
|
return !operator==(other);
|
2016-08-21 11:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reference operator*() const {
|
2016-08-26 20:38:59 +01:00
|
|
|
DEBUG_ASSERT(!node->IsSentinel());
|
|
|
|
return static_cast<reference>(*node);
|
2016-08-21 11:04:00 +01:00
|
|
|
}
|
|
|
|
pointer operator->() const {
|
|
|
|
return std::addressof(operator*());
|
|
|
|
}
|
|
|
|
|
|
|
|
node_pointer AsNodePointer() const {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class IntrusiveList<T>;
|
|
|
|
node_pointer node = nullptr;
|
|
|
|
};
|
|
|
|
|
2016-08-06 22:23:01 +01:00
|
|
|
template <typename T>
|
|
|
|
class IntrusiveList {
|
|
|
|
public:
|
2016-08-18 18:52:23 +01:00
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using size_type = std::size_t;
|
|
|
|
using value_type = T;
|
|
|
|
using pointer = value_type*;
|
|
|
|
using const_pointer = const value_type*;
|
|
|
|
using reference = value_type&;
|
|
|
|
using const_reference = const value_type&;
|
|
|
|
using iterator = IntrusiveListIterator<value_type>;
|
|
|
|
using const_iterator = IntrusiveListIterator<const value_type>;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
|
2016-08-06 22:23:01 +01:00
|
|
|
/**
|
2016-08-19 02:13:14 +01:00
|
|
|
* Inserts a node at the given location indicated by an iterator.
|
|
|
|
*
|
|
|
|
* @param location The location to insert the node.
|
|
|
|
* @param new_node The node to add.
|
2016-08-06 22:23:01 +01:00
|
|
|
*/
|
2016-08-19 02:13:14 +01:00
|
|
|
iterator insert(iterator location, pointer new_node) {
|
2016-08-23 03:52:06 +01:00
|
|
|
return insert_before(location, new_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a node at the given location, moving the previous
|
|
|
|
* node occupant ahead of the one inserted.
|
|
|
|
*
|
|
|
|
* @param location The location to insert the new node.
|
|
|
|
* @param new_node The node to insert into the list.
|
|
|
|
*/
|
|
|
|
iterator insert_before(iterator location, pointer new_node) {
|
2016-08-19 02:13:14 +01:00
|
|
|
auto existing_node = location.AsNodePointer();
|
2016-08-06 22:23:01 +01:00
|
|
|
|
2016-08-19 02:13:14 +01:00
|
|
|
new_node->next = existing_node;
|
|
|
|
new_node->prev = existing_node->prev;
|
|
|
|
existing_node->prev->next = new_node;
|
|
|
|
existing_node->prev = new_node;
|
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
return iterator(new_node);
|
2016-08-06 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:52:06 +01:00
|
|
|
/**
|
|
|
|
* Inserts a new node into the list ahead of the position indicated.
|
|
|
|
*
|
|
|
|
* @param position Location to insert the node in front of.
|
|
|
|
* @param new_node The node to be inserted into the list.
|
|
|
|
*/
|
|
|
|
iterator insert_after(iterator position, pointer new_node) {
|
|
|
|
if (empty())
|
|
|
|
return insert(begin(), new_node);
|
|
|
|
|
|
|
|
return insert(++position, new_node);
|
|
|
|
}
|
|
|
|
|
2016-08-06 22:23:01 +01:00
|
|
|
/**
|
2016-08-19 02:13:14 +01:00
|
|
|
* Add an entry to the start of the list.
|
|
|
|
* @param node Node to add to the list.
|
2016-08-06 22:23:01 +01:00
|
|
|
*/
|
2016-08-19 02:13:14 +01:00
|
|
|
void push_front(pointer node) {
|
|
|
|
insert(begin(), node);
|
2016-08-06 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 02:13:14 +01:00
|
|
|
* Add an entry to the end of the list
|
|
|
|
* @param node Node to add to the list.
|
2016-08-06 22:23:01 +01:00
|
|
|
*/
|
2016-08-19 02:13:14 +01:00
|
|
|
void push_back(pointer node) {
|
|
|
|
insert(end(), node);
|
2016-08-06 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-20 04:18:46 +01:00
|
|
|
/**
|
|
|
|
* Erases the node at the front of the list.
|
|
|
|
* @note Must not be called on an empty list.
|
|
|
|
*/
|
|
|
|
void pop_front() {
|
|
|
|
DEBUG_ASSERT(!empty());
|
|
|
|
erase(begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Erases the node at the back of the list.
|
|
|
|
* @note Must not be called on an empty list.
|
|
|
|
*/
|
|
|
|
void pop_back() {
|
|
|
|
DEBUG_ASSERT(!empty());
|
|
|
|
erase(--end());
|
|
|
|
}
|
|
|
|
|
2016-08-06 22:23:01 +01:00
|
|
|
/**
|
2016-08-26 20:38:59 +01:00
|
|
|
* Removes a node from this list
|
2016-09-07 03:51:16 +01:00
|
|
|
* @param it An iterator that points to the node to remove from list.
|
2016-08-26 20:38:59 +01:00
|
|
|
*/
|
|
|
|
pointer remove(iterator& it) {
|
|
|
|
DEBUG_ASSERT(it != end());
|
|
|
|
|
|
|
|
pointer node = &*it++;
|
|
|
|
|
|
|
|
node->prev->next = node->next;
|
|
|
|
node->next->prev = node->prev;
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
node->next = nullptr;
|
|
|
|
node->prev = nullptr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a node from this list
|
2016-09-07 03:51:16 +01:00
|
|
|
* @param it A constant iterator that points to the node to remove from list.
|
2016-08-26 20:38:59 +01:00
|
|
|
*/
|
|
|
|
pointer remove(const iterator& it) {
|
|
|
|
iterator copy = it;
|
2016-09-07 03:50:02 +01:00
|
|
|
return remove(copy);
|
2016-08-26 20:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a node from this list.
|
|
|
|
* @param node A pointer to the node to remove.
|
2016-08-06 22:23:01 +01:00
|
|
|
*/
|
2016-08-26 20:38:59 +01:00
|
|
|
pointer remove(pointer node) {
|
|
|
|
return remove(iterator(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a node from this list.
|
|
|
|
* @param node A reference to the node to remove.
|
|
|
|
*/
|
|
|
|
pointer remove(reference node) {
|
|
|
|
return remove(iterator(node));
|
2016-08-06 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this list empty?
|
|
|
|
* @returns true if there are no nodes in this list.
|
|
|
|
*/
|
2016-08-19 02:23:51 +01:00
|
|
|
bool empty() const {
|
2016-08-06 22:23:01 +01:00
|
|
|
return root->next == root.get();
|
|
|
|
}
|
|
|
|
|
2016-08-23 18:05:00 +01:00
|
|
|
/**
|
|
|
|
* Gets the total number of elements within this list.
|
|
|
|
* @return the number of elements in this list.
|
|
|
|
*/
|
|
|
|
size_type size() const {
|
|
|
|
return static_cast<size_type>(std::distance(begin(), end()));
|
|
|
|
}
|
|
|
|
|
2016-08-20 04:18:46 +01:00
|
|
|
/**
|
|
|
|
* Retrieves a reference to the node at the front of the list.
|
|
|
|
* @note Must not be called on an empty list.
|
|
|
|
*/
|
|
|
|
reference front() {
|
|
|
|
DEBUG_ASSERT(!empty());
|
|
|
|
return *begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a constant reference to the node at the front of the list.
|
|
|
|
* @note Must not be called on an empty list.
|
|
|
|
*/
|
|
|
|
const_reference front() const {
|
|
|
|
DEBUG_ASSERT(!empty());
|
|
|
|
return *begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a reference to the node at the back of the list.
|
|
|
|
* @note Must not be called on an empty list.
|
|
|
|
*/
|
|
|
|
reference back() {
|
|
|
|
DEBUG_ASSERT(!empty());
|
|
|
|
return *--end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a constant reference to the node at the back of the list.
|
|
|
|
* @note Must not be called on an empty list.
|
|
|
|
*/
|
|
|
|
const_reference back() const {
|
|
|
|
DEBUG_ASSERT(!empty());
|
|
|
|
return *--end();
|
|
|
|
}
|
|
|
|
|
2016-08-18 18:52:23 +01:00
|
|
|
// Iterator interface
|
2016-08-26 20:38:59 +01:00
|
|
|
iterator begin() { return iterator(root->next); }
|
|
|
|
const_iterator begin() const { return const_iterator(root->next); }
|
|
|
|
const_iterator cbegin() const { return begin(); }
|
2016-08-18 18:52:23 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
iterator end() { return iterator(root.get()); }
|
|
|
|
const_iterator end() const { return const_iterator(root.get()); }
|
|
|
|
const_iterator cend() const { return end(); }
|
2016-08-18 18:52:23 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator crbegin() const { return rbegin(); }
|
2016-08-06 22:23:01 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator crend() const { return rend(); }
|
2016-08-18 18:52:23 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
/**
|
|
|
|
* Erases a node from the list, indicated by an iterator.
|
|
|
|
* @param it The iterator that points to the node to erase.
|
|
|
|
*/
|
2016-08-18 18:52:23 +01:00
|
|
|
iterator erase(iterator it) {
|
2016-08-26 20:38:59 +01:00
|
|
|
remove(it);
|
2016-08-18 18:52:23 +01:00
|
|
|
return it;
|
|
|
|
}
|
2016-08-06 22:23:01 +01:00
|
|
|
|
2016-08-26 20:38:59 +01:00
|
|
|
/**
|
|
|
|
* Erases a node from this list.
|
|
|
|
* @param node A pointer to the node to erase from this list.
|
|
|
|
*/
|
|
|
|
iterator erase(pointer node) {
|
|
|
|
return erase(iterator(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Erases a node from this list.
|
|
|
|
* @param node A reference to the node to erase from this list.
|
|
|
|
*/
|
|
|
|
iterator erase(reference node) {
|
|
|
|
return erase(iterator(node));
|
|
|
|
}
|
|
|
|
|
2016-08-21 11:13:08 +01:00
|
|
|
/**
|
|
|
|
* Exchanges contents of this list with another list instance.
|
|
|
|
* @param other The other list to swap with.
|
|
|
|
*/
|
2016-09-07 03:47:52 +01:00
|
|
|
void swap(IntrusiveList& other) noexcept {
|
2016-08-21 11:13:08 +01:00
|
|
|
root.swap(other.root);
|
|
|
|
}
|
|
|
|
|
2016-08-06 22:23:01 +01:00
|
|
|
private:
|
2016-08-26 20:38:59 +01:00
|
|
|
std::shared_ptr<IntrusiveListNode<T>> root = std::make_shared<IntrusiveListSentinel<T>>();
|
2016-08-06 22:23:01 +01:00
|
|
|
};
|
|
|
|
|
2016-08-21 11:13:08 +01:00
|
|
|
/**
|
|
|
|
* Exchanges contents of an intrusive list with another intrusive list.
|
|
|
|
* @tparam T The type of data being kept track of by the lists.
|
|
|
|
* @param lhs The first list.
|
|
|
|
* @param rhs The second list.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-09-07 03:47:52 +01:00
|
|
|
void swap(IntrusiveList<T>& lhs, IntrusiveList<T>& rhs) noexcept {
|
2016-08-21 11:13:08 +01:00
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::Common
|