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;
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;
}

View File

@ -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<String, String, MemoryDomain::EnvVars>;
using EnvVarMap = IdMap<String, MemoryDomain::EnvVars>;
EnvVarMap get_env_vars();

View File

@ -69,10 +69,28 @@ public:
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; }

View File

@ -6,7 +6,7 @@
#include "units.hh"
#include "coord.hh"
#include "array_view.hh"
#include "unordered_map.hh"
#include "id_map.hh"
#include <tuple>
#include <vector>
@ -67,8 +67,8 @@ bool option_add(Vector<T, domain>& opt, const Vector<T, domain>& vec)
return not vec.empty();
}
template<typename Key, typename Value, MemoryDomain domain>
String option_to_string(const UnorderedMap<Key, Value, domain>& opt)
template<typename Value, MemoryDomain domain>
String option_to_string(const IdMap<Value, domain>& opt)
{
String res;
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;
}
template<typename Key, typename Value, MemoryDomain domain>
void option_from_string(StringView str, UnorderedMap<Key, Value, domain>& opt)
template<typename Value, MemoryDomain domain>
void option_from_string(StringView str, IdMap<Value, domain>& opt)
{
opt.clear();
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, '=', '\\');
if (pair_str.size() != 2)
throw runtime_error("map option expects key=value");
std::pair<Key, Value> 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) });
}
}

View File

@ -7,6 +7,7 @@
#include "display_buffer.hh"
#include "event_manager.hh"
#include "file.hh"
#include "id_map.hh"
#include <sys/types.h>
#include <sys/socket.h>
@ -84,8 +85,8 @@ public:
write(ConstArrayView<T>(vec));
}
template<typename Key, typename Val, MemoryDomain domain>
void write(const UnorderedMap<Key, Val, domain>& map)
template<typename Val, MemoryDomain domain>
void write(const IdMap<Val, domain>& map)
{
write<uint32_t>(map.size());
for (auto& val : map)
@ -215,16 +216,17 @@ DisplayBuffer read<DisplayBuffer>(int socket)
return db;
}
template<typename Key, typename Val, MemoryDomain domain>
UnorderedMap<Key, Val, domain> read_map(int socket)
template<typename Val, MemoryDomain domain>
IdMap<Val, domain> read_idmap(int socket)
{
uint32_t size = read<uint32_t>(socket);
UnorderedMap<Key, Val, domain> res;
IdMap<Val, domain> res;
res.reserve(size);
while (size--)
{
auto key = read<Key>(socket);
auto key = read<String>(socket);
auto val = read<Val>(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<String, String, MemoryDomain::Options>(socket));
m_ui->set_ui_options(read_idmap<String, MemoryDomain::Options>(socket));
break;
}
}
@ -579,7 +581,7 @@ private:
}
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}};
ClientManager::instance().create_client(std::move(ui),
std::move(env_vars),

View File

@ -109,7 +109,7 @@ std::pair<String, int> 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

View File

@ -3,7 +3,7 @@
#include "array_view.hh"
#include "safe_ptr.hh"
#include "unordered_map.hh"
#include "id_map.hh"
#include <functional>
@ -67,7 +67,7 @@ public:
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;
};