Binary search Lua struct fields
This commit is contained in:
parent
1269eef048
commit
3274fca929
|
@ -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 = {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue