From f0ae0b84102a0cb7d0dda0b8b7b1ba425d5d6eb7 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 7 Mar 2017 01:12:37 +0000 Subject: [PATCH] Replace IdMap with HashMap --- src/alias_registry.cc | 2 +- src/alias_registry.hh | 4 +- src/commands.cc | 2 +- src/env_vars.cc | 2 +- src/env_vars.hh | 4 +- src/hash_map.cc | 4 +- src/hash_map.hh | 5 -- src/highlighter.hh | 4 +- src/highlighter_group.cc | 6 +- src/highlighter_group.hh | 4 +- src/highlighters.cc | 34 +++++------ src/hook_manager.cc | 4 +- src/hook_manager.hh | 4 +- src/id_map.hh | 124 --------------------------------------- src/json_ui.cc | 4 +- src/json_ui.hh | 1 + src/ncurses_ui.hh | 6 +- src/option_types.hh | 39 ------------ src/parameters_parser.hh | 4 +- src/register_manager.cc | 4 +- src/remote.cc | 20 +++---- src/user_interface.hh | 4 +- 22 files changed, 60 insertions(+), 225 deletions(-) delete mode 100644 src/id_map.hh diff --git a/src/alias_registry.cc b/src/alias_registry.cc index 11d83ff0..9804aefc 100644 --- a/src/alias_registry.cc +++ b/src/alias_registry.cc @@ -12,7 +12,7 @@ void AliasRegistry::add_alias(String alias, String command) kak_assert(CommandManager::instance().command_defined(command)); auto it = m_aliases.find(alias); if (it == m_aliases.end()) - m_aliases.append({std::move(alias), std::move(command) }); + m_aliases.insert({std::move(alias), std::move(command) }); else it->value = std::move(command); } diff --git a/src/alias_registry.hh b/src/alias_registry.hh index f2022aeb..e9437ab0 100644 --- a/src/alias_registry.hh +++ b/src/alias_registry.hh @@ -3,7 +3,7 @@ #include "safe_ptr.hh" #include "string.hh" -#include "id_map.hh" +#include "hash_map.hh" namespace Kakoune { @@ -16,7 +16,7 @@ public: void remove_alias(StringView alias); StringView operator[](StringView name) const; - using AliasMap = IdMap; + using AliasMap = HashMap; using iterator = AliasMap::const_iterator; Vector aliases_for(StringView command) const; diff --git a/src/commands.cc b/src/commands.cc index 943f843a..a84a0f41 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -663,7 +663,7 @@ Completions add_highlighter_completer( if (token_to_complete == 1 and params[0] == "-group") return complete_highlighter(context, params[1], pos_in_token, true); else if (token_to_complete == 0 or (token_to_complete == 2 and params[0] == "-group")) - return { 0_byte, arg.length(), complete(arg, pos_in_token, HighlighterRegistry::instance() | transform(HighlighterRegistry::get_id)) }; + return { 0_byte, arg.length(), complete(arg, pos_in_token, HighlighterRegistry::instance() | transform(std::mem_fn(&HighlighterRegistry::Item::key))) }; return Completions{}; } diff --git a/src/env_vars.cc b/src/env_vars.cc index 0e5c53e8..8b293ff2 100644 --- a/src/env_vars.cc +++ b/src/env_vars.cc @@ -16,7 +16,7 @@ EnvVarMap get_env_vars() const char* value = name; while (*value != 0 and *value != '=') ++value; - env_vars.append({{name, value}, (*value == '=') ? value+1 : String{}}); + env_vars.insert({{name, value}, (*value == '=') ? value+1 : String{}}); } return env_vars; } diff --git a/src/env_vars.hh b/src/env_vars.hh index 7445c216..ed496d6c 100644 --- a/src/env_vars.hh +++ b/src/env_vars.hh @@ -1,13 +1,13 @@ #ifndef env_vars_hh_INCLUDED #define env_vars_hh_INCLUDED -#include "id_map.hh" +#include "hash_map.hh" namespace Kakoune { class String; -using EnvVarMap = IdMap; +using EnvVarMap = HashMap; EnvVarMap get_env_vars(); diff --git a/src/hash_map.cc b/src/hash_map.cc index 131dc615..4db90f8c 100644 --- a/src/hash_map.cc +++ b/src/hash_map.cc @@ -50,8 +50,8 @@ UnitTest test_hash_map{[] { { HashMap map; map.insert({"test", 10}); - kak_assert(map["test"_sv] == 10); - map.remove("test"_sv); + kak_assert(map[StringView{"test"}] == 10); + map.remove(StringView{"test"}); } // make sure we get what we expect from the hash map diff --git a/src/hash_map.hh b/src/hash_map.hh index bbe9f1f9..df8fd306 100644 --- a/src/hash_map.hh +++ b/src/hash_map.hh @@ -160,11 +160,6 @@ struct HashMap HashCompatible::type>::value >::type; - // For IdMap inteface compatibility, to remove - using Element = Item; - Value& append(Item item) { return insert(std::move(item)); } - static const String& get_id(const Element& e) { return e.key; } - template> int find_index(const KeyType& key, size_t hash) const { diff --git a/src/highlighter.hh b/src/highlighter.hh index 23f273a1..f8b020a4 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -5,7 +5,7 @@ #include "completion.hh" #include "display_buffer.hh" #include "exception.hh" -#include "id_map.hh" +#include "hash_map.hh" #include "array_view.hh" #include "string.hh" #include "utils.hh" @@ -71,7 +71,7 @@ struct HighlighterFactoryAndDocstring String docstring; }; -struct HighlighterRegistry : IdMap, +struct HighlighterRegistry : HashMap, Singleton {}; diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc index 2d0a7a74..ebb28cf6 100644 --- a/src/highlighter_group.cc +++ b/src/highlighter_group.cc @@ -19,7 +19,7 @@ void HighlighterGroup::add_child(HighlighterAndId&& hl) if (m_highlighters.contains(hl.first)) throw runtime_error(format("duplicate id: '{}'", hl.first)); - m_highlighters.append({ std::move(hl.first), std::move(hl.second) }); + m_highlighters.insert({std::move(hl.first), std::move(hl.second)}); } void HighlighterGroup::remove_child(StringView id) @@ -52,9 +52,9 @@ Completions HighlighterGroup::complete_child(StringView path, ByteCount cursor_p auto candidates = complete( path, cursor_pos, - m_highlighters | filter([=](const HighlighterMap::Element& hl) + m_highlighters | filter([=](const HighlighterMap::Item& hl) { return not group or hl.value->has_children(); }) - | transform(HighlighterMap::get_id)); + | transform(std::mem_fn(&HighlighterMap::Item::key))); return { 0, 0, std::move(candidates) }; } diff --git a/src/highlighter_group.hh b/src/highlighter_group.hh index 7cd525a6..690e9514 100644 --- a/src/highlighter_group.hh +++ b/src/highlighter_group.hh @@ -2,7 +2,7 @@ #define highlighter_group_hh_INCLUDED #include "exception.hh" -#include "id_map.hh" +#include "hash_map.hh" #include "highlighter.hh" #include "utils.hh" @@ -28,7 +28,7 @@ public: Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const override; private: - using HighlighterMap = IdMap, MemoryDomain::Highlight>; + using HighlighterMap = HashMap, MemoryDomain::Highlight>; HighlighterMap m_highlighters; }; diff --git a/src/highlighters.cc b/src/highlighters.cc index fe16f6c3..f3cc1baa 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1291,12 +1291,12 @@ public: for (auto& region : m_regions) { - m_groups.append({region.m_name, HighlighterGroup{}}); + m_groups.insert({region.m_name, HighlighterGroup{}}); if (region.m_begin.empty() or region.m_end.empty()) throw runtime_error("invalid regex for region highlighter"); } if (not m_default_group.empty()) - m_groups.append({m_default_group, HighlighterGroup{}}); + m_groups.insert({m_default_group, HighlighterGroup{}}); } void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange range) override @@ -1371,7 +1371,7 @@ public: return offset_pos(hl.complete_child(path.substr(offset), cursor_pos - offset, group), offset); } - auto container = m_groups | transform(decltype(m_groups)::get_id); + auto container = m_groups | transform(std::mem_fn(&decltype(m_groups)::Item::key)); return { 0, 0, complete(path, cursor_pos, container) }; } @@ -1411,7 +1411,7 @@ public: private: const RegionDescList m_regions; const String m_default_group; - IdMap m_groups; + HashMap m_groups; struct Region { @@ -1521,66 +1521,66 @@ void register_highlighters() { HighlighterRegistry& registry = HighlighterRegistry::instance(); - registry.append({ + registry.insert({ "number_lines", { number_lines_factory, "Display line numbers \n" "Parameters: -relative, -hlcursor, -separator \n" } }); - registry.append({ + registry.insert({ "show_matching", { create_matching_char_highlighter, "Apply the MatchingChar face to the char matching the one under the cursor" } }); - registry.append({ + registry.insert({ "show_whitespaces", { show_whitespaces_factory, "Display whitespaces using symbols \n" "Parameters: -tab -tabpad -lf -spc -nbsp \n" } }); - registry.append({ + registry.insert({ "fill", { create_fill_highlighter, "Fill the whole highlighted range with the given face" } }); - registry.append({ + registry.insert({ "regex", { RegexHighlighter::create, "Parameters: : :...\n" "Highlights the matches for captures from the regex with the given faces" } }); - registry.append({ + registry.insert({ "dynregex", { create_dynamic_regex_highlighter, "Parameters: : :...\n" "Evaluate expression at every redraw to gather a regex" } }); - registry.append({ + registry.insert({ "group", { create_highlighter_group, "Parameters: \n" "Creates a named group that can contain other highlighters" } }); - registry.append({ + registry.insert({ "flag_lines", { create_flag_lines_highlighter, "Parameters: