Array.h 2.9 KB

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