Use IdMap instead of UnorderedMap for ui options and env vars

This commit is contained in:
Maxime Coste 2015-09-16 19:04:19 +01:00
parent 73bedda8b9
commit 73c0fa175e
7 changed files with 47 additions and 26 deletions

View File

@ -16,7 +16,7 @@ EnvVarMap get_env_vars()
const char* value = name; const char* value = name;
while (*value != 0 and *value != '=') while (*value != 0 and *value != '=')
++value; ++value;
env_vars[{name, value}] = (*value == '=') ? value+1 : String{}; env_vars.append({{name, value}, (*value == '=') ? value+1 : String{}});
} }
return env_vars; return env_vars;
} }

View File

@ -1,13 +1,13 @@
#ifndef env_vars_hh_INCLUDED #ifndef env_vars_hh_INCLUDED
#define env_vars_hh_INCLUDED #define env_vars_hh_INCLUDED
#include "unordered_map.hh" #include "id_map.hh"
namespace Kakoune namespace Kakoune
{ {
class String; class String;
using EnvVarMap = UnorderedMap<String, String, MemoryDomain::EnvVars>; using EnvVarMap = IdMap<String, MemoryDomain::EnvVars>;
EnvVarMap get_env_vars(); EnvVarMap get_env_vars();

View File

@ -69,10 +69,28 @@ public:
return (m_content.end()-1)->second; return (m_content.end()-1)->second;
} }
const Value& operator[](StringView id) const template<MemoryDomain dom>
bool operator==(const IdMap<Value, dom>& other) const
{ {
return (*const_cast<IdMap*>(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<MemoryDomain dom>
bool operator!=(const IdMap<Value, dom>& 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; } static const String& get_id(const value_type& v) { return v.first; }

View File

@ -6,7 +6,7 @@
#include "units.hh" #include "units.hh"
#include "coord.hh" #include "coord.hh"
#include "array_view.hh" #include "array_view.hh"
#include "unordered_map.hh" #include "id_map.hh"
#include <tuple> #include <tuple>
#include <vector> #include <vector>
@ -67,8 +67,8 @@ bool option_add(Vector<T, domain>& opt, const Vector<T, domain>& vec)
return not vec.empty(); return not vec.empty();
} }
template<typename Key, typename Value, MemoryDomain domain> template<typename Value, MemoryDomain domain>
String option_to_string(const UnorderedMap<Key, Value, domain>& opt) String option_to_string(const IdMap<Value, domain>& opt)
{ {
String res; String res;
for (auto it = begin(opt); it != end(opt); ++it) for (auto it = begin(opt); it != end(opt); ++it)
@ -82,8 +82,8 @@ String option_to_string(const UnorderedMap<Key, Value, domain>& opt)
return res; return res;
} }
template<typename Key, typename Value, MemoryDomain domain> template<typename Value, MemoryDomain domain>
void option_from_string(StringView str, UnorderedMap<Key, Value, domain>& opt) void option_from_string(StringView str, IdMap<Value, domain>& opt)
{ {
opt.clear(); opt.clear();
for (auto& elem : split(str, list_separator, '\\')) for (auto& elem : split(str, list_separator, '\\'))
@ -91,10 +91,11 @@ void option_from_string(StringView str, UnorderedMap<Key, Value, domain>& opt)
Vector<String> pair_str = split(elem, '=', '\\'); Vector<String> pair_str = split(elem, '=', '\\');
if (pair_str.size() != 2) if (pair_str.size() != 2)
throw runtime_error("map option expects key=value"); throw runtime_error("map option expects key=value");
std::pair<Key, Value> pair; String key;
option_from_string(pair_str[0], pair.first); Value value;
option_from_string(pair_str[1], pair.second); option_from_string(pair_str[0], key);
opt.insert(std::move(pair)); option_from_string(pair_str[1], value);
opt.append({ std::move(key), std::move(value) });
} }
} }

View File

@ -7,6 +7,7 @@
#include "display_buffer.hh" #include "display_buffer.hh"
#include "event_manager.hh" #include "event_manager.hh"
#include "file.hh" #include "file.hh"
#include "id_map.hh"
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -84,8 +85,8 @@ public:
write(ConstArrayView<T>(vec)); write(ConstArrayView<T>(vec));
} }
template<typename Key, typename Val, MemoryDomain domain> template<typename Val, MemoryDomain domain>
void write(const UnorderedMap<Key, Val, domain>& map) void write(const IdMap<Val, domain>& map)
{ {
write<uint32_t>(map.size()); write<uint32_t>(map.size());
for (auto& val : map) for (auto& val : map)
@ -215,16 +216,17 @@ DisplayBuffer read<DisplayBuffer>(int socket)
return db; return db;
} }
template<typename Key, typename Val, MemoryDomain domain> template<typename Val, MemoryDomain domain>
UnorderedMap<Key, Val, domain> read_map(int socket) IdMap<Val, domain> read_idmap(int socket)
{ {
uint32_t size = read<uint32_t>(socket); uint32_t size = read<uint32_t>(socket);
UnorderedMap<Key, Val, domain> res; IdMap<Val, domain> res;
res.reserve(size);
while (size--) while (size--)
{ {
auto key = read<Key>(socket); auto key = read<String>(socket);
auto val = read<Val>(socket); auto val = read<Val>(socket);
res.insert({std::move(key), std::move(val)}); res.append({std::move(key), std::move(val)});
} }
return res; return res;
} }
@ -512,7 +514,7 @@ void RemoteClient::process_next_message()
m_ui->refresh(); m_ui->refresh();
break; break;
case RemoteUIMsg::SetOptions: case RemoteUIMsg::SetOptions:
m_ui->set_ui_options(read_map<String, String, MemoryDomain::Options>(socket)); m_ui->set_ui_options(read_idmap<String, MemoryDomain::Options>(socket));
break; break;
} }
} }
@ -579,7 +581,7 @@ private:
} }
if (c == 0) // end of initial command stream, go to interactive ui if (c == 0) // end of initial command stream, go to interactive ui
{ {
EnvVarMap env_vars = read_map<String, String, MemoryDomain::EnvVars>(socket); EnvVarMap env_vars = read_idmap<String, MemoryDomain::EnvVars>(socket);
std::unique_ptr<UserInterface> ui{new RemoteUI{socket}}; std::unique_ptr<UserInterface> ui{new RemoteUI{socket}};
ClientManager::instance().create_client(std::move(ui), ClientManager::instance().create_client(std::move(ui),
std::move(env_vars), std::move(env_vars),

View File

@ -109,7 +109,7 @@ std::pair<String, int> ShellManager::eval(
StringView name{match[1].first, match[1].second}; StringView name{match[1].first, match[1].second};
kak_assert(name.length() > 0); 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()) if (local_var != env_vars.end())
setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1); setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1);
else try else try

View File

@ -3,7 +3,7 @@
#include "array_view.hh" #include "array_view.hh"
#include "safe_ptr.hh" #include "safe_ptr.hh"
#include "unordered_map.hh" #include "id_map.hh"
#include <functional> #include <functional>
@ -67,7 +67,7 @@ public:
virtual void set_input_callback(InputCallback callback) = 0; virtual void set_input_callback(InputCallback callback) = 0;
using Options = UnorderedMap<String, String, MemoryDomain::Options>; using Options = IdMap<String, MemoryDomain::Options>;
virtual void set_ui_options(const Options& options) = 0; virtual void set_ui_options(const Options& options) = 0;
}; };