| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | 4 |
| 26 #ifndef DoublyLinkedList_h | 5 #include "platform/wtf/DoublyLinkedList.h" |
| 27 #define DoublyLinkedList_h | |
| 28 | 6 |
| 29 #include "wtf/Allocator.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 30 | 8 // WTF migration project. See the following post for details: |
| 31 namespace WTF { | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 32 | |
| 33 // This class allows nodes to share code without dictating data member layout. | |
| 34 template <typename T> | |
| 35 class DoublyLinkedListNode { | |
| 36 public: | |
| 37 DoublyLinkedListNode(); | |
| 38 | |
| 39 void setPrev(T*); | |
| 40 void setNext(T*); | |
| 41 | |
| 42 T* prev() const; | |
| 43 T* next() const; | |
| 44 }; | |
| 45 | |
| 46 template <typename T> | |
| 47 inline DoublyLinkedListNode<T>::DoublyLinkedListNode() { | |
| 48 setPrev(0); | |
| 49 setNext(0); | |
| 50 } | |
| 51 | |
| 52 template <typename T> | |
| 53 inline void DoublyLinkedListNode<T>::setPrev(T* prev) { | |
| 54 static_cast<T*>(this)->m_prev = prev; | |
| 55 } | |
| 56 | |
| 57 template <typename T> | |
| 58 inline void DoublyLinkedListNode<T>::setNext(T* next) { | |
| 59 static_cast<T*>(this)->m_next = next; | |
| 60 } | |
| 61 | |
| 62 template <typename T> | |
| 63 inline T* DoublyLinkedListNode<T>::prev() const { | |
| 64 return static_cast<const T*>(this)->m_prev; | |
| 65 } | |
| 66 | |
| 67 template <typename T> | |
| 68 inline T* DoublyLinkedListNode<T>::next() const { | |
| 69 return static_cast<const T*>(this)->m_next; | |
| 70 } | |
| 71 | |
| 72 template <typename T> | |
| 73 class DoublyLinkedList { | |
| 74 USING_FAST_MALLOC(DoublyLinkedList); | |
| 75 | |
| 76 public: | |
| 77 DoublyLinkedList(); | |
| 78 | |
| 79 bool isEmpty() const; | |
| 80 size_t size() const; // This is O(n). | |
| 81 void clear(); | |
| 82 | |
| 83 T* head() const; | |
| 84 T* removeHead(); | |
| 85 | |
| 86 T* tail() const; | |
| 87 | |
| 88 void push(T*); | |
| 89 void append(T*); | |
| 90 void remove(T*); | |
| 91 | |
| 92 private: | |
| 93 T* m_head; | |
| 94 T* m_tail; | |
| 95 }; | |
| 96 | |
| 97 template <typename T> | |
| 98 inline DoublyLinkedList<T>::DoublyLinkedList() : m_head(0), m_tail(0) {} | |
| 99 | |
| 100 template <typename T> | |
| 101 inline bool DoublyLinkedList<T>::isEmpty() const { | |
| 102 return !m_head; | |
| 103 } | |
| 104 | |
| 105 template <typename T> | |
| 106 inline size_t DoublyLinkedList<T>::size() const { | |
| 107 size_t size = 0; | |
| 108 for (T* node = m_head; node; node = node->next()) | |
| 109 ++size; | |
| 110 return size; | |
| 111 } | |
| 112 | |
| 113 template <typename T> | |
| 114 inline void DoublyLinkedList<T>::clear() { | |
| 115 m_head = 0; | |
| 116 m_tail = 0; | |
| 117 } | |
| 118 | |
| 119 template <typename T> | |
| 120 inline T* DoublyLinkedList<T>::head() const { | |
| 121 return m_head; | |
| 122 } | |
| 123 | |
| 124 template <typename T> | |
| 125 inline T* DoublyLinkedList<T>::tail() const { | |
| 126 return m_tail; | |
| 127 } | |
| 128 | |
| 129 template <typename T> | |
| 130 inline void DoublyLinkedList<T>::push(T* node) { | |
| 131 if (!m_head) { | |
| 132 DCHECK(!m_tail); | |
| 133 m_head = node; | |
| 134 m_tail = node; | |
| 135 node->setPrev(0); | |
| 136 node->setNext(0); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 DCHECK(m_tail); | |
| 141 m_head->setPrev(node); | |
| 142 node->setNext(m_head); | |
| 143 node->setPrev(0); | |
| 144 m_head = node; | |
| 145 } | |
| 146 | |
| 147 template <typename T> | |
| 148 inline void DoublyLinkedList<T>::append(T* node) { | |
| 149 if (!m_tail) { | |
| 150 DCHECK(!m_head); | |
| 151 m_head = node; | |
| 152 m_tail = node; | |
| 153 node->setPrev(0); | |
| 154 node->setNext(0); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 DCHECK(m_head); | |
| 159 m_tail->setNext(node); | |
| 160 node->setPrev(m_tail); | |
| 161 node->setNext(0); | |
| 162 m_tail = node; | |
| 163 } | |
| 164 | |
| 165 template <typename T> | |
| 166 inline void DoublyLinkedList<T>::remove(T* node) { | |
| 167 if (node->prev()) { | |
| 168 DCHECK_NE(node, m_head); | |
| 169 node->prev()->setNext(node->next()); | |
| 170 } else { | |
| 171 DCHECK_EQ(node, m_head); | |
| 172 m_head = node->next(); | |
| 173 } | |
| 174 | |
| 175 if (node->next()) { | |
| 176 DCHECK_NE(node, m_tail); | |
| 177 node->next()->setPrev(node->prev()); | |
| 178 } else { | |
| 179 DCHECK_EQ(node, m_tail); | |
| 180 m_tail = node->prev(); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 template <typename T> | |
| 185 inline T* DoublyLinkedList<T>::removeHead() { | |
| 186 T* node = head(); | |
| 187 if (node) | |
| 188 remove(node); | |
| 189 return node; | |
| 190 } | |
| 191 | |
| 192 } // namespace WTF | |
| 193 | |
| 194 using WTF::DoublyLinkedListNode; | |
| 195 using WTF::DoublyLinkedList; | |
| 196 | |
| 197 #endif | |
| OLD | NEW |