Move sort_order to stack during ::presort

This commit is contained in:
Alex Voinea 2021-02-10 18:16:26 +02:00
parent 8397dae386
commit 1c1ff722c0
No known key found for this signature in database
GPG Key ID: F5034E7CFCF2F973
2 changed files with 26 additions and 4 deletions

View File

@ -791,7 +791,7 @@ void CardReader::updir()
*/ */
void CardReader::getfilename_sorted(const uint16_t nr) { void CardReader::getfilename_sorted(const uint16_t nr) {
if (nr < sort_count) if (nr < sort_count)
getfilename_simple(sort_positions[sort_order[nr]]); getfilename_simple(sort_positions[nr]);
else else
getfilename(nr); getfilename(nr);
} }
@ -840,6 +840,7 @@ 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();
@ -1012,6 +1013,30 @@ void CardReader::presort() {
if (!didSwap) break; if (!didSwap) break;
} //end of bubble sort loop } //end of bubble sort loop
#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_positions[i];
uint8_t idx = sort_order_reverse_index[i];
while (idx != i)
{
uint32_t el1 = sort_positions[idx];
uint8_t idx1 = sort_order_reverse_index[idx];
sort_order_reverse_index[idx] = idx;
sort_positions[idx] = el;
idx = idx1;
el = el1;
}
sort_order_reverse_index[idx] = idx;
sort_positions[idx] = el;
}
}
lcd_set_cursor(0, 2); lcd_set_cursor(0, 2);
for (int column = 0; column <= 19; column++) for (int column = 0; column <= 19; column++)
lcd_print('\xFF'); //simple progress bar lcd_print('\xFF'); //simple progress bar
@ -1019,7 +1044,6 @@ void CardReader::presort() {
lcd_clear(); lcd_clear();
} }
else { else {
sort_order[0] = 0;
getfilename(0); getfilename(0);
sort_positions[0] = position; sort_positions[0] = position;
} }

View File

@ -95,8 +95,6 @@ private:
// Sort files and folders alphabetically. // Sort files and folders alphabetically.
#ifdef SDCARD_SORT_ALPHA #ifdef SDCARD_SORT_ALPHA
uint16_t sort_count; // Count of sorted items in the current directory uint16_t sort_count; // Count of sorted items in the current directory
uint8_t sort_order[SDSORT_LIMIT]; // By default the sort index is static.
uint32_t sort_positions[SDSORT_LIMIT]; uint32_t sort_positions[SDSORT_LIMIT];
#endif // SDCARD_SORT_ALPHA #endif // SDCARD_SORT_ALPHA