Array.h 1.0 KB

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