Array.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef CORE_ARRAY_H
  2. #define CORE_ARRAY_H
  3. #include "utils/String.h"
  4. namespace Core {
  5. template<typename T, int N>
  6. class Array final {
  7. static_assert(N > 0, "Array size must be positive");
  8. T data[static_cast<unsigned int>(N)];
  9. public:
  10. Array() = default;
  11. Array(const T& t) {
  12. fill(t);
  13. }
  14. void fill(const T& t) {
  15. for(int i = 0; i < N; i++) {
  16. data[i] = t;
  17. }
  18. }
  19. T& operator[](int index) {
  20. return data[index];
  21. }
  22. const T& operator[](int index) const {
  23. return data[index];
  24. }
  25. T* begin() {
  26. return data;
  27. }
  28. T* end() {
  29. return data + N;
  30. }
  31. const T* begin() const {
  32. return data;
  33. }
  34. const T* end() const {
  35. return data + N;
  36. }
  37. constexpr int getLength() const {
  38. return N;
  39. }
  40. void toString(String& s) const {
  41. s.append("[");
  42. for(int i = 0; i < N - 1; i++) {
  43. s.append(data[i]);
  44. s.append(", ");
  45. }
  46. if(N > 0) {
  47. s.append(data[N - 1]);
  48. }
  49. s.append("]");
  50. }
  51. };
  52. }
  53. #endif