diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-06-15 13:24:25 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-06-15 13:24:25 -0700 |
| commit | 205187b561c3b31fa931e73e8f7263f0c4b1de41 (patch) | |
| tree | 7bd2cd5ae3c14416b71ef8319ff02ace429d1132 /source/core/array.h | |
| parent | 517513645afb8eaf4841e7b7035f1ba3a9c7cd57 (diff) | |
Rename `CoreLib::*` to `Slang`
Getting rid of more namespace complexity and stripping things down to the basics.
This also gets rid of some dead code in the "core" library.
Diffstat (limited to 'source/core/array.h')
| -rw-r--r-- | source/core/array.h | 219 |
1 files changed, 108 insertions, 111 deletions
diff --git a/source/core/array.h b/source/core/array.h index b6dbeab07..96508879f 100644 --- a/source/core/array.h +++ b/source/core/array.h @@ -4,142 +4,139 @@ #include "exception.h" #include "array-view.h" -namespace CoreLib +namespace Slang { - namespace Basic + template<typename T, int size> + class Array { - template<typename T, int size> - class Array - { - private: - T _buffer[size]; - int _count = 0; - public: - T* begin() const - { - return (T*)_buffer; - } - T* end() const - { - return (T*)_buffer + _count; - } - public: - inline int GetCapacity() const - { - return size; - } - inline int Count() const - { - return _count; - } - inline T & First() const - { - return const_cast<T&>(_buffer[0]); - } - inline T & Last() const - { - return const_cast<T&>(_buffer[_count - 1]); - } - inline void SetSize(int newSize) - { + private: + T _buffer[size]; + int _count = 0; + public: + T* begin() const + { + return (T*)_buffer; + } + T* end() const + { + return (T*)_buffer + _count; + } + public: + inline int GetCapacity() const + { + return size; + } + inline int Count() const + { + return _count; + } + inline T & First() const + { + return const_cast<T&>(_buffer[0]); + } + inline T & Last() const + { + return const_cast<T&>(_buffer[_count - 1]); + } + inline void SetSize(int newSize) + { #ifdef _DEBUG - if (newSize > size) - throw IndexOutofRangeException("size too large."); + if (newSize > size) + throw IndexOutofRangeException("size too large."); #endif - _count = newSize; - } - inline void Add(const T & item) - { + _count = newSize; + } + inline void Add(const T & item) + { #ifdef _DEBUG - if (_count == size) - throw IndexOutofRangeException("out of range access to static array."); + if (_count == size) + throw IndexOutofRangeException("out of range access to static array."); #endif - _buffer[_count++] = item; - } - inline void Add(T && item) - { + _buffer[_count++] = item; + } + inline void Add(T && item) + { #ifdef _DEBUG - if (_count == size) - throw IndexOutofRangeException("out of range access to static array."); + if (_count == size) + throw IndexOutofRangeException("out of range access to static array."); #endif - _buffer[_count++] = _Move(item); - } + _buffer[_count++] = _Move(item); + } - inline T & operator [](int id) const - { + inline T & operator [](int id) const + { #if _DEBUG - if (id >= _count || id < 0) - throw IndexOutofRangeException("Operator[]: Index out of Range."); + if (id >= _count || id < 0) + throw IndexOutofRangeException("Operator[]: Index out of Range."); #endif - return ((T*)_buffer)[id]; - } - - inline T* Buffer() const - { - return (T*)_buffer; - } + return ((T*)_buffer)[id]; + } - inline void Clear() - { - _count = 0; - } + inline T* Buffer() const + { + return (T*)_buffer; + } - template<typename T2> - int IndexOf(const T2 & val) const - { - for (int i = 0; i < _count; i++) - { - if (_buffer[i] == val) - return i; - } - return -1; - } + inline void Clear() + { + _count = 0; + } - template<typename T2> - int LastIndexOf(const T2 & val) const + template<typename T2> + int IndexOf(const T2 & val) const + { + for (int i = 0; i < _count; i++) { - for (int i = _count - 1; i >= 0; i--) - { - if (_buffer[i] == val) - return i; - } - return -1; + if (_buffer[i] == val) + return i; } + return -1; + } - inline ArrayView<T> GetArrayView() const - { - return ArrayView<T>((T*)_buffer, _count); - } - inline ArrayView<T> GetArrayView(int start, int count) const + template<typename T2> + int LastIndexOf(const T2 & val) const + { + for (int i = _count - 1; i >= 0; i--) { - return ArrayView<T>((T*)_buffer + start, count); + if (_buffer[i] == val) + return i; } - }; + return -1; + } - template<typename T, typename ...TArgs> - struct FirstType + inline ArrayView<T> GetArrayView() const { - typedef T type; - }; + return ArrayView<T>((T*)_buffer, _count); + } + inline ArrayView<T> GetArrayView(int start, int count) const + { + return ArrayView<T>((T*)_buffer + start, count); + } + }; + template<typename T, typename ...TArgs> + struct FirstType + { + typedef T type; + }; - 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) - { - arr.Add(val); - InsertArray(arr, args...); - } + template<typename T, int size> + void InsertArray(Array<T, size> &) {} - template<typename ...TArgs> - auto MakeArray(TArgs ...args) -> Array<typename FirstType<TArgs...>::type, sizeof...(args)> - { - Array<typename FirstType<TArgs...>::type, sizeof...(args)> rs; - InsertArray(rs, args...); - return rs; - } + template<typename T, typename ...TArgs, int size> + void InsertArray(Array<T, size> & arr, const T & val, TArgs... args) + { + arr.Add(val); + InsertArray(arr, args...); + } + + template<typename ...TArgs> + auto MakeArray(TArgs ...args) -> Array<typename FirstType<TArgs...>::type, sizeof...(args)> + { + Array<typename FirstType<TArgs...>::type, sizeof...(args)> rs; + InsertArray(rs, args...); + return rs; } } |
