2013-11-18 23:24:31 +01:00
|
|
|
#ifndef id_map_hh_INCLUDED
|
|
|
|
#define id_map_hh_INCLUDED
|
2013-11-18 23:17:50 +01:00
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "containers.hh"
|
2013-11-18 23:17:50 +01:00
|
|
|
#include "completion.hh"
|
2013-11-18 23:24:31 +01:00
|
|
|
#include "string.hh"
|
2013-11-18 23:17:50 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-11-18 23:24:31 +01:00
|
|
|
template<typename Value>
|
|
|
|
class id_map
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
|
|
|
public:
|
2014-01-09 20:50:01 +01:00
|
|
|
using value_type = std::pair<String, Value>;
|
|
|
|
using container_type = std::vector<value_type>;
|
|
|
|
using iterator = typename container_type::iterator;
|
|
|
|
using const_iterator = typename container_type::const_iterator;
|
2013-11-18 23:17:50 +01:00
|
|
|
|
2014-06-06 00:49:52 +02:00
|
|
|
id_map() = default;
|
|
|
|
id_map(std::initializer_list<value_type> val) : m_content{val} {}
|
|
|
|
|
2013-11-18 23:17:50 +01:00
|
|
|
void append(const value_type& value)
|
|
|
|
{
|
|
|
|
m_content.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(value_type&& value)
|
|
|
|
{
|
|
|
|
m_content.push_back(std::move(value));
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:47:21 +02:00
|
|
|
iterator find(StringView id)
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
2014-12-11 14:55:04 +01:00
|
|
|
return find_if(m_content,
|
|
|
|
[&](const value_type& v){ return v.first == id; });
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-21 11:47:21 +02:00
|
|
|
const_iterator find(StringView id) const
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
2014-12-11 14:55:04 +01:00
|
|
|
return find_if(m_content,
|
|
|
|
[&](const value_type& v){ return v.first == id; });
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-21 11:47:21 +02:00
|
|
|
bool contains(StringView id) const
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
|
|
|
return find(id) != end();
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:47:21 +02:00
|
|
|
void remove(StringView id)
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
|
|
|
auto it = find(id);
|
|
|
|
if (it != end())
|
|
|
|
m_content.erase(it);
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:47:21 +02:00
|
|
|
void remove_all(StringView id)
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
2014-12-11 14:55:04 +01:00
|
|
|
auto it = std::remove_if(begin(), end(),
|
|
|
|
[&](value_type& v){ return v.first == id; });
|
|
|
|
m_content.erase(it, end());
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Condition>
|
2014-04-18 15:02:14 +02:00
|
|
|
CandidateList complete_id_if(StringView prefix,
|
2013-11-18 23:17:50 +01:00
|
|
|
ByteCount cursor_pos,
|
|
|
|
Condition condition) const
|
|
|
|
{
|
2014-04-18 15:02:14 +02:00
|
|
|
auto real_prefix = prefix.substr(0, cursor_pos);
|
2013-11-18 23:17:50 +01:00
|
|
|
CandidateList result;
|
|
|
|
for (auto& value : m_content)
|
|
|
|
{
|
|
|
|
if (not condition(value))
|
|
|
|
continue;
|
|
|
|
|
2013-11-18 23:24:31 +01:00
|
|
|
if (prefix_match(value.first, real_prefix))
|
|
|
|
result.push_back(value.first);
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-04-18 15:02:14 +02:00
|
|
|
CandidateList complete_id(StringView prefix,
|
2013-11-18 23:17:50 +01:00
|
|
|
ByteCount cursor_pos) const
|
|
|
|
{
|
|
|
|
return complete_id_if(
|
|
|
|
prefix, cursor_pos, [](const value_type&) { return true; });
|
|
|
|
}
|
|
|
|
|
2014-06-06 00:49:52 +02:00
|
|
|
bool empty() const { return m_content.empty(); }
|
|
|
|
|
2013-11-18 23:17:50 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:24:31 +01:00
|
|
|
#endif // id_map_hh_INCLUDED
|