Sorry for the delay here-I spent most of yesterday dealing with an errant 3D printer. At any rate I was trying save you having you look at my code using pseudo code but it lead to confusion and I apologize. I am attaching one example of the actual code here (there are several similar ones in about 10,000 lines). This type of pattern-copying the same code for both the GT and LT condition-- occurs a lot and I want to condense the code for simplicity. (if you search on the "<" sign you will find the statement in question)
Keep in mind that the user calls the appropriate routine dynamically so it cannot be "hard compiled" with a #define for example.
I have tested the code with timers and the addition of an "if" statement takes up to 20% extra time depending matrix size and upon which compiler I use ranging from the gnu with O1 and O3 to the MPLAB. More important however is that this raises a general question regarding the ability of C++ to have a function that shares all code except perhaps one line. It does this type of thing with Class overloading but not, to my knowledge, for functions.
My 3rd example in the attachment shows my attempt to use the "lambda" but I cannot really find documentation to clarify this method and perhaps it is not even the right way to go.
cheers
Fritz
Code:
//======================================== colsort_bubble =================================
IRIS_EU_STATUS Matrix::colsort_bubble()
{
uint32_t ii, jj, kk;
Data_t holdsw;
bool swapped;
for (jj = 0; jj < ncols; jj++)
{
for (ii = 0; ii < mrows - 1; ii++)
{
swapped = false;
for (kk = 0; kk < mrows-ii-1; kk++)
{
if (pDataBuffer[kk*ncols + jj] > pDataBuffer[kk*ncols + jj + ncols] )
{
holdsw = pDataBuffer[kk*ncols + jj]; //swap elements
pDataBuffer[kk*ncols + jj] = pDataBuffer[kk*ncols + jj + ncols ];
pDataBuffer[kk*ncols + jj + ncols ] = holdsw;
swapped = true;
} //end if
} //end for(kk)
if (swapped == false) break;
} //end for(ii)
} //end for(jj)
return(0);
} //end colsort_bubble
//======================================== colsort_bubble descending ======================
IRIS_EU_STATUS Matrix::colsort_bubble_desc()
{
//This function reverse sorts the matrix on a column by column basis. Specifying Matrix X(N,1) will
//allow use as a col vector sort.
uint32_t ii, jj, kk;
Data_t holdsw;
bool swapped;
for (jj = 0; jj < ncols; jj++)
{
for (ii = 0; ii < mrows - 1; ii++)
{
swapped = false;
for (kk = 0; kk < mrows-ii-1; kk++)
{
if (pDataBuffer[kk*ncols + jj] < pDataBuffer[kk*ncols + jj + ncols] )
{
holdsw = pDataBuffer[kk*ncols + jj]; //swap elements
pDataBuffer[kk*ncols + jj] = pDataBuffer[kk*ncols + jj + ncols ];
pDataBuffer[kk*ncols + jj + ncols ] = holdsw;
swapped = true;
} //end if
} //end for(kk)
if (swapped == false) break;
} //end for(ii)
} //end for(jj)
return(0);
} //end colsort_bubble
//======================================== colsort_bubble both ways ========================
IRIS_EU_STATUS Matrix::colsort_bubble_both(char dir)
{
uint32_t ii, jj, kk;
Data_t holdsw;
bool swapped;
auto greater = [&](auto a, auto b) -> bool
{
if(dir == 'd') return a < b;
else return a > b;
};
for (jj = 0; jj < ncols; jj++)
{
for (ii = 0; ii < mrows - 1; ii++)
{
swapped = false;
for (kk = 0; kk < mrows-ii-1; kk++)
{
//if (pDataBuffer[kk*ncols + jj] > pDataBuffer[kk*ncols + jj + ncols] )
if ( greater( pDataBuffer[kk*ncols + jj] , pDataBuffer[kk*ncols + jj + ncols] ) )
{
holdsw = pDataBuffer[kk*ncols + jj]; //swap elements
pDataBuffer[kk*ncols + jj] = pDataBuffer[kk*ncols + jj + ncols ];
pDataBuffer[kk*ncols + jj + ncols ] = holdsw;
swapped = true;
} //end if
} //end for(kk)
if (swapped == false) break;
} //end for(ii)
} //end for(jj)
return(0);
} //end colsort_bubble