Replace uses of UnorderedMap with HashMap

This commit is contained in:
Maxime Coste 2017-03-07 00:30:54 +00:00
parent e9f93f1b2f
commit 6373338c50
24 changed files with 96 additions and 116 deletions

View File

@ -437,13 +437,13 @@ void CommandManager::execute_single_command(CommandParameters params,
try
{
ParametersParser parameter_parser(param_view,
command_it->second.param_desc);
command_it->second.command(parameter_parser, context, shell_context);
command_it->value.param_desc);
command_it->value.command(parameter_parser, context, shell_context);
}
catch (runtime_error& error)
{
throw runtime_error(format("{}:{}: '{}' {}", pos.line+1, pos.column+1,
command_it->first, error.what()));
command_it->key, error.what()));
}
}
@ -507,11 +507,11 @@ Optional<CommandInfo> CommandManager::command_info(const Context& context, Strin
return {};
CommandInfo res;
res.name = cmd->first;
if (not cmd->second.docstring.empty())
res.info += cmd->second.docstring + "\n";
res.name = cmd->key;
if (not cmd->value.docstring.empty())
res.info += cmd->value.docstring + "\n";
if (cmd->second.helper)
if (cmd->value.helper)
{
Vector<String> params;
for (auto it = tokens.begin() + cmd_idx + 1;
@ -523,7 +523,7 @@ Optional<CommandInfo> CommandManager::command_info(const Context& context, Strin
it->type() == Token::Type::RawEval)
params.push_back(it->content());
}
String helpstr = cmd->second.helper(context, params);
String helpstr = cmd->value.helper(context, params);
if (not helpstr.empty())
{
if (helpstr.back() != '\n')
@ -533,13 +533,13 @@ Optional<CommandInfo> CommandManager::command_info(const Context& context, Strin
}
String aliases;
for (auto& alias : context.aliases().aliases_for(cmd->first))
for (auto& alias : context.aliases().aliases_for(cmd->key))
aliases += " " + alias;
if (not aliases.empty())
res.info += "Aliases:" + aliases + "\n";
auto& switches = cmd->second.param_desc.switches;
auto& switches = cmd->value.param_desc.switches;
if (not switches.empty())
{
res.info += "Switches:\n";
@ -553,8 +553,8 @@ Completions CommandManager::complete_command_name(const Context& context,
StringView query, bool with_aliases) const
{
auto commands = m_commands
| filter([](const CommandMap::value_type& cmd) { return not (cmd.second.flags & CommandFlags::Hidden); })
| transform(std::mem_fn(&CommandMap::value_type::first));
| filter([](const CommandMap::Item& cmd) { return not (cmd.value.flags & CommandFlags::Hidden); })
| transform(std::mem_fn(&CommandMap::Item::key));
if (not with_aliases)
return {0, query.length(), Kakoune::complete(query, query.length(), commands)};
@ -636,7 +636,7 @@ Completions CommandManager::complete(const Context& context,
auto command_it = find_command(context, command_name);
if (command_it == m_commands.end() or
not command_it->second.completer)
not command_it->value.completer)
return Completions();
Vector<String> params;
@ -644,7 +644,7 @@ Completions CommandManager::complete(const Context& context,
params.push_back(it->content());
if (tok_idx == tokens.size())
params.emplace_back("");
Completions completions = offset_pos(command_it->second.completer(
Completions completions = offset_pos(command_it->value.completer(
context, flags, params, tok_idx - cmd_idx - 1,
cursor_pos_in_token), start);
@ -683,8 +683,8 @@ Completions CommandManager::complete(const Context& context,
}
auto command_it = find_command(context, command_name);
if (command_it != m_commands.end() and command_it->second.completer)
return command_it->second.completer(
if (command_it != m_commands.end() and command_it->value.completer)
return command_it->value.completer(
context, flags, params.subrange(1),
token_to_complete-1, pos_in_token);
}

View File

@ -10,7 +10,7 @@
#include "string.hh"
#include "optional.hh"
#include "utils.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
#include <functional>
#include <initializer_list>
@ -123,7 +123,7 @@ private:
CommandHelper helper;
CommandCompleter completer;
};
using CommandMap = UnorderedMap<String, CommandDescriptor, MemoryDomain::Commands>;
using CommandMap = HashMap<String, CommandDescriptor, MemoryDomain::Commands>;
CommandMap m_commands;
String m_last_complete_command;
int m_command_depth = 0;

View File

@ -1780,7 +1780,7 @@ const CommandDesc prompt_cmd = {
(event == PromptEvent::Change and on_change.empty()))
return;
auto& text = sc.env_vars["text"] = str.str();
auto& text = sc.env_vars["text"_sv] = str.str();
auto clear_password = on_scope_end([&] {
if (flags & PromptFlags::Password)
memset(text.data(), 0, (int)text.length());
@ -1872,7 +1872,7 @@ const CommandDesc on_key_cmd = {
CapturedShellContext sc{shell_context};
context.input_handler().on_next_key(
KeymapMode::None, [=](Key key, Context& context) mutable {
sc.env_vars["key"] = key_to_str(key);
sc.env_vars["key"_sv] = key_to_str(key);
ScopedSetBool disable_history{context.history_disabled()};
CommandManager::instance().execute(command, context, sc);

View File

@ -48,9 +48,9 @@ Face FaceRegistry::operator[](const String& facedesc)
auto it = m_aliases.find(facedesc);
while (it != m_aliases.end())
{
if (it->second.alias.empty())
return it->second.face;
it = m_aliases.find(it->second.alias);
if (it->value.alias.empty())
return it->value.face;
it = m_aliases.find(it->value.alias);
}
return parse_face(facedesc);
}
@ -75,11 +75,11 @@ void FaceRegistry::register_alias(const String& name, const String& facedesc,
{
while (it != m_aliases.end())
{
if (it->second.alias.empty())
if (it->value.alias.empty())
break;
if (it->second.alias == name)
if (it->value.alias == name)
throw runtime_error("face cycle detected");
it = m_aliases.find(it->second.alias);
it = m_aliases.find(it->value.alias);
}
alias.alias = facedesc;
@ -95,7 +95,7 @@ CandidateList FaceRegistry::complete_alias_name(StringView prefix,
ByteCount cursor_pos) const
{
return complete(prefix, cursor_pos,
m_aliases | transform(std::mem_fn(&AliasMap::value_type::first)));
m_aliases | transform(std::mem_fn(&AliasMap::Item::key)));
}
FaceRegistry::FaceRegistry()

View File

@ -4,7 +4,7 @@
#include "face.hh"
#include "utils.hh"
#include "completion.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
namespace Kakoune
{
@ -29,7 +29,7 @@ private:
FaceOrAlias(Face face = Face{}) : face(face) {}
};
using AliasMap = UnorderedMap<String, FaceOrAlias, MemoryDomain::Faces>;
using AliasMap = HashMap<String, FaceOrAlias, MemoryDomain::Faces>;
AliasMap m_aliases;
};

View File

@ -477,7 +477,7 @@ CandidateList complete_command(StringView prefix, ByteCount cursor_pos)
TimeSpec mtim = {};
Vector<String> commands;
};
static UnorderedMap<String, CommandCache, MemoryDomain::Commands> command_cache;
static HashMap<String, CommandCache, MemoryDomain::Commands> command_cache;
Vector<RankedMatch> matches;
for (auto dir : StringView{getenv("PATH")} | split<StringView>(':'))

View File

@ -6,6 +6,7 @@
#include "unit_tests.hh"
#include <random>
#include <unordered_map>
namespace Kakoune
{
@ -148,7 +149,7 @@ void profile_hash_maps()
{
for (auto i : { 1000, 10000, 100000, 1000000, 10000000 })
{
do_profile<UnorderedMap<size_t, size_t>>(i, "UnorderedMap");
do_profile<std::unordered_map<size_t, size_t>>(i, "UnorderedMap");
do_profile<HashMap<size_t, size_t>>(i, " HashMap ");
}
}

View File

@ -128,15 +128,18 @@ private:
Vector<Entry, domain> m_entries;
};
template<typename Key, typename Value, MemoryDomain domain = MemoryDomain::Undefined>
struct HashMap
{
struct Item
template<typename Key, typename Value>
struct HashItem
{
Key key;
Value value;
};
template<typename Key, typename Value, MemoryDomain domain = MemoryDomain::Undefined>
struct HashMap
{
using Item = HashItem<Key, Value>;
HashMap() = default;
HashMap(std::initializer_list<Item> val) : m_items{val}

View File

@ -1425,7 +1425,7 @@ private:
{
size_t timestamp = 0;
Vector<RegionMatches, MemoryDomain::Highlight> matches;
UnorderedMap<BufferRange, RegionList, MemoryDomain::Highlight> regions;
HashMap<BufferRange, RegionList, MemoryDomain::Highlight> regions;
};
BufferSideCache<Cache> m_cache;
@ -1471,7 +1471,7 @@ private:
auto it = cache.regions.find(range);
if (it != cache.regions.end())
return it->second;
return it->value;
RegionList& regions = cache.regions[range];

View File

@ -11,7 +11,7 @@
#include "normal.hh"
#include "regex.hh"
#include "register_manager.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
#include "user_interface.hh"
#include "utf8.hh"
#include "window.hh"
@ -944,7 +944,7 @@ private:
PromptFlags m_flags;
using History = Vector<String, MemoryDomain::History>;
static UnorderedMap<String, History, MemoryDomain::History> ms_history;
static HashMap<String, History, MemoryDomain::History> ms_history;
History::iterator m_history_it;
void history_push(History& history, StringView entry)
@ -959,7 +959,7 @@ private:
history.push_back(entry.str());
}
};
UnorderedMap<String, Prompt::History, MemoryDomain::History> Prompt::ms_history;
HashMap<String, Prompt::History, MemoryDomain::History> Prompt::ms_history;
class NextKey : public InputMode
{

View File

@ -91,7 +91,7 @@ InsertCompletion complete_word(const SelectionList& sels, const OptionManager& o
BufferCoord word_begin;
StringView prefix;
UnorderedMap<StringView, int> sel_word_counts;
HashMap<StringView, int> sel_word_counts;
for (int i = 0; i < sels.size(); ++i)
{
Utf8It end{buffer.iterator_at(sels[i].cursor()), buffer};
@ -136,8 +136,8 @@ InsertCompletion complete_word(const SelectionList& sels, const OptionManager& o
// Remove words that are being edited
for (auto& word_count : sel_word_counts)
{
if (get_word_db(buffer).get_word_occurences(word_count.first) <= word_count.second)
unordered_erase(matches, word_count.first);
if (get_word_db(buffer).get_word_occurences(word_count.key) <= word_count.value)
unordered_erase(matches, word_count.key);
}
if (other_buffers)

View File

@ -348,16 +348,16 @@ void JsonUI::eval_json(const Value& json)
throw runtime_error("json request is not an object");
const JsonObject& object = json.as<JsonObject>();
auto json_it = object.find("jsonrpc");
auto json_it = object.find("jsonrpc"_sv);
if (json_it == object.end() or json_it->value.as<String>() != "2.0")
throw runtime_error("invalid json rpc request");
auto method_it = object.find("method");
auto method_it = object.find("method"_sv);
if (method_it == object.end())
throw runtime_error("invalid json rpc request (method missing)");
StringView method = method_it->value.as<String>();
auto params_it = object.find("params");
auto params_it = object.find("params"_sv);
if (params_it == object.end())
throw runtime_error("invalid json rpc request (params missing)");
const JsonArray& params = params_it->value.as<JsonArray>();

View File

@ -11,27 +11,27 @@ namespace Kakoune
void KeymapManager::map_key(Key key, KeymapMode mode,
KeyList mapping, String docstring)
{
m_mapping[{key, mode}] = {std::move(mapping), std::move(docstring)};
m_mapping[KeyAndMode{key, mode}] = {std::move(mapping), std::move(docstring)};
}
void KeymapManager::unmap_key(Key key, KeymapMode mode)
{
m_mapping.erase({key, mode});
m_mapping.unordered_remove(KeyAndMode{key, mode});
}
bool KeymapManager::is_mapped(Key key, KeymapMode mode) const
{
return m_mapping.find({key, mode}) != m_mapping.end() or
return m_mapping.find(KeyAndMode{key, mode}) != m_mapping.end() or
(m_parent and m_parent->is_mapped(key, mode));
}
const KeymapManager::KeyMapInfo&
KeymapManager::get_mapping(Key key, KeymapMode mode) const
{
auto it = m_mapping.find({key, mode});
auto it = m_mapping.find(KeyAndMode{key, mode});
if (it != m_mapping.end())
return it->second;
return it->value;
kak_assert(m_parent);
return m_parent->get_mapping(key, mode);
}
@ -43,8 +43,8 @@ KeymapManager::KeyList KeymapManager::get_mapped_keys(KeymapMode mode) const
res = m_parent->get_mapped_keys(mode);
for (auto& map : m_mapping)
{
if (map.first.second == mode)
res.emplace_back(map.first.first);
if (map.key.second == mode)
res.emplace_back(map.key.first);
}
std::sort(res.begin(), res.end());
res.erase(std::unique(res.begin(), res.end()), res.end());

View File

@ -5,7 +5,7 @@
#include "keys.hh"
#include "hash.hh"
#include "string.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
#include "vector.hh"
namespace Kakoune
@ -51,7 +51,7 @@ private:
KeymapManager* m_parent;
using KeyAndMode = std::pair<Key, KeymapMode>;
UnorderedMap<KeyAndMode, KeyMapInfo, MemoryDomain::Mapping> m_mapping;
HashMap<KeyAndMode, KeyMapInfo, MemoryDomain::Mapping> m_mapping;
};
}

View File

@ -132,7 +132,7 @@ int NCursesUI::get_color(Color color)
{
auto it = m_colors.find(color);
if (it != m_colors.end())
return it->second;
return it->value;
else if (m_change_colors and can_change_color() and COLORS > 16)
{
kak_assert(color.color == Color::RGB);
@ -171,7 +171,7 @@ int NCursesUI::get_color_pair(const Face& face)
ColorPair colors{face.fg, face.bg};
auto it = m_colorpairs.find(colors);
if (it != m_colorpairs.end())
return it->second;
return it->value;
else
{
init_pair(m_next_pair, get_color(face.fg), get_color(face.bg));
@ -214,7 +214,7 @@ void on_term_resize(int)
EventManager::instance().force_signal(0);
}
static const std::initializer_list<std::pair<const Kakoune::Color, int>>
static const std::initializer_list<HashMap<Kakoune::Color, int>::Item>
default_colors = {
{ Color::Default, -1 },
{ Color::Black, COLOR_BLACK },
@ -1005,7 +1005,7 @@ void NCursesUI::enable_mouse(bool enabled)
void NCursesUI::set_ui_options(const Options& options)
{
{
auto it = options.find("ncurses_assistant");
auto it = options.find("ncurses_assistant"_sv);
if (it == options.end() or it->value == "clippy")
m_assistant = assistant_clippy;
else if (it->value == "cat")
@ -1015,19 +1015,19 @@ void NCursesUI::set_ui_options(const Options& options)
}
{
auto it = options.find("ncurses_status_on_top");
auto it = options.find("ncurses_status_on_top"_sv);
m_status_on_top = it != options.end() and
(it->value == "yes" or it->value == "true");
}
{
auto it = options.find("ncurses_set_title");
auto it = options.find("ncurses_set_title"_sv);
m_set_title = it == options.end() or
(it->value == "yes" or it->value == "true");
}
{
auto it = options.find("ncurses_change_colors");
auto it = options.find("ncurses_change_colors"_sv);
auto value = it == options.end() or
(it->value == "yes" or it->value == "true");
@ -1045,16 +1045,16 @@ void NCursesUI::set_ui_options(const Options& options)
}
{
auto enable_mouse_it = options.find("ncurses_enable_mouse");
auto enable_mouse_it = options.find("ncurses_enable_mouse"_sv);
enable_mouse(enable_mouse_it == options.end() or
enable_mouse_it->value == "yes" or
enable_mouse_it->value == "true");
auto wheel_up_it = options.find("ncurses_wheel_up_button");
auto wheel_up_it = options.find("ncurses_wheel_up_button"_sv);
m_wheel_up_button = wheel_up_it != options.end() ?
str_to_int_ifp(wheel_up_it->value).value_or(4) : 4;
auto wheel_down_it = options.find("ncurses_wheel_down_button");
auto wheel_down_it = options.find("ncurses_wheel_down_button"_sv);
m_wheel_down_button = wheel_down_it != options.end() ?
str_to_int_ifp(wheel_down_it->value).value_or(5) : 5;
}

View File

@ -6,7 +6,7 @@
#include "face.hh"
#include "user_interface.hh"
#include "array_view.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
namespace Kakoune
{
@ -77,8 +77,8 @@ private:
DisplayCoord m_dimensions;
using ColorPair = std::pair<Color, Color>;
UnorderedMap<Color, int, MemoryDomain::Faces> m_colors;
UnorderedMap<ColorPair, int, MemoryDomain::Faces> m_colorpairs;
HashMap<Color, int, MemoryDomain::Faces> m_colors;
HashMap<ColorPair, int, MemoryDomain::Faces> m_colorpairs;
int m_next_color = 16;
int m_next_pair = 1;
int m_active_pair = -1;

View File

@ -35,10 +35,10 @@ Register& RegisterManager::operator[](Codepoint c) const
if (it == m_registers.end())
throw runtime_error(format("no such register: '{}'", c));
return *(it->second);
return *(it->value);
}
void RegisterManager::add_register(char c, std::unique_ptr<Register> reg)
void RegisterManager::add_register(Codepoint c, std::unique_ptr<Register> reg)
{
auto& reg_ptr = m_registers[c];
kak_assert(not reg_ptr);

View File

@ -4,7 +4,7 @@
#include "array_view.hh"
#include "exception.hh"
#include "utils.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
#include "string.hh"
#include "vector.hh"
@ -100,10 +100,10 @@ class RegisterManager : public Singleton<RegisterManager>
public:
Register& operator[](StringView reg) const;
Register& operator[](Codepoint c) const;
void add_register(char c, std::unique_ptr<Register> reg);
void add_register(Codepoint c, std::unique_ptr<Register> reg);
protected:
UnorderedMap<char, std::unique_ptr<Register>, MemoryDomain::Registers> m_registers;
HashMap<Codepoint, std::unique_ptr<Register>, MemoryDomain::Registers> m_registers;
};
}

View File

@ -25,19 +25,18 @@ StringDataPtr StringData::Registry::intern(StringView str)
{
auto it = m_strings.find(str);
if (it != m_strings.end())
return StringDataPtr{it->second};
return StringDataPtr{it->value};
auto data = StringData::create(str);
data->refcount |= interned_flag;
m_strings.emplace(data->strview(), data.get());
m_strings.insert({data->strview(), data.get()});
return data;
}
void StringData::Registry::remove(StringView str)
{
auto it = m_strings.find(str);
kak_assert(it != m_strings.end());
m_strings.erase(it);
kak_assert(m_strings.contains(str));
m_strings.unordered_remove(str);
}
void StringData::Registry::debug_stats() const
@ -48,8 +47,8 @@ void StringData::Registry::debug_stats() const
size_t count = m_strings.size();
for (auto& st : m_strings)
{
total_refcount += st.second->refcount - 1;
total_size += (int)st.second->length;
total_refcount += st.value->refcount - 1;
total_size += (int)st.value->length;
}
write_to_debug_buffer(format(" data size: {}, mean: {}", total_size, (float)total_size/count));
write_to_debug_buffer(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count));

View File

@ -4,7 +4,7 @@
#include "string.hh"
#include "ref_ptr.hh"
#include "utils.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
#include <numeric>
@ -53,7 +53,7 @@ public:
void remove(StringView str);
private:
UnorderedMap<StringView, StringData*, MemoryDomain::SharedString> m_strings;
HashMap<StringView, StringData*, MemoryDomain::SharedString> m_strings;
};
static Ptr create(ArrayView<const StringView> strs);

View File

@ -1,23 +0,0 @@
#ifndef unordered_map_hh_INCLUDED
#define unordered_map_hh_INCLUDED
#include "hash.hh"
#include "memory.hh"
#include <unordered_map>
#include <unordered_set>
namespace Kakoune
{
template<typename Key, typename Value, MemoryDomain domain = TypeDomain<Key>::domain()>
using UnorderedMap = std::unordered_map<Key, Value, Hash<Key>, std::equal_to<Key>,
Allocator<std::pair<const Key, Value>, domain>>;
template<typename Key, MemoryDomain domain = TypeDomain<Key>::domain()>
using UnorderedSet = std::unordered_set<Key, Hash<Key>, std::equal_to<Key>,
Allocator<Key, domain>>;
}
#endif // unordered_map_hh_INCLUDED

View File

@ -1,7 +1,7 @@
#ifndef value_hh_INCLUDED
#define value_hh_INCLUDED
#include "unordered_map.hh"
#include "hash_map.hh"
#include "units.hh"
#include <type_traits>
@ -76,7 +76,7 @@ inline ValueId get_free_value_id()
return (ValueId)(next++);
}
using ValueMap = UnorderedMap<ValueId, Value, MemoryDomain::Values>;
using ValueMap = HashMap<ValueId, Value, MemoryDomain::Values>;
}

View File

@ -45,7 +45,7 @@ void WordDB::add_words(StringView line)
{
auto it = m_words.find(w);
if (it != m_words.end())
++it->second.refcount;
++it->value.refcount;
else
{
auto word = intern(w);
@ -60,9 +60,9 @@ void WordDB::remove_words(StringView line)
for (auto& w : get_words(line, get_extra_word_chars(*m_buffer)))
{
auto it = m_words.find(w);
kak_assert(it != m_words.end() and it->second.refcount > 0);
if (--it->second.refcount == 0)
m_words.erase(it);
kak_assert(it != m_words.end() and it->value.refcount > 0);
if (--it->value.refcount == 0)
m_words.unordered_remove(it->key);
}
}
@ -159,7 +159,7 @@ int WordDB::get_word_occurences(StringView word) const
{
auto it = m_words.find(word);
if (it != m_words.end())
return it->second.refcount;
return it->value.refcount;
return 0;
}
@ -170,7 +170,7 @@ RankedMatchList WordDB::find_matching(StringView query)
RankedMatchList res;
for (auto&& word : m_words)
{
if (RankedMatch match{word.first, word.second.letters, query, letters})
if (RankedMatch match{word.key, word.value.letters, query, letters})
res.push_back(match);
}

View File

@ -3,7 +3,7 @@
#include "buffer.hh"
#include "shared_string.hh"
#include "unordered_map.hh"
#include "hash_map.hh"
#include "vector.hh"
#include "ranked_match.hh"
@ -39,7 +39,7 @@ private:
UsedLetters letters;
int refcount;
};
using WordToInfo = UnorderedMap<StringView, WordInfo, MemoryDomain::WordDB>;
using WordToInfo = HashMap<StringView, WordInfo, MemoryDomain::WordDB>;
using Lines = Vector<StringDataPtr, MemoryDomain::WordDB>;
SafePtr<const Buffer> m_buffer;