From 688dc5a73d06c4e1fb681e47844e03cf4918e2ee Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 18 Feb 2022 23:39:18 -0800 Subject: [PATCH] Binary search Lua struct fields --- autogen/convert_structs.py | 2 -- src/pc/lua/smlua_cobject.c | 22 +++++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/autogen/convert_structs.py b/autogen/convert_structs.py index 8f1fd3c1..8ed0628e 100644 --- a/autogen/convert_structs.py +++ b/autogen/convert_structs.py @@ -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 = { diff --git a/src/pc/lua/smlua_cobject.c b/src/pc/lua/smlua_cobject.c index bd97f5a4..b38d88c8 100644 --- a/src/pc/lua/smlua_cobject.c +++ b/src/pc/lua/smlua_cobject.c @@ -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; }