12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #ifndef CORE_ARRAY_H
- #define CORE_ARRAY_H
- #include "utils/String.h"
- namespace Core {
- template<typename T, int N>
- class Array final {
- static_assert(N > 0, "Array size must be positive");
- T data[static_cast<unsigned int>(N)];
- public:
- Array() = default;
- Array(const T& t) {
- fill(t);
- }
- void fill(const T& t) {
- for(int i = 0; i < N; i++) {
- data[i] = t;
- }
- }
- T& operator[](int index) {
- return data[index];
- }
- const T& operator[](int index) const {
- return data[index];
- }
- T* begin() {
- return data;
- }
- T* end() {
- return data + N;
- }
- const T* begin() const {
- return data;
- }
- const T* end() const {
- return data + N;
- }
- constexpr int getLength() const {
- return N;
- }
- void toString(String& s) const {
- s.append("[");
- for(int i = 0; i < N - 1; i++) {
- s.append(data[i]);
- s.append(", ");
- }
- if(N > 0) {
- s.append(data[N - 1]);
- }
- s.append("]");
- }
- };
- }
- #endif
|