diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-12-04 12:38:38 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-04 12:38:38 -0500 |
| commit | 5df582dd3229789364ae3fa75575fd978ca3282d (patch) | |
| tree | 89f66f7c2427030b0e9a0ed0754fc380a5f4b21c /source/core/slang-array-view.h | |
| parent | 9653dcc2c9d5d20d3d0e8918aaf1d5b09e963060 (diff) | |
Feature/string hash review (#1142)
* * Added ConstArrayView
* Made StringSlicePool have styles
* Remove point about strings not having terminating 0 (they do), and restriction around ""
* spCalcStringHash -> spComputeStringHash
* Small code improvements.
Closer to coding conventions.
* Fix small bug with Empty adding c string.
* Fix typo in assert.
* Fix ArrayView compiling issue on gcc/clang.
* Remove tabs.
* Improve comments around StringSlicePool.
Simplify getting the added slices.
Diffstat (limited to 'source/core/slang-array-view.h')
| -rw-r--r-- | source/core/slang-array-view.h | 220 |
1 files changed, 130 insertions, 90 deletions
diff --git a/source/core/slang-array-view.h b/source/core/slang-array-view.h index 8b653f4c7..56c936073 100644 --- a/source/core/slang-array-view.h +++ b/source/core/slang-array-view.h @@ -5,108 +5,148 @@ namespace Slang { - template<typename T> - class ArrayView - { - private: - T* m_buffer; - int m_count; - public: - const T* begin() const { return m_buffer; } - T* begin() { return m_buffer; } + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ConstArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + template<typename T> + class ConstArrayView + { + public: + const T* begin() const { return m_buffer; } + const T* end() const { return m_buffer + m_count; } - T* end() { return m_buffer + m_count; } - public: - ArrayView(): - m_buffer(nullptr), - m_count(0) + inline Index getCount() const { return m_count; } + + inline const T& operator [](Index idx) const + { + SLANG_ASSERT(idx >= 0 && idx < m_count); + return m_buffer[idx]; + } + + 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; + } + + 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; + } + + 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; + } + + 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; + } + + ConstArrayView() : + m_buffer(nullptr), + m_count(0) { - } - ArrayView(T& singleObj): - m_buffer(&singleObj), - m_count(1) + } + + ConstArrayView(const T* buffer, Index count) : + m_buffer(const_cast<T*>(buffer)), + m_count(count) { - } - ArrayView(T* buffer, int size): + } + + protected: + ConstArrayView(T* buffer, Index count) : m_buffer(buffer), - m_count(size) - { - } - - inline int getCount() const { return m_count; } - - inline const T& operator [](int idx) const - { - SLANG_ASSERT(idx >= 0 && idx <= m_count); - return m_buffer[idx]; - } - inline T& operator [](int idx) + m_count(count) { - SLANG_ASSERT(idx >= 0 && idx <= m_count); + } + + T* m_buffer; ///< Note that this isn't const, as is used for derived class ArrayView also + Index m_count; + }; + + template<typename T> + ConstArrayView<T> makeConstArrayView(const T& obj) + { + return ConstArrayView<T>(&obj, 1); + } + + template<typename T> + ConstArrayView<T> makeConstArrayView(const T* buffer, Index count) + { + return ConstArrayView<T>(buffer, count); + } + + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + template<typename T> + class ArrayView: public ConstArrayView<T> + { + public: + typedef ConstArrayView<T> Super; + + using Super::m_buffer; + using Super::m_count; + + using Super::begin; + T* begin() { return m_buffer; } + + using Super::end; + T* end() { return m_buffer + m_count; } + + using Super::operator[]; + inline T& operator [](Index idx) + { + SLANG_ASSERT(idx >= 0 && idx < m_count); return m_buffer[idx]; } - inline const T* getBuffer() const { return m_buffer; } + using Super::getBuffer; inline T* getBuffer() { return m_buffer; } - template<typename T2> - int indexOf(const T2 & val) const - { - for (int i = 0; i < m_count; i++) - { - if (m_buffer[i] == val) - return i; - } - return -1; - } - - template<typename T2> - int lastIndexOf(const T2 & val) const - { - for (int i = m_count - 1; i >= 0; i--) - { - if (m_buffer[i] == val) - return i; - } - return -1; - } - - template<typename Func> - int findFirstIndex(const Func& predicate) const - { - for (int i = 0; i < m_count; i++) - { - if (predicate(m_buffer[i])) - return i; - } - return -1; - } - - template<typename Func> - int findLastIndex(const Func& predicate) const - { - for (int i = m_count - 1; i >= 0; i--) - { - if (predicate(m_buffer[i])) - return i; - } - return -1; - } - }; - - template<typename T> - ArrayView<T> makeArrayView(T& obj) - { - return ArrayView<T>(obj); - } - - template<typename T> - ArrayView<T> makeArrayView(T* buffer, int count) - { - return ArrayView<T>(buffer, count); - } + ArrayView() : Super() {} + ArrayView(T* buffer, Index size) :Super(buffer, size) {} + }; + + template<typename T> + ArrayView<T> makeArrayView(T& obj) + { + return ArrayView<T>(&obj, 1); + } + + template<typename T> + ArrayView<T> makeArrayView(T* buffer, Index count) + { + return ArrayView<T>(buffer, count); + } } #endif |
