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-view.h | 111 ++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 64 deletions(-) (limited to 'source/core/array-view.h') diff --git a/source/core/array-view.h b/source/core/array-view.h index 193aedbbb..ad9673e2e 100644 --- a/source/core/array-view.h +++ b/source/core/array-view.h @@ -1,7 +1,7 @@ #ifndef CORE_LIB_ARRAY_VIEW_H #define CORE_LIB_ARRAY_VIEW_H -#include "exception.h" +#include "common.h" namespace Slang { @@ -9,104 +9,87 @@ namespace Slang class ArrayView { private: - T * _buffer; - int _count; - int stride; + T* m_buffer; + int m_count; public: - T* begin() const - { - return _buffer; - } - T* end() const - { - return (T*)((char*)_buffer + _count*stride); - } + const T* begin() const { return m_buffer; } + T* begin() { return m_buffer; } + + const T* end() const { return m_buffer + m_count; } + T* end() { return m_buffer + m_count; } + public: - ArrayView() - { - _buffer = 0; - _count = 0; - } - ArrayView(const T & singleObj) - { - SetData((T*)&singleObj, 1, sizeof(T)); - } - ArrayView(T * buffer, int count) - { - SetData(buffer, count, sizeof(T)); + ArrayView(): + m_buffer(nullptr), + m_count(0) + { } - ArrayView(void * buffer, int count, int _stride) - { - SetData(buffer, count, _stride); - } - void SetData(void * buffer, int count, int _stride) - { - this->_buffer = (T*)buffer; - this->_count = count; - this->stride = _stride; - } - inline int GetCapacity() const - { - return _count; + ArrayView(T& singleObj): + m_buffer(&singleObj), + m_count(1) + { } - inline int Count() const + ArrayView(T* buffer, int size): + m_buffer(buffer), + m_count(size) { - return _count; } + + inline int getCount() const { return m_count; } - 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*)((char*)_buffer+id*stride); + 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 _buffer; - } + inline const T* getBuffer() const { return m_buffer; } + inline T* getBuffer() { return m_buffer; } 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 (*(T*)((char*)_buffer + i*stride) == 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 (*(T*)((char*)_buffer + i*stride) == val) + if (m_buffer[i] == val) return i; } return -1; } template - int FindFirst(const Func & predicate) const + int findFirstIndex(const Func& predicate) const { - for (int i = 0; i < _count; i++) + for (int i = 0; i < m_count; i++) { - if (predicate(_buffer[i])) + if (predicate(m_buffer[i])) return i; } return -1; } template - int FindLast(const Func & predicate) const + int findLastIndex(const Func& predicate) const { - for (int i = _count - 1; i >= 0; i--) + for (int i = m_count - 1; i >= 0; i--) { - if (predicate(_buffer[i])) + if (predicate(m_buffer[i])) return i; } return -1; @@ -114,16 +97,16 @@ namespace Slang }; template - ArrayView MakeArrayView(const T & obj) + ArrayView makeArrayView(T& obj) { return ArrayView(obj); } template - ArrayView MakeArrayView(T * buffer, int count) + ArrayView makeArrayView(T* buffer, int count) { return ArrayView(buffer, count); } } -#endif \ No newline at end of file +#endif -- cgit v1.2.3