diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-04-29 17:03:46 -0400 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-04-29 14:03:46 -0700 |
| commit | 4880789e3003441732cca4471091563f36531635 (patch) | |
| tree | 8e0d3ed58a561373b35729d24787afe6b39732e3 /source/core/array.h | |
| parent | ded340beb4b5197b559626acc39920abb2d39e77 (diff) | |
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<T>.Count() -> getSize()
* List<T>
Add -> add
First -> getFirst
Last -> getLast
RemoveLast -> removeLast
ReleaseBuffer -> detachBuffer
GetArrayView -> getArrayView
* List<T>::
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<T>
FreeBuffer -> _deallocateBuffer
Free -> clearAndDeallocate
SwapWith -> swapWith
* List<T>
SetSize -> setSize
Reserve -> reserve
GrowToSize growToSize
* UnsafeShrinkToSize -> unsafeShrinkToSize
Compress -> compress
FindLast -> findLastIndex
FindLast -> findLastIndex
Simplify Contains
* List<T>
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<T>::
Allocate -> _allocate
Init -> _init
IndexOf -> indexOf
* * Put #include <assert.h> 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.
Diffstat (limited to 'source/core/array.h')
| -rw-r--r-- | source/core/array.h | 144 |
1 files changed, 68 insertions, 76 deletions
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<typename T, int size> + template<typename T, int COUNT> 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<T&>(_buffer[0]); + SLANG_ASSERT(newCount >= 0 && newCount <= COUNT); + m_count = newCount; } - inline T & Last() const + inline void add(const T & item) { - return const_cast<T&>(_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<typename T2> - 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<typename T2> - 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<T> GetArrayView() const + inline ArrayView<T> getArrayView() const { - return ArrayView<T>((T*)_buffer, _count); + return ArrayView<T>((T*)m_buffer, m_count); } - inline ArrayView<T> GetArrayView(int start, int count) const + inline ArrayView<T> getArrayView(int start, int count) const { - return ArrayView<T>((T*)_buffer + start, count); + return ArrayView<T>((T*)m_buffer + start, count); } }; template<typename T, typename ...TArgs> struct FirstType { - typedef T type; + typedef T Type; }; - template<typename T, int size> - void InsertArray(Array<T, size> &) {} + template<typename T, int SIZE> + void insertArray(Array<T, SIZE>&) {} - template<typename T, typename ...TArgs, int size> - void InsertArray(Array<T, size> & arr, const T & val, TArgs... args) + template<typename T, typename ...TArgs, int SIZE> + void insertArray(Array<T, SIZE>& arr, const T& val, TArgs... args) { - arr.Add(val); - InsertArray(arr, args...); + arr.add(val); + insertArray(arr, args...); } template<typename ...TArgs> - auto MakeArray(TArgs ...args) -> Array<typename FirstType<TArgs...>::type, sizeof...(args)> + auto makeArray(TArgs ...args) -> Array<typename FirstType<TArgs...>::Type, sizeof...(args)> { - Array<typename FirstType<TArgs...>::type, sizeof...(args)> rs; - InsertArray(rs, args...); + Array<typename FirstType<TArgs...>::Type, sizeof...(args)> rs; + insertArray(rs, args...); return rs; } } -#endif
\ No newline at end of file +#endif |
