General Purpose library for Freestanding C++ and POSIX systems
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

85 строки
1.4 KiB

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