#include "keys.h"
-#include <map>
#include <stdexcept>
+#include <vector>
+#include <msp/core/algorithm.h>
#include <msp/strings/format.h>
using namespace std;
unsigned key_from_sys(unsigned code)
{
- static bool init_done = false;
- static map<unsigned, unsigned> reverse_map;
+ struct MappedKey
+ {
+ unsigned code;
+ Key key;
+ };
+
+ static vector<MappedKey> reverse_map;
- if(!init_done)
+ if(reverse_map.empty())
{
for(unsigned i=0; i<N_KEYS_; ++i)
if(sys_keymap[i])
- reverse_map[sys_keymap[i]] = i;
-
- init_done = true;
+ reverse_map.push_back({ sys_keymap[i], static_cast<Key>(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;
}