From fcf83dbf9effab3bd98bad2b83b2468b7eb05cfd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 9 Jun 2017 11:34:21 -0700 Subject: Initial import of code. --- source/core/array.h | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 source/core/array.h (limited to 'source/core/array.h') diff --git a/source/core/array.h b/source/core/array.h new file mode 100644 index 000000000..b6dbeab07 --- /dev/null +++ b/source/core/array.h @@ -0,0 +1,146 @@ +#ifndef CORE_LIB_ARRAY_H +#define CORE_LIB_ARRAY_H + +#include "exception.h" +#include "array-view.h" + +namespace CoreLib +{ + namespace Basic + { + template + 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(_buffer[0]); + } + inline T & Last() const + { + return const_cast(_buffer[_count - 1]); + } + inline void SetSize(int newSize) + { +#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); + } + + inline T & operator [](int id) const + { +#if _DEBUG + if (id >= _count || id < 0) + throw IndexOutofRangeException("Operator[]: Index out of Range."); +#endif + return ((T*)_buffer)[id]; + } + + inline T* Buffer() const + { + return (T*)_buffer; + } + + inline void Clear() + { + _count = 0; + } + + template + int IndexOf(const T2 & val) const + { + for (int i = 0; i < _count; i++) + { + if (_buffer[i] == val) + return i; + } + return -1; + } + + template + int LastIndexOf(const T2 & val) const + { + for (int i = _count - 1; i >= 0; i--) + { + if (_buffer[i] == val) + return i; + } + return -1; + } + + inline ArrayView GetArrayView() const + { + return ArrayView((T*)_buffer, _count); + } + inline ArrayView GetArrayView(int start, int count) const + { + return ArrayView((T*)_buffer + start, count); + } + }; + + 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, sizeof...(args)> rs; + InsertArray(rs, args...); + return rs; + } + } +} + +#endif \ No newline at end of file -- cgit v1.2.3