diff options
Diffstat (limited to 'source/core/slang-array-view.h')
| -rw-r--r-- | source/core/slang-array-view.h | 329 |
1 files changed, 166 insertions, 163 deletions
diff --git a/source/core/slang-array-view.h b/source/core/slang-array-view.h index 50270e0a0..21b6ce113 100644 --- a/source/core/slang-array-view.h +++ b/source/core/slang-array-view.h @@ -6,222 +6,225 @@ namespace Slang { - // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ConstArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ConstArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!! - template<typename T> - class ConstArrayView - { - public: - typedef ConstArrayView ThisType; +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* begin() const { return m_buffer; } - 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; - } + SLANG_FORCE_INLINE const T* end() const { return m_buffer + m_count; } - 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; - } + SLANG_FORCE_INLINE Count getCount() const { return m_count; } - 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; - } + SLANG_FORCE_INLINE const T& operator[](Index idx) const + { + SLANG_ASSERT(idx >= 0 && idx < m_count); + return m_buffer[idx]; + } - 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; - } + SLANG_FORCE_INLINE const T* getBuffer() const { return m_buffer; } - bool containsMemory(const ThisType& rhs) const + template<typename T2> + Index indexOf(const T2& val) const + { + for (Index i = 0; i < m_count; i++) { - return rhs.getBuffer() >= getBuffer() && rhs.end() <= end(); + if (m_buffer[i] == val) + return i; } + return -1; + } - bool operator==(const ThisType& rhs) const + template<typename T2> + Index lastIndexOf(const T2& val) const + { + for (Index i = m_count - 1; i >= 0; i--) { - 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; + if (m_buffer[i] == val) + return i; } - SLANG_FORCE_INLINE bool operator!=(const ThisType& rhs) const { return !(*this == rhs); } + return -1; + } - ThisType head(Index index) const + template<typename Func> + Index findFirstIndex(const Func& predicate) const + { + for (Index i = 0; i < m_count; i++) { - SLANG_ASSERT(index >= 0 && index <= m_count); - return ThisType(m_buffer, index); + if (predicate(m_buffer[i])) + return i; } - ThisType tail(Index index) const + return -1; + } + + template<typename Func> + Index findLastIndex(const Func& predicate) const + { + for (Index i = m_count - 1; i >= 0; i--) { - SLANG_ASSERT(index >= 0 && index <= m_count); - return ThisType(m_buffer + index, m_count - index); + if (predicate(m_buffer[i])) + return i; } + return -1; + } - ConstArrayView() : - m_buffer(nullptr), - m_count(0) + bool containsMemory(const ThisType& rhs) const + { + return rhs.getBuffer() >= getBuffer() && rhs.end() <= end(); + } + + bool operator==(const ThisType& rhs) const + { + if (&rhs == this) { + return true; } - - ConstArrayView(const T* buffer, Count count) : - m_buffer(const_cast<T*>(buffer)), - m_count(count) + const Count count = getCount(); + if (count != rhs.getCount()) { + return false; } - - protected: - ConstArrayView(T* buffer, Count count) : - m_buffer(buffer), - m_count(count) + 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); } - T* m_buffer; ///< Note that this isn't const, as is used for derived class ArrayView also - Count m_count; - }; + 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); + } - template<typename T> - ConstArrayView<T> makeConstArrayViewSingle(const T& obj) + ConstArrayView() + : m_buffer(nullptr), m_count(0) { - return ConstArrayView<T>(&obj, 1); - } + } - template<typename T> - ConstArrayView<T> makeConstArrayView(const T* buffer, Count count) + ConstArrayView(const T* buffer, Count count) + : m_buffer(const_cast<T*>(buffer)), m_count(count) { - return ConstArrayView<T>(buffer, count); } - template<typename T, size_t N> - ConstArrayView<T> makeConstArrayView(const T (&arr)[N]) +protected: + ConstArrayView(T* buffer, Count count) + : m_buffer(buffer), m_count(count) { - return ConstArrayView<T>(arr, Index(N)); } + T* m_buffer; ///< Note that this isn't const, as is used for derived class ArrayView also + Count m_count; +}; - // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +template<typename T> +ConstArrayView<T> makeConstArrayViewSingle(const T& obj) +{ + return ConstArrayView<T>(&obj, 1); +} - template<typename T> - class ArrayView: public ConstArrayView<T> - { - public: - typedef ArrayView ThisType; +template<typename T> +ConstArrayView<T> makeConstArrayView(const T* buffer, Count count) +{ + return ConstArrayView<T>(buffer, count); +} - typedef ConstArrayView<T> Super; - - using Super::m_buffer; - using Super::m_count; +template<typename T, size_t N> +ConstArrayView<T> makeConstArrayView(const T (&arr)[N]) +{ + return ConstArrayView<T>(arr, Index(N)); +} - using Super::begin; - T* begin() { return m_buffer; } - using Super::end; - T* end() { return m_buffer + m_count; } - - using Super::head; - using Super::tail; +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - using Super::operator[]; - inline T& operator [](Index idx) - { - SLANG_ASSERT(idx >= 0 && idx < m_count); - return m_buffer[idx]; - } +template<typename T> +class ArrayView : public ConstArrayView<T> +{ +public: + typedef ArrayView ThisType; - using Super::getBuffer; - inline T* getBuffer() { return m_buffer; } + typedef ConstArrayView<T> Super; - 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); - } + using Super::m_buffer; + using Super::m_count; + + using Super::begin; + T* begin() { return m_buffer; } - T& getLast() { return m_buffer[m_count - 1]; } + using Super::end; + T* end() { return m_buffer + m_count; } - ArrayView() : Super() {} - ArrayView(T* buffer, Index size) :Super(buffer, size) {} - }; + using Super::head; + using Super::tail; - template<typename T> - ArrayView<T> makeArrayViewSingle(T& obj) + using Super::operator[]; + inline T& operator[](Index idx) { - return ArrayView<T>(&obj, 1); - } - - template<typename T> - ArrayView<T> makeArrayView(T* buffer, Count count) + SLANG_ASSERT(idx >= 0 && idx < m_count); + return m_buffer[idx]; + } + + using Super::getBuffer; + inline T* getBuffer() { return m_buffer; } + + ThisType head(Index index) { - return ArrayView<T>(buffer, count); + 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]; } - template<typename T, size_t N> - ArrayView<T> makeArrayView(T (&arr)[N]) + ArrayView() + : Super() { - return ArrayView<T>(arr, Count(N)); } + 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 |
