From 73c0fa175ed7016ffe2980b29d090de770718efa Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 16 Sep 2015 19:04:19 +0100 Subject: [PATCH] Use IdMap instead of UnorderedMap for ui options and env vars --- src/env_vars.cc | 2 +- src/env_vars.hh | 4 ++-- src/id_map.hh | 22 ++++++++++++++++++++-- src/option_types.hh | 19 ++++++++++--------- src/remote.cc | 20 +++++++++++--------- src/shell_manager.cc | 2 +- src/user_interface.hh | 4 ++-- 7 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/env_vars.cc b/src/env_vars.cc index 1d52df93..0e5c53e8 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[{name, value}] = (*value == '=') ? value+1 : String{}; + env_vars.append({{name, value}, (*value == '=') ? value+1 : String{}}); } return env_vars; } diff --git a/src/env_vars.hh b/src/env_vars.hh index 43bbe2de..7445c216 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 "unordered_map.hh" +#include "id_map.hh" namespace Kakoune { class String; -using EnvVarMap = UnorderedMap; +using EnvVarMap = IdMap; EnvVarMap get_env_vars(); diff --git a/src/id_map.hh b/src/id_map.hh index 72d1c2f0..9b6f553b 100644 --- a/src/id_map.hh +++ b/src/id_map.hh @@ -69,11 +69,29 @@ public: return (m_content.end()-1)->second; } - const Value& operator[](StringView id) const + template + bool operator==(const IdMap& other) const { - return (*const_cast(this))[id]; + if (size() != other.size()) + return false; + for (size_t i = 0, s = size(); i < s; ++i) + { + if (m_content[i] != other.m_content[i]) + return false; + } + return true; } + template + bool operator!=(const IdMap& other) const + { + return not (*this == other); + } + + void reserve(size_t size) { m_content.reserve(size); } + size_t size() const { return m_content.size(); } + void clear() { m_content.clear(); } + static const String& get_id(const value_type& v) { return v.first; } bool empty() const { return m_content.empty(); } diff --git a/src/option_types.hh b/src/option_types.hh index a4d29227..d04544dd 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -6,7 +6,7 @@ #include "units.hh" #include "coord.hh" #include "array_view.hh" -#include "unordered_map.hh" +#include "id_map.hh" #include #include @@ -67,8 +67,8 @@ bool option_add(Vector& opt, const Vector& vec) return not vec.empty(); } -template -String option_to_string(const UnorderedMap& opt) +template +String option_to_string(const IdMap& opt) { String res; for (auto it = begin(opt); it != end(opt); ++it) @@ -82,8 +82,8 @@ String option_to_string(const UnorderedMap& opt) return res; } -template -void option_from_string(StringView str, UnorderedMap& opt) +template +void option_from_string(StringView str, IdMap& opt) { opt.clear(); for (auto& elem : split(str, list_separator, '\\')) @@ -91,10 +91,11 @@ void option_from_string(StringView str, UnorderedMap& opt) Vector pair_str = split(elem, '=', '\\'); if (pair_str.size() != 2) throw runtime_error("map option expects key=value"); - std::pair pair; - option_from_string(pair_str[0], pair.first); - option_from_string(pair_str[1], pair.second); - opt.insert(std::move(pair)); + String key; + Value value; + option_from_string(pair_str[0], key); + option_from_string(pair_str[1], value); + opt.append({ std::move(key), std::move(value) }); } } diff --git a/src/remote.cc b/src/remote.cc index 1c8795d3..d22ee6f8 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -7,6 +7,7 @@ #include "display_buffer.hh" #include "event_manager.hh" #include "file.hh" +#include "id_map.hh" #include #include @@ -84,8 +85,8 @@ public: write(ConstArrayView(vec)); } - template - void write(const UnorderedMap& map) + template + void write(const IdMap& map) { write(map.size()); for (auto& val : map) @@ -215,16 +216,17 @@ DisplayBuffer read(int socket) return db; } -template -UnorderedMap read_map(int socket) +template +IdMap read_idmap(int socket) { uint32_t size = read(socket); - UnorderedMap res; + IdMap res; + res.reserve(size); while (size--) { - auto key = read(socket); + auto key = read(socket); auto val = read(socket); - res.insert({std::move(key), std::move(val)}); + res.append({std::move(key), std::move(val)}); } return res; } @@ -512,7 +514,7 @@ void RemoteClient::process_next_message() m_ui->refresh(); break; case RemoteUIMsg::SetOptions: - m_ui->set_ui_options(read_map(socket)); + m_ui->set_ui_options(read_idmap(socket)); break; } } @@ -579,7 +581,7 @@ private: } if (c == 0) // end of initial command stream, go to interactive ui { - EnvVarMap env_vars = read_map(socket); + EnvVarMap env_vars = read_idmap(socket); std::unique_ptr ui{new RemoteUI{socket}}; ClientManager::instance().create_client(std::move(ui), std::move(env_vars), diff --git a/src/shell_manager.cc b/src/shell_manager.cc index 19d55bb3..1e29ba2d 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -109,7 +109,7 @@ std::pair ShellManager::eval( StringView name{match[1].first, match[1].second}; kak_assert(name.length() > 0); - auto local_var = env_vars.find(name.str()); + auto local_var = env_vars.find(name); if (local_var != env_vars.end()) setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1); else try diff --git a/src/user_interface.hh b/src/user_interface.hh index 5f87d58b..daae4f9d 100644 --- a/src/user_interface.hh +++ b/src/user_interface.hh @@ -3,7 +3,7 @@ #include "array_view.hh" #include "safe_ptr.hh" -#include "unordered_map.hh" +#include "id_map.hh" #include @@ -67,7 +67,7 @@ public: virtual void set_input_callback(InputCallback callback) = 0; - using Options = UnorderedMap; + using Options = IdMap; virtual void set_ui_options(const Options& options) = 0; };