Zigbee replace qsort with insertion sort

This commit is contained in:
Stephan Hadinger 2021-01-03 15:09:20 +01:00
parent 1e49cb5a2a
commit be5d9f90a8
2 changed files with 803 additions and 795 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1856,12 +1856,11 @@ const char ZB_WEB[] PROGMEM = "\x00\x66\x3D\x0E\xCA\xB1\xC1\x33\xF0\xF6\xD1\xEE\
// ++++++++++++++++++++ DO NOT EDIT ABOVE ++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
extern "C" {
// comparator function used to sort Zigbee devices by alphabetical order (if friendlyname)
// then by shortaddr if they don't have friendlyname
int device_cmp(const void * a, const void * b) {
const Z_Device &dev_a = zigbee_devices.devicesAt(*(uint8_t*)a);
const Z_Device &dev_b = zigbee_devices.devicesAt(*(uint8_t*)b);
int device_cmp(uint8_t a, uint8_t b) {
const Z_Device &dev_a = zigbee_devices.devicesAt(a);
const Z_Device &dev_b = zigbee_devices.devicesAt(b);
const char * fn_a = dev_a.friendlyName;
const char * fn_b = dev_b.friendlyName;
@ -1901,7 +1900,6 @@ extern "C" {
}
return 0;
}
} // extern "C"
const char HTTP_BTN_ZB_BUTTONS[] PROGMEM =
"<button onclick='la(\"&zbj=1\");'>" D_ZIGBEE_PERMITJOIN "</button>"
@ -1927,7 +1925,17 @@ void ZigbeeShow(bool json)
for (uint32_t i = 0; i < zigbee_num; i++) {
sorted_idx[i] = i;
}
qsort(sorted_idx, zigbee_num, sizeof(sorted_idx[0]), device_cmp);
// insertion sort
for (uint32_t i = 1; i < zigbee_num; i++) {
uint8_t key = sorted_idx[i];
uint8_t j = i;
while ((j > 0) && (device_cmp(sorted_idx[j - 1], key) > 0)) {
sorted_idx[j] = sorted_idx[j - 1];
j--;
}
sorted_idx[j] = key;
}
uint32_t now = Rtc.utc_time;