remove idvaluemap, use unordered_map in place
This commit is contained in:
parent
c764fa7e25
commit
2b9b161d42
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
@ -40,5 +41,34 @@ inline Completions complete_nothing(const Context& context, CompletionFlags,
|
|||
return Completions(cursor_pos, cursor_pos);
|
||||
}
|
||||
|
||||
template<typename Condition, typename Value>
|
||||
CandidateList complete_key_if(const std::unordered_map<String, Value>& map,
|
||||
const String& prefix,
|
||||
ByteCount cursor_pos,
|
||||
Condition condition)
|
||||
{
|
||||
String real_prefix = prefix.substr(0, cursor_pos);
|
||||
CandidateList result;
|
||||
for (auto& value : map)
|
||||
{
|
||||
if (not condition(value))
|
||||
continue;
|
||||
|
||||
if (prefix_match(value.first, real_prefix))
|
||||
result.push_back(value.first);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename Value>
|
||||
CandidateList complete_key(const std::unordered_map<String, Value>& map,
|
||||
const String& prefix,
|
||||
ByteCount cursor_pos)
|
||||
{
|
||||
return complete_key_if(
|
||||
map, prefix, cursor_pos,
|
||||
[](const std::pair<String, Value>&) { return true; });
|
||||
}
|
||||
|
||||
}
|
||||
#endif // completion_hh_INCLUDED
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
#define function_group_hh_INCLUDED
|
||||
|
||||
#include "exception.hh"
|
||||
#include "idvaluemap.hh"
|
||||
#include "string.hh"
|
||||
#include "completion.hh"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
@ -23,14 +25,14 @@ public:
|
|||
|
||||
void append(FunctionAndId&& function)
|
||||
{
|
||||
if (m_functions.contains(function.first))
|
||||
if (m_functions.find(function.first) != m_functions.end())
|
||||
throw runtime_error("duplicate id: " + function.first);
|
||||
|
||||
m_functions.append(std::forward<FunctionAndId>(function));
|
||||
m_functions.insert(std::forward<FunctionAndId>(function));
|
||||
}
|
||||
void remove(const String& id)
|
||||
{
|
||||
m_functions.remove(id);
|
||||
m_functions.erase(id);
|
||||
}
|
||||
|
||||
FunctionGroup& get_group(const String& id)
|
||||
|
@ -46,19 +48,20 @@ public:
|
|||
|
||||
CandidateList complete_id(const String& prefix, ByteCount cursor_pos) const
|
||||
{
|
||||
return m_functions.complete_id(prefix, cursor_pos);
|
||||
return complete_key(m_functions, prefix, cursor_pos);
|
||||
}
|
||||
|
||||
CandidateList complete_group_id(const String& prefix, ByteCount cursor_pos) const
|
||||
{
|
||||
return m_functions.complete_id_if(
|
||||
prefix, cursor_pos, [](const FunctionAndId& func) {
|
||||
return complete_key_if(
|
||||
m_functions, prefix, cursor_pos,
|
||||
[](const FunctionAndId& func) {
|
||||
return func.second.template target<FunctionGroup>() != nullptr;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
idvaluemap<String, Function> m_functions;
|
||||
std::unordered_map<String, Function> m_functions;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
#define function_registry_h_INCLUDED
|
||||
|
||||
#include "completion.hh"
|
||||
#include "idvaluemap.hh"
|
||||
#include "string.hh"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
|
@ -20,8 +21,8 @@ class FunctionRegistry
|
|||
public:
|
||||
void register_func(const String& name, const FunctionType& function)
|
||||
{
|
||||
kak_assert(not m_functions.contains(name));
|
||||
m_functions.append(std::make_pair(name, function));
|
||||
kak_assert(m_functions.find(name) == m_functions.end());
|
||||
m_functions[name] = function;
|
||||
}
|
||||
|
||||
const FunctionType& operator[](const String& name) const
|
||||
|
@ -34,11 +35,11 @@ public:
|
|||
|
||||
CandidateList complete_name(const String& prefix, ByteCount cursor_pos)
|
||||
{
|
||||
return m_functions.complete_id(prefix, cursor_pos);
|
||||
return complete_key(m_functions, prefix, cursor_pos);
|
||||
}
|
||||
|
||||
private:
|
||||
idvaluemap<String, FunctionType> m_functions;
|
||||
std::unordered_map<String, FunctionType> m_functions;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace Kakoune
|
|||
void HookManager::add_hook(const String& hook_name, String id, HookFunc hook)
|
||||
{
|
||||
auto& hooks = m_hook[hook_name];
|
||||
hooks.append({std::move(id), std::move(hook)});
|
||||
hooks.insert({std::move(id), std::move(hook)});
|
||||
}
|
||||
|
||||
void HookManager::remove_hooks(const String& id)
|
||||
|
@ -16,7 +16,7 @@ void HookManager::remove_hooks(const String& id)
|
|||
if (id.empty())
|
||||
throw runtime_error("invalid id");
|
||||
for (auto& hooks : m_hook)
|
||||
hooks.second.remove_all(id);
|
||||
hooks.second.erase(id);
|
||||
}
|
||||
|
||||
void HookManager::run_hook(const String& hook_name,
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#ifndef hook_manager_hh_INCLUDED
|
||||
#define hook_manager_hh_INCLUDED
|
||||
|
||||
#include "idvaluemap.hh"
|
||||
#include "utils.hh"
|
||||
|
||||
#include <unordered_map>
|
||||
|
@ -29,7 +28,7 @@ private:
|
|||
friend class GlobalHooks;
|
||||
|
||||
HookManager* m_parent;
|
||||
std::unordered_map<String, idvaluemap<String, HookFunc>> m_hook;
|
||||
std::unordered_map<String, std::unordered_multimap<String, HookFunc>> m_hook;
|
||||
};
|
||||
|
||||
class GlobalHooks : public HookManager,
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
#ifndef idvaluemap_hh_INCLUDED
|
||||
#define idvaluemap_hh_INCLUDED
|
||||
|
||||
#include "completion.hh"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
template<typename Id, typename Value>
|
||||
class idvaluemap
|
||||
{
|
||||
public:
|
||||
typedef std::pair<Id, Value> value_type;
|
||||
typedef std::vector<value_type> container_type;
|
||||
typedef typename container_type::iterator iterator;
|
||||
typedef typename container_type::const_iterator const_iterator;
|
||||
|
||||
void append(const value_type& value)
|
||||
{
|
||||
m_content.push_back(value);
|
||||
}
|
||||
|
||||
void append(value_type&& value)
|
||||
{
|
||||
m_content.push_back(std::move(value));
|
||||
}
|
||||
|
||||
iterator find(const Id& id)
|
||||
{
|
||||
for (auto it = begin(); it != end(); ++it)
|
||||
{
|
||||
if (it->first == id)
|
||||
return it;
|
||||
}
|
||||
return end();
|
||||
}
|
||||
|
||||
const_iterator find(const Id& id) const
|
||||
{
|
||||
for (auto it = begin(); it != end(); ++it)
|
||||
{
|
||||
if (it->first == id)
|
||||
return it;
|
||||
}
|
||||
return end();
|
||||
}
|
||||
|
||||
bool contains(const Id& id) const
|
||||
{
|
||||
return find(id) != end();
|
||||
}
|
||||
|
||||
void remove(const Id& id)
|
||||
{
|
||||
auto it = find(id);
|
||||
if (it != end())
|
||||
m_content.erase(it);
|
||||
}
|
||||
|
||||
void remove_all(const Id& id)
|
||||
{
|
||||
for (auto it = find(id); it != end(); it = find(id))
|
||||
m_content.erase(it);
|
||||
}
|
||||
|
||||
template<typename Condition>
|
||||
CandidateList complete_id_if(const String& prefix,
|
||||
ByteCount cursor_pos,
|
||||
Condition condition) const
|
||||
{
|
||||
String real_prefix = prefix.substr(0, cursor_pos);
|
||||
CandidateList result;
|
||||
for (auto& value : m_content)
|
||||
{
|
||||
if (not condition(value))
|
||||
continue;
|
||||
|
||||
String id_str = value.first;
|
||||
if (prefix_match(id_str, real_prefix))
|
||||
result.push_back(std::move(id_str));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
CandidateList complete_id(const String& prefix,
|
||||
ByteCount cursor_pos) const
|
||||
{
|
||||
return complete_id_if(
|
||||
prefix, cursor_pos, [](const value_type&) { return true; });
|
||||
}
|
||||
|
||||
iterator begin() { return m_content.begin(); }
|
||||
iterator end() { return m_content.end(); }
|
||||
const_iterator begin() const { return m_content.begin(); }
|
||||
const_iterator end() const { return m_content.end(); }
|
||||
|
||||
private:
|
||||
container_type m_content;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // idvaluemap_hh_INCLUDED
|
|
@ -1,7 +1,6 @@
|
|||
#ifndef keymap_manager_hh_INCLUDED
|
||||
#define keymap_manager_hh_INCLUDED
|
||||
|
||||
#include "idvaluemap.hh"
|
||||
#include "keys.hh"
|
||||
#include "utils.hh"
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user