yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
4.9 KiB230 linesraw
1#ifndef SLANG_CORE_ARRAY_VIEW_H
2#define SLANG_CORE_ARRAY_VIEW_H
3
4#include "slang-common.h"
5
6namespace Slang
7{
8
9// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ConstArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10
11template<typename T>
12class ConstArrayView
13{
14public:
15    typedef ConstArrayView ThisType;
16
17    SLANG_FORCE_INLINE const T* begin() const { return m_buffer; }
18
19    SLANG_FORCE_INLINE const T* end() const { return m_buffer + m_count; }
20
21    SLANG_FORCE_INLINE Count getCount() const { return m_count; }
22
23    SLANG_FORCE_INLINE const T& operator[](Index idx) const
24    {
25        SLANG_ASSERT(idx >= 0 && idx < m_count);
26        return m_buffer[idx];
27    }
28
29    SLANG_FORCE_INLINE const T* getBuffer() const { return m_buffer; }
30
31    template<typename T2>
32    Index indexOf(const T2& val) const
33    {
34        for (Index i = 0; i < m_count; i++)
35        {
36            if (m_buffer[i] == val)
37                return i;
38        }
39        return -1;
40    }
41
42    template<typename T2>
43    Index lastIndexOf(const T2& val) const
44    {
45        for (Index i = m_count - 1; i >= 0; i--)
46        {
47            if (m_buffer[i] == val)
48                return i;
49        }
50        return -1;
51    }
52
53    template<typename Func>
54    Index findFirstIndex(const Func& predicate) const
55    {
56        for (Index i = 0; i < m_count; i++)
57        {
58            if (predicate(m_buffer[i]))
59                return i;
60        }
61        return -1;
62    }
63
64    template<typename Func>
65    Index findLastIndex(const Func& predicate) const
66    {
67        for (Index i = m_count - 1; i >= 0; i--)
68        {
69            if (predicate(m_buffer[i]))
70                return i;
71        }
72        return -1;
73    }
74
75    bool containsMemory(const ThisType& rhs) const
76    {
77        return rhs.getBuffer() >= getBuffer() && rhs.end() <= end();
78    }
79
80    bool operator==(const ThisType& rhs) const
81    {
82        if (&rhs == this)
83        {
84            return true;
85        }
86        const Count count = getCount();
87        if (count != rhs.getCount())
88        {
89            return false;
90        }
91        const T* thisEle = getBuffer();
92        const T* rhsEle = rhs.getBuffer();
93        for (Index i = 0; i < count; ++i)
94        {
95            if (thisEle[i] != rhsEle[i])
96            {
97                return false;
98            }
99        }
100        return true;
101    }
102    SLANG_FORCE_INLINE bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
103
104    ThisType head(Index index) const
105    {
106        SLANG_ASSERT(index >= 0 && index <= m_count);
107        return ThisType(m_buffer, index);
108    }
109    ThisType tail(Index index) const
110    {
111        SLANG_ASSERT(index >= 0 && index <= m_count);
112        return ThisType(m_buffer + index, m_count - index);
113    }
114
115    ConstArrayView()
116        : m_buffer(nullptr), m_count(0)
117    {
118    }
119
120    ConstArrayView(const T* buffer, Count count)
121        : m_buffer(const_cast<T*>(buffer)), m_count(count)
122    {
123    }
124
125protected:
126    ConstArrayView(T* buffer, Count count)
127        : m_buffer(buffer), m_count(count)
128    {
129    }
130
131    T* m_buffer; ///< Note that this isn't const, as is used for derived class ArrayView also
132    Count m_count;
133};
134
135template<typename T>
136ConstArrayView<T> makeConstArrayViewSingle(const T& obj)
137{
138    return ConstArrayView<T>(&obj, 1);
139}
140
141template<typename T>
142ConstArrayView<T> makeConstArrayView(const T* buffer, Count count)
143{
144    return ConstArrayView<T>(buffer, count);
145}
146
147template<typename T, size_t N>
148ConstArrayView<T> makeConstArrayView(const T (&arr)[N])
149{
150    return ConstArrayView<T>(arr, Index(N));
151}
152
153
154// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
155
156template<typename T>
157class ArrayView : public ConstArrayView<T>
158{
159public:
160    typedef ArrayView ThisType;
161
162    typedef ConstArrayView<T> Super;
163
164    using Super::m_buffer;
165    using Super::m_count;
166
167    using Super::begin;
168    T* begin() { return m_buffer; }
169
170    using Super::end;
171    T* end() { return m_buffer + m_count; }
172
173    using Super::head;
174    using Super::tail;
175
176    using Super::operator[];
177    inline T& operator[](Index idx)
178    {
179        SLANG_ASSERT(idx >= 0 && idx < m_count);
180        return m_buffer[idx];
181    }
182
183    using Super::getBuffer;
184    inline T* getBuffer() { return m_buffer; }
185
186    ThisType head(Index index)
187    {
188        SLANG_ASSERT(index >= 0 && index <= m_count);
189        return ThisType(m_buffer, index);
190    }
191    ThisType tail(Index index)
192    {
193        SLANG_ASSERT(index >= 0 && index <= m_count);
194        return ThisType(m_buffer + index, m_count - index);
195    }
196
197    T& getLast() { return m_buffer[m_count - 1]; }
198
199    ArrayView()
200        : Super()
201    {
202    }
203    ArrayView(T* buffer, Index size)
204        : Super(buffer, size)
205    {
206    }
207};
208
209template<typename T>
210ArrayView<T> makeArrayViewSingle(T& obj)
211{
212    return ArrayView<T>(&obj, 1);
213}
214
215template<typename T>
216ArrayView<T> makeArrayView(T* buffer, Count count)
217{
218    return ArrayView<T>(buffer, count);
219}
220
221template<typename T, size_t N>
222ArrayView<T> makeArrayView(T (&arr)[N])
223{
224    return ArrayView<T>(arr, Count(N));
225}
226
227
228} // namespace Slang
229
230#endif