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
|
|
|
|
2013-11-18 23:24:31 +01:00
|
|
|
#include "string.hh"
|
2015-01-07 20:29:31 +01:00
|
|
|
#include "vector.hh"
|
2013-11-18 23:17:50 +01:00
|
|
|
|
2015-09-16 20:57:57 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-11-18 23:17:50 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-07 20:29:31 +01:00
|
|
|
template<typename Value, MemoryDomain domain = MemoryDomain::Undefined>
|
2014-12-23 23:51:00 +01:00
|
|
|
class IdMap
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
|
|
|
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>;
|
2014-01-09 20:50:01 +01:00
|
|
|
using iterator = typename container_type::iterator;
|
|
|
|
using const_iterator = typename container_type::const_iterator;
|
2013-11-18 23:17:50 +01:00
|
|
|
|
2015-09-16 23:22:51 +02:00
|
|
|
IdMap() : m_sorted(true) {}
|
2014-06-06 00:49:52 +02:00
|
|
|
|
2015-09-16 23:22:51 +02:00
|
|
|
IdMap(std::initializer_list<Element> val)
|
|
|
|
: m_content{val},
|
2015-09-16 23:29:19 +02:00
|
|
|
m_sorted(std::is_sorted(begin(), end(), cmp_hashes))
|
2015-09-16 23:22:51 +02:00
|
|
|
{}
|
|
|
|
|
2015-09-16 23:29:19 +02:00
|
|
|
bool sorted() const { return m_sorted; }
|
|
|
|
void assume_sorted()
|
|
|
|
{
|
|
|
|
kak_assert(std::is_sorted(begin(), end(), cmp_hashes));
|
|
|
|
m_sorted = true;
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:22:51 +02:00
|
|
|
void append(const Element& value, bool keep_sorted = false)
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
2015-09-16 23:22:51 +02:00
|
|
|
if (keep_sorted and m_sorted)
|
|
|
|
{
|
|
|
|
auto it = std::lower_bound(begin(), end(), value.hash,
|
|
|
|
[](const Element& e, size_t hash)
|
|
|
|
{ return e.hash < hash; });
|
|
|
|
m_content.insert(it, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_content.push_back(value);
|
|
|
|
m_sorted = false;
|
|
|
|
}
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:22:51 +02:00
|
|
|
void append(Element&& value, bool keep_sorted = false)
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
2015-09-16 23:22:51 +02:00
|
|
|
if (keep_sorted and m_sorted)
|
|
|
|
{
|
2015-09-16 23:29:19 +02:00
|
|
|
auto it = std::lower_bound(begin(), end(), value, cmp_hashes);
|
2015-09-16 23:22:51 +02:00
|
|
|
m_content.insert(it, std::move(value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_content.push_back(std::move(value));
|
|
|
|
m_sorted = false;
|
|
|
|
}
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-21 11:47:21 +02:00
|
|
|
iterator find(StringView id)
|
2013-11-18 23:17:50 +01:00
|
|
|
{
|
2015-09-16 20:57:57 +02:00
|
|
|
const size_t hash = hash_value(id);
|
2015-09-16 23:22:51 +02:00
|
|
|
if (m_sorted)
|
|
|
|
{
|
|
|
|
auto it = std::lower_bound(begin(), end(), hash,
|
|
|
|
[](const Element& e, size_t hash)
|
|
|
|
{ return e.hash < hash; });
|
|
|
|
for (auto e = end(); it != e and it->hash == hash; ++it)
|
|
|
|
{
|
|
|
|
if (it->key == id)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return std::find_if(begin(), end(),
|
|
|
|
[id, hash](const Element& e)
|
|
|
|
{ return e.hash == hash and e.key == 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
|
|
|
{
|
2015-09-16 20:57:57 +02:00
|
|
|
return const_cast<IdMap*>(this)->find(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
|
|
|
{
|
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; });
|
2014-12-11 14:55:04 +01:00
|
|
|
m_content.erase(it, end());
|
2013-11-18 23:17:50 +01:00
|
|
|
}
|
|
|
|
|
2015-03-05 15:59:27 +01:00
|
|
|
Value& operator[](StringView id)
|
|
|
|
{
|
|
|
|
auto it = find(id);
|
|
|
|
if (it != m_content.end())
|
2015-09-16 20:57:57 +02:00
|
|
|
return it->value;
|
2015-03-05 15:59:27 +01:00
|
|
|
|
2015-03-10 20:33:46 +01:00
|
|
|
append({ id.str(), Value{} });
|
2015-09-16 20:57:57 +02:00
|
|
|
return (m_content.end()-1)->value;
|
2015-03-05 15:59:27 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 20:04:19 +02:00
|
|
|
template<MemoryDomain dom>
|
|
|
|
bool operator==(const IdMap<Value, dom>& other) const
|
2015-03-05 15:59:27 +01:00
|
|
|
{
|
2015-09-16 20:25:12 +02:00
|
|
|
return size() == other.size() and std::equal(begin(), end(), other.begin());
|
2015-03-05 15:59:27 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 20:04:19 +02:00
|
|
|
template<MemoryDomain dom>
|
|
|
|
bool operator!=(const IdMap<Value, dom>& other) const
|
|
|
|
{
|
|
|
|
return not (*this == other);
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:22:51 +02:00
|
|
|
void sort()
|
|
|
|
{
|
2015-09-16 23:29:19 +02:00
|
|
|
std::sort(begin(), end(), cmp_hashes);
|
2015-09-16 23:22:51 +02:00
|
|
|
m_sorted = true;
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:04:19 +02:00
|
|
|
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 23:22:51 +02:00
|
|
|
void erase(const_iterator it) { m_content.erase(it); }
|
2015-09-16 20:04:19 +02:00
|
|
|
|
2015-09-16 20:57:57 +02:00
|
|
|
static const String& get_id(const Element& e) { return e.key; }
|
2013-11-18 23:17:50 +01:00
|
|
|
|
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:
|
2015-09-16 23:29:19 +02:00
|
|
|
static bool cmp_hashes(const Element& lhs, const Element& rhs)
|
|
|
|
{
|
|
|
|
return lhs.hash < rhs.hash;
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:17:50 +01:00
|
|
|
container_type m_content;
|
2015-09-16 23:22:51 +02:00
|
|
|
bool m_sorted;
|
2013-11-18 23:17:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:24:31 +01:00
|
|
|
#endif // id_map_hh_INCLUDED
|