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