Get rid of uses of unordered_set, vector is just simpler and faster...

This commit is contained in:
Maxime Coste 2014-12-09 21:59:47 +00:00
parent fd84ad5adf
commit 319cfcda34
6 changed files with 42 additions and 65 deletions

View File

@ -8,12 +8,12 @@ namespace Kakoune
FDWatcher::FDWatcher(int fd, Callback callback) FDWatcher::FDWatcher(int fd, Callback callback)
: m_fd{fd}, m_callback{std::move(callback)} : m_fd{fd}, m_callback{std::move(callback)}
{ {
EventManager::instance().m_fd_watchers.insert(this); EventManager::instance().m_fd_watchers.push_back(this);
} }
FDWatcher::~FDWatcher() FDWatcher::~FDWatcher()
{ {
EventManager::instance().m_fd_watchers.erase(this); unordered_erase(EventManager::instance().m_fd_watchers, this);
} }
void FDWatcher::run(EventMode mode) void FDWatcher::run(EventMode mode)
@ -31,13 +31,13 @@ Timer::Timer(TimePoint date, Callback callback, EventMode mode)
: m_date{date}, m_callback{std::move(callback)}, m_mode(mode) : m_date{date}, m_callback{std::move(callback)}, m_mode(mode)
{ {
if (EventManager::has_instance()) if (EventManager::has_instance())
EventManager::instance().m_timers.insert(this); EventManager::instance().m_timers.push_back(this);
} }
Timer::~Timer() Timer::~Timer()
{ {
if (EventManager::has_instance()) if (EventManager::has_instance())
EventManager::instance().m_timers.erase(this); unordered_erase(EventManager::instance().m_timers, this);
} }
void Timer::run(EventMode mode) void Timer::run(EventMode mode)

View File

@ -5,7 +5,7 @@
#include "flags.hh" #include "flags.hh"
#include <chrono> #include <chrono>
#include <unordered_set> #include <vector>
#include <sys/select.h> #include <sys/select.h>
@ -83,8 +83,8 @@ public:
private: private:
friend class FDWatcher; friend class FDWatcher;
friend class Timer; friend class Timer;
std::unordered_set<FDWatcher*> m_fd_watchers; std::vector<FDWatcher*> m_fd_watchers;
std::unordered_set<Timer*> m_timers; std::vector<Timer*> m_timers;
fd_set m_forced_fd; fd_set m_forced_fd;
TimePoint m_last; TimePoint m_last;

View File

@ -93,14 +93,11 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
String current_word{begin, end}; String current_word{begin, end};
auto& word_db = get_word_db(buffer); auto& word_db = get_word_db(buffer);
std::unordered_set<ComplAndDesc> matches; auto matches = subseq ? word_db.find_subsequence(prefix)
auto bufmatches = subseq ? word_db.find_subsequence(prefix) : word_db.find_prefix(prefix);
: word_db.find_prefix(prefix);
for (auto& match : bufmatches)
matches.insert(ComplAndDesc{match, ""});
if (word_db.get_word_occurences(current_word) <= 1) if (word_db.get_word_occurences(current_word) <= 1)
matches.erase(ComplAndDesc{current_word, ""}); unordered_erase(matches, current_word);
if (other_buffers) if (other_buffers)
{ {
@ -109,17 +106,21 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
if (buf.get() == &buffer) if (buf.get() == &buffer)
continue; continue;
auto& buf_word_db = get_word_db(*buf); auto& buf_word_db = get_word_db(*buf);
bufmatches = subseq ? buf_word_db.find_subsequence(prefix) auto bufmatches = subseq ? buf_word_db.find_subsequence(prefix)
: buf_word_db.find_prefix(prefix); : buf_word_db.find_prefix(prefix);
for (auto& match : bufmatches) std::move(bufmatches.begin(), bufmatches.end(),
matches.insert(ComplAndDesc{match, ""}); std::back_inserter(matches));
} }
} }
matches.erase(ComplAndDesc{prefix, ""}); unordered_erase(matches, prefix);
std::sort(matches.begin(), matches.end());
matches.erase(std::unique(matches.begin(), matches.end()), matches.end());
ComplAndDescList result; ComplAndDescList result;
std::copy(matches.begin(), matches.end(), result.reserve(matches.size());
inserter(result, result.begin())); for (auto& m : matches)
std::sort(result.begin(), result.end()); result.emplace_back(m, "");
return { begin.coord(), cursor_pos, std::move(result), buffer.timestamp() }; return { begin.coord(), cursor_pos, std::move(result), buffer.timestamp() };
} }

View File

@ -973,6 +973,11 @@ void select_to_next_char(Context& context, NormalParams params)
}, "select to next char","enter char to select to"); }, "select to next char","enter char to select to");
} }
static bool is_basic_alpha(Codepoint c)
{
return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z');
}
void start_or_end_macro_recording(Context& context, NormalParams) void start_or_end_macro_recording(Context& context, NormalParams)
{ {
if (context.input_handler().is_recording()) if (context.input_handler().is_recording())
@ -980,7 +985,7 @@ void start_or_end_macro_recording(Context& context, NormalParams)
else else
on_next_key_with_autoinfo(context, KeymapMode::None, on_next_key_with_autoinfo(context, KeymapMode::None,
[](Key key, Context& context) { [](Key key, Context& context) {
if (key.modifiers == Key::Modifiers::None and isalpha(key.key)) if (key.modifiers == Key::Modifiers::None and is_basic_alpha(key.key))
context.input_handler().start_recording(tolower(key.key)); context.input_handler().start_recording(tolower(key.key));
}, "record macro", "enter macro name "); }, "record macro", "enter macro name ");
} }
@ -995,18 +1000,19 @@ void replay_macro(Context& context, NormalParams params)
{ {
on_next_key_with_autoinfo(context, KeymapMode::None, on_next_key_with_autoinfo(context, KeymapMode::None,
[params](Key key, Context& context) mutable { [params](Key key, Context& context) mutable {
if (key.modifiers == Key::Modifiers::None and isalpha(key.key)) if (key.modifiers == Key::Modifiers::None and is_basic_alpha(key.key))
{ {
static std::unordered_set<char> running_macros; static bool running_macros[26] = {};
const char name = tolower(key.key); const char name = tolower(key.key);
if (contains(running_macros, name)) const size_t idx = (size_t)(name - 'a');
if (running_macros[idx])
throw runtime_error("recursive macros call detected"); throw runtime_error("recursive macros call detected");
memoryview<String> reg_val = RegisterManager::instance()[name].values(context); memoryview<String> reg_val = RegisterManager::instance()[name].values(context);
if (not reg_val.empty()) if (not reg_val.empty())
{ {
running_macros.insert(name); running_macros[idx] = true;
auto stop = on_scope_end([&]{ running_macros.erase(name); }); auto stop = on_scope_end([&]{ running_macros[idx] = false; });
auto keys = parse_keys(reg_val[0]); auto keys = parse_keys(reg_val[0]);
ScopedEdition edition(context); ScopedEdition edition(context);

View File

@ -10,7 +10,6 @@
#include <tuple> #include <tuple>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
namespace Kakoune namespace Kakoune
{ {
@ -68,39 +67,6 @@ bool option_add(std::vector<T>& opt, const std::vector<T>& vec)
return not vec.empty(); return not vec.empty();
} }
template<typename T>
String option_to_string(const std::unordered_set<T>& opt)
{
String res;
for (auto it = begin(opt); it != end(opt); ++it)
{
if (it != begin(opt))
res += list_separator;
res += escape(option_to_string(*it), list_separator, '\\');
}
return res;
}
template<typename T>
void option_from_string(StringView str, std::unordered_set<T>& opt)
{
opt.clear();
std::vector<String> elems = split(str, list_separator, '\\');
for (auto& elem: elems)
{
T opt_elem;
option_from_string(elem, opt_elem);
opt.insert(opt_elem);
}
}
template<typename T>
bool option_add(std::unordered_set<T>& opt, const std::unordered_set<T>& set)
{
std::copy(set.begin(), set.end(), std::inserter(opt, opt.begin()));
return not set.empty();
}
template<typename Key, typename Value> template<typename Key, typename Value>
String option_to_string(const std::unordered_map<Key, Value>& opt) String option_to_string(const std::unordered_map<Key, Value>& opt)
{ {

View File

@ -7,7 +7,6 @@
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <unordered_set>
namespace Kakoune namespace Kakoune
{ {
@ -113,10 +112,15 @@ bool contains(Container&& container, const T& value)
return find(container, value) != end(container); return find(container, value) != end(container);
} }
template<typename T1, typename T2> template<typename T, typename U>
bool contains(const std::unordered_set<T1>& container, const T2& value) void unordered_erase(std::vector<T>& vec, U&& value)
{ {
return container.find(value) != container.end(); auto it = find(vec, std::forward<U>(value));
if (it != vec.end())
{
std::swap(vec.back(), *it);
vec.pop_back();
}
} }
template<typename Iterator, typename EndIterator, typename T> template<typename Iterator, typename EndIterator, typename T>