kakoune/src/id_map.hh

123 lines
3.0 KiB
C++
Raw Normal View History

#ifndef id_map_hh_INCLUDED
#define id_map_hh_INCLUDED
#include "string.hh"
#include "vector.hh"
2015-09-16 20:57:57 +02:00
#include <algorithm>
namespace Kakoune
{
template<typename Value, MemoryDomain domain = MemoryDomain::Undefined>
2014-12-23 23:51:00 +01:00
class IdMap
{
public:
2015-09-16 20:57:57 +02:00
struct Element
{
Element(String k, Value v)
: key(std::move(k)), hash(hash_value(key)), value(std::move(v)) {}
String key;
size_t hash;
Value value;
bool operator==(const Element& other) const
{
return hash == other.hash and key == other.key and value == other.value;
}
};
using container_type = Vector<Element, domain>;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
2014-12-23 23:51:00 +01:00
IdMap() = default;
2015-09-16 20:57:57 +02:00
IdMap(std::initializer_list<Element> val) : m_content{val} {}
2015-09-16 20:57:57 +02:00
void append(const Element& value)
{
m_content.push_back(value);
}
2015-09-16 20:57:57 +02:00
void append(Element&& value)
{
m_content.push_back(std::move(value));
}
2014-04-21 11:47:21 +02:00
iterator find(StringView id)
{
2015-09-16 20:57:57 +02:00
const size_t hash = hash_value(id);
return std::find_if(begin(), end(),
[id, hash](const Element& e)
{ return e.hash == hash and e.key == id; });
}
2014-04-21 11:47:21 +02:00
const_iterator find(StringView id) const
{
2015-09-16 20:57:57 +02:00
return const_cast<IdMap*>(this)->find(id);
}
2014-04-21 11:47:21 +02:00
bool contains(StringView id) const
{
return find(id) != end();
}
2014-04-21 11:47:21 +02:00
void remove(StringView id)
{
auto it = find(id);
if (it != end())
m_content.erase(it);
}
2014-04-21 11:47:21 +02:00
void remove_all(StringView id)
{
2015-09-16 20:57:57 +02:00
const size_t hash = hash_value(id);
auto it = std::remove_if(begin(), end(), [id, hash](const Element& e)
{ return e.hash == hash and e.key == id; });
m_content.erase(it, end());
}
Value& operator[](StringView id)
{
auto it = find(id);
if (it != m_content.end())
2015-09-16 20:57:57 +02:00
return it->value;
append({ id.str(), Value{} });
2015-09-16 20:57:57 +02:00
return (m_content.end()-1)->value;
}
template<MemoryDomain dom>
bool operator==(const IdMap<Value, dom>& other) const
{
2015-09-16 20:25:12 +02:00
return size() == other.size() and std::equal(begin(), end(), other.begin());
}
template<MemoryDomain dom>
bool operator!=(const IdMap<Value, dom>& other) const
{
return not (*this == other);
}
void reserve(size_t size) { m_content.reserve(size); }
size_t size() const { return m_content.size(); }
void clear() { m_content.clear(); }
2015-09-16 20:57:57 +02:00
static const String& get_id(const Element& e) { return e.key; }
bool empty() const { return m_content.empty(); }
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 // id_map_hh_INCLUDED