/* * Copyright (C) 2011 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once namespace WTF { // This class allows nodes to share code without dictating data member layout. template class DoublyLinkedListNode { public: DoublyLinkedListNode(); void setPrev(T*); void setNext(T*); T* prev() const; T* next() const; }; template inline DoublyLinkedListNode::DoublyLinkedListNode() { setPrev(0); setNext(0); } template inline void DoublyLinkedListNode::setPrev(T* prev) { static_cast(this)->m_prev = prev; } template inline void DoublyLinkedListNode::setNext(T* next) { static_cast(this)->m_next = next; } template inline T* DoublyLinkedListNode::prev() const { return static_cast(this)->m_prev; } template inline T* DoublyLinkedListNode::next() const { return static_cast(this)->m_next; } template class DoublyLinkedList { public: DoublyLinkedList(); bool isEmpty() const; size_t size() const; // This is O(n). void clear(); T* head() const; T* removeHead(); T* tail() const; void push(T*); void append(T*); void remove(T*); void append(DoublyLinkedList&); private: T* m_head; T* m_tail; }; template inline DoublyLinkedList::DoublyLinkedList() : m_head(0) , m_tail(0) { } template inline bool DoublyLinkedList::isEmpty() const { return !m_head; } template inline size_t DoublyLinkedList::size() const { size_t size = 0; for (T* node = m_head; node; node = node->next()) ++size; return size; } template inline void DoublyLinkedList::clear() { m_head = 0; m_tail = 0; } template inline T* DoublyLinkedList::head() const { return m_head; } template inline T* DoublyLinkedList::tail() const { return m_tail; } template inline void DoublyLinkedList::push(T* node) { 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; } template inline void DoublyLinkedList::append(T* node) { 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; } template inline void DoublyLinkedList::remove(T* node) { 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(); } } template inline T* DoublyLinkedList::removeHead() { T* node = head(); if (node) remove(node); return node; } template inline void DoublyLinkedList::append(DoublyLinkedList& other) { 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; } } // namespace WTF using WTF::DoublyLinkedListNode; using WTF::DoublyLinkedList;