IdMaps can be sorted, in which case the find method uses a binary search
This commit is contained in:
parent
c859e8ab5f
commit
36828e6059
|
@ -26,6 +26,8 @@ Client::Client(std::unique_ptr<UserInterface>&& ui,
|
|||
std::move(name)},
|
||||
m_env_vars(env_vars)
|
||||
{
|
||||
m_env_vars.sort();
|
||||
|
||||
context().set_client(*this);
|
||||
context().set_window(*m_window);
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ EnvVarMap get_env_vars()
|
|||
++value;
|
||||
env_vars.append({{name, value}, (*value == '=') ? value+1 : String{}});
|
||||
}
|
||||
env_vars.sort();
|
||||
return env_vars;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,22 +32,63 @@ public:
|
|||
using iterator = typename container_type::iterator;
|
||||
using const_iterator = typename container_type::const_iterator;
|
||||
|
||||
IdMap() = default;
|
||||
IdMap(std::initializer_list<Element> val) : m_content{val} {}
|
||||
IdMap() : m_sorted(true) {}
|
||||
|
||||
void append(const Element& value)
|
||||
IdMap(std::initializer_list<Element> val)
|
||||
: m_content{val},
|
||||
m_sorted(std::is_sorted(begin(), end(),
|
||||
[](const Element& lhs, const Element& rhs)
|
||||
{ return lhs.hash < rhs.hash; }))
|
||||
{}
|
||||
|
||||
void append(const Element& value, bool keep_sorted = false)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void append(Element&& value)
|
||||
void append(Element&& value, bool keep_sorted = false)
|
||||
{
|
||||
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, std::move(value));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_content.push_back(std::move(value));
|
||||
m_sorted = false;
|
||||
}
|
||||
}
|
||||
|
||||
iterator find(StringView id)
|
||||
{
|
||||
const size_t hash = hash_value(id);
|
||||
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; });
|
||||
|
@ -100,9 +141,18 @@ public:
|
|||
return not (*this == other);
|
||||
}
|
||||
|
||||
void sort()
|
||||
{
|
||||
std::sort(begin(), end(),
|
||||
[](const Element& lhs, const Element& rhs)
|
||||
{ return lhs.hash < rhs.hash; });
|
||||
m_sorted = true;
|
||||
}
|
||||
|
||||
void reserve(size_t size) { m_content.reserve(size); }
|
||||
size_t size() const { return m_content.size(); }
|
||||
void clear() { m_content.clear(); }
|
||||
void erase(const_iterator it) { m_content.erase(it); }
|
||||
|
||||
static const String& get_id(const Element& e) { return e.key; }
|
||||
|
||||
|
@ -115,6 +165,7 @@ public:
|
|||
|
||||
private:
|
||||
container_type m_content;
|
||||
bool m_sorted;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user