From 38e38ba14e753b2c7d2f5fc2ed62e4991a28c703 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 2 Jan 2014 12:45:19 +1100 Subject: Use a std::map for KeyMap to avoid custom iteration in favour of standard provided functionality. --- src/KeyMap.cxx | 21 ++++----------------- src/KeyMap.h | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/KeyMap.cxx b/src/KeyMap.cxx index 740776104..cb6f2b8d4 100644 --- a/src/KeyMap.cxx +++ b/src/KeyMap.cxx @@ -8,6 +8,7 @@ #include #include +#include #include "Platform.h" @@ -36,26 +37,12 @@ void KeyMap::Clear() { } void KeyMap::AssignCmdKey(int key, int modifiers, unsigned int msg) { - for (size_t keyIndex = 0; keyIndex < kmap.size(); keyIndex++) { - if ((key == kmap[keyIndex].key) && (modifiers == kmap[keyIndex].modifiers)) { - kmap[keyIndex].msg = msg; - return; - } - } - KeyToCommand ktc; - ktc.key = key; - ktc.modifiers = modifiers; - ktc.msg = msg; - kmap.push_back(ktc); + kmap[KeyModifiers(key, modifiers)] = msg; } unsigned int KeyMap::Find(int key, int modifiers) const { - for (size_t i = 0; i < kmap.size(); i++) { - if ((key == kmap[i].key) && (modifiers == kmap[i].modifiers)) { - return kmap[i].msg; - } - } - return 0; + std::map::const_iterator it = kmap.find(KeyModifiers(key, modifiers)); + return (it == kmap.end()) ? 0 : it->second; } #if PLAT_GTK_MACOSX diff --git a/src/KeyMap.h b/src/KeyMap.h index 2f14e2488..b102b356f 100644 --- a/src/KeyMap.h +++ b/src/KeyMap.h @@ -20,6 +20,22 @@ namespace Scintilla { #define SCI_CSHIFT (SCI_CTRL | SCI_SHIFT) #define SCI_ASHIFT (SCI_ALT | SCI_SHIFT) +/** + */ +class KeyModifiers { +public: + int key; + int modifiers; + KeyModifiers(int key_, int modifiers_) : key(key_), modifiers(modifiers_) { + } + bool operator<(const KeyModifiers &other) const { + if (key == other.key) + return modifiers < other.modifiers; + else + return key < other.key; + } +}; + /** */ class KeyToCommand { @@ -32,7 +48,7 @@ public: /** */ class KeyMap { - std::vector kmap; + std::map kmap; static const KeyToCommand MapDefault[]; public: -- cgit v1.2.3