Array3.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <cstddef>
  9. #include <vector>
  10. // The Array3 class represents a 3-dimensional array that minimizes the number
  11. // of new and delete calls. The T objects are stored in a contiguous array.
  12. namespace WwiseGTE
  13. {
  14. template <typename T>
  15. class Array3
  16. {
  17. public:
  18. // Construction. The first constructor generates an array of objects
  19. // that are owned by Array3. The second constructor is given an array
  20. // of objects that are owned by the caller. The array has bound0
  21. // columns, bound1 rows, and bound2 slices.
  22. Array3(size_t bound0, size_t bound1, size_t bound2)
  23. :
  24. mBound0(bound0),
  25. mBound1(bound1),
  26. mBound2(bound2),
  27. mObjects(bound0 * bound1 * bound2),
  28. mIndirect1(bound1 * bound2),
  29. mIndirect2(bound2)
  30. {
  31. SetPointers(mObjects.data());
  32. }
  33. Array3(size_t bound0, size_t bound1, size_t bound2, T* objects)
  34. :
  35. mBound0(bound0),
  36. mBound1(bound1),
  37. mBound2(bound2),
  38. mIndirect1(bound1 * bound2),
  39. mIndirect2(bound2)
  40. {
  41. SetPointers(objects);
  42. }
  43. // Support for dynamic resizing, copying, or moving. If 'other' does
  44. // not own the original 'objects', they are not copied by the
  45. // assignment operator.
  46. Array3()
  47. :
  48. mBound0(0),
  49. mBound1(0),
  50. mBound2(0)
  51. {
  52. }
  53. Array3(Array3 const& other)
  54. :
  55. mBound0(other.mBound0),
  56. mBound1(other.mBound1),
  57. mBound2(other.mBound2)
  58. {
  59. *this = other;
  60. }
  61. Array3& operator=(Array3 const& other)
  62. {
  63. // The copy is valid whether or not other.mObjects has elements.
  64. mObjects = other.mObjects;
  65. SetPointers(other);
  66. return *this;
  67. }
  68. Array3(Array3&& other) noexcept
  69. :
  70. mBound0(other.mBound0),
  71. mBound1(other.mBound1),
  72. mBound2(other.mBound2)
  73. {
  74. *this = std::move(other);
  75. }
  76. Array3& operator=(Array3&& other) noexcept
  77. {
  78. // The move is valid whether or not other.mObjects has elements.
  79. mObjects = std::move(other.mObjects);
  80. SetPointers(other);
  81. return *this;
  82. }
  83. // Access to the array. Sample usage is
  84. // Array3<T> myArray(4, 3, 2);
  85. // T** slice1 = myArray[1];
  86. // T* slice1row2 = myArray[1][2];
  87. // T slice1Row2Col3 = myArray[1][2][3];
  88. inline size_t GetBound0() const
  89. {
  90. return mBound0;
  91. }
  92. inline size_t GetBound1() const
  93. {
  94. return mBound1;
  95. }
  96. inline size_t GetBound2() const
  97. {
  98. return mBound2;
  99. }
  100. inline T* const* operator[](int slice) const
  101. {
  102. return mIndirect2[slice];
  103. }
  104. inline T** operator[](int slice)
  105. {
  106. return mIndirect2[slice];
  107. }
  108. private:
  109. void SetPointers(T* objects)
  110. {
  111. for (size_t i2 = 0; i2 < mBound2; ++i2)
  112. {
  113. size_t j1 = mBound1 * i2; // = bound1*(i2 + j2) where j2 = 0
  114. mIndirect2[i2] = &mIndirect1[j1];
  115. for (size_t i1 = 0; i1 < mBound1; ++i1)
  116. {
  117. size_t j0 = mBound0 * (i1 + j1);
  118. mIndirect2[i2][i1] = &objects[j0];
  119. }
  120. }
  121. }
  122. void SetPointers(Array3 const& other)
  123. {
  124. mBound0 = other.mBound0;
  125. mBound1 = other.mBound1;
  126. mBound2 = other.mBound2;
  127. mIndirect1.resize(mBound1 * mBound2);
  128. mIndirect2.resize(mBound2);
  129. if (mBound0 > 0)
  130. {
  131. // The objects are owned.
  132. SetPointers(mObjects.data());
  133. }
  134. else if (mIndirect1.size() > 0)
  135. {
  136. // The objects are not owned.
  137. SetPointers(other.mIndirect2[0][0]);
  138. }
  139. // else 'other' is an empty Array3.
  140. }
  141. size_t mBound0, mBound1, mBound2;
  142. std::vector<T> mObjects;
  143. std::vector<T*> mIndirect1;
  144. std::vector<T**> mIndirect2;
  145. };
  146. }