Refactor Alias_FindRandomRange

This commit is contained in:
smallmodel 2025-03-02 13:49:01 +01:00
parent 9963a99868
commit 5c0adf9a28
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512

View file

@ -553,69 +553,69 @@ void Alias_ListFindRandomRange(
return;
}
for (index = 0; index < length; index++) {
convalias[index] = tolower(alias[index]);
for (i = 0; i < length; i++) {
convalias[i] = tolower(alias[i]);
}
convalias[length] = 0;
l = 0;
r = list->num_in_list - 1;
if (r >= 0) {
do {
index = (l + r) >> 1;
diff = strncmp(convalias, list->sorted_list[index]->alias_name, length);
while (r >= l) {
index = (l + r) >> 1;
diff = strncmp(convalias, list->sorted_list[index]->alias_name, length);
if (diff == 0) {
break;
} else if (diff > 0) {
l = index + 1;
} else {
r = index - 1;
}
} while (r >= l);
if (r >= l) {
numfound = 0;
*min_index = index;
*max_index = index;
totalfoundweight = 0.f;
ptr = &list->sorted_list[index];
for (i = index + 1; i > 0; i--, ptr--) {
if (strncmp(convalias, (*ptr)->alias_name, length)) {
break;
}
if (numfound < ARRAY_LEN(foundlist) && (*ptr)->alias_name[length] != '_') {
foundlist[numfound++] = *ptr;
totalfoundweight += (*ptr)->weight;
*min_index = i - 1;
}
}
ptr = &list->sorted_list[index + 1];
for (i = index + 1; i < list->num_in_list; i++, ptr++) {
if (strncmp(convalias, (*ptr)->alias_name, length)) {
break;
}
if (numfound < ARRAY_LEN(foundlist) && (*ptr)->alias_name[length] != '_') {
foundlist[numfound++] = *ptr;
totalfoundweight += (*ptr)->weight;
*max_index = i;
}
}
if (numfound) {
*total_weight = totalfoundweight;
} else {
*min_index = -1;
*max_index = -1;
}
if (diff == 0) {
break;
} else if (diff > 0) {
l = index + 1;
} else {
r = index - 1;
}
}
if (r < l) {
return;
}
numfound = 0;
*min_index = index;
*max_index = index;
totalfoundweight = 0.f;
ptr = &list->sorted_list[index];
for (i = index + 1; i > 0; i--, ptr--) {
if (strncmp(convalias, (*ptr)->alias_name, length)) {
break;
}
if (numfound < ARRAY_LEN(foundlist) && (*ptr)->alias_name[length] != '_') {
foundlist[numfound++] = *ptr;
totalfoundweight += (*ptr)->weight;
*min_index = i - 1;
}
}
ptr = &list->sorted_list[index + 1];
for (i = index + 1; i < list->num_in_list; i++, ptr++) {
if (strncmp(convalias, (*ptr)->alias_name, length)) {
break;
}
if (numfound < ARRAY_LEN(foundlist) && (*ptr)->alias_name[length] != '_') {
foundlist[numfound++] = *ptr;
totalfoundweight += (*ptr)->weight;
*max_index = i;
}
}
if (!numfound) {
*min_index = -1;
*max_index = -1;
return;
}
*total_weight = totalfoundweight;
}
void Alias_ListDump(AliasList_t *list)