Binary search Lua struct fields

This commit is contained in:
MysterD 2022-02-18 23:39:18 -08:00
parent 453d494549
commit 688dc5a73d
2 changed files with 19 additions and 5 deletions

View File

@ -46,8 +46,6 @@ struct LuaObjectField* smlua_get_object_field_autogen(u16 lot, const char* key);
"""
override_field_names = {
"Animation": { "unk02": "animYTransDivisor", "unk04": "startFrame", "unk06": "loopStart", "unk08": "loopEnd", "unk0A": "unusedBoneCount" },
"GraphNodeObject": { "unk38": "animInfo" },
}
override_field_types = {

View File

@ -33,12 +33,28 @@ static struct LuaObjectField* smlua_get_object_field(u16 lot, const char* key) {
}
struct LuaObjectTable* ot = &sLuaObjectTable[lot];
// TODO: change this to binary search or hash table or something
for (int i = 0; i < ot->fieldCount; i++) {
if (!strcmp(ot->fields[i].key, key)) {
// binary search
int min = 0;
int max = ot->fieldCount - 1;
int i = (min + max) / 2;
while (true) {
int rc = strcmp(key, ot->fields[i].key);
if (rc == 0) {
return &ot->fields[i];
} else if (rc < 0) {
max = i - 1;
i = (min + max) / 2;
} else if (rc > 0) {
min = i + 1;
i = (min + max) / 2;
}
if (min > max || max < min) {
return NULL;
}
}
return NULL;
}