From 4880789e3003441732cca4471091563f36531635 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 29 Apr 2019 17:03:46 -0400 Subject: String/List closer to conventions, and use Index type (#959) * List made members m_ Tweaked types to closer match conventions. * Use asserts for checking conditions on List. Other small improvements. * List.Count() -> getSize() * List Add -> add First -> getFirst Last -> getLast RemoveLast -> removeLast ReleaseBuffer -> detachBuffer GetArrayView -> getArrayView * List:: AddRange -> addRange Capacity -> getCapacity Insert -> insert InsertRange -> insertRange AddRange -> addRange RemoveRange -> removeRange RemoveAt -> removeAt Remove -> remove Reverse -> reverse FastRemove -> fastRemove FastRemoveAt -> fastRemoveAt Clear -> clear * List FreeBuffer -> _deallocateBuffer Free -> clearAndDeallocate SwapWith -> swapWith * List SetSize -> setSize Reserve -> reserve GrowToSize growToSize * UnsafeShrinkToSize -> unsafeShrinkToSize Compress -> compress FindLast -> findLastIndex FindLast -> findLastIndex Simplify Contains * List Removed m_allocator (wasn't used) Swap -> swapElements Sort -> sort Contains -> contains ForEach -> forEach QuickSort -> quickSort InsertionSort -> insertionSort BinarySearch -> binarySearch Max -> calcMax Min -> calcMin * Initializer::Initialize -> initialize List:: Allocate -> _allocate Init -> _init IndexOf -> indexOf * * Put #include in common.h, and remove unneeded inclusions * Small refactor of ArrayView - remove stride as not used * getSize -> getCount setSize -> setCount unsafeShrinkToSize->unsafeShrinkToCount growToSize -> growToCount m_size -> m_count * Some tidy up around Allocator. * Use Index type on List. * Refactor of IntSet. First tentative look at using Index. * Made Index an Int Did preliminary fixes. Made String use Index. * Partial refactor of String. * String::Buffer -> getBuffer ToWString -> toWString * Small improvements to String. String:: Buffer() -> getBuffer() Equals() -> equals * Try to use Index where appropriate. * Fix warnings on windows x86 builds. --- source/core/array.h | 144 +++++++++++++++++++++++++--------------------------- 1 file changed, 68 insertions(+), 76 deletions(-) (limited to 'source/core/array.h') diff --git a/source/core/array.h b/source/core/array.h index 96508879f..2a5fa0aa7 100644 --- a/source/core/array.h +++ b/source/core/array.h @@ -6,138 +6,130 @@ namespace Slang { - template + template class Array { private: - T _buffer[size]; - int _count = 0; + T m_buffer[COUNT]; + int m_count = 0; public: - T* begin() const - { - return (T*)_buffer; - } - T* end() const - { - return (T*)_buffer + _count; - } + 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; } + public: - inline int GetCapacity() const + inline int getCapacity() const { return COUNT; } + inline int getCount() const { return m_count; } + inline const T& getFirst() const { - return size; + SLANG_ASSERT(m_count > 0); + return m_buffer[0]; } - inline int Count() const + inline T& getFirst() + { + SLANG_ASSERT(m_count > 0); + return m_buffer[0]; + } + inline const T& getLast() const { - return _count; + SLANG_ASSERT(m_count > 0); + return m_buffer[m_count - 1]; } - inline T & First() const + inline T& getLast() + { + SLANG_ASSERT(m_count > 0); + return m_buffer[m_count - 1]; + } + inline void setCount(int newCount) { - return const_cast(_buffer[0]); + SLANG_ASSERT(newCount >= 0 && newCount <= COUNT); + m_count = newCount; } - inline T & Last() const + inline void add(const T & item) { - return const_cast(_buffer[_count - 1]); + SLANG_ASSERT(m_count < COUNT); + m_buffer[m_count++] = item; } - inline void SetSize(int newSize) + inline void add(T && item) { -#ifdef _DEBUG - if (newSize > size) - throw IndexOutofRangeException("size too large."); -#endif - _count = newSize; - } - inline void Add(const T & item) - { -#ifdef _DEBUG - if (_count == size) - throw IndexOutofRangeException("out of range access to static array."); -#endif - _buffer[_count++] = item; - } - inline void Add(T && item) - { -#ifdef _DEBUG - if (_count == size) - throw IndexOutofRangeException("out of range access to static array."); -#endif - _buffer[_count++] = _Move(item); + SLANG_ASSERT(m_count < COUNT); + m_buffer[m_count++] = _Move(item); } - inline T & operator [](int id) const + inline const T& operator [](int idx) const { -#if _DEBUG - if (id >= _count || id < 0) - throw IndexOutofRangeException("Operator[]: Index out of Range."); -#endif - return ((T*)_buffer)[id]; + SLANG_ASSERT(idx >= 0 && idx < m_count); + return m_buffer[idx]; } + inline T& operator [](int idx) + { + SLANG_ASSERT(idx >= 0 && idx < m_count); + return m_buffer[idx]; + } - inline T* Buffer() const - { - return (T*)_buffer; - } + inline const T* getBuffer() const { return m_buffer; } + inline T* getBuffer() { return m_buffer; } - inline void Clear() - { - _count = 0; - } + inline void clear() { m_count = 0; } template - int IndexOf(const T2 & val) const + int indexOf(const T2& val) const { - for (int i = 0; i < _count; i++) + for (int i = 0; i < m_count; i++) { - if (_buffer[i] == val) + if (m_buffer[i] == val) return i; } return -1; } template - int LastIndexOf(const T2 & val) const + int lastIndexOf(const T2& val) const { - for (int i = _count - 1; i >= 0; i--) + for (int i = m_count - 1; i >= 0; i--) { - if (_buffer[i] == val) + if (m_buffer[i] == val) return i; } return -1; } - inline ArrayView GetArrayView() const + inline ArrayView getArrayView() const { - return ArrayView((T*)_buffer, _count); + return ArrayView((T*)m_buffer, m_count); } - inline ArrayView GetArrayView(int start, int count) const + inline ArrayView getArrayView(int start, int count) const { - return ArrayView((T*)_buffer + start, count); + return ArrayView((T*)m_buffer + start, count); } }; template struct FirstType { - typedef T type; + typedef T Type; }; - template - void InsertArray(Array &) {} + template + void insertArray(Array&) {} - template - void InsertArray(Array & arr, const T & val, TArgs... args) + template + void insertArray(Array& arr, const T& val, TArgs... args) { - arr.Add(val); - InsertArray(arr, args...); + arr.add(val); + insertArray(arr, args...); } template - auto MakeArray(TArgs ...args) -> Array::type, sizeof...(args)> + auto makeArray(TArgs ...args) -> Array::Type, sizeof...(args)> { - Array::type, sizeof...(args)> rs; - InsertArray(rs, args...); + Array::Type, sizeof...(args)> rs; + insertArray(rs, args...); return rs; } } -#endif \ No newline at end of file +#endif -- cgit v1.2.3