Array.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef ANKI_PtrSizeTIL_ARRAY_H
  6. #define ANKI_PtrSizeTIL_ARRAY_H
  7. #include "anki/util/Assert.h"
  8. #include "anki/util/StdTypes.h"
  9. namespace anki {
  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. Reference operator[](const PtrSize n)
  29. {
  30. ANKI_ASSERT(n < N);
  31. return m_data[n];
  32. }
  33. ConstReference operator[](const PtrSize n) const
  34. {
  35. ANKI_ASSERT(n < N);
  36. return m_data[n];
  37. }
  38. Iterator getBegin()
  39. {
  40. return &m_data[0];
  41. }
  42. ConstIterator getBegin() const
  43. {
  44. return &m_data[0];
  45. }
  46. Iterator getEnd()
  47. {
  48. return &m_data[0] + N;
  49. }
  50. ConstIterator getEnd() const
  51. {
  52. return &m_data[0] + N;
  53. }
  54. Reference getFront()
  55. {
  56. return m_data[0];
  57. }
  58. ConstReference getFront() const
  59. {
  60. return m_data[0];
  61. }
  62. Reference getBack()
  63. {
  64. return m_data[N - 1];
  65. }
  66. ConstReference getBack() const
  67. {
  68. return m_data[N - 1];
  69. }
  70. /// Make it compatible with the C++11 range based for loop
  71. Iterator begin()
  72. {
  73. return getBegin();
  74. }
  75. /// Make it compatible with the C++11 range based for loop
  76. ConstIterator begin() const
  77. {
  78. return getBegin();
  79. }
  80. /// Make it compatible with the C++11 range based for loop
  81. Iterator end()
  82. {
  83. return getEnd();
  84. }
  85. /// Make it compatible with the C++11 range based for loop
  86. ConstIterator end() const
  87. {
  88. return getEnd();
  89. }
  90. /// Make it compatible with STL
  91. Reference front()
  92. {
  93. return getFront();
  94. }
  95. /// Make it compatible with STL
  96. ConstReference front() const
  97. {
  98. return getFront();
  99. }
  100. /// Make it compatible with STL
  101. Reference back()
  102. {
  103. return getBack;
  104. }
  105. /// Make it compatible with STL
  106. ConstReference back() const
  107. {
  108. return getBack();
  109. }
  110. static constexpr PtrSize getSize()
  111. {
  112. return N;
  113. }
  114. /// Make it compatible with STL
  115. static constexpr PtrSize size()
  116. {
  117. return N;
  118. }
  119. };
  120. /// 2D Array. @code Array2d<X, 10, 2> a; @endcode is equivelent to
  121. /// @code X a[10][2]; @endcode
  122. template<typename T, PtrSize I, PtrSize J>
  123. using Array2d = Array<Array<T, J>, I>;
  124. /// 3D Array. @code Array3d<X, 10, 2, 3> a; @endcode is equivelent to
  125. /// @code X a[10][2][3]; @endcode
  126. template<typename T, PtrSize I, PtrSize J, PtrSize K>
  127. using Array3d = Array<Array<Array<T, K>, J>, I>;
  128. /// @}
  129. } // end namespace anki
  130. #endif