From ee081ca70322cd01d0642fdb1860e6bfbfb48e85 Mon Sep 17 00:00:00 2001 From: steven-vd Date: Sun, 16 Aug 2026 15:00:32 +0200 Subject: Initial commit --- benchmarks/RapidXML/count_highway_crossings.cpp | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 benchmarks/RapidXML/count_highway_crossings.cpp (limited to 'benchmarks/RapidXML/count_highway_crossings.cpp') diff --git a/benchmarks/RapidXML/count_highway_crossings.cpp b/benchmarks/RapidXML/count_highway_crossings.cpp new file mode 100644 index 0000000..6d0ecf6 --- /dev/null +++ b/benchmarks/RapidXML/count_highway_crossings.cpp @@ -0,0 +1,84 @@ +#include "rapidxml.hpp" +#include +#include + +namespace rx = rapidxml; + +inline bool is_highway_crossing(rx::xml_node<>* tag_node) { + rx::xml_attribute<>* k = tag_node->first_attribute(); + if (!k) return false; + + rx::xml_attribute<>* v = k->next_attribute(); + if (!v) return false; + + return std::strcmp(k->value(), "highway") == 0 && + std::strcmp(v->value(), "crossing") == 0; +} + +size_t count_highway_crossings(char* buffer, size_t len) { + rx::xml_document<> doc; + + try { + doc.parse(buffer); + } catch (const rx::parse_error&) { + return 0; + } + + size_t count = 0; + + rx::xml_node<>* osm = doc.first_node("osm"); + if (!osm) + return count; + + for (rx::xml_node<>* node = osm->first_node("node"); + node; + node = node->next_sibling("node")) { + + for (rx::xml_node<>* tag = node->first_node("tag"); + tag; + tag = tag->next_sibling("tag")) { + + if (is_highway_crossing(tag)) + ++count; + } + } + + return count; +} + +size_t count_highway_crossings_cheat_and_early_out(char* buffer, size_t len) { + rx::xml_document<> doc; + + try { + doc.parse(buffer); + } catch (const rx::parse_error&) { + return 0; + } + + size_t count = 0; + + rx::xml_node<>* osm = doc.first_node("osm"); + if (!osm) + return count; + + // Equivalent to iterating over all children and stopping at "way". + // next_sibling() avoids inspecting unrelated nodes between elements. + for (rx::xml_node<>* top = osm->first_node(); + top; + top = top->next_sibling()) { + + if (std::strcmp(top->name(), "way") == 0) + break; + + for (rx::xml_node<>* tag = top->first_node("tag"); + tag; + tag = tag->next_sibling("tag")) { + + if (is_highway_crossing(tag)) + ++count; + } + } + + return count; +} + -- cgit v1.2.3