General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
1.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <gp/containers/buffer.hpp>
  3. #include <initializer_list>
  4. namespace gp{
  5. template<typename T, std::size_t sz>
  6. class array{
  7. public:
  8. T ary[sz];
  9. using associated_iterator = pointer_iterator<T, 1>;
  10. using associated_const_iterator = const_pointer_iterator<T, 1>;
  11. using associated_riterator = pointer_iterator<T, -1>;
  12. using associated_const_riterator = const_pointer_iterator<T, -1>;
  13. constexpr T& operator[] (size_t off)
  14. {
  15. if constexpr (gp_config::has_buffer_bounds)
  16. {
  17. gp_config::assertion(
  18. off < sz,
  19. "Array bounds infringed"
  20. );
  21. }
  22. return ary[off];
  23. }
  24. constexpr const T& operator[] (size_t off) const
  25. {
  26. return ary[off];
  27. }
  28. constexpr size_t size() const
  29. {
  30. return sz;
  31. }
  32. constexpr pointer_iterator<T, 1> begin()
  33. {
  34. return associated_iterator(&ary[0]);
  35. }
  36. constexpr pointer_iterator<T, 1> end()
  37. {
  38. return associated_iterator(&ary[sz]);
  39. }
  40. constexpr const_pointer_iterator<T, 1> cbegin() const
  41. {
  42. return associated_const_iterator(&ary[0]);
  43. }
  44. constexpr const_pointer_iterator<T, 1> cend() const
  45. {
  46. return associated_const_iterator(&ary[sz]);
  47. }
  48. constexpr pointer_iterator<T, -1> rbegin()
  49. {
  50. return associated_riterator(&ary[sz-1]);
  51. }
  52. constexpr pointer_iterator<T, -1> rend()
  53. {
  54. return associated_riterator(ary-1);
  55. }
  56. constexpr const_pointer_iterator<T, -1> crbegin() const
  57. {
  58. return associated_const_riterator(&ary[sz-1]);
  59. }
  60. constexpr const_pointer_iterator<T, -1> crend() const
  61. {
  62. return associated_const_riterator(ary-1);
  63. }
  64. constexpr bool operator==(const array& oth) const
  65. {
  66. for(size_t idx = 0; idx<sz; idx++)
  67. {
  68. if(ary[idx] != oth.ary[idx])
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. constexpr bool operator!=(const array& oth) const
  76. {
  77. return !(*this == oth);
  78. }
  79. gp::buffer<T> as_buffer()
  80. {
  81. return gp::buffer<T>{(T*)ary, (T*)ary+sz};
  82. }
  83. };
  84. }