Remove postponed sorting of elements
Now that the position is a uint16_t instead of uint32_t, it is simpler to just sort the positions in place without using the uint8_t indices. Also, this approach is considerably lighter on the stack usage and it also removes a delay after the sorting happens
This commit is contained in:
parent
c100df7ba3
commit
d9d3d68217
|
|
@ -825,7 +825,6 @@ void CardReader::presort() {
|
||||||
|
|
||||||
if (fileCnt > 1) {
|
if (fileCnt > 1) {
|
||||||
// Init sort order.
|
// Init sort order.
|
||||||
uint8_t sort_order[fileCnt];
|
|
||||||
for (uint16_t i = 0; i < fileCnt; i++) {
|
for (uint16_t i = 0; i < fileCnt; i++) {
|
||||||
if (!IS_SD_INSERTED) return;
|
if (!IS_SD_INSERTED) return;
|
||||||
manage_heater();
|
manage_heater();
|
||||||
|
|
@ -833,7 +832,6 @@ void CardReader::presort() {
|
||||||
getfilename(0);
|
getfilename(0);
|
||||||
else
|
else
|
||||||
getfilename_next(position);
|
getfilename_next(position);
|
||||||
sort_order[i] = i;
|
|
||||||
sort_entries[i] = position >> 5;
|
sort_entries[i] = position >> 5;
|
||||||
#if HAS_FOLDER_SORTING
|
#if HAS_FOLDER_SORTING
|
||||||
if (filenameIsDir) dirCnt++;
|
if (filenameIsDir) dirCnt++;
|
||||||
|
|
@ -864,8 +862,8 @@ void CardReader::presort() {
|
||||||
counter += i;
|
counter += i;
|
||||||
|
|
||||||
/// pop the position
|
/// pop the position
|
||||||
const uint16_t o1 = sort_order[i];
|
const uint16_t o1 = sort_entries[i];
|
||||||
getfilename_simple(sort_entries[o1]);
|
getfilename_simple(o1);
|
||||||
strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
|
strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
|
||||||
crmod_date_bckp = crmodDate;
|
crmod_date_bckp = crmodDate;
|
||||||
crmod_time_bckp = crmodTime;
|
crmod_time_bckp = crmodTime;
|
||||||
|
|
@ -880,15 +878,15 @@ void CardReader::presort() {
|
||||||
|
|
||||||
#ifdef SORTING_DUMP
|
#ifdef SORTING_DUMP
|
||||||
for (uint16_t z = 0; z < fileCnt; z++){
|
for (uint16_t z = 0; z < fileCnt; z++){
|
||||||
printf_P(PSTR("%2u "), sort_order[z]);
|
printf_P(PSTR("%2u "), sort_entries[z]);
|
||||||
}
|
}
|
||||||
MYSERIAL.println();
|
MYSERIAL.println();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
manage_heater();
|
manage_heater();
|
||||||
const uint16_t o2 = sort_order[j - 1];
|
const uint16_t o2 = sort_entries[j - 1];
|
||||||
|
|
||||||
getfilename_simple(sort_entries[o2]);
|
getfilename_simple(o2);
|
||||||
char *name2 = LONGEST_FILENAME; // use the string in-place
|
char *name2 = LONGEST_FILENAME; // use the string in-place
|
||||||
|
|
||||||
// Sort the current pair according to settings.
|
// Sort the current pair according to settings.
|
||||||
|
|
@ -905,11 +903,11 @@ void CardReader::presort() {
|
||||||
#ifdef SORTING_DUMP
|
#ifdef SORTING_DUMP
|
||||||
puts_P(PSTR("shift"));
|
puts_P(PSTR("shift"));
|
||||||
#endif
|
#endif
|
||||||
sort_order[j] = o2;
|
sort_entries[j] = o2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// place the position
|
/// place the position
|
||||||
sort_order[j] = o1;
|
sort_entries[j] = o1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else //Bubble Sort
|
#else //Bubble Sort
|
||||||
|
|
@ -937,22 +935,22 @@ void CardReader::presort() {
|
||||||
#ifdef SORTING_DUMP
|
#ifdef SORTING_DUMP
|
||||||
for (uint16_t z = 0; z < fileCnt; z++)
|
for (uint16_t z = 0; z < fileCnt; z++)
|
||||||
{
|
{
|
||||||
printf_P(PSTR("%2u "), sort_order[z]);
|
printf_P(PSTR("%2u "), sort_entries[z]);
|
||||||
}
|
}
|
||||||
MYSERIAL.println();
|
MYSERIAL.println();
|
||||||
#endif
|
#endif
|
||||||
manage_heater();
|
manage_heater();
|
||||||
const uint16_t o1 = sort_order[j], o2 = sort_order[j + 1];
|
const uint16_t o1 = sort_entries[j], o2 = sort_entries[j + 1];
|
||||||
|
|
||||||
counter++;
|
counter++;
|
||||||
getfilename_simple(sort_entries[o1]);
|
getfilename_simple(o1);
|
||||||
strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
|
strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
|
||||||
crmod_date_bckp = crmodDate;
|
crmod_date_bckp = crmodDate;
|
||||||
crmod_time_bckp = crmodTime;
|
crmod_time_bckp = crmodTime;
|
||||||
#if HAS_FOLDER_SORTING
|
#if HAS_FOLDER_SORTING
|
||||||
bool dir1 = filenameIsDir;
|
bool dir1 = filenameIsDir;
|
||||||
#endif
|
#endif
|
||||||
getfilename_simple(sort_entries[o2]);
|
getfilename_simple(o2);
|
||||||
char *name2 = LONGEST_FILENAME; // use the string in-place
|
char *name2 = LONGEST_FILENAME; // use the string in-place
|
||||||
|
|
||||||
// Sort the current pair according to settings.
|
// Sort the current pair according to settings.
|
||||||
|
|
@ -968,8 +966,8 @@ void CardReader::presort() {
|
||||||
puts_P(PSTR("swap"));
|
puts_P(PSTR("swap"));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
sort_order[j] = o2;
|
sort_entries[j] = o2;
|
||||||
sort_order[j + 1] = o1;
|
sort_entries[j + 1] = o1;
|
||||||
didSwap = true;
|
didSwap = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -983,32 +981,10 @@ void CardReader::presort() {
|
||||||
|
|
||||||
#ifdef SORTING_DUMP
|
#ifdef SORTING_DUMP
|
||||||
for (uint16_t z = 0; z < fileCnt; z++)
|
for (uint16_t z = 0; z < fileCnt; z++)
|
||||||
printf_P(PSTR("%2u "), sort_order[z]);
|
printf_P(PSTR("%2u "), sort_entries[z]);
|
||||||
SERIAL_PROTOCOLLN();
|
SERIAL_PROTOCOLLN();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint8_t sort_order_reverse_index[fileCnt];
|
|
||||||
for (uint8_t i = 0; i < fileCnt; i++)
|
|
||||||
sort_order_reverse_index[sort_order[i]] = i;
|
|
||||||
for (uint8_t i = 0; i < fileCnt; i++)
|
|
||||||
{
|
|
||||||
if (sort_order_reverse_index[i] != i)
|
|
||||||
{
|
|
||||||
uint32_t el = sort_entries[i];
|
|
||||||
uint8_t idx = sort_order_reverse_index[i];
|
|
||||||
while (idx != i)
|
|
||||||
{
|
|
||||||
uint32_t el1 = sort_entries[idx];
|
|
||||||
uint8_t idx1 = sort_order_reverse_index[idx];
|
|
||||||
sort_order_reverse_index[idx] = idx;
|
|
||||||
sort_entries[idx] = el;
|
|
||||||
idx = idx1;
|
|
||||||
el = el1;
|
|
||||||
}
|
|
||||||
sort_order_reverse_index[idx] = idx;
|
|
||||||
sort_entries[idx] = el;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
menu_progressbar_finish();
|
menu_progressbar_finish();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue