OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 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 #ifndef SkSinglyLinkedList_DEFINED | |
8 #define SkSinglyLinkedList_DEFINED | |
9 | |
10 #include <new> | |
11 #include <utility> | |
12 | |
13 #include "SkTypes.h" | |
14 | |
15 template <typename T> class SkSinglyLinkedList { | |
16 struct Node; | |
17 public: | |
18 SkSinglyLinkedList() : fHead(nullptr), fTail(nullptr) {} | |
19 ~SkSinglyLinkedList() { | |
20 SkASSERT(fHead != nullptr || nullptr == fTail); | |
21 Node* node = fHead; | |
22 while (node) { | |
23 Node* next = node->fNext; | |
24 SkASSERT(next != nullptr || node == fTail); | |
25 delete node; | |
26 node = next; | |
27 } | |
28 } | |
29 void reset() { | |
30 this->~SkSinglyLinkedList(); | |
31 new (this) SkSinglyLinkedList<T>(); | |
bungeman-skia
2016/03/28 16:28:18
Instead of doing it this way, put the clearing cod
hal.canary
2016/03/28 17:13:30
Done.
| |
32 } | |
33 T* back() { return fTail ? &fTail->fData : nullptr; } | |
34 T* front() { return fHead ? &fHead->fData : nullptr; } | |
35 int count() { | |
bungeman-skia
2016/03/28 16:28:17
Is it expected that count will be O(n)?
hal.canary
2016/03/28 16:31:22
only used in asserts.
| |
36 int count = 0; | |
37 for (Node* node = fHead; node; node = node->fNext) { | |
38 ++count; | |
39 } | |
40 return count; | |
41 } | |
42 void pop_front() { | |
43 if (Node* node = fHead) { | |
44 fHead = node->fNext; | |
45 delete node; | |
46 if (fHead == nullptr) { | |
47 fTail = nullptr; | |
48 } | |
49 } | |
50 } | |
51 template <class... Args> T* emplace_front(Args&&... args) { | |
52 Node* n = new Node(std::forward<Args>(args)...); | |
53 n->fNext = fHead; | |
54 if (!fTail) { | |
55 fTail = n; | |
56 SkASSERT(!fHead); | |
57 } | |
58 fHead = n; | |
59 return &n->fData; | |
60 } | |
61 template <class... Args> T* emplace_back(Args&&... args) { | |
62 Node* n = new Node(std::forward<Args>(args)...); | |
63 if (fTail) { | |
64 fTail->fNext = n; | |
65 } else { | |
66 fHead = n; | |
67 } | |
68 fTail = n; | |
69 return &n->fData; | |
70 } | |
71 class ConstIter { | |
72 public: | |
73 void operator++() { fNode = fNode->fNext; } | |
74 const T& operator*() const { return fNode->fData; } | |
75 bool operator!=(const ConstIter& rhs) const { return fNode != rhs.fNode; } | |
76 ConstIter(const Node* n) : fNode(n) {} | |
77 private: | |
78 const Node* fNode; | |
79 }; | |
80 ConstIter begin() const { return ConstIter(fHead); } | |
81 ConstIter end() const { return ConstIter(nullptr); } | |
82 | |
83 private: | |
84 struct Node { | |
85 T fData; | |
86 Node* fNext; | |
87 template <class... Args> | |
88 Node(Args&&... args) : fData(std::forward<Args>(args)...), fNext(nullptr ) {} | |
89 }; | |
90 Node* fHead; | |
91 Node* fTail; | |
92 SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete; | |
93 SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete; | |
94 }; | |
95 #endif // SkSinglyLinkedList_DEFINED | |
OLD | NEW |