2018-10-16 09:01:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Assertions.h"
|
|
|
|
#include "types.h"
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> class InlineLinkedListNode {
|
2018-10-16 09:01:38 +00:00
|
|
|
public:
|
2018-10-17 08:55:43 +00:00
|
|
|
InlineLinkedListNode();
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
void setPrev(T*);
|
|
|
|
void setNext(T*);
|
|
|
|
|
|
|
|
T* prev() const;
|
|
|
|
T* next() const;
|
|
|
|
};
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline InlineLinkedListNode<T>::InlineLinkedListNode()
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
setPrev(0);
|
|
|
|
setNext(0);
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedListNode<T>::setPrev(T* prev)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
static_cast<T*>(this)->m_prev = prev;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedListNode<T>::setNext(T* next)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
static_cast<T*>(this)->m_next = next;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline T* InlineLinkedListNode<T>::prev() const
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(this)->m_prev;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline T* InlineLinkedListNode<T>::next() const
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(this)->m_next;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> class InlineLinkedList {
|
2018-10-16 09:01:38 +00:00
|
|
|
public:
|
2018-10-17 08:55:43 +00:00
|
|
|
InlineLinkedList() { }
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
bool isEmpty() const { return !m_head; }
|
2018-10-23 10:44:46 +00:00
|
|
|
size_t sizeSlow() const;
|
2018-10-16 09:01:38 +00:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
T* head() const { return m_head; }
|
|
|
|
T* removeHead();
|
|
|
|
|
|
|
|
T* tail() const { return m_tail; }
|
|
|
|
|
|
|
|
void prepend(T*);
|
|
|
|
void append(T*);
|
|
|
|
void remove(T*);
|
2018-10-17 08:55:43 +00:00
|
|
|
void append(InlineLinkedList<T>&);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-10-21 21:48:27 +00:00
|
|
|
bool containsSlow(T* value) const
|
|
|
|
{
|
|
|
|
for (T* node = m_head; node; node = node->next()) {
|
|
|
|
if (node == value)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-16 09:01:38 +00:00
|
|
|
private:
|
|
|
|
T* m_head { nullptr };
|
|
|
|
T* m_tail { nullptr };
|
|
|
|
};
|
|
|
|
|
2018-10-23 10:44:46 +00:00
|
|
|
template<typename T> inline size_t InlineLinkedList<T>::sizeSlow() const
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
for (T* node = m_head; node; node = node->next())
|
|
|
|
++size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedList<T>::clear()
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
m_head = 0;
|
|
|
|
m_tail = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedList<T>::prepend(T* node)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
if (!m_head) {
|
|
|
|
ASSERT(!m_tail);
|
|
|
|
m_head = node;
|
|
|
|
m_tail = node;
|
|
|
|
node->setPrev(0);
|
|
|
|
node->setNext(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(m_tail);
|
|
|
|
m_head->setPrev(node);
|
|
|
|
node->setNext(m_head);
|
|
|
|
node->setPrev(0);
|
|
|
|
m_head = node;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedList<T>::append(T* node)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
if (!m_tail) {
|
|
|
|
ASSERT(!m_head);
|
|
|
|
m_head = node;
|
|
|
|
m_tail = node;
|
|
|
|
node->setPrev(0);
|
|
|
|
node->setNext(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(m_head);
|
|
|
|
m_tail->setNext(node);
|
|
|
|
node->setPrev(m_tail);
|
|
|
|
node->setNext(0);
|
|
|
|
m_tail = node;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedList<T>::remove(T* node)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
if (node->prev()) {
|
|
|
|
ASSERT(node != m_head);
|
|
|
|
node->prev()->setNext(node->next());
|
|
|
|
} else {
|
|
|
|
ASSERT(node == m_head);
|
|
|
|
m_head = node->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->next()) {
|
|
|
|
ASSERT(node != m_tail);
|
|
|
|
node->next()->setPrev(node->prev());
|
|
|
|
} else {
|
|
|
|
ASSERT(node == m_tail);
|
|
|
|
m_tail = node->prev();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline T* InlineLinkedList<T>::removeHead()
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
T* node = head();
|
|
|
|
if (node)
|
|
|
|
remove(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<typename T> inline void InlineLinkedList<T>::append(InlineLinkedList<T>& other)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
if (!other.head())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!head()) {
|
|
|
|
m_head = other.head();
|
|
|
|
m_tail = other.tail();
|
|
|
|
other.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(tail());
|
|
|
|
ASSERT(other.head());
|
|
|
|
T* otherHead = other.head();
|
|
|
|
T* otherTail = other.tail();
|
|
|
|
other.clear();
|
|
|
|
|
|
|
|
ASSERT(!m_tail->next());
|
|
|
|
m_tail->setNext(otherHead);
|
|
|
|
ASSERT(!otherHead->prev());
|
|
|
|
otherHead->setPrev(m_tail);
|
|
|
|
m_tail = otherTail;
|
|
|
|
}
|