123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- //
- // Academic License - for use in teaching, academic research, and meeting
- // course requirements at degree granting institutions only. Not for
- // government, commercial, or other organizational use.
- // File: sort.cpp
- //
- // MATLAB Coder version : 5.3
- // C/C++ source code generated on : 27-Mar-2023 15:34:32
- //
- // Include Files
- #include "fun10_sort.h"
- #include "rt_nonfinite.h"
- #include "fun10_sortIdx.h"
- #include "coder_array.h"
- #include <cmath>
- #include <cstring>
- // Function Definitions
- //
- // Arguments : double x[16]
- // Return Type : void
- //
- namespace coder {
- namespace internal {
- void sort(double x[16])
- {
- double xwork[16];
- double x4[4];
- int idx[16];
- int iwork[16];
- int i2;
- int i3;
- int ib;
- int k;
- signed char idx4[4];
- signed char perm[4];
- x4[0] = 0.0;
- idx4[0] = 0;
- x4[1] = 0.0;
- idx4[1] = 0;
- x4[2] = 0.0;
- idx4[2] = 0;
- x4[3] = 0.0;
- idx4[3] = 0;
- ib = 0;
- for (k = 0; k < 16; k++) {
- idx[k] = 0;
- xwork[k] = 0.0;
- ib++;
- idx4[ib - 1] = static_cast<signed char>(k + 1);
- x4[ib - 1] = x[k];
- if (ib == 4) {
- double d;
- double d1;
- int i4;
- if (x4[0] <= x4[1]) {
- ib = 1;
- i2 = 2;
- } else {
- ib = 2;
- i2 = 1;
- }
- if (x4[2] <= x4[3]) {
- i3 = 3;
- i4 = 4;
- } else {
- i3 = 4;
- i4 = 3;
- }
- d = x4[ib - 1];
- d1 = x4[i3 - 1];
- if (d <= d1) {
- d = x4[i2 - 1];
- if (d <= d1) {
- perm[0] = static_cast<signed char>(ib);
- perm[1] = static_cast<signed char>(i2);
- perm[2] = static_cast<signed char>(i3);
- perm[3] = static_cast<signed char>(i4);
- } else if (d <= x4[i4 - 1]) {
- perm[0] = static_cast<signed char>(ib);
- perm[1] = static_cast<signed char>(i3);
- perm[2] = static_cast<signed char>(i2);
- perm[3] = static_cast<signed char>(i4);
- } else {
- perm[0] = static_cast<signed char>(ib);
- perm[1] = static_cast<signed char>(i3);
- perm[2] = static_cast<signed char>(i4);
- perm[3] = static_cast<signed char>(i2);
- }
- } else {
- d1 = x4[i4 - 1];
- if (d <= d1) {
- if (x4[i2 - 1] <= d1) {
- perm[0] = static_cast<signed char>(i3);
- perm[1] = static_cast<signed char>(ib);
- perm[2] = static_cast<signed char>(i2);
- perm[3] = static_cast<signed char>(i4);
- } else {
- perm[0] = static_cast<signed char>(i3);
- perm[1] = static_cast<signed char>(ib);
- perm[2] = static_cast<signed char>(i4);
- perm[3] = static_cast<signed char>(i2);
- }
- } else {
- perm[0] = static_cast<signed char>(i3);
- perm[1] = static_cast<signed char>(i4);
- perm[2] = static_cast<signed char>(ib);
- perm[3] = static_cast<signed char>(i2);
- }
- }
- idx[k - 3] = idx4[perm[0] - 1];
- idx[k - 2] = idx4[perm[1] - 1];
- idx[k - 1] = idx4[perm[2] - 1];
- idx[k] = idx4[perm[3] - 1];
- x[k - 3] = x4[perm[0] - 1];
- x[k - 2] = x4[perm[1] - 1];
- x[k - 1] = x4[perm[2] - 1];
- x[k] = x4[perm[3] - 1];
- ib = 0;
- }
- }
- if (ib > 0) {
- perm[1] = 0;
- perm[2] = 0;
- perm[3] = 0;
- if (ib == 1) {
- perm[0] = 1;
- } else if (ib == 2) {
- if (x4[0] <= x4[1]) {
- perm[0] = 1;
- perm[1] = 2;
- } else {
- perm[0] = 2;
- perm[1] = 1;
- }
- } else if (x4[0] <= x4[1]) {
- if (x4[1] <= x4[2]) {
- perm[0] = 1;
- perm[1] = 2;
- perm[2] = 3;
- } else if (x4[0] <= x4[2]) {
- perm[0] = 1;
- perm[1] = 3;
- perm[2] = 2;
- } else {
- perm[0] = 3;
- perm[1] = 1;
- perm[2] = 2;
- }
- } else if (x4[0] <= x4[2]) {
- perm[0] = 2;
- perm[1] = 1;
- perm[2] = 3;
- } else if (x4[1] <= x4[2]) {
- perm[0] = 2;
- perm[1] = 3;
- perm[2] = 1;
- } else {
- perm[0] = 3;
- perm[1] = 2;
- perm[2] = 1;
- }
- for (k = 0; k < ib; k++) {
- i2 = perm[k] - 1;
- i3 = (k - ib) + 16;
- idx[i3] = idx4[i2];
- x[i3] = x4[i2];
- }
- }
- std::memset(&iwork[0], 0, 16U * sizeof(int));
- i3 = 4;
- i2 = 4;
- while (i3 > 1) {
- if ((i3 & 1) != 0) {
- i3--;
- ib = i2 * i3;
- if (16 - ib > i2) {
- merge(idx, x, ib, i2, 16 - (ib + i2), iwork, xwork);
- }
- }
- ib = i2 << 1;
- i3 >>= 1;
- for (k = 0; k < i3; k++) {
- merge(idx, x, k * ib, i2, i2, iwork, xwork);
- }
- i2 = ib;
- }
- if (16 > i2) {
- merge(idx, x, 0, i2, 16 - i2, iwork, xwork);
- }
- }
- //
- // Arguments : ::coder::array<double, 2U> &x
- // Return Type : void
- //
- void sort(::coder::array<double, 2U> &x)
- {
- array<double, 1U> xwork;
- array<int, 2U> idx;
- array<int, 1U> iwork;
- double b_xwork[256];
- double x4[4];
- int b_iwork[256];
- int idx4[4];
- int ib;
- int quartetOffset;
- signed char perm[4];
- idx.set_size(1, x.size(1));
- quartetOffset = x.size(1);
- for (ib = 0; ib < quartetOffset; ib++) {
- idx[ib] = 0;
- }
- if (x.size(1) != 0) {
- int bLen;
- int b_n;
- int i2;
- int i3;
- int i4;
- int idx_tmp;
- int k;
- int n;
- int nNonNaN;
- n = x.size(1);
- b_n = x.size(1);
- x4[0] = 0.0;
- idx4[0] = 0;
- x4[1] = 0.0;
- idx4[1] = 0;
- x4[2] = 0.0;
- idx4[2] = 0;
- x4[3] = 0.0;
- idx4[3] = 0;
- quartetOffset = x.size(1);
- iwork.set_size(quartetOffset);
- for (ib = 0; ib < quartetOffset; ib++) {
- iwork[ib] = 0;
- }
- quartetOffset = x.size(1);
- xwork.set_size(quartetOffset);
- for (ib = 0; ib < quartetOffset; ib++) {
- xwork[ib] = 0.0;
- }
- bLen = 0;
- ib = -1;
- for (k = 0; k < b_n; k++) {
- if (std::isnan(x[k])) {
- idx_tmp = (b_n - bLen) - 1;
- idx[idx_tmp] = k + 1;
- xwork[idx_tmp] = x[k];
- bLen++;
- } else {
- ib++;
- idx4[ib] = k + 1;
- x4[ib] = x[k];
- if (ib + 1 == 4) {
- double d;
- double d1;
- quartetOffset = k - bLen;
- if (x4[0] <= x4[1]) {
- ib = 1;
- i2 = 2;
- } else {
- ib = 2;
- i2 = 1;
- }
- if (x4[2] <= x4[3]) {
- i3 = 3;
- i4 = 4;
- } else {
- i3 = 4;
- i4 = 3;
- }
- d = x4[ib - 1];
- d1 = x4[i3 - 1];
- if (d <= d1) {
- d = x4[i2 - 1];
- if (d <= d1) {
- perm[0] = static_cast<signed char>(ib);
- perm[1] = static_cast<signed char>(i2);
- perm[2] = static_cast<signed char>(i3);
- perm[3] = static_cast<signed char>(i4);
- } else if (d <= x4[i4 - 1]) {
- perm[0] = static_cast<signed char>(ib);
- perm[1] = static_cast<signed char>(i3);
- perm[2] = static_cast<signed char>(i2);
- perm[3] = static_cast<signed char>(i4);
- } else {
- perm[0] = static_cast<signed char>(ib);
- perm[1] = static_cast<signed char>(i3);
- perm[2] = static_cast<signed char>(i4);
- perm[3] = static_cast<signed char>(i2);
- }
- } else {
- d1 = x4[i4 - 1];
- if (d <= d1) {
- if (x4[i2 - 1] <= d1) {
- perm[0] = static_cast<signed char>(i3);
- perm[1] = static_cast<signed char>(ib);
- perm[2] = static_cast<signed char>(i2);
- perm[3] = static_cast<signed char>(i4);
- } else {
- perm[0] = static_cast<signed char>(i3);
- perm[1] = static_cast<signed char>(ib);
- perm[2] = static_cast<signed char>(i4);
- perm[3] = static_cast<signed char>(i2);
- }
- } else {
- perm[0] = static_cast<signed char>(i3);
- perm[1] = static_cast<signed char>(i4);
- perm[2] = static_cast<signed char>(ib);
- perm[3] = static_cast<signed char>(i2);
- }
- }
- idx[quartetOffset - 3] = idx4[perm[0] - 1];
- idx[quartetOffset - 2] = idx4[perm[1] - 1];
- idx[quartetOffset - 1] = idx4[perm[2] - 1];
- idx[quartetOffset] = idx4[perm[3] - 1];
- x[quartetOffset - 3] = x4[perm[0] - 1];
- x[quartetOffset - 2] = x4[perm[1] - 1];
- x[quartetOffset - 1] = x4[perm[2] - 1];
- x[quartetOffset] = x4[perm[3] - 1];
- ib = -1;
- }
- }
- }
- i3 = (b_n - bLen) - 1;
- if (ib + 1 > 0) {
- perm[1] = 0;
- perm[2] = 0;
- perm[3] = 0;
- if (ib + 1 == 1) {
- perm[0] = 1;
- } else if (ib + 1 == 2) {
- if (x4[0] <= x4[1]) {
- perm[0] = 1;
- perm[1] = 2;
- } else {
- perm[0] = 2;
- perm[1] = 1;
- }
- } else if (x4[0] <= x4[1]) {
- if (x4[1] <= x4[2]) {
- perm[0] = 1;
- perm[1] = 2;
- perm[2] = 3;
- } else if (x4[0] <= x4[2]) {
- perm[0] = 1;
- perm[1] = 3;
- perm[2] = 2;
- } else {
- perm[0] = 3;
- perm[1] = 1;
- perm[2] = 2;
- }
- } else if (x4[0] <= x4[2]) {
- perm[0] = 2;
- perm[1] = 1;
- perm[2] = 3;
- } else if (x4[1] <= x4[2]) {
- perm[0] = 2;
- perm[1] = 3;
- perm[2] = 1;
- } else {
- perm[0] = 3;
- perm[1] = 2;
- perm[2] = 1;
- }
- for (k = 0; k <= ib; k++) {
- idx_tmp = perm[k] - 1;
- quartetOffset = (i3 - ib) + k;
- idx[quartetOffset] = idx4[idx_tmp];
- x[quartetOffset] = x4[idx_tmp];
- }
- }
- ib = (bLen >> 1) + 1;
- for (k = 0; k <= ib - 2; k++) {
- quartetOffset = (i3 + k) + 1;
- i2 = idx[quartetOffset];
- idx_tmp = (b_n - k) - 1;
- idx[quartetOffset] = idx[idx_tmp];
- idx[idx_tmp] = i2;
- x[quartetOffset] = xwork[idx_tmp];
- x[idx_tmp] = xwork[quartetOffset];
- }
- if ((bLen & 1) != 0) {
- ib += i3;
- x[ib] = xwork[ib];
- }
- nNonNaN = n - bLen;
- quartetOffset = 2;
- if (nNonNaN > 1) {
- if (n >= 256) {
- int nBlocks;
- nBlocks = nNonNaN >> 8;
- if (nBlocks > 0) {
- for (int b{0}; b < nBlocks; b++) {
- i4 = (b << 8) - 1;
- for (int b_b{0}; b_b < 6; b_b++) {
- bLen = 1 << (b_b + 2);
- b_n = bLen << 1;
- n = 256 >> (b_b + 3);
- for (k = 0; k < n; k++) {
- i2 = (i4 + k * b_n) + 1;
- for (quartetOffset = 0; quartetOffset < b_n; quartetOffset++) {
- ib = i2 + quartetOffset;
- b_iwork[quartetOffset] = idx[ib];
- b_xwork[quartetOffset] = x[ib];
- }
- i3 = 0;
- quartetOffset = bLen;
- ib = i2 - 1;
- int exitg1;
- do {
- exitg1 = 0;
- ib++;
- if (b_xwork[i3] <= b_xwork[quartetOffset]) {
- idx[ib] = b_iwork[i3];
- x[ib] = b_xwork[i3];
- if (i3 + 1 < bLen) {
- i3++;
- } else {
- exitg1 = 1;
- }
- } else {
- idx[ib] = b_iwork[quartetOffset];
- x[ib] = b_xwork[quartetOffset];
- if (quartetOffset + 1 < b_n) {
- quartetOffset++;
- } else {
- ib -= i3;
- for (quartetOffset = i3 + 1; quartetOffset <= bLen;
- quartetOffset++) {
- idx_tmp = ib + quartetOffset;
- idx[idx_tmp] = b_iwork[quartetOffset - 1];
- x[idx_tmp] = b_xwork[quartetOffset - 1];
- }
- exitg1 = 1;
- }
- }
- } while (exitg1 == 0);
- }
- }
- }
- quartetOffset = nBlocks << 8;
- ib = nNonNaN - quartetOffset;
- if (ib > 0) {
- merge_block(idx, x, quartetOffset, ib, 2, iwork, xwork);
- }
- quartetOffset = 8;
- }
- }
- merge_block(idx, x, 0, nNonNaN, quartetOffset, iwork, xwork);
- }
- }
- }
- } // namespace internal
- } // namespace coder
- //
- // File trailer for sort.cpp
- //
- // [EOF]
- //
|