fun10_sort.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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: sort.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_sort.h"
  12. #include "rt_nonfinite.h"
  13. #include "fun10_sortIdx.h"
  14. #include "coder_array.h"
  15. #include <cmath>
  16. #include <cstring>
  17. // Function Definitions
  18. //
  19. // Arguments : double x[16]
  20. // Return Type : void
  21. //
  22. namespace coder {
  23. namespace internal {
  24. void sort(double x[16])
  25. {
  26. double xwork[16];
  27. double x4[4];
  28. int idx[16];
  29. int iwork[16];
  30. int i2;
  31. int i3;
  32. int ib;
  33. int k;
  34. signed char idx4[4];
  35. signed char perm[4];
  36. x4[0] = 0.0;
  37. idx4[0] = 0;
  38. x4[1] = 0.0;
  39. idx4[1] = 0;
  40. x4[2] = 0.0;
  41. idx4[2] = 0;
  42. x4[3] = 0.0;
  43. idx4[3] = 0;
  44. ib = 0;
  45. for (k = 0; k < 16; k++) {
  46. idx[k] = 0;
  47. xwork[k] = 0.0;
  48. ib++;
  49. idx4[ib - 1] = static_cast<signed char>(k + 1);
  50. x4[ib - 1] = x[k];
  51. if (ib == 4) {
  52. double d;
  53. double d1;
  54. int i4;
  55. if (x4[0] <= x4[1]) {
  56. ib = 1;
  57. i2 = 2;
  58. } else {
  59. ib = 2;
  60. i2 = 1;
  61. }
  62. if (x4[2] <= x4[3]) {
  63. i3 = 3;
  64. i4 = 4;
  65. } else {
  66. i3 = 4;
  67. i4 = 3;
  68. }
  69. d = x4[ib - 1];
  70. d1 = x4[i3 - 1];
  71. if (d <= d1) {
  72. d = x4[i2 - 1];
  73. if (d <= d1) {
  74. perm[0] = static_cast<signed char>(ib);
  75. perm[1] = static_cast<signed char>(i2);
  76. perm[2] = static_cast<signed char>(i3);
  77. perm[3] = static_cast<signed char>(i4);
  78. } else if (d <= x4[i4 - 1]) {
  79. perm[0] = static_cast<signed char>(ib);
  80. perm[1] = static_cast<signed char>(i3);
  81. perm[2] = static_cast<signed char>(i2);
  82. perm[3] = static_cast<signed char>(i4);
  83. } else {
  84. perm[0] = static_cast<signed char>(ib);
  85. perm[1] = static_cast<signed char>(i3);
  86. perm[2] = static_cast<signed char>(i4);
  87. perm[3] = static_cast<signed char>(i2);
  88. }
  89. } else {
  90. d1 = x4[i4 - 1];
  91. if (d <= d1) {
  92. if (x4[i2 - 1] <= d1) {
  93. perm[0] = static_cast<signed char>(i3);
  94. perm[1] = static_cast<signed char>(ib);
  95. perm[2] = static_cast<signed char>(i2);
  96. perm[3] = static_cast<signed char>(i4);
  97. } else {
  98. perm[0] = static_cast<signed char>(i3);
  99. perm[1] = static_cast<signed char>(ib);
  100. perm[2] = static_cast<signed char>(i4);
  101. perm[3] = static_cast<signed char>(i2);
  102. }
  103. } else {
  104. perm[0] = static_cast<signed char>(i3);
  105. perm[1] = static_cast<signed char>(i4);
  106. perm[2] = static_cast<signed char>(ib);
  107. perm[3] = static_cast<signed char>(i2);
  108. }
  109. }
  110. idx[k - 3] = idx4[perm[0] - 1];
  111. idx[k - 2] = idx4[perm[1] - 1];
  112. idx[k - 1] = idx4[perm[2] - 1];
  113. idx[k] = idx4[perm[3] - 1];
  114. x[k - 3] = x4[perm[0] - 1];
  115. x[k - 2] = x4[perm[1] - 1];
  116. x[k - 1] = x4[perm[2] - 1];
  117. x[k] = x4[perm[3] - 1];
  118. ib = 0;
  119. }
  120. }
  121. if (ib > 0) {
  122. perm[1] = 0;
  123. perm[2] = 0;
  124. perm[3] = 0;
  125. if (ib == 1) {
  126. perm[0] = 1;
  127. } else if (ib == 2) {
  128. if (x4[0] <= x4[1]) {
  129. perm[0] = 1;
  130. perm[1] = 2;
  131. } else {
  132. perm[0] = 2;
  133. perm[1] = 1;
  134. }
  135. } else if (x4[0] <= x4[1]) {
  136. if (x4[1] <= x4[2]) {
  137. perm[0] = 1;
  138. perm[1] = 2;
  139. perm[2] = 3;
  140. } else if (x4[0] <= x4[2]) {
  141. perm[0] = 1;
  142. perm[1] = 3;
  143. perm[2] = 2;
  144. } else {
  145. perm[0] = 3;
  146. perm[1] = 1;
  147. perm[2] = 2;
  148. }
  149. } else if (x4[0] <= x4[2]) {
  150. perm[0] = 2;
  151. perm[1] = 1;
  152. perm[2] = 3;
  153. } else if (x4[1] <= x4[2]) {
  154. perm[0] = 2;
  155. perm[1] = 3;
  156. perm[2] = 1;
  157. } else {
  158. perm[0] = 3;
  159. perm[1] = 2;
  160. perm[2] = 1;
  161. }
  162. for (k = 0; k < ib; k++) {
  163. i2 = perm[k] - 1;
  164. i3 = (k - ib) + 16;
  165. idx[i3] = idx4[i2];
  166. x[i3] = x4[i2];
  167. }
  168. }
  169. std::memset(&iwork[0], 0, 16U * sizeof(int));
  170. i3 = 4;
  171. i2 = 4;
  172. while (i3 > 1) {
  173. if ((i3 & 1) != 0) {
  174. i3--;
  175. ib = i2 * i3;
  176. if (16 - ib > i2) {
  177. merge(idx, x, ib, i2, 16 - (ib + i2), iwork, xwork);
  178. }
  179. }
  180. ib = i2 << 1;
  181. i3 >>= 1;
  182. for (k = 0; k < i3; k++) {
  183. merge(idx, x, k * ib, i2, i2, iwork, xwork);
  184. }
  185. i2 = ib;
  186. }
  187. if (16 > i2) {
  188. merge(idx, x, 0, i2, 16 - i2, iwork, xwork);
  189. }
  190. }
  191. //
  192. // Arguments : ::coder::array<double, 2U> &x
  193. // Return Type : void
  194. //
  195. void sort(::coder::array<double, 2U> &x)
  196. {
  197. array<double, 1U> xwork;
  198. array<int, 2U> idx;
  199. array<int, 1U> iwork;
  200. double b_xwork[256];
  201. double x4[4];
  202. int b_iwork[256];
  203. int idx4[4];
  204. int ib;
  205. int quartetOffset;
  206. signed char perm[4];
  207. idx.set_size(1, x.size(1));
  208. quartetOffset = x.size(1);
  209. for (ib = 0; ib < quartetOffset; ib++) {
  210. idx[ib] = 0;
  211. }
  212. if (x.size(1) != 0) {
  213. int bLen;
  214. int b_n;
  215. int i2;
  216. int i3;
  217. int i4;
  218. int idx_tmp;
  219. int k;
  220. int n;
  221. int nNonNaN;
  222. n = x.size(1);
  223. b_n = x.size(1);
  224. x4[0] = 0.0;
  225. idx4[0] = 0;
  226. x4[1] = 0.0;
  227. idx4[1] = 0;
  228. x4[2] = 0.0;
  229. idx4[2] = 0;
  230. x4[3] = 0.0;
  231. idx4[3] = 0;
  232. quartetOffset = x.size(1);
  233. iwork.set_size(quartetOffset);
  234. for (ib = 0; ib < quartetOffset; ib++) {
  235. iwork[ib] = 0;
  236. }
  237. quartetOffset = x.size(1);
  238. xwork.set_size(quartetOffset);
  239. for (ib = 0; ib < quartetOffset; ib++) {
  240. xwork[ib] = 0.0;
  241. }
  242. bLen = 0;
  243. ib = -1;
  244. for (k = 0; k < b_n; k++) {
  245. if (std::isnan(x[k])) {
  246. idx_tmp = (b_n - bLen) - 1;
  247. idx[idx_tmp] = k + 1;
  248. xwork[idx_tmp] = x[k];
  249. bLen++;
  250. } else {
  251. ib++;
  252. idx4[ib] = k + 1;
  253. x4[ib] = x[k];
  254. if (ib + 1 == 4) {
  255. double d;
  256. double d1;
  257. quartetOffset = k - bLen;
  258. if (x4[0] <= x4[1]) {
  259. ib = 1;
  260. i2 = 2;
  261. } else {
  262. ib = 2;
  263. i2 = 1;
  264. }
  265. if (x4[2] <= x4[3]) {
  266. i3 = 3;
  267. i4 = 4;
  268. } else {
  269. i3 = 4;
  270. i4 = 3;
  271. }
  272. d = x4[ib - 1];
  273. d1 = x4[i3 - 1];
  274. if (d <= d1) {
  275. d = x4[i2 - 1];
  276. if (d <= d1) {
  277. perm[0] = static_cast<signed char>(ib);
  278. perm[1] = static_cast<signed char>(i2);
  279. perm[2] = static_cast<signed char>(i3);
  280. perm[3] = static_cast<signed char>(i4);
  281. } else if (d <= x4[i4 - 1]) {
  282. perm[0] = static_cast<signed char>(ib);
  283. perm[1] = static_cast<signed char>(i3);
  284. perm[2] = static_cast<signed char>(i2);
  285. perm[3] = static_cast<signed char>(i4);
  286. } else {
  287. perm[0] = static_cast<signed char>(ib);
  288. perm[1] = static_cast<signed char>(i3);
  289. perm[2] = static_cast<signed char>(i4);
  290. perm[3] = static_cast<signed char>(i2);
  291. }
  292. } else {
  293. d1 = x4[i4 - 1];
  294. if (d <= d1) {
  295. if (x4[i2 - 1] <= d1) {
  296. perm[0] = static_cast<signed char>(i3);
  297. perm[1] = static_cast<signed char>(ib);
  298. perm[2] = static_cast<signed char>(i2);
  299. perm[3] = static_cast<signed char>(i4);
  300. } else {
  301. perm[0] = static_cast<signed char>(i3);
  302. perm[1] = static_cast<signed char>(ib);
  303. perm[2] = static_cast<signed char>(i4);
  304. perm[3] = static_cast<signed char>(i2);
  305. }
  306. } else {
  307. perm[0] = static_cast<signed char>(i3);
  308. perm[1] = static_cast<signed char>(i4);
  309. perm[2] = static_cast<signed char>(ib);
  310. perm[3] = static_cast<signed char>(i2);
  311. }
  312. }
  313. idx[quartetOffset - 3] = idx4[perm[0] - 1];
  314. idx[quartetOffset - 2] = idx4[perm[1] - 1];
  315. idx[quartetOffset - 1] = idx4[perm[2] - 1];
  316. idx[quartetOffset] = idx4[perm[3] - 1];
  317. x[quartetOffset - 3] = x4[perm[0] - 1];
  318. x[quartetOffset - 2] = x4[perm[1] - 1];
  319. x[quartetOffset - 1] = x4[perm[2] - 1];
  320. x[quartetOffset] = x4[perm[3] - 1];
  321. ib = -1;
  322. }
  323. }
  324. }
  325. i3 = (b_n - bLen) - 1;
  326. if (ib + 1 > 0) {
  327. perm[1] = 0;
  328. perm[2] = 0;
  329. perm[3] = 0;
  330. if (ib + 1 == 1) {
  331. perm[0] = 1;
  332. } else if (ib + 1 == 2) {
  333. if (x4[0] <= x4[1]) {
  334. perm[0] = 1;
  335. perm[1] = 2;
  336. } else {
  337. perm[0] = 2;
  338. perm[1] = 1;
  339. }
  340. } else if (x4[0] <= x4[1]) {
  341. if (x4[1] <= x4[2]) {
  342. perm[0] = 1;
  343. perm[1] = 2;
  344. perm[2] = 3;
  345. } else if (x4[0] <= x4[2]) {
  346. perm[0] = 1;
  347. perm[1] = 3;
  348. perm[2] = 2;
  349. } else {
  350. perm[0] = 3;
  351. perm[1] = 1;
  352. perm[2] = 2;
  353. }
  354. } else if (x4[0] <= x4[2]) {
  355. perm[0] = 2;
  356. perm[1] = 1;
  357. perm[2] = 3;
  358. } else if (x4[1] <= x4[2]) {
  359. perm[0] = 2;
  360. perm[1] = 3;
  361. perm[2] = 1;
  362. } else {
  363. perm[0] = 3;
  364. perm[1] = 2;
  365. perm[2] = 1;
  366. }
  367. for (k = 0; k <= ib; k++) {
  368. idx_tmp = perm[k] - 1;
  369. quartetOffset = (i3 - ib) + k;
  370. idx[quartetOffset] = idx4[idx_tmp];
  371. x[quartetOffset] = x4[idx_tmp];
  372. }
  373. }
  374. ib = (bLen >> 1) + 1;
  375. for (k = 0; k <= ib - 2; k++) {
  376. quartetOffset = (i3 + k) + 1;
  377. i2 = idx[quartetOffset];
  378. idx_tmp = (b_n - k) - 1;
  379. idx[quartetOffset] = idx[idx_tmp];
  380. idx[idx_tmp] = i2;
  381. x[quartetOffset] = xwork[idx_tmp];
  382. x[idx_tmp] = xwork[quartetOffset];
  383. }
  384. if ((bLen & 1) != 0) {
  385. ib += i3;
  386. x[ib] = xwork[ib];
  387. }
  388. nNonNaN = n - bLen;
  389. quartetOffset = 2;
  390. if (nNonNaN > 1) {
  391. if (n >= 256) {
  392. int nBlocks;
  393. nBlocks = nNonNaN >> 8;
  394. if (nBlocks > 0) {
  395. for (int b{0}; b < nBlocks; b++) {
  396. i4 = (b << 8) - 1;
  397. for (int b_b{0}; b_b < 6; b_b++) {
  398. bLen = 1 << (b_b + 2);
  399. b_n = bLen << 1;
  400. n = 256 >> (b_b + 3);
  401. for (k = 0; k < n; k++) {
  402. i2 = (i4 + k * b_n) + 1;
  403. for (quartetOffset = 0; quartetOffset < b_n; quartetOffset++) {
  404. ib = i2 + quartetOffset;
  405. b_iwork[quartetOffset] = idx[ib];
  406. b_xwork[quartetOffset] = x[ib];
  407. }
  408. i3 = 0;
  409. quartetOffset = bLen;
  410. ib = i2 - 1;
  411. int exitg1;
  412. do {
  413. exitg1 = 0;
  414. ib++;
  415. if (b_xwork[i3] <= b_xwork[quartetOffset]) {
  416. idx[ib] = b_iwork[i3];
  417. x[ib] = b_xwork[i3];
  418. if (i3 + 1 < bLen) {
  419. i3++;
  420. } else {
  421. exitg1 = 1;
  422. }
  423. } else {
  424. idx[ib] = b_iwork[quartetOffset];
  425. x[ib] = b_xwork[quartetOffset];
  426. if (quartetOffset + 1 < b_n) {
  427. quartetOffset++;
  428. } else {
  429. ib -= i3;
  430. for (quartetOffset = i3 + 1; quartetOffset <= bLen;
  431. quartetOffset++) {
  432. idx_tmp = ib + quartetOffset;
  433. idx[idx_tmp] = b_iwork[quartetOffset - 1];
  434. x[idx_tmp] = b_xwork[quartetOffset - 1];
  435. }
  436. exitg1 = 1;
  437. }
  438. }
  439. } while (exitg1 == 0);
  440. }
  441. }
  442. }
  443. quartetOffset = nBlocks << 8;
  444. ib = nNonNaN - quartetOffset;
  445. if (ib > 0) {
  446. merge_block(idx, x, quartetOffset, ib, 2, iwork, xwork);
  447. }
  448. quartetOffset = 8;
  449. }
  450. }
  451. merge_block(idx, x, 0, nNonNaN, quartetOffset, iwork, xwork);
  452. }
  453. }
  454. }
  455. } // namespace internal
  456. } // namespace coder
  457. //
  458. // File trailer for sort.cpp
  459. //
  460. // [EOF]
  461. //