From 99996902de8d28565e3a3ae26e60e0977e209c24 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 5 Mar 2015 14:59:27 +0000 Subject: [PATCH] Use a IdMap for storing hooks The number of hook names is small (<20), using an UnorderedMap is overkill, and prevents using StringView for key searching. --- src/buffer.cc | 2 +- src/buffer.hh | 2 +- src/hook_manager.cc | 4 ++-- src/hook_manager.hh | 6 +++--- src/id_map.hh | 15 +++++++++++++++ src/window.cc | 2 +- src/window.hh | 2 +- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 60c9fdce..34b636f7 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -516,7 +516,7 @@ void Buffer::on_option_changed(const Option& option) option.name() + "=" + option.get_as_string()); } -void Buffer::run_hook_in_own_context(const String& hook_name, StringView param) +void Buffer::run_hook_in_own_context(StringView hook_name, StringView param) { InputHandler hook_handler({ *this, Selection{} }, Context::Flags::Transient); hooks().run_hook(hook_name, param, hook_handler.context()); diff --git a/src/buffer.hh b/src/buffer.hh index a7c8de25..d6906c87 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -150,7 +150,7 @@ public: ValueMap& values() const { return m_values; } - void run_hook_in_own_context(const String& hook_name, StringView param); + void run_hook_in_own_context(StringView hook_name, StringView param); void reload(BufferLines lines, time_t fs_timestamp = InvalidTime); diff --git a/src/hook_manager.cc b/src/hook_manager.cc index e460d696..cc4c1ec6 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -8,7 +8,7 @@ namespace Kakoune { -void HookManager::add_hook(const String& hook_name, String group, HookFunc hook) +void HookManager::add_hook(StringView hook_name, String group, HookFunc hook) { auto& hooks = m_hook[hook_name]; hooks.append({std::move(group), std::move(hook)}); @@ -37,7 +37,7 @@ CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_ return res; } -void HookManager::run_hook(const String& hook_name, +void HookManager::run_hook(StringView hook_name, StringView param, Context& context) const { if (m_parent) diff --git a/src/hook_manager.hh b/src/hook_manager.hh index 410afd61..80c99ff8 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -16,10 +16,10 @@ class HookManager public: HookManager(HookManager& parent) : m_parent(&parent) {} - void add_hook(const String& hook_name, String group, HookFunc hook); + void add_hook(StringView hook_name, String group, HookFunc hook); void remove_hooks(StringView group); CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token); - void run_hook(const String& hook_name, StringView param, + void run_hook(StringView hook_name, StringView param, Context& context) const; private: @@ -29,7 +29,7 @@ private: friend class Scope; HookManager* m_parent; - UnorderedMap, MemoryDomain::Hooks> m_hook; + IdMap, MemoryDomain::Hooks> m_hook; }; } diff --git a/src/id_map.hh b/src/id_map.hh index 4854e065..e6f2f7b0 100644 --- a/src/id_map.hh +++ b/src/id_map.hh @@ -59,6 +59,21 @@ public: m_content.erase(it, end()); } + Value& operator[](StringView id) + { + auto it = find(id); + if (it != m_content.end()) + return it->second; + + append({ id, Value{} }); + return (m_content.end()-1)->second; + } + + const Value& operator[](StringView id) const + { + return (*const_cast(this))[id]; + } + static const String& get_id(const value_type& v) { return v.first; } bool empty() const { return m_content.empty(); } diff --git a/src/window.cc b/src/window.cc index b46aaaaf..0e999162 100644 --- a/src/window.cc +++ b/src/window.cc @@ -282,7 +282,7 @@ void Window::on_option_changed(const Option& option) } -void Window::run_hook_in_own_context(const String& hook_name, StringView param) +void Window::run_hook_in_own_context(StringView hook_name, StringView param) { InputHandler hook_handler({ *m_buffer, Selection{} }, Context::Flags::Transient); hook_handler.context().set_window(*this); diff --git a/src/window.hh b/src/window.hh index ad04396d..54973d2b 100644 --- a/src/window.hh +++ b/src/window.hh @@ -50,7 +50,7 @@ private: void on_option_changed(const Option& option) override; void scroll_to_keep_selection_visible_ifn(const Context& context); - void run_hook_in_own_context(const String& hook_name, StringView param); + void run_hook_in_own_context(StringView hook_name, StringView param); SafePtr m_buffer;