Remove Set and use unordered_set

This commit is contained in:
Maxime Coste 2013-01-31 18:58:25 +01:00
parent 7f02ef334f
commit edef8e4e98
6 changed files with 20 additions and 54 deletions

View File

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <list> #include <list>
#include <memory> #include <memory>
#include <unordered_set>
#include "line_and_column.hh" #include "line_and_column.hh"
#include "option_manager.hh" #include "option_manager.hh"
@ -167,7 +168,7 @@ public:
HookManager& hooks() { return m_hooks; } HookManager& hooks() { return m_hooks; }
const HookManager& hooks() const { return m_hooks; } const HookManager& hooks() const { return m_hooks; }
Set<BufferChangeListener*>& change_listeners() const { return m_change_listeners; } std::unordered_set<BufferChangeListener*>& change_listeners() const { return m_change_listeners; }
private: private:
friend class BufferIterator; friend class BufferIterator;
@ -211,7 +212,7 @@ private:
// this is mutable as adding or removing listeners is not muting the // this is mutable as adding or removing listeners is not muting the
// buffer observable state. // buffer observable state.
mutable Set<BufferChangeListener*> m_change_listeners; mutable std::unordered_set<BufferChangeListener*> m_change_listeners;
OptionManager m_options; OptionManager m_options;
HookManager m_hooks; HookManager m_hooks;

View File

@ -7,19 +7,19 @@ DynamicSelectionList::DynamicSelectionList(const Buffer& buffer,
SelectionList selections) SelectionList selections)
: m_buffer(&buffer), SelectionList(std::move(selections)) : m_buffer(&buffer), SelectionList(std::move(selections))
{ {
m_buffer->change_listeners().add(this); m_buffer->change_listeners().insert(this);
check_invariant(); check_invariant();
} }
DynamicSelectionList::~DynamicSelectionList() DynamicSelectionList::~DynamicSelectionList()
{ {
m_buffer->change_listeners().remove(this); m_buffer->change_listeners().erase(this);
} }
DynamicSelectionList::DynamicSelectionList(const DynamicSelectionList& other) DynamicSelectionList::DynamicSelectionList(const DynamicSelectionList& other)
: SelectionList(other), m_buffer(other.m_buffer) : SelectionList(other), m_buffer(other.m_buffer)
{ {
m_buffer->change_listeners().add(this); m_buffer->change_listeners().insert(this);
} }
DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList& other) DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList& other)
@ -27,9 +27,9 @@ DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList
SelectionList::operator=((const SelectionList&)other); SelectionList::operator=((const SelectionList&)other);
if (m_buffer != other.m_buffer) if (m_buffer != other.m_buffer)
{ {
m_buffer->change_listeners().remove(this); m_buffer->change_listeners().erase(this);
m_buffer = other.m_buffer; m_buffer = other.m_buffer;
m_buffer->change_listeners().add(this); m_buffer->change_listeners().insert(this);
} }
check_invariant(); check_invariant();
return *this; return *this;
@ -38,7 +38,7 @@ DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList
DynamicSelectionList::DynamicSelectionList(DynamicSelectionList&& other) DynamicSelectionList::DynamicSelectionList(DynamicSelectionList&& other)
: SelectionList(std::move(other)), m_buffer(other.m_buffer) : SelectionList(std::move(other)), m_buffer(other.m_buffer)
{ {
m_buffer->change_listeners().add(this); m_buffer->change_listeners().insert(this);
} }
DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& other) DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& other)
@ -46,9 +46,9 @@ DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& oth
SelectionList::operator=(std::move(other)); SelectionList::operator=(std::move(other));
if (m_buffer != other.m_buffer) if (m_buffer != other.m_buffer)
{ {
m_buffer->change_listeners().remove(this); m_buffer->change_listeners().erase(this);
m_buffer = other.m_buffer; m_buffer = other.m_buffer;
m_buffer->change_listeners().add(this); m_buffer->change_listeners().insert(this);
} }
check_invariant(); check_invariant();
return *this; return *this;

View File

@ -319,10 +319,10 @@ class LastModifiedRangeListener : public BufferChangeListener
public: public:
LastModifiedRangeListener(Buffer& buffer) LastModifiedRangeListener(Buffer& buffer)
: m_buffer(buffer) : m_buffer(buffer)
{ m_buffer.change_listeners().add(this); } { m_buffer.change_listeners().insert(this); }
~LastModifiedRangeListener() ~LastModifiedRangeListener()
{ m_buffer.change_listeners().remove(this); } { m_buffer.change_listeners().erase(this); }
void on_insert(const BufferIterator& begin, const BufferIterator& end) void on_insert(const BufferIterator& begin, const BufferIterator& end)
{ {

View File

@ -8,23 +8,23 @@ 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.add(this); EventManager::instance().m_fd_watchers.insert(this);
} }
FDWatcher::~FDWatcher() FDWatcher::~FDWatcher()
{ {
EventManager::instance().m_fd_watchers.remove(this); EventManager::instance().m_fd_watchers.erase(this);
} }
Timer::Timer(TimePoint date, Callback callback) Timer::Timer(TimePoint date, Callback callback)
: m_date{date}, m_callback{std::move(callback)} : m_date{date}, m_callback{std::move(callback)}
{ {
EventManager::instance().m_timers.add(this); EventManager::instance().m_timers.insert(this);
} }
Timer::~Timer() Timer::~Timer()
{ {
EventManager::instance().m_timers.remove(this); EventManager::instance().m_timers.erase(this);
} }
void Timer::run() void Timer::run()

View File

@ -4,6 +4,7 @@
#include "utils.hh" #include "utils.hh"
#include <chrono> #include <chrono>
#include <unordered_set>
namespace Kakoune namespace Kakoune
{ {
@ -62,8 +63,8 @@ public:
private: private:
friend class FDWatcher; friend class FDWatcher;
friend class Timer; friend class Timer;
Set<FDWatcher*> m_fd_watchers; std::unordered_set<FDWatcher*> m_fd_watchers;
Set<Timer*> m_timers; std::unordered_set<Timer*> m_timers;
std::vector<int> m_forced_fd; std::vector<int> m_forced_fd;
TimePoint m_last; TimePoint m_last;

View File

@ -207,42 +207,6 @@ const T& clamp(const T& val, const T& min, const T& max)
return (val < min ? min : (val > max ? max : val)); return (val < min ? min : (val > max ? max : val));
} }
// *** set ***
// generic simple set based on vector
template<typename T>
class Set
{
public:
using iterator = typename std::vector<T>::iterator;
using const_iterator = typename std::vector<T>::const_iterator;
void add(T value)
{
assert(not contains(m_values, value));
m_values.push_back(value);
}
void remove(T value)
{
auto it = find(m_values, value);
assert(it != m_values.end());
m_values.erase(it);
}
size_t size() const { return m_values.size(); }
bool empty() const { return m_values.empty(); }
iterator begin() { return m_values.begin(); }
iterator end() { return m_values.end(); }
const_iterator begin() const { return m_values.begin(); }
const_iterator end() const { return m_values.end(); }
private:
std::vector<T> m_values;
};
} }
#endif // utils_hh_INCLUDED #endif // utils_hh_INCLUDED