From f6e9ba4eb206c6e23db24f6f0ba5375009b8efba Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Wed, 6 Oct 2021 18:26:25 -0600 Subject: [PATCH] qbsp: drop mapentity_t::epair_order and std::map for epairs just store as a std::vector of pairs. Lookups aren't done much in qbsp so linear search performance should be fine. epair_order approach gets messy because we sometimes add epairs --- include/qbsp/map.hh | 7 +++---- qbsp/map.cc | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index 6e811f0d..39d12887 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -26,6 +26,7 @@ #include #include +#include #include struct mapface_t @@ -92,10 +93,8 @@ public: brush_t *solid = nullptr, *sky = nullptr, *detail = nullptr, *detail_illusionary = nullptr, *detail_fence = nullptr, *liquid = nullptr; - // tree of key/value pairs - std::map epairs; - // order of parse - std::vector epair_order; + // key/value pairs in the order they were parsed + std::vector> epairs; aabb3d bounds; brush_t *brushes = nullptr; /* NULL terminated list */ diff --git a/qbsp/map.cc b/qbsp/map.cc index 40a187b5..efedeb83 100644 --- a/qbsp/map.cc +++ b/qbsp/map.cc @@ -495,7 +495,8 @@ static void ParseEpair(parser_t &parser, mapentity_t *entity) goto parse_error; { - std::string &value = (entity->epairs[key] = parser.token); + std::string value = parser.token; + SetKeyValue(entity, key.c_str(), value.c_str()); if (!Q_strcasecmp(key.c_str(), "origin")) { GetVectorForKey(entity, key.c_str(), entity->origin); @@ -514,8 +515,6 @@ static void ParseEpair(parser_t &parser, mapentity_t *entity) } } - entity->epair_order.push_back(std::move(key)); - return; } } @@ -2186,8 +2185,8 @@ static void ConvertEntity(std::ofstream &f, const mapentity_t *entity, const con { f << "{\n"; - for (const auto &key : entity->epair_order) { - fmt::print(f, "\"{}\" \"{}\"\n", key, entity->epairs.at(key)); + for (const auto &[key, value] : entity->epairs) { + fmt::print(f, "\"{}\" \"{}\"\n", key, value); } for (int i = 0; i < entity->nummapbrushes; i++) { @@ -2235,17 +2234,23 @@ void PrintEntity(const mapentity_t *entity) const char *ValueForKey(const mapentity_t *entity, const char *key) { - auto it = entity->epairs.find(key); - - if (it == entity->epairs.end()) - return ""; - - return it->second.c_str(); + for (auto &epair : entity->epairs) { + if (!Q_strcasecmp(key, epair.first.c_str())) { + return epair.second.c_str(); + } + } + return ""; } void SetKeyValue(mapentity_t *entity, const char *key, const char *value) { - entity->epairs[key] = value; + for (auto &epair : entity->epairs) { + if (!Q_strcasecmp(key, epair.first.c_str())) { + epair.second = value; + return; + } + } + entity->epairs.emplace_back(key, value); } /**