2013-11-19 02:24:31 +04:00
|
|
|
#ifndef id_map_hh_INCLUDED
|
|
|
|
#define id_map_hh_INCLUDED
|
2013-11-19 02:17:50 +04:00
|
|
|
|
2014-12-23 16:34:21 +03:00
|
|
|
#include "containers.hh"
|
2013-11-19 02:24:31 +04:00
|
|
|
#include "string.hh"
|
2015-01-07 22:29:31 +03:00
|
|
|
#include "vector.hh"
|
2013-11-19 02:17:50 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-07 22:29:31 +03:00
|
|
|
template<typename Value, MemoryDomain domain = MemoryDomain::Undefined>
|
2014-12-24 01:51:00 +03:00
|
|
|
class IdMap
|
2013-11-19 02:17:50 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-01-09 23:50:01 +04:00
|
|
|
using value_type = std::pair<String, Value>;
|
2015-01-07 22:29:31 +03:00
|
|
|
using container_type = Vector<value_type, domain>;
|
2014-01-09 23:50:01 +04:00
|
|
|
using iterator = typename container_type::iterator;
|
|
|
|
using const_iterator = typename container_type::const_iterator;
|
2013-11-19 02:17:50 +04:00
|
|
|
|
2014-12-24 01:51:00 +03:00
|
|
|
IdMap() = default;
|
|
|
|
IdMap(std::initializer_list<value_type> val) : m_content{val} {}
|
2014-06-06 02:49:52 +04:00
|
|
|
|
2013-11-19 02:17:50 +04: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 13:47:21 +04:00
|
|
|
iterator find(StringView id)
|
2013-11-19 02:17:50 +04:00
|
|
|
{
|
2014-12-24 01:15:25 +03:00
|
|
|
return Kakoune::find(transformed(m_content, get_id), id).base();
|
2013-11-19 02:17:50 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 13:47:21 +04:00
|
|
|
const_iterator find(StringView id) const
|
2013-11-19 02:17:50 +04:00
|
|
|
{
|
2014-12-24 01:15:25 +03:00
|
|
|
return Kakoune::find(transformed(m_content, get_id), id).base();
|
2013-11-19 02:17:50 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 13:47:21 +04:00
|
|
|
bool contains(StringView id) const
|
2013-11-19 02:17:50 +04:00
|
|
|
{
|
|
|
|
return find(id) != end();
|
|
|
|
}
|
|
|
|
|
2014-04-21 13:47:21 +04:00
|
|
|
void remove(StringView id)
|
2013-11-19 02:17:50 +04:00
|
|
|
{
|
|
|
|
auto it = find(id);
|
|
|
|
if (it != end())
|
|
|
|
m_content.erase(it);
|
|
|
|
}
|
|
|
|
|
2014-04-21 13:47:21 +04:00
|
|
|
void remove_all(StringView id)
|
2013-11-19 02:17:50 +04:00
|
|
|
{
|
2014-12-11 16:55:04 +03:00
|
|
|
auto it = std::remove_if(begin(), end(),
|
|
|
|
[&](value_type& v){ return v.first == id; });
|
|
|
|
m_content.erase(it, end());
|
2013-11-19 02:17:50 +04:00
|
|
|
}
|
|
|
|
|
2015-03-05 17:59:27 +03:00
|
|
|
Value& operator[](StringView id)
|
|
|
|
{
|
|
|
|
auto it = find(id);
|
|
|
|
if (it != m_content.end())
|
|
|
|
return it->second;
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
append({ id.str(), Value{} });
|
2015-03-05 17:59:27 +03:00
|
|
|
return (m_content.end()-1)->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Value& operator[](StringView id) const
|
|
|
|
{
|
|
|
|
return (*const_cast<IdMap*>(this))[id];
|
|
|
|
}
|
|
|
|
|
2014-12-23 16:54:09 +03:00
|
|
|
static const String& get_id(const value_type& v) { return v.first; }
|
2013-11-19 02:17:50 +04:00
|
|
|
|
2014-06-06 02:49:52 +04:00
|
|
|
bool empty() const { return m_content.empty(); }
|
|
|
|
|
2013-11-19 02:17:50 +04: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-19 02:24:31 +04:00
|
|
|
#endif // id_map_hh_INCLUDED
|