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