From a5e7b6a22f83160b0fa7b02a3586a7160c62967e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 7 Jun 2017 09:34:07 +0100 Subject: [PATCH] Use a vector instead of a hash map to store hooks --- src/hook_manager.cc | 26 ++++++++++++++------------ src/hook_manager.hh | 10 ++++++++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/hook_manager.cc b/src/hook_manager.cc index ad30c350..c9410345 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -12,18 +12,20 @@ namespace Kakoune { -void HookManager::add_hook(StringView hook_name, String group, HookFunc hook) +void HookManager::add_hook(StringView hook_name, String group, HookFunc func) { auto& hooks = m_hooks[hook_name]; - hooks.insert({std::move(group), std::move(hook)}); + hooks.push_back({std::move(group), std::move(func)}); } void HookManager::remove_hooks(StringView group) { if (group.empty()) throw runtime_error("invalid id"); - for (auto& hooks : m_hooks) - hooks.value.remove_all(group); + for (auto& list : m_hooks) + list.value.erase(std::remove_if(list.value.begin(), list.value.end(), + [&](const Hook& h) { return h.group == group; }), + list.value.end()); } CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_in_token) @@ -31,7 +33,7 @@ CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_ CandidateList res; for (auto& list : m_hooks) { - auto container = list.value | transform(std::mem_fn(&decltype(list.value)::Item::key)); + auto container = list.value | transform(std::mem_fn(&decltype(list.value)::value_type::group)); for (auto& c : complete(prefix, pos_in_token, container)) { if (!contains(res, c)) @@ -69,12 +71,12 @@ void HookManager::run_hook(StringView hook_name, auto start_time = profile ? Clock::now() : TimePoint{}; auto& disabled_hooks = context.options()["disabled_hooks"].get(); - Vector> hooks_to_run; + Vector hooks_to_run; for (auto& hook : hook_list_it->value) { - if (hook.key.empty() or disabled_hooks.empty() or - not regex_match(hook.key.begin(), hook.key.end(), disabled_hooks)) - hooks_to_run.push_back({hook.key, hook.value}); + if (hook.group.empty() or disabled_hooks.empty() or + not regex_match(hook.group.begin(), hook.group.end(), disabled_hooks)) + hooks_to_run.push_back(hook); } bool hook_error = false; @@ -83,14 +85,14 @@ void HookManager::run_hook(StringView hook_name, try { if (debug_flags & DebugFlags::Hooks) - write_to_debug_buffer(format("hook {}/{}", hook_name, hook.first)); - hook.second(param, context); + write_to_debug_buffer(format("hook {}({})/{}", hook_name, param, hook.group)); + hook.func(param, context); } catch (runtime_error& err) { hook_error = true; write_to_debug_buffer(format("error running hook {}({})/{}: {}", - hook_name, param, hook.first, err.what())); + hook_name, param, hook.group, err.what())); } } diff --git a/src/hook_manager.hh b/src/hook_manager.hh index 8198e079..ae27b352 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -16,7 +16,7 @@ class HookManager : public SafeCountable public: HookManager(HookManager& parent) : m_parent(&parent) {} - void add_hook(StringView hook_name, String group, HookFunc hook); + void add_hook(StringView hook_name, String group, HookFunc func); void remove_hooks(StringView group); CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token); void run_hook(StringView hook_name, StringView param, @@ -28,8 +28,14 @@ private: // the only one allowed to construct a root hook manager friend class Scope; + struct Hook + { + String group; + HookFunc func; + }; + SafePtr m_parent; - HashMap, MemoryDomain::Hooks> m_hooks; + HashMap, MemoryDomain::Hooks> m_hooks; mutable Vector, MemoryDomain::Hooks> m_running_hooks; };