LexicoArray2.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. namespace WwiseGTE
  9. {
  10. // A template class to provide 2D array access that conforms to row-major
  11. // order (RowMajor = true) or column-major order (RowMajor = false). The
  12. template <bool RowMajor, typename Real, int... Dimensions>
  13. class LexicoArray2 {};
  14. // The array dimensions are known only at run time.
  15. template <typename Real>
  16. class LexicoArray2<true, Real>
  17. {
  18. public:
  19. LexicoArray2(int numRows, int numCols, Real* matrix)
  20. :
  21. mNumRows(numRows),
  22. mNumCols(numCols),
  23. mMatrix(matrix)
  24. {
  25. }
  26. inline int GetNumRows() const
  27. {
  28. return mNumRows;
  29. }
  30. inline int GetNumCols() const
  31. {
  32. return mNumCols;
  33. }
  34. inline Real& operator()(int r, int c)
  35. {
  36. return mMatrix[c + mNumCols * r];
  37. }
  38. inline Real const& operator()(int r, int c) const
  39. {
  40. return mMatrix[c + mNumCols * r];
  41. }
  42. private:
  43. int mNumRows, mNumCols;
  44. Real* mMatrix;
  45. };
  46. template <typename Real>
  47. class LexicoArray2<false, Real>
  48. {
  49. public:
  50. LexicoArray2(int numRows, int numCols, Real* matrix)
  51. :
  52. mNumRows(numRows),
  53. mNumCols(numCols),
  54. mMatrix(matrix)
  55. {
  56. }
  57. inline int GetNumRows() const
  58. {
  59. return mNumRows;
  60. }
  61. inline int GetNumCols() const
  62. {
  63. return mNumCols;
  64. }
  65. inline Real& operator()(int r, int c)
  66. {
  67. return mMatrix[r + mNumRows * c];
  68. }
  69. inline Real const& operator()(int r, int c) const
  70. {
  71. return mMatrix[r + mNumRows * c];
  72. }
  73. private:
  74. int mNumRows, mNumCols;
  75. Real* mMatrix;
  76. };
  77. // The array dimensions are known at compile time.
  78. template <typename Real, int NumRows, int NumCols>
  79. class LexicoArray2<true, Real, NumRows, NumCols>
  80. {
  81. public:
  82. LexicoArray2(Real* matrix)
  83. :
  84. mMatrix(matrix)
  85. {
  86. }
  87. inline int GetNumRows() const
  88. {
  89. return NumRows;
  90. }
  91. inline int GetNumCols() const
  92. {
  93. return NumCols;
  94. }
  95. inline Real& operator()(int r, int c)
  96. {
  97. return mMatrix[c + NumCols * r];
  98. }
  99. inline Real const& operator()(int r, int c) const
  100. {
  101. return mMatrix[c + NumCols * r];
  102. }
  103. private:
  104. Real* mMatrix;
  105. };
  106. template <typename Real, int NumRows, int NumCols>
  107. class LexicoArray2<false, Real, NumRows, NumCols>
  108. {
  109. public:
  110. LexicoArray2(Real* matrix)
  111. :
  112. mMatrix(matrix)
  113. {
  114. }
  115. inline int GetNumRows() const
  116. {
  117. return NumRows;
  118. }
  119. inline int GetNumCols() const
  120. {
  121. return NumCols;
  122. }
  123. inline Real& operator()(int r, int c)
  124. {
  125. return mMatrix[r + NumRows * c];
  126. }
  127. inline Real const& operator()(int r, int c) const
  128. {
  129. return mMatrix[r + NumRows * c];
  130. }
  131. private:
  132. Real* mMatrix;
  133. };
  134. }