From 40f78ba587d2844fbf19c35c077ab92c0026174e Mon Sep 17 00:00:00 2001 From: Lukhnos Liu Date: Mon, 11 Dec 2023 06:40:38 -0800 Subject: [PATCH] Use O(1) lookup for input macros --- src/InputMacro.cpp | 58 +++++++++++++++++++++++++--------------------- src/InputMacro.h | 4 ++-- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/InputMacro.cpp b/src/InputMacro.cpp index 5e37094..845012b 100644 --- a/src/InputMacro.cpp +++ b/src/InputMacro.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace McBopomofo { @@ -139,38 +140,43 @@ class InputMacroNextYearChineseZodiac : public InputMacro { std::string replacement() const; }; +static void AddMacro( + std::unordered_map>& m, + std::unique_ptr p) { + m.insert({p->name(), std::move(p)}); +} + InputMacroController::InputMacroController() { - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); - macros_.push_back(std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); + AddMacro(macros_, std::make_unique()); } InputMacroController::~InputMacroController() {} std::string InputMacroController::handle(std::string input) { - for (auto& macro : macros_) { - if (input == macro->name()) { - return macro->replacement(); - } + const auto& it = macros_.find(input); + if (it != macros_.cend()) { + return it->second->replacement(); } return input; } diff --git a/src/InputMacro.h b/src/InputMacro.h index d32c5c1..7215f85 100644 --- a/src/InputMacro.h +++ b/src/InputMacro.h @@ -3,7 +3,7 @@ #include #include -#include +#include namespace McBopomofo { class InputMacro { @@ -19,7 +19,7 @@ class InputMacroController { std::string handle(std::string input); private: - std::vector> macros_; + std::unordered_map> macros_; }; } // namespace McBopomofo