Array.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef ARRAY_H
  2. #define ARRAY_H
  3. #include "utils/StringBuffer.h"
  4. template<typename T, int N>
  5. class Array final {
  6. static_assert(N > 0, "Array size must be positive");
  7. T data[static_cast<unsigned int>(N)];
  8. public:
  9. Array() = default;
  10. Array(const T& t) {
  11. fill(t);
  12. }
  13. void fill(const T& t) {
  14. for(int i = 0; i < N; i++) {
  15. data[i] = t;
  16. }
  17. }
  18. T& operator[](int index) {
  19. return data[index];
  20. }
  21. const T& operator[](int index) const {
  22. return data[index];
  23. }
  24. T* begin() {
  25. return data;
  26. }
  27. T* end() {
  28. return data + N;
  29. }
  30. const T* begin() const {
  31. return data;
  32. }
  33. const T* end() const {
  34. return data + N;
  35. }
  36. constexpr int getLength() const {
  37. return N;
  38. }
  39. template<int L>
  40. void toString(StringBuffer<L>& 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. #endif