#ifndef SLANG_CORE_ARRAY_H #define SLANG_CORE_ARRAY_H #include "slang-exception.h" #include "slang-array-view.h" namespace Slang { /* An array container with fixed maximum size defined by COUNT. */ template class Array { public: T* begin() { return m_buffer; } const T* begin() const { return m_buffer; } const T* end() const { return m_buffer + m_count; } T* end() { return m_buffer + m_count; } inline Index getCapacity() const { return COUNT; } inline Index getCount() const { return m_count; } inline const T& getFirst() const { SLANG_ASSERT(m_count > 0); return m_buffer[0]; } inline T& getFirst() { SLANG_ASSERT(m_count > 0); return m_buffer[0]; } inline const T& getLast() const { SLANG_ASSERT(m_count > 0); return m_buffer[m_count - 1]; } inline T& getLast() { SLANG_ASSERT(m_count > 0); return m_buffer[m_count - 1]; } inline void setCount(Index newCount) { SLANG_ASSERT(newCount >= 0 && newCount <= COUNT); m_count = newCount; } inline void add(const T& item) { SLANG_ASSERT(m_count < COUNT); m_buffer[m_count++] = item; } inline void add(T&& item) { SLANG_ASSERT(m_count < COUNT); m_buffer[m_count++] = _Move(item); } inline const T& operator [](Index idx) const { SLANG_ASSERT(idx >= 0 && idx < m_count); return m_buffer[idx]; } inline T& operator [](Index idx) { SLANG_ASSERT(idx >= 0 && idx < m_count); return m_buffer[idx]; } inline const T* getBuffer() const { return m_buffer; } inline T* getBuffer() { return m_buffer; } inline void clear() { m_count = 0; } template Index indexOf(const T2& val) const { return getView().indexOf(val); } template Index lastIndexOf(const T2& val) const { return getView().lastIndexOf(val); } template Index findFirstIndex(const Func& predicate) const { return getView().findFirstIndex(predicate); } template Index findLastIndex(const Func& predicate) const { return getView().findLastIndex(predicate); } inline ConstArrayView getView() const { return ConstArrayView(m_buffer, m_count); } inline ConstArrayView getView(Index start, Index count) const { SLANG_ASSERT(start >= 0 && count >= 0); SLANG_ASSERT(start <= m_count && start + count < m_count); return ConstArrayView(m_buffer + start, count); } inline ArrayView getView() { return ArrayView(m_buffer, m_count); } inline ArrayView getView(Index start, Index count) { SLANG_ASSERT(start >= 0 && count >= 0); SLANG_ASSERT(start <= m_count && start + count < m_count); return ArrayView(m_buffer + start, count); } private: T m_buffer[COUNT]; Index m_count = 0; }; template struct FirstType { typedef T Type; }; template void insertArray(Array&) {} template void insertArray(Array& arr, const T& val, TArgs... args) { arr.add(val); insertArray(arr, args...); } template auto makeArray(TArgs ...args) -> Array::Type, sizeof...(args)> { Array::Type, Index(sizeof...(args))> rs; insertArray(rs, args...); return rs; } template void addToList(TList&) { } template void addToList(TList& list, T node) { list.add(node); } template void addToList(TList& list, T node, TArgs ... args) { list.add(node); addToList(list, args...); } } #endif