From: Mikko Rasa Date: Sun, 27 Nov 2022 11:09:58 +0000 (+0200) Subject: Reimplement reverse key lookup with a sorted vector X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9d273406df9becf1471065b34324f18b9eec894a;p=libs%2Fgui.git Reimplement reverse key lookup with a sorted vector It's more efficient than map due to compact storage. --- diff --git a/source/input/keys.cpp b/source/input/keys.cpp index 9b3349a..d5687ae 100644 --- a/source/input/keys.cpp +++ b/source/input/keys.cpp @@ -1,6 +1,7 @@ #include "keys.h" -#include #include +#include +#include #include using namespace std; @@ -12,21 +13,25 @@ extern unsigned sys_keymap[]; unsigned key_from_sys(unsigned code) { - static bool init_done = false; - static map reverse_map; + struct MappedKey + { + unsigned code; + Key key; + }; + + static vector reverse_map; - if(!init_done) + if(reverse_map.empty()) { for(unsigned i=0; i(i) }); + sort_member(reverse_map, &MappedKey::code); } - auto i = reverse_map.find(code); - if(i!=reverse_map.end()) - return i->second; + auto i = lower_bound_member(reverse_map, code, &MappedKey::code); + if(i!=reverse_map.end() && i->code==code) + return i->key; return 0; }