SkSinglyLinkedList: use unique_ptr to manage ownership

Change-Id: I7b0fe6bd601b9ae0e9042ff28aa5b4dec9a9639b
Reviewed-on: https://skia-review.googlesource.com/126180
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
This commit is contained in:
Hal Canary 2018-05-04 14:57:47 -04:00 committed by Skia Commit-Bot
parent fa7e9a813e
commit 694ebb348a

View File

@ -9,24 +9,23 @@
#include <utility> #include <utility>
#include "SkMakeUnique.h"
#include "SkTypes.h" #include "SkTypes.h"
template <typename T> class SkSinglyLinkedList { template <typename T> class SkSinglyLinkedList {
struct Node; struct Node;
public: public:
SkSinglyLinkedList() : fHead(nullptr), fTail(nullptr) {} SkSinglyLinkedList() {}
~SkSinglyLinkedList() { this->reset(); } ~SkSinglyLinkedList() { this->reset(); }
void reset() { void reset() {
SkASSERT(fHead != nullptr || nullptr == fTail); SkASSERT(fHead != nullptr || nullptr == fTail);
// Use a while loop rather than recursion to avoid stack overflow. // Use a while loop rather than recursion to avoid stack overflow.
Node* node = fHead; std::unique_ptr<Node> node = std::move(fHead);
while (node) { while (node) {
Node* next = node->fNext; std::unique_ptr<Node> next = std::move(node->fNext);
SkASSERT(next != nullptr || node == fTail); SkASSERT(next || node.get() == fTail);
delete node; node = std::move(next);
node = next;
} }
fHead = nullptr;
fTail = nullptr; fTail = nullptr;
} }
T* back() { return fTail ? &fTail->fData : nullptr; } T* back() { return fTail ? &fTail->fData : nullptr; }
@ -35,62 +34,55 @@ public:
#ifdef SK_DEBUG #ifdef SK_DEBUG
int count() { // O(n), debug only. int count() { // O(n), debug only.
int count = 0; int count = 0;
for (Node* node = fHead; node; node = node->fNext) { for (Node* node = fHead.get(); node; node = node->fNext.get()) {
++count; ++count;
} }
return count; return count;
} }
#endif #endif
void pop_front() { void pop_front() {
if (Node* node = fHead) { if (fHead) {
fHead = node->fNext; fHead = std::move(fHead->fNext);
delete node; if (!fHead) {
if (fHead == nullptr) {
fTail = nullptr; fTail = nullptr;
} }
} }
} }
template <class... Args> T* emplace_front(Args&&... args) { template <class... Args> T* emplace_front(Args&&... args) {
Node* n = new Node(std::forward<Args>(args)...); fHead = skstd::make_unique<Node>(std::move(fHead), std::forward<Args>(args)...);
n->fNext = fHead;
if (!fTail) { if (!fTail) {
fTail = n; fTail = fHead.get();
SkASSERT(!fHead);
} }
fHead = n; return &fHead->fData;
return &n->fData;
} }
template <class... Args> T* emplace_back(Args&&... args) { template <class... Args> T* emplace_back(Args&&... args) {
Node* n = new Node(std::forward<Args>(args)...); std::unique_ptr<Node>* dst = fTail ? &fTail->fNext : &fHead;
if (fTail) { *dst = skstd::make_unique<Node>(nullptr, std::forward<Args>(args)...);
fTail->fNext = n; fTail = dst->get();
} else { return &fTail->fData;
fHead = n;
}
fTail = n;
return &n->fData;
} }
class ConstIter { class ConstIter {
public: public:
void operator++() { fNode = fNode->fNext; } void operator++() { fNode = fNode->fNext.get(); }
const T& operator*() const { return fNode->fData; } const T& operator*() const { return fNode->fData; }
bool operator!=(const ConstIter& rhs) const { return fNode != rhs.fNode; } bool operator!=(const ConstIter& rhs) const { return fNode != rhs.fNode; }
ConstIter(const Node* n) : fNode(n) {} ConstIter(const Node* n) : fNode(n) {}
private: private:
const Node* fNode; const Node* fNode;
}; };
ConstIter begin() const { return ConstIter(fHead); } ConstIter begin() const { return ConstIter(fHead.get()); }
ConstIter end() const { return ConstIter(nullptr); } ConstIter end() const { return ConstIter(nullptr); }
private: private:
struct Node { struct Node {
T fData; T fData;
Node* fNext; std::unique_ptr<Node> fNext;
template <class... Args> template <class... Args>
Node(Args&&... args) : fData(std::forward<Args>(args)...), fNext(nullptr) {} Node(std::unique_ptr<Node> n, Args&&... args)
: fData(std::forward<Args>(args)...), fNext(std::move(n)) {}
}; };
Node* fHead; std::unique_ptr<Node> fHead;
Node* fTail; Node* fTail = nullptr;
SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete; SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete;
SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete; SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete;
}; };