robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 8 | #ifndef SkTInternalLList_DEFINED |
| 9 | #define SkTInternalLList_DEFINED |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 10 | |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 11 | #include "../private/SkNoncopyable.h" |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 14 | /** |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 15 | * Helper class to automatically initialize the doubly linked list created pointers. |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 16 | */ |
| 17 | template <typename T> class SkPtrWrapper { |
| 18 | public: |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 19 | SkPtrWrapper() : fPtr(nullptr) {} |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 20 | SkPtrWrapper& operator =(T* ptr) { fPtr = ptr; return *this; } |
| 21 | operator T*() const { return fPtr; } |
| 22 | T* operator->() { return fPtr; } |
| 23 | private: |
| 24 | T* fPtr; |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | /** |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 29 | * This macro creates the member variables required by the SkTInternalLList class. It should be |
| 30 | * placed in the private section of any class that will be stored in a double linked list. |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 31 | */ |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 32 | #define SK_DECLARE_INTERNAL_LLIST_INTERFACE(ClassName) \ |
| 33 | friend class SkTInternalLList<ClassName>; \ |
| 34 | /* back pointer to the owning list - for debugging */ \ |
| 35 | SkDEBUGCODE(SkPtrWrapper<SkTInternalLList<ClassName> > fList;) \ |
| 36 | SkPtrWrapper<ClassName> fPrev; \ |
| 37 | SkPtrWrapper<ClassName> fNext |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * This class implements a templated internal doubly linked list data structure. |
| 41 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 42 | template <class T> class SkTInternalLList : SkNoncopyable { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 43 | public: |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 44 | SkTInternalLList() |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 45 | : fHead(nullptr) |
| 46 | , fTail(nullptr) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 49 | void reset() { |
| 50 | fHead = nullptr; |
| 51 | fTail = nullptr; |
| 52 | } |
| 53 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 54 | void remove(T* entry) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 55 | SkASSERT(fHead && fTail); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 56 | SkASSERT(this->isInList(entry)); |
| 57 | |
| 58 | T* prev = entry->fPrev; |
| 59 | T* next = entry->fNext; |
| 60 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 61 | if (prev) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 62 | prev->fNext = next; |
| 63 | } else { |
| 64 | fHead = next; |
| 65 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 66 | if (next) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 67 | next->fPrev = prev; |
| 68 | } else { |
| 69 | fTail = prev; |
| 70 | } |
| 71 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 72 | entry->fPrev = nullptr; |
| 73 | entry->fNext = nullptr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 39ac1ae | 2012-08-23 13:24:31 +0000 | [diff] [blame] | 75 | #ifdef SK_DEBUG |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 76 | entry->fList = nullptr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 77 | #endif |
| 78 | } |
| 79 | |
| 80 | void addToHead(T* entry) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 81 | SkASSERT(nullptr == entry->fPrev && nullptr == entry->fNext); |
| 82 | SkASSERT(nullptr == entry->fList); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 83 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 84 | entry->fPrev = nullptr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 85 | entry->fNext = fHead; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 86 | if (fHead) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 87 | fHead->fPrev = entry; |
| 88 | } |
| 89 | fHead = entry; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 90 | if (nullptr == fTail) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 91 | fTail = entry; |
| 92 | } |
| 93 | |
bsalomon@google.com | 39ac1ae | 2012-08-23 13:24:31 +0000 | [diff] [blame] | 94 | #ifdef SK_DEBUG |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 95 | entry->fList = this; |
| 96 | #endif |
| 97 | } |
| 98 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 99 | void addToTail(T* entry) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 100 | SkASSERT(nullptr == entry->fPrev && nullptr == entry->fNext); |
| 101 | SkASSERT(nullptr == entry->fList); |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 102 | |
| 103 | entry->fPrev = fTail; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 104 | entry->fNext = nullptr; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 105 | if (fTail) { |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 106 | fTail->fNext = entry; |
| 107 | } |
| 108 | fTail = entry; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 109 | if (nullptr == fHead) { |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 110 | fHead = entry; |
| 111 | } |
| 112 | |
| 113 | #ifdef SK_DEBUG |
| 114 | entry->fList = this; |
| 115 | #endif |
| 116 | } |
| 117 | |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Inserts a new list entry before an existing list entry. The new entry must not already be |
| 120 | * a member of this or any other list. If existingEntry is NULL then the new entry is added |
| 121 | * at the tail. |
| 122 | */ |
| 123 | void addBefore(T* newEntry, T* existingEntry) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 124 | SkASSERT(newEntry); |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 125 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 126 | if (nullptr == existingEntry) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 127 | this->addToTail(newEntry); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | SkASSERT(this->isInList(existingEntry)); |
| 132 | newEntry->fNext = existingEntry; |
| 133 | T* prev = existingEntry->fPrev; |
| 134 | existingEntry->fPrev = newEntry; |
| 135 | newEntry->fPrev = prev; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 136 | if (nullptr == prev) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 137 | SkASSERT(fHead == existingEntry); |
| 138 | fHead = newEntry; |
| 139 | } else { |
| 140 | prev->fNext = newEntry; |
| 141 | } |
robertphillips@google.com | b8b8ba0 | 2012-12-03 23:34:32 +0000 | [diff] [blame] | 142 | #ifdef SK_DEBUG |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 143 | newEntry->fList = this; |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Inserts a new list entry after an existing list entry. The new entry must not already be |
| 149 | * a member of this or any other list. If existingEntry is NULL then the new entry is added |
| 150 | * at the head. |
| 151 | */ |
| 152 | void addAfter(T* newEntry, T* existingEntry) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 153 | SkASSERT(newEntry); |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 154 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 155 | if (nullptr == existingEntry) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 156 | this->addToHead(newEntry); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | SkASSERT(this->isInList(existingEntry)); |
| 161 | newEntry->fPrev = existingEntry; |
| 162 | T* next = existingEntry->fNext; |
| 163 | existingEntry->fNext = newEntry; |
| 164 | newEntry->fNext = next; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 165 | if (nullptr == next) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 166 | SkASSERT(fTail == existingEntry); |
| 167 | fTail = newEntry; |
| 168 | } else { |
| 169 | next->fPrev = newEntry; |
| 170 | } |
robertphillips@google.com | b8b8ba0 | 2012-12-03 23:34:32 +0000 | [diff] [blame] | 171 | #ifdef SK_DEBUG |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 172 | newEntry->fList = this; |
| 173 | #endif |
| 174 | } |
| 175 | |
Chris Dalton | 832bd2b | 2017-06-21 12:33:39 -0700 | [diff] [blame] | 176 | void concat(SkTInternalLList&& list) { |
| 177 | if (list.isEmpty()) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | list.fHead->fPrev = fTail; |
| 182 | if (!fHead) { |
| 183 | SkASSERT(!list.fHead->fPrev); |
| 184 | fHead = list.fHead; |
| 185 | } else { |
| 186 | SkASSERT(fTail); |
| 187 | fTail->fNext = list.fHead; |
| 188 | } |
| 189 | fTail = list.fTail; |
| 190 | |
| 191 | #ifdef SK_DEBUG |
| 192 | for (T* node = list.fHead; node; node = node->fNext) { |
| 193 | SkASSERT(node->fList == &list); |
| 194 | node->fList = this; |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | list.fHead = list.fTail = nullptr; |
| 199 | } |
| 200 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 201 | bool isEmpty() const { |
Chris Dalton | 832bd2b | 2017-06-21 12:33:39 -0700 | [diff] [blame] | 202 | SkASSERT(SkToBool(fHead) == SkToBool(fTail)); |
| 203 | return !fHead; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 204 | } |
| 205 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 206 | T* head() { return fHead; } |
| 207 | T* tail() { return fTail; } |
| 208 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 209 | class Iter { |
| 210 | public: |
| 211 | enum IterStart { |
| 212 | kHead_IterStart, |
| 213 | kTail_IterStart |
| 214 | }; |
| 215 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 216 | Iter() : fCurr(nullptr) {} |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 217 | Iter(const Iter& iter) : fCurr(iter.fCurr) {} |
| 218 | Iter& operator= (const Iter& iter) { fCurr = iter.fCurr; return *this; } |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 219 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 220 | T* init(const SkTInternalLList& list, IterStart startLoc) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 221 | if (kHead_IterStart == startLoc) { |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 222 | fCurr = list.fHead; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 223 | } else { |
| 224 | SkASSERT(kTail_IterStart == startLoc); |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 225 | fCurr = list.fTail; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 226 | } |
| 227 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 228 | return fCurr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 231 | T* get() { return fCurr; } |
| 232 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 233 | /** |
| 234 | * Return the next/previous element in the list or NULL if at the end. |
| 235 | */ |
| 236 | T* next() { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 237 | if (nullptr == fCurr) { |
| 238 | return nullptr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 239 | } |
| 240 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 241 | fCurr = fCurr->fNext; |
| 242 | return fCurr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | T* prev() { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 246 | if (nullptr == fCurr) { |
| 247 | return nullptr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 248 | } |
| 249 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 250 | fCurr = fCurr->fPrev; |
| 251 | return fCurr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Dalton | f112ea2 | 2018-05-09 13:10:44 -0600 | [diff] [blame] | 254 | /** |
| 255 | * C++11 range-for interface. |
| 256 | */ |
| 257 | bool operator!=(const Iter& that) { return fCurr != that.fCurr; } |
| 258 | T* operator*() { return this->get(); } |
| 259 | void operator++() { this->next(); } |
| 260 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 261 | private: |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 262 | T* fCurr; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Chris Dalton | f112ea2 | 2018-05-09 13:10:44 -0600 | [diff] [blame] | 265 | Iter begin() const { |
| 266 | Iter iter; |
| 267 | iter.init(*this, Iter::kHead_IterStart); |
| 268 | return iter; |
| 269 | } |
| 270 | |
| 271 | Iter end() const { return Iter(); } |
| 272 | |
bsalomon@google.com | 39ac1ae | 2012-08-23 13:24:31 +0000 | [diff] [blame] | 273 | #ifdef SK_DEBUG |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 274 | void validate() const { |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 275 | SkASSERT(!fHead == !fTail); |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 276 | Iter iter; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 277 | for (T* item = iter.init(*this, Iter::kHead_IterStart); item; item = iter.next()) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 278 | SkASSERT(this->isInList(item)); |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 279 | if (nullptr == item->fPrev) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 280 | SkASSERT(fHead == item); |
| 281 | } else { |
| 282 | SkASSERT(item->fPrev->fNext == item); |
| 283 | } |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 284 | if (nullptr == item->fNext) { |
bsalomon@google.com | dd3f7a9 | 2012-12-03 19:47:41 +0000 | [diff] [blame] | 285 | SkASSERT(fTail == item); |
| 286 | } else { |
| 287 | SkASSERT(item->fNext->fPrev == item); |
| 288 | } |
| 289 | } |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 293 | * Debugging-only method that uses the list back pointer to check if 'entry' is indeed in 'this' |
| 294 | * list. |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 295 | */ |
| 296 | bool isInList(const T* entry) const { |
| 297 | return entry->fList == this; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Debugging-only method that laboriously counts the list entries. |
| 302 | */ |
| 303 | int countEntries() const { |
| 304 | int count = 0; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 305 | for (T* entry = fHead; entry; entry = entry->fNext) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 306 | ++count; |
| 307 | } |
| 308 | return count; |
| 309 | } |
| 310 | #endif // SK_DEBUG |
| 311 | |
| 312 | private: |
| 313 | T* fHead; |
| 314 | T* fTail; |
| 315 | |
| 316 | typedef SkNoncopyable INHERITED; |
| 317 | }; |
| 318 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 319 | #endif |