yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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 : 14T * begin () {return m_buffer ; } 15const T * begin ()const {return m_buffer ; } 16 17const T * end ()const {return m_buffer + m_count ; } 18T * end () {return m_buffer + m_count ; } 19 20inline Index getCapacity ()const {return COUNT ; } 21inline Index getCount ()const {return m_count ; } 22inline const T & getFirst ()const 23 { 24SLANG_ASSERT (m_count > 0 ); 25return m_buffer[ 0 ]; 26} 27inline T & getFirst () 28{ 29SLANG_ASSERT (m_count > 0 ); 30return m_buffer[ 0 ]; 31} 32inline const T & getLast () const 33{ 34SLANG_ASSERT ( m_count > 0 ); 35return m_buffer[m_count - 1 ]; 36} 37inline T & getLast () 38{ 39SLANG_ASSERT (m_count > 0 ); 40return m_buffer[m_count - 1 ]; 41} 42inline void setCount ( Index newCount) 43{ 44SLANG_ASSERT (newCount >= 0 && newCount <= COUNT ); 45m_count = newCount; 46} 47inline void add ( const T & item) 48{ 49SLANG_ASSERT (m_count < COUNT ); 50m_buffer[m_count ++ ] = item; 51} 52inline void add ( T && item) 53{ 54SLANG_ASSERT (m_count < COUNT ); 55m_buffer[m_count ++ ] = _Move (item); 56} 57 58inline const T & operator[]( Index idx) const 59{ 60SLANG_ASSERT (idx >= 0 && idx < m_count); 61return m_buffer[idx]; 62} 63inline T & operator[]( Index idx) 64{ 65SLANG_ASSERT (idx >= 0 && idx < m_count); 66return m_buffer[idx]; 67} 68 69inline const T * getBuffer () const { return m_buffer; } 70inline T * getBuffer () { return m_buffer; } 71 72inline void clear () { m_count = 0 ; } 73 74template < typename T2 > 75Index indexOf( const T2 & val) const 76{ 77return getView (). indexOf (val); 78} 79template < typename T2 > 80Index lastIndexOf( const T2 & val) const 81{ 82return getView (). lastIndexOf (val); 83} 84template < typename Func > 85Index findFirstIndex( const Func & predicate) const 86{ 87return getView (). findFirstIndex (predicate); 88} 89template < typename Func > 90Index findLastIndex( const Func & predicate) const 91{ 92return getView (). findLastIndex (predicate); 93} 94 95inline ConstArrayView < T > getView () const { return ConstArrayView < T > (m_buffer, m_count); } 96inline ConstArrayView < T > getView ( Index start, Index count) const 97{ 98SLANG_ASSERT (start >= 0 && count >= 0 ); 99SLANG_ASSERT (start <= m_count && start + count < m_count); 100return ConstArrayView < T > (m_buffer + start, count); 101} 102 103inline ArrayView < T > getView () { return ArrayView < T > (m_buffer, m_count); } 104inline ArrayView < T > getView ( Index start, Index count) 105{ 106SLANG_ASSERT (start >= 0 && count >= 0 ); 107SLANG_ASSERT (start <= m_count && start + count < m_count); 108return ArrayView < T > (m_buffer + start, count); 109} 110 111private : 112T m_buffer[ COUNT ]; 113Index m_count = 0 ; 114}; 115 116template < typename T > 117class Array < T , 0 > 118{ 119public : 120T * begin () { return nullptr ; } 121const T * begin () const { return nullptr ; } 122 123const T * end () const { return nullptr ; } 124T * end () { return nullptr ; } 125 126inline Index getCapacity () const { return 0 ; } 127inline Index getCount () const { return 0 ; } 128inline void setCount ( Index newCount) { SLANG_ASSERT (newCount == 0 ); } 129inline const T * getBuffer () const { return nullptr ; } 130inline T * getBuffer () { return nullptr ; } 131inline void clear () {} 132 133template < typename T2 > 134Index indexOf( const T2 & val) const 135{ 136return getView (). indexOf (val); 137} 138template < typename T2 > 139Index lastIndexOf( const T2 & val) const 140{ 141return getView (). lastIndexOf (val); 142} 143template < typename Func > 144Index findFirstIndex( const Func & predicate) const 145{ 146return getView (). findFirstIndex (predicate); 147} 148template < typename Func > 149Index findLastIndex( const Func & predicate) const 150{ 151return getView (). findLastIndex (predicate); 152} 153 154inline ConstArrayView < T > getView () const { return ConstArrayView < T > ( nullptr , 0 ); } 155inline ConstArrayView < T > getView ( Index start, Index count) const 156{ 157SLANG_ASSERT (start == 0 && count == 0 ); 158return ConstArrayView < T > ( nullptr , 0 ); 159} 160 161inline ArrayView < T > getView () { return ArrayView < T > ( nullptr , 0 ); } 162inline ArrayView < T > getView ( Index start, Index count) 163{ 164SLANG_ASSERT (start == 0 && count == 0 ); 165return ArrayView < T > ( nullptr , 0 ); 166} 167}; 168 169template < typename T , typename... TArgs > 170struct FirstType 171{ 172typedef 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{ 184arr. add (val); 185insertArray < T > (arr, args...); 186} 187 188template < typename... TArgs > 189auto makeArray (TArgs... args) -> Array < typename FirstType < TArgs... > ::Type, sizeof ...(args) > 190{ 191Array < typename FirstType < TArgs... > ::Type, Index ( sizeof ...(args)) > rs; 192insertArray < typename FirstType < TArgs... > ::Type > (rs, args...); 193return rs; 194} 195 196template < typename T > 197auto makeArray () -> Array < T , 0 > 198{ 199return 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{ 210list. add (node); 211} 212template < typename TList, typename T , typename... TArgs > 213void addToList (TList & list, T node, TArgs... args) 214{ 215list. add (node); 216addToList (list, args...); 217} 218} // namespace Slang 219 220#endif