feat(library/head_map): add head_map_prio

This commit is contained in:
Leonardo de Moura 2015-11-16 22:07:04 -08:00
parent 9e492a8771
commit f7c6eee791

View file

@ -30,10 +30,24 @@ struct head_index {
This index is very naive, but it is effective for many applications. This index is very naive, but it is effective for many applications.
*/ */
template<typename V> template<typename V, typename GetPrio>
class head_map { class head_map_prio : private GetPrio {
rb_map<head_index, list<V>, head_index::cmp> m_map; rb_map<head_index, list<V>, head_index::cmp> m_map;
unsigned get_priority(V const & v) const { return GetPrio::operator()(v); }
list<V> insert_prio(V const & v, list<V> const & vs) {
if (!vs)
return to_list(v);
else if (get_priority(v) >= get_priority(head(vs)))
return cons(v, vs);
else
return cons(head(vs), insert_prio(v, tail(vs)));
}
public: public:
head_map_prio() {}
head_map_prio(GetPrio const & g):GetPrio(g) {}
bool empty() const { return m_map.empty(); } bool empty() const { return m_map.empty(); }
bool contains(head_index const & h) const { return m_map.contains(h); } bool contains(head_index const & h) const { return m_map.contains(h); }
list<V> const * find(head_index const & h) const { return m_map.find(h); } list<V> const * find(head_index const & h) const { return m_map.find(h); }
@ -52,7 +66,7 @@ public:
} }
void insert(head_index const & h, V const & v) { void insert(head_index const & h, V const & v) {
if (auto it = m_map.find(h)) if (auto it = m_map.find(h))
m_map.insert(h, cons(v, ::lean::filter(*it, [&](V const & v2) { return v != v2; }))); m_map.insert(h, insert_prio(v, ::lean::filter(*it, [&](V const & v2) { return v != v2; })));
else else
m_map.insert(h, to_list(v)); m_map.insert(h, to_list(v));
} }
@ -64,4 +78,13 @@ public:
}); });
} }
}; };
template<typename V>
struct constant_priority_fn { unsigned operator()(V const &) const { return 0; } };
template<typename V>
class head_map : public head_map_prio<V, constant_priority_fn<V>> {
public:
head_map() {}
};
} }