fun10_sortIdx.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Academic License - for use in teaching, academic research, and meeting
  3. // course requirements at degree granting institutions only. Not for
  4. // government, commercial, or other organizational use.
  5. // File: sortIdx.cpp
  6. //
  7. // MATLAB Coder version : 5.3
  8. // C/C++ source code generated on : 27-Mar-2023 15:34:32
  9. //
  10. // Include Files
  11. #include "fun10_sortIdx.h"
  12. #include "rt_nonfinite.h"
  13. #include "coder_array.h"
  14. // Function Declarations
  15. namespace coder {
  16. namespace internal {
  17. static void merge(::coder::array<int, 2U> &idx, ::coder::array<double, 2U> &x,
  18. int offset, int np, int nq, ::coder::array<int, 1U> &iwork,
  19. ::coder::array<double, 1U> &xwork);
  20. }
  21. } // namespace coder
  22. // Function Definitions
  23. //
  24. // Arguments : ::coder::array<int, 2U> &idx
  25. // ::coder::array<double, 2U> &x
  26. // int offset
  27. // int np
  28. // int nq
  29. // ::coder::array<int, 1U> &iwork
  30. // ::coder::array<double, 1U> &xwork
  31. // Return Type : void
  32. //
  33. namespace coder {
  34. namespace internal {
  35. static void merge(::coder::array<int, 2U> &idx, ::coder::array<double, 2U> &x,
  36. int offset, int np, int nq, ::coder::array<int, 1U> &iwork,
  37. ::coder::array<double, 1U> &xwork)
  38. {
  39. if (nq != 0) {
  40. int iout;
  41. int j;
  42. int n_tmp;
  43. int p;
  44. int q;
  45. n_tmp = np + nq;
  46. for (j = 0; j < n_tmp; j++) {
  47. iout = offset + j;
  48. iwork[j] = idx[iout];
  49. xwork[j] = x[iout];
  50. }
  51. p = 0;
  52. q = np;
  53. iout = offset - 1;
  54. int exitg1;
  55. do {
  56. exitg1 = 0;
  57. iout++;
  58. if (xwork[p] <= xwork[q]) {
  59. idx[iout] = iwork[p];
  60. x[iout] = xwork[p];
  61. if (p + 1 < np) {
  62. p++;
  63. } else {
  64. exitg1 = 1;
  65. }
  66. } else {
  67. idx[iout] = iwork[q];
  68. x[iout] = xwork[q];
  69. if (q + 1 < n_tmp) {
  70. q++;
  71. } else {
  72. q = iout - p;
  73. for (j = p + 1; j <= np; j++) {
  74. iout = q + j;
  75. idx[iout] = iwork[j - 1];
  76. x[iout] = xwork[j - 1];
  77. }
  78. exitg1 = 1;
  79. }
  80. }
  81. } while (exitg1 == 0);
  82. }
  83. }
  84. //
  85. // Arguments : int idx[16]
  86. // double x[16]
  87. // int offset
  88. // int np
  89. // int nq
  90. // int iwork[16]
  91. // double xwork[16]
  92. // Return Type : void
  93. //
  94. void merge(int idx[16], double x[16], int offset, int np, int nq, int iwork[16],
  95. double xwork[16])
  96. {
  97. if (nq != 0) {
  98. int iout;
  99. int j;
  100. int n_tmp;
  101. int p;
  102. int q;
  103. n_tmp = np + nq;
  104. for (j = 0; j < n_tmp; j++) {
  105. iout = offset + j;
  106. iwork[j] = idx[iout];
  107. xwork[j] = x[iout];
  108. }
  109. p = 0;
  110. q = np;
  111. iout = offset - 1;
  112. int exitg1;
  113. do {
  114. exitg1 = 0;
  115. iout++;
  116. if (xwork[p] <= xwork[q]) {
  117. idx[iout] = iwork[p];
  118. x[iout] = xwork[p];
  119. if (p + 1 < np) {
  120. p++;
  121. } else {
  122. exitg1 = 1;
  123. }
  124. } else {
  125. idx[iout] = iwork[q];
  126. x[iout] = xwork[q];
  127. if (q + 1 < n_tmp) {
  128. q++;
  129. } else {
  130. q = iout - p;
  131. for (j = p + 1; j <= np; j++) {
  132. iout = q + j;
  133. idx[iout] = iwork[j - 1];
  134. x[iout] = xwork[j - 1];
  135. }
  136. exitg1 = 1;
  137. }
  138. }
  139. } while (exitg1 == 0);
  140. }
  141. }
  142. //
  143. // Arguments : ::coder::array<int, 2U> &idx
  144. // ::coder::array<double, 2U> &x
  145. // int offset
  146. // int n
  147. // int preSortLevel
  148. // ::coder::array<int, 1U> &iwork
  149. // ::coder::array<double, 1U> &xwork
  150. // Return Type : void
  151. //
  152. void merge_block(::coder::array<int, 2U> &idx, ::coder::array<double, 2U> &x,
  153. int offset, int n, int preSortLevel,
  154. ::coder::array<int, 1U> &iwork,
  155. ::coder::array<double, 1U> &xwork)
  156. {
  157. int bLen;
  158. int nPairs;
  159. nPairs = n >> preSortLevel;
  160. bLen = 1 << preSortLevel;
  161. while (nPairs > 1) {
  162. int nTail;
  163. int tailOffset;
  164. if ((nPairs & 1) != 0) {
  165. nPairs--;
  166. tailOffset = bLen * nPairs;
  167. nTail = n - tailOffset;
  168. if (nTail > bLen) {
  169. merge(idx, x, offset + tailOffset, bLen, nTail - bLen, iwork, xwork);
  170. }
  171. }
  172. tailOffset = bLen << 1;
  173. nPairs >>= 1;
  174. for (nTail = 0; nTail < nPairs; nTail++) {
  175. merge(idx, x, offset + nTail * tailOffset, bLen, bLen, iwork, xwork);
  176. }
  177. bLen = tailOffset;
  178. }
  179. if (n > bLen) {
  180. merge(idx, x, offset, bLen, n - bLen, iwork, xwork);
  181. }
  182. }
  183. } // namespace internal
  184. } // namespace coder
  185. //
  186. // File trailer for sortIdx.cpp
  187. //
  188. // [EOF]
  189. //