diff --git a/lib/tag_filter.cc b/lib/tag_filter.cc index 10cc7eb..741aa5a 100644 --- a/lib/tag_filter.cc +++ b/lib/tag_filter.cc @@ -9,22 +9,17 @@ #include +#include +#include +#include +#include + #include "base_filter.h" namespace py = pybind11; namespace { -struct Tag -{ - Tag(std::string k, std::string v) - : key(std::move(k)), value(std::move(v)) - {} - - std::string key; - std::string value; -}; - class TagFilter : public pyosmium::BaseFilter { public: @@ -34,7 +29,6 @@ class TagFilter : public pyosmium::BaseFilter throw py::type_error{"Need tags to filter on."}; } - m_tags.reserve(args.size()); for (auto const &arg: args) { if (!py::isinstance(arg)) { throw py::type_error{"Each tag must be a tuple of (key, value)."}; @@ -45,37 +39,46 @@ class TagFilter : public pyosmium::BaseFilter || !py::isinstance(tag[1])) { throw py::type_error{"Each tag must be a tuple of (key, value)."}; } - m_tags.emplace_back(tag[0].cast(), - tag[1].cast()); + m_tags[tag[0].cast()].insert(tag[1].cast()); } } bool filter(osmium::OSMObject const *o) override { - auto const &tags = o->tags(); - for (auto const &tag: m_tags) { - if (tags.has_tag(tag.key.c_str(), tag.value.c_str())) { - return false; - } - } - - return true; + return !has_matching_tag(o->tags()); } bool filter_changeset(pyosmium::PyOSMChangeset &o) override { - auto const &tags = o.get()->tags(); - for (auto const &tag: m_tags) { - if (tags.has_tag(tag.key.c_str(), tag.value.c_str())) { - return false; + return !has_matching_tag(o.get()->tags()); + } + +private: + bool has_matching_tag(osmium::TagList const &tags) const + { + // With a single key, looking it up directly is faster than + // walking all tags of the object. + if (m_tags.size() == 1) { + auto const &[key, values] = *m_tags.begin(); + char const *value = tags.get_value_by_key(key.c_str()); + return value && values.count(std::string_view{value}) > 0; + } + + for (auto const &tag: tags) { + auto const it = m_tags.find(std::string_view{tag.key()}); + if (it != m_tags.end() + && it->second.count(std::string_view{tag.value()}) > 0 + // Like TagList::has_tag(), only the first tag with a key counts. + && tags.get_value_by_key(tag.key()) == tag.value()) { + return true; } } - return true; + return false; } -private: - std::vector m_tags; + // std::less<> allows lookups by string_view without copying. + std::map>, std::less<>> m_tags; }; } diff --git a/test/test_tag_filter.py b/test/test_tag_filter.py index 9985aee..9b46a16 100644 --- a/test/test_tag_filter.py +++ b/test/test_tag_filter.py @@ -25,7 +25,9 @@ def test_tag_filter_bad_arguments(kv): @pytest.mark.parametrize('tags,filt', [('foo=bar', [('foo', 'bar')]), ('a=1,b=2', [('x', 'x'), ('a', '1')]), - ('a=1,b=2', [('x', 'x'), ('b', '2')]) + ('a=1,b=2', [('x', 'x'), ('b', '2')]), + ('a=1,b=2', [('a', '0'), ('a', '1'), ('a', '3')]), + ('a=1,b=2', [('a', '2'), ('b', '2')]) ]) def test_tag_filter_pass(opl_reader, tags, filt): data = f"""\ @@ -46,7 +48,10 @@ def test_tag_filter_pass(opl_reader, tags, filt): @pytest.mark.parametrize('tags,filt', [('foo=bar', [('foo', 'bars')]), - ('a=1,b=2', [('x', 'x'), ('a', '2')]) + ('a=1,b=2', [('x', 'x'), ('a', '2')]), + ('a=1,b=2', [('a', '2')]), + ('a=1,a=2', [('a', '2')]), + ('a=1,a=2', [('a', '2'), ('x', 'x')]) ]) def test_tag_filter_fail(opl_reader, tags, filt): data = f"""\