summaryrefslogtreecommitdiffstats
path: root/source/core/slang-array-view.h
blob: 21b6ce1134d458038999d5ae47d71f1e877f8c76 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef SLANG_CORE_ARRAY_VIEW_H
#define SLANG_CORE_ARRAY_VIEW_H

#include "slang-common.h"

namespace Slang
{

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ConstArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

template<typename T>
class ConstArrayView
{
public:
    typedef ConstArrayView ThisType;

    SLANG_FORCE_INLINE const T* begin() const { return m_buffer; }

    SLANG_FORCE_INLINE const T* end() const { return m_buffer + m_count; }

    SLANG_FORCE_INLINE Count getCount() const { return m_count; }

    SLANG_FORCE_INLINE const T& operator[](Index idx) const
    {
        SLANG_ASSERT(idx >= 0 && idx < m_count);
        return m_buffer[idx];
    }

    SLANG_FORCE_INLINE const T* getBuffer() const { return m_buffer; }

    template<typename T2>
    Index indexOf(const T2& val) const
    {
        for (Index i = 0; i < m_count; i++)
        {
            if (m_buffer[i] == val)
                return i;
        }
        return -1;
    }

    template<typename T2>
    Index lastIndexOf(const T2& val) const
    {
        for (Index i = m_count - 1; i >= 0; i--)
        {
            if (m_buffer[i] == val)
                return i;
        }
        return -1;
    }

    template<typename Func>
    Index findFirstIndex(const Func& predicate) const
    {
        for (Index i = 0; i < m_count; i++)
        {
            if (predicate(m_buffer[i]))
                return i;
        }
        return -1;
    }

    template<typename Func>
    Index findLastIndex(const Func& predicate) const
    {
        for (Index i = m_count - 1; i >= 0; i--)
        {
            if (predicate(m_buffer[i]))
                return i;
        }
        return -1;
    }

    bool containsMemory(const ThisType& rhs) const
    {
        return rhs.getBuffer() >= getBuffer() && rhs.end() <= end();
    }

    bool operator==(const ThisType& rhs) const
    {
        if (&rhs == this)
        {
            return true;
        }
        const Count count = getCount();
        if (count != rhs.getCount())
        {
            return false;
        }
        const T* thisEle = getBuffer();
        const T* rhsEle = rhs.getBuffer();
        for (Index i = 0; i < count; ++i)
        {
            if (thisEle[i] != rhsEle[i])
            {
                return false;
            }
        }
        return true;
    }
    SLANG_FORCE_INLINE bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }

    ThisType head(Index index) const
    {
        SLANG_ASSERT(index >= 0 && index <= m_count);
        return ThisType(m_buffer, index);
    }
    ThisType tail(Index index) const
    {
        SLANG_ASSERT(index >= 0 && index <= m_count);
        return ThisType(m_buffer + index, m_count - index);
    }

    ConstArrayView()
        : m_buffer(nullptr), m_count(0)
    {
    }

    ConstArrayView(const T* buffer, Count count)
        : m_buffer(const_cast<T*>(buffer)), m_count(count)
    {
    }

protected:
    ConstArrayView(T* buffer, Count count)
        : m_buffer(buffer), m_count(count)
    {
    }

    T* m_buffer; ///< Note that this isn't const, as is used for derived class ArrayView also
    Count m_count;
};

template<typename T>
ConstArrayView<T> makeConstArrayViewSingle(const T& obj)
{
    return ConstArrayView<T>(&obj, 1);
}

template<typename T>
ConstArrayView<T> makeConstArrayView(const T* buffer, Count count)
{
    return ConstArrayView<T>(buffer, count);
}

template<typename T, size_t N>
ConstArrayView<T> makeConstArrayView(const T (&arr)[N])
{
    return ConstArrayView<T>(arr, Index(N));
}


// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

template<typename T>
class ArrayView : public ConstArrayView<T>
{
public:
    typedef ArrayView ThisType;

    typedef ConstArrayView<T> Super;

    using Super::m_buffer;
    using Super::m_count;

    using Super::begin;
    T* begin() { return m_buffer; }

    using Super::end;
    T* end() { return m_buffer + m_count; }

    using Super::head;
    using Super::tail;

    using Super::operator[];
    inline T& operator[](Index idx)
    {
        SLANG_ASSERT(idx >= 0 && idx < m_count);
        return m_buffer[idx];
    }

    using Super::getBuffer;
    inline T* getBuffer() { return m_buffer; }

    ThisType head(Index index)
    {
        SLANG_ASSERT(index >= 0 && index <= m_count);
        return ThisType(m_buffer, index);
    }
    ThisType tail(Index index)
    {
        SLANG_ASSERT(index >= 0 && index <= m_count);
        return ThisType(m_buffer + index, m_count - index);
    }

    T& getLast() { return m_buffer[m_count - 1]; }

    ArrayView()
        : Super()
    {
    }
    ArrayView(T* buffer, Index size)
        : Super(buffer, size)
    {
    }
};

template<typename T>
ArrayView<T> makeArrayViewSingle(T& obj)
{
    return ArrayView<T>(&obj, 1);
}

template<typename T>
ArrayView<T> makeArrayView(T* buffer, Count count)
{
    return ArrayView<T>(buffer, count);
}

template<typename T, size_t N>
ArrayView<T> makeArrayView(T (&arr)[N])
{
    return ArrayView<T>(arr, Count(N));
}


} // namespace Slang

#endif