2011-12-02 15:18:31 +01:00
|
|
|
#ifndef idvaluemap_hh_INCLUDED
|
|
|
|
#define idvaluemap_hh_INCLUDED
|
|
|
|
|
2012-01-15 14:40:58 +01:00
|
|
|
#include "completion.hh"
|
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2011-12-02 15:18:31 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-03-27 18:42:00 +01:00
|
|
|
template<typename Id, typename Value>
|
2011-12-02 15:18:31 +01:00
|
|
|
class idvaluemap
|
|
|
|
{
|
|
|
|
public:
|
2013-03-27 18:42:00 +01:00
|
|
|
typedef std::pair<Id, Value> value_type;
|
2011-12-02 15:18:31 +01:00
|
|
|
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)
|
|
|
|
{
|
2013-03-27 18:42:00 +01:00
|
|
|
m_content.push_back(std::move(value));
|
2011-12-02 15:18:31 +01:00
|
|
|
}
|
|
|
|
|
2013-03-27 18:42:00 +01:00
|
|
|
iterator find(const Id& id)
|
2011-12-02 15:18:31 +01:00
|
|
|
{
|
|
|
|
for (auto it = begin(); it != end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == id)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2013-03-27 18:42:00 +01:00
|
|
|
const_iterator find(const Id& id) const
|
2011-12-02 15:18:31 +01:00
|
|
|
{
|
|
|
|
for (auto it = begin(); it != end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == id)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2013-03-27 18:42:00 +01:00
|
|
|
bool contains(const Id& id) const
|
2011-12-02 15:18:31 +01:00
|
|
|
{
|
|
|
|
return find(id) != end();
|
|
|
|
}
|
|
|
|
|
2013-03-27 18:42:00 +01:00
|
|
|
void remove(const Id& id)
|
2011-12-02 15:18:31 +01:00
|
|
|
{
|
|
|
|
for (auto it = m_content.begin(); it != m_content.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == id)
|
|
|
|
{
|
|
|
|
m_content.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 18:42:00 +01:00
|
|
|
template<typename Condition>
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList complete_id_if(const String& prefix,
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount cursor_pos,
|
2013-03-27 18:42:00 +01:00
|
|
|
Condition condition) const
|
2011-12-02 15:18:31 +01:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
2011-12-02 15:18:31 +01:00
|
|
|
CandidateList result;
|
|
|
|
for (auto& value : m_content)
|
|
|
|
{
|
2012-01-19 21:52:08 +01:00
|
|
|
if (not condition(value))
|
|
|
|
continue;
|
|
|
|
|
2012-06-25 19:11:13 +02:00
|
|
|
String id_str = value.first;
|
2011-12-02 15:18:31 +01:00
|
|
|
if (id_str.substr(0, real_prefix.length()) == real_prefix)
|
|
|
|
result.push_back(std::move(id_str));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList complete_id(const String& prefix,
|
2013-03-27 14:09:09 +01:00
|
|
|
ByteCount cursor_pos) const
|
2012-01-19 21:52:08 +01:00
|
|
|
{
|
2012-06-25 19:11:13 +02:00
|
|
|
return complete_id_if(
|
2012-01-19 21:52:08 +01:00
|
|
|
prefix, cursor_pos, [](const value_type&) { return true; });
|
|
|
|
}
|
|
|
|
|
2011-12-02 15:18:31 +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;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // idvaluemap_hh_INCLUDED
|