OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #ifndef SkSinglyLinkedList_DEFINED | 7 #ifndef SkSinglyLinkedList_DEFINED |
8 #define SkSinglyLinkedList_DEFINED | 8 #define SkSinglyLinkedList_DEFINED |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
13 | 13 |
14 template <typename T> class SkSinglyLinkedList { | 14 template <typename T> class SkSinglyLinkedList { |
15 struct Node; | 15 struct Node; |
16 public: | 16 public: |
17 SkSinglyLinkedList() : fHead(nullptr), fTail(nullptr) {} | 17 SkSinglyLinkedList() : fHead(nullptr), fTail(nullptr) {} |
18 ~SkSinglyLinkedList() { this->reset(); } | 18 ~SkSinglyLinkedList() { this->reset(); } |
19 void reset() { | 19 void reset() { |
20 SkASSERT(fHead != nullptr || nullptr == fTail); | 20 SkASSERT(fHead != nullptr || nullptr == fTail); |
| 21 // Use a while loop rather than recursion to avoid stack overflow. |
21 Node* node = fHead; | 22 Node* node = fHead; |
22 while (node) { | 23 while (node) { |
23 Node* next = node->fNext; | 24 Node* next = node->fNext; |
24 SkASSERT(next != nullptr || node == fTail); | 25 SkASSERT(next != nullptr || node == fTail); |
25 delete node; | 26 delete node; |
26 node = next; | 27 node = next; |
27 } | 28 } |
28 fHead = nullptr; | 29 fHead = nullptr; |
29 fTail = nullptr; | 30 fTail = nullptr; |
30 } | 31 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 Node* fNext; | 87 Node* fNext; |
87 template <class... Args> | 88 template <class... Args> |
88 Node(Args&&... args) : fData(std::forward<Args>(args)...), fNext(nullptr
) {} | 89 Node(Args&&... args) : fData(std::forward<Args>(args)...), fNext(nullptr
) {} |
89 }; | 90 }; |
90 Node* fHead; | 91 Node* fHead; |
91 Node* fTail; | 92 Node* fTail; |
92 SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete; | 93 SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete; |
93 SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete; | 94 SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete; |
94 }; | 95 }; |
95 #endif // SkSinglyLinkedList_DEFINED | 96 #endif // SkSinglyLinkedList_DEFINED |
OLD | NEW |