File: use insert sort

This commit is contained in:
espr14 2021-03-08 20:56:51 +01:00 committed by Alex Voinea
parent 65e19f6b0b
commit 02ca9496e8
2 changed files with 68 additions and 0 deletions

View file

@ -230,6 +230,7 @@
#define SD_SORT_ALPHA 1
#define SD_SORT_NONE 2
// #define SHELLSORT
#define INSERTSORT
// #define SORTING_DUMP
#define SDSORT_LIMIT 100 // Maximum number of sorted items (10-256).

View file

@ -922,6 +922,73 @@ void CardReader::presort() {
}
}
#elif defined(INSERTSORT)
#define _SORT_CMP_NODIR() (strcasecmp(name1, name2) < 0) //true if lowercase(name1) < lowercase(name2)
#define _SORT_CMP_TIME_NODIR() (((crmod_date_bckp == crmodDate) && (crmod_time_bckp > crmodTime)) || (crmod_date_bckp > crmodDate))
#if HAS_FOLDER_SORTING
#define _SORT_CMP_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_NODIR() : (fs < 0 ? dir1 : !dir1))
#define _SORT_CMP_TIME_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_TIME_NODIR() : (fs < 0 ? dir1 : !dir1))
#endif
uint16_t counter = 0;
menu_progressbar_init(fileCnt * fileCnt / 2, _i("Sorting files"));
for (uint16_t i = 1; i < fileCnt; ++i){
// if (!IS_SD_INSERTED) return;
menu_progressbar_update(counter);
counter += i;
/// pop the position
const uint16_t o1 = sort_order[i];
getfilename_simple(sort_positions[o1]);
strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
crmod_date_bckp = crmodDate;
crmod_time_bckp = crmodTime;
#if HAS_FOLDER_SORTING
bool dir1 = filenameIsDir;
#endif
/// find proper place
uint16_t j = i;
for (; j > 0; --j){
if (!IS_SD_INSERTED) return;
#ifdef SORTING_DUMP
for (uint16_t z = 0; z < fileCnt; z++){
printf_P(PSTR("%2u "), sort_order[z]);
}
MYSERIAL.println();
#endif
manage_heater();
const uint16_t o2 = sort_order[j - 1];
getfilename_simple(sort_positions[o2]);
char *name2 = LONGEST_FILENAME; // use the string in-place
// Sort the current pair according to settings.
if (
#if HAS_FOLDER_SORTING
(sdSort == SD_SORT_TIME && _SORT_CMP_TIME_DIR(FOLDER_SORTING)) || (sdSort == SD_SORT_ALPHA && !_SORT_CMP_DIR(FOLDER_SORTING))
#else
(sdSort == SD_SORT_TIME && _SORT_CMP_TIME_NODIR()) || (sdSort == SD_SORT_ALPHA && !_SORT_CMP_NODIR())
#endif
)
{
break;
} else {
#ifdef SORTING_DUMP
puts_P(PSTR("shift"));
#endif
sort_order[j] = o2;
}
}
/// place the position
sort_order[j] = o1;
}
#else //Bubble Sort
#define _SORT_CMP_NODIR() (strcasecmp(name1, name2) < 0) //true if lowercase(name1) < lowercase(name2)