Array2.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Array2 class represents a 2-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 Array2
  16. {
  17. public:
  18. // Construction. The first constructor generates an array of objects
  19. // that are owned by Array2. The second constructor is given an array
  20. // of objects that are owned by the caller. The array has bound0
  21. // columns and bound1 rows.
  22. Array2(size_t bound0, size_t bound1)
  23. :
  24. mBound0(bound0),
  25. mBound1(bound1),
  26. mObjects(bound0 * bound1),
  27. mIndirect1(bound1)
  28. {
  29. SetPointers(mObjects.data());
  30. }
  31. Array2(size_t bound0, size_t bound1, T* objects)
  32. :
  33. mBound0(bound0),
  34. mBound1(bound1),
  35. mIndirect1(bound1)
  36. {
  37. SetPointers(objects);
  38. }
  39. // Support for dynamic resizing, copying, or moving. If 'other' does
  40. // not own the original 'objects', they are not copied by the
  41. // assignment operator.
  42. Array2()
  43. :
  44. mBound0(0),
  45. mBound1(0)
  46. {
  47. }
  48. Array2(Array2 const& other)
  49. :
  50. mBound0(other.mBound0),
  51. mBound1(other.mBound1)
  52. {
  53. *this = other;
  54. }
  55. Array2& operator=(Array2 const& other)
  56. {
  57. // The copy is valid whether or not other.mObjects has elements.
  58. mObjects = other.mObjects;
  59. SetPointers(other);
  60. return *this;
  61. }
  62. Array2(Array2&& other) noexcept
  63. :
  64. mBound0(other.mBound0),
  65. mBound1(other.mBound1)
  66. {
  67. *this = std::move(other);
  68. }
  69. Array2& operator=(Array2&& other) noexcept
  70. {
  71. // The move is valid whether or not other.mObjects has elements.
  72. mObjects = std::move(other.mObjects);
  73. SetPointers(other);
  74. return *this;
  75. }
  76. // Access to the array. Sample usage is
  77. // Array2<T> myArray(3, 2);
  78. // T* row1 = myArray[1];
  79. // T row1Col2 = myArray[1][2];
  80. inline size_t GetBound0() const
  81. {
  82. return mBound0;
  83. }
  84. inline size_t GetBound1() const
  85. {
  86. return mBound1;
  87. }
  88. inline T const* operator[](int row) const
  89. {
  90. return mIndirect1[row];
  91. }
  92. inline T* operator[](int row)
  93. {
  94. return mIndirect1[row];
  95. }
  96. private:
  97. void SetPointers(T* objects)
  98. {
  99. for (size_t i1 = 0; i1 < mBound1; ++i1)
  100. {
  101. size_t j0 = mBound0 * i1; // = bound0*(i1 + j1) where j1 = 0
  102. mIndirect1[i1] = &objects[j0];
  103. }
  104. }
  105. void SetPointers(Array2 const& other)
  106. {
  107. mBound0 = other.mBound0;
  108. mBound1 = other.mBound1;
  109. mIndirect1.resize(mBound1);
  110. if (mBound0 > 0)
  111. {
  112. // The objects are owned.
  113. SetPointers(mObjects.data());
  114. }
  115. else if (mIndirect1.size() > 0)
  116. {
  117. // The objects are not owned.
  118. SetPointers(other.mIndirect1[0]);
  119. }
  120. // else 'other' is an empty Array2.
  121. }
  122. size_t mBound0, mBound1;
  123. std::vector<T> mObjects;
  124. std::vector<T*> mIndirect1;
  125. };
  126. }