aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkSinglyLinkedList.h
blob: 981ce76f68bd058d0c8978e830b285f656f14800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkSinglyLinkedList_DEFINED
#define SkSinglyLinkedList_DEFINED

#include <utility>

#include "SkMakeUnique.h"
#include "SkTypes.h"

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

private:
    struct Node {
        T fData;
        std::unique_ptr<Node> fNext;
        template <class... Args>
        Node(std::unique_ptr<Node> n, Args&&... args)
            : fData(std::forward<Args>(args)...), fNext(std::move(n)) {}
    };
    std::unique_ptr<Node> fHead;
    Node* fTail = nullptr;
    SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete;
    SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete;
};
#endif  // SkSinglyLinkedList_DEFINED