Array.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include "anki/util/Assert.h"
  7. #include "anki/util/StdTypes.h"
  8. namespace anki {
  9. /// @addtogroup util_containers
  10. /// @{
  11. /// Like std::array but with some additions
  12. template<typename T, PtrSize N>
  13. class Array
  14. {
  15. public:
  16. using Value = T;
  17. using Iterator = Value*;
  18. using ConstIterator = const Value*;
  19. using Reference = Value&;
  20. using ConstReference = const Value&;
  21. // STL compatible
  22. using iterator = Iterator;
  23. using const_iterator = ConstIterator;
  24. using reference = Reference;
  25. using const_reference = ConstReference;
  26. Value m_data[N];
  27. template<typename Y>
  28. Reference operator[](const Y n)
  29. {
  30. ANKI_ASSERT(static_cast<U>(n) < N);
  31. return m_data[static_cast<U>(n)];
  32. }
  33. template<typename Y>
  34. ConstReference operator[](const Y n) const
  35. {
  36. ANKI_ASSERT(static_cast<U>(n) < N);
  37. return m_data[static_cast<U>(n)];
  38. }
  39. Iterator getBegin()
  40. {
  41. return &m_data[0];
  42. }
  43. ConstIterator getBegin() const
  44. {
  45. return &m_data[0];
  46. }
  47. Iterator getEnd()
  48. {
  49. return &m_data[0] + N;
  50. }
  51. ConstIterator getEnd() const
  52. {
  53. return &m_data[0] + N;
  54. }
  55. Reference getFront()
  56. {
  57. return m_data[0];
  58. }
  59. ConstReference getFront() const
  60. {
  61. return m_data[0];
  62. }
  63. Reference getBack()
  64. {
  65. return m_data[N - 1];
  66. }
  67. ConstReference getBack() const
  68. {
  69. return m_data[N - 1];
  70. }
  71. /// Make it compatible with the C++11 range based for loop
  72. Iterator begin()
  73. {
  74. return getBegin();
  75. }
  76. /// Make it compatible with the C++11 range based for loop
  77. ConstIterator begin() const
  78. {
  79. return getBegin();
  80. }
  81. /// Make it compatible with the C++11 range based for loop
  82. Iterator end()
  83. {
  84. return getEnd();
  85. }
  86. /// Make it compatible with the C++11 range based for loop
  87. ConstIterator end() const
  88. {
  89. return getEnd();
  90. }
  91. /// Make it compatible with STL
  92. Reference front()
  93. {
  94. return getFront();
  95. }
  96. /// Make it compatible with STL
  97. ConstReference front() const
  98. {
  99. return getFront();
  100. }
  101. /// Make it compatible with STL
  102. Reference back()
  103. {
  104. return getBack;
  105. }
  106. /// Make it compatible with STL
  107. ConstReference back() const
  108. {
  109. return getBack();
  110. }
  111. static constexpr PtrSize getSize()
  112. {
  113. return N;
  114. }
  115. /// Make it compatible with STL
  116. static constexpr PtrSize size()
  117. {
  118. return N;
  119. }
  120. };
  121. /// 2D Array. @code Array2d<X, 10, 2> a; @endcode is equivelent to
  122. /// @code X a[10][2]; @endcode
  123. template<typename T, PtrSize I, PtrSize J>
  124. using Array2d = Array<Array<T, J>, I>;
  125. /// 3D Array. @code Array3d<X, 10, 2, 3> a; @endcode is equivelent to
  126. /// @code X a[10][2][3]; @endcode
  127. template<typename T, PtrSize I, PtrSize J, PtrSize K>
  128. using Array3d = Array<Array<Array<T, K>, J>, I>;
  129. /// 4D Array. @code Array4d<X, 10, 2, 3, 4> a; @endcode is equivelent to
  130. /// @code X a[10][2][3][4]; @endcode
  131. template<typename T, PtrSize I, PtrSize J, PtrSize K, PtrSize L>
  132. using Array4d = Array<Array<Array<Array<T, L>, K>, J>, I>;
  133. /// @}
  134. } // end namespace anki