summaryrefslogtreecommitdiffstats
path: root/rsList.h
blob: 24720a269cd7a1ca2cd0583f1f7071c2e63ddf34 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef ANDROID_RENDERSCRIPT_LIST_H
#define ANDROID_RENDERSCRIPT_LIST_H

namespace android {
namespace renderscript {

namespace {

constexpr size_t BUFFER_SIZE = 64;

}  // anonymous namespace

template <class T>
class List {
private:
    class LinkedBuffer {
    public:
        LinkedBuffer() : next(nullptr) {}

        union {
            char raw[BUFFER_SIZE - sizeof(LinkedBuffer*)];
            T typed;
        } data;
        LinkedBuffer* next;
    };

public:
    class iterator;

    List() : last(nullptr), first(&firstBuffer.data.typed),
             beginIterator(this, &firstBuffer, const_cast<T*>(first)),
             _size(0) {
        current = const_cast<T*>(first);
        currentBuffer = &firstBuffer;
    }

    template <class InputIterator>
    List(InputIterator first, InputIterator last) : List() {
        for (InputIterator it = first; it != last; ++it) {
            push_back(*it);
        }
    }

    ~List() {
        LinkedBuffer* p = firstBuffer.next;
        LinkedBuffer* next;
        while (p != nullptr) {
            next = p->next;
            delete p;
            p = next;
        }
    }

    void push_back(const T& value) {
        last = current;
        *current++ = value;
        _size++;
        if ((void*)current >= (void*)&currentBuffer->next) {
            LinkedBuffer* newBuffer = new LinkedBuffer();
            currentBuffer->next = newBuffer;
            currentBuffer = newBuffer;
            current = &currentBuffer->data.typed;
        }
    }

    class iterator {
        friend class List;
    public:
        iterator& operator++() {
            p++;
            if ((void*)p >= (void*)&buffer->next) {
                buffer = buffer->next;
                if (buffer != nullptr) {
                    p = &buffer->data.typed;
                } else {
                    p = nullptr;
                }
            }
            return *this;
        }

        bool operator==(const iterator& other) const {
            return p == other.p && buffer == other.buffer && list == other.list;
        }

        bool operator!=(const iterator& other) const {
            return p != other.p || buffer != other.buffer || list != other.list;
        }

        const T& operator*() const { return *p; }

        T* operator->() { return p; }

    protected:
        iterator(const List* list_) : list(list_) {}
        iterator(const List* list_, LinkedBuffer* buffer_, T* p_) :
            p(p_), buffer(buffer_), list(list_) {}

    private:
        T* p;
        LinkedBuffer* buffer;
        const List* list;
    };

    const iterator& begin() const { return beginIterator; }

    iterator end() const { return iterator(this, currentBuffer, current); }

    bool empty() const { return current == first; }

    T& front() const { return *const_cast<T*>(first); }

    T& back() const { return *last; }

    size_t size() const { return _size; }

private:
    T* current;
    T* last;
    LinkedBuffer* currentBuffer;
    LinkedBuffer firstBuffer;
    const T* first;
    const iterator beginIterator;
    size_t _size;
};

}  // namespace renderscript
}  // namespace android

#endif  //  ANDROID_RENDERSCRIPT_LIST_H