yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
5.5 KiB220 linesraw
1#ifndef SLANG_CORE_ARRAY_H
2#define SLANG_CORE_ARRAY_H
3
4#include "slang-array-view.h"
5#include "slang-exception.h"
6
7namespace Slang
8{
9/* An array container with fixed maximum size defined by COUNT. */
10template<typename T, Index COUNT>
11class Array
12{
13public:
14    T* begin() { return m_buffer; }
15    const T* begin() const { return m_buffer; }
16
17    const T* end() const { return m_buffer + m_count; }
18    T* end() { return m_buffer + m_count; }
19
20    inline Index getCapacity() const { return COUNT; }
21    inline Index getCount() const { return m_count; }
22    inline const T& getFirst() const
23    {
24        SLANG_ASSERT(m_count > 0);
25        return m_buffer[0];
26    }
27    inline T& getFirst()
28    {
29        SLANG_ASSERT(m_count > 0);
30        return m_buffer[0];
31    }
32    inline const T& getLast() const
33    {
34        SLANG_ASSERT(m_count > 0);
35        return m_buffer[m_count - 1];
36    }
37    inline T& getLast()
38    {
39        SLANG_ASSERT(m_count > 0);
40        return m_buffer[m_count - 1];
41    }
42    inline void setCount(Index newCount)
43    {
44        SLANG_ASSERT(newCount >= 0 && newCount <= COUNT);
45        m_count = newCount;
46    }
47    inline void add(const T& item)
48    {
49        SLANG_ASSERT(m_count < COUNT);
50        m_buffer[m_count++] = item;
51    }
52    inline void add(T&& item)
53    {
54        SLANG_ASSERT(m_count < COUNT);
55        m_buffer[m_count++] = _Move(item);
56    }
57
58    inline const T& operator[](Index idx) const
59    {
60        SLANG_ASSERT(idx >= 0 && idx < m_count);
61        return m_buffer[idx];
62    }
63    inline T& operator[](Index idx)
64    {
65        SLANG_ASSERT(idx >= 0 && idx < m_count);
66        return m_buffer[idx];
67    }
68
69    inline const T* getBuffer() const { return m_buffer; }
70    inline T* getBuffer() { return m_buffer; }
71
72    inline void clear() { m_count = 0; }
73
74    template<typename T2>
75    Index indexOf(const T2& val) const
76    {
77        return getView().indexOf(val);
78    }
79    template<typename T2>
80    Index lastIndexOf(const T2& val) const
81    {
82        return getView().lastIndexOf(val);
83    }
84    template<typename Func>
85    Index findFirstIndex(const Func& predicate) const
86    {
87        return getView().findFirstIndex(predicate);
88    }
89    template<typename Func>
90    Index findLastIndex(const Func& predicate) const
91    {
92        return getView().findLastIndex(predicate);
93    }
94
95    inline ConstArrayView<T> getView() const { return ConstArrayView<T>(m_buffer, m_count); }
96    inline ConstArrayView<T> getView(Index start, Index count) const
97    {
98        SLANG_ASSERT(start >= 0 && count >= 0);
99        SLANG_ASSERT(start <= m_count && start + count < m_count);
100        return ConstArrayView<T>(m_buffer + start, count);
101    }
102
103    inline ArrayView<T> getView() { return ArrayView<T>(m_buffer, m_count); }
104    inline ArrayView<T> getView(Index start, Index count)
105    {
106        SLANG_ASSERT(start >= 0 && count >= 0);
107        SLANG_ASSERT(start <= m_count && start + count < m_count);
108        return ArrayView<T>(m_buffer + start, count);
109    }
110
111private:
112    T m_buffer[COUNT];
113    Index m_count = 0;
114};
115
116template<typename T>
117class Array<T, 0>
118{
119public:
120    T* begin() { return nullptr; }
121    const T* begin() const { return nullptr; }
122
123    const T* end() const { return nullptr; }
124    T* end() { return nullptr; }
125
126    inline Index getCapacity() const { return 0; }
127    inline Index getCount() const { return 0; }
128    inline void setCount(Index newCount) { SLANG_ASSERT(newCount == 0); }
129    inline const T* getBuffer() const { return nullptr; }
130    inline T* getBuffer() { return nullptr; }
131    inline void clear() {}
132
133    template<typename T2>
134    Index indexOf(const T2& val) const
135    {
136        return getView().indexOf(val);
137    }
138    template<typename T2>
139    Index lastIndexOf(const T2& val) const
140    {
141        return getView().lastIndexOf(val);
142    }
143    template<typename Func>
144    Index findFirstIndex(const Func& predicate) const
145    {
146        return getView().findFirstIndex(predicate);
147    }
148    template<typename Func>
149    Index findLastIndex(const Func& predicate) const
150    {
151        return getView().findLastIndex(predicate);
152    }
153
154    inline ConstArrayView<T> getView() const { return ConstArrayView<T>(nullptr, 0); }
155    inline ConstArrayView<T> getView(Index start, Index count) const
156    {
157        SLANG_ASSERT(start == 0 && count == 0);
158        return ConstArrayView<T>(nullptr, 0);
159    }
160
161    inline ArrayView<T> getView() { return ArrayView<T>(nullptr, 0); }
162    inline ArrayView<T> getView(Index start, Index count)
163    {
164        SLANG_ASSERT(start == 0 && count == 0);
165        return ArrayView<T>(nullptr, 0);
166    }
167};
168
169template<typename T, typename... TArgs>
170struct FirstType
171{
172    typedef T Type;
173};
174
175
176template<typename T, Index SIZE>
177void insertArray(Array<T, SIZE>&)
178{
179}
180
181template<typename T, typename... TArgs, Index SIZE>
182void insertArray(Array<T, SIZE>& arr, const T& val, TArgs... args)
183{
184    arr.add(val);
185    insertArray<T>(arr, args...);
186}
187
188template<typename... TArgs>
189auto makeArray(TArgs... args) -> Array<typename FirstType<TArgs...>::Type, sizeof...(args)>
190{
191    Array<typename FirstType<TArgs...>::Type, Index(sizeof...(args))> rs;
192    insertArray<typename FirstType<TArgs...>::Type>(rs, args...);
193    return rs;
194}
195
196template<typename T>
197auto makeArray() -> Array<T, 0>
198{
199    return Array<T, 0>();
200}
201
202
203template<typename TList>
204void addToList(TList&)
205{
206}
207template<typename TList, typename T>
208void addToList(TList& list, T node)
209{
210    list.add(node);
211}
212template<typename TList, typename T, typename... TArgs>
213void addToList(TList& list, T node, TArgs... args)
214{
215    list.add(node);
216    addToList(list, args...);
217}
218} // namespace Slang
219
220#endif