From c0709b8c03cc9368bd4769d58de298e31041bca3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 13 Oct 2022 20:04:05 +0100 Subject: Search node network without creating temporary nodes on the heap --- game/network/network.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'game/network/network.cpp') diff --git a/game/network/network.cpp b/game/network/network.cpp index aff1aa4..9683143 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -16,20 +16,23 @@ Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(t NodePtr Network::nodeAt(glm::vec3 pos) { - return *nodes.insert(std::make_shared(pos)).first; + return newNodeAt(pos).first; } std::pair Network::newNodeAt(glm::vec3 pos) { - const auto i = nodes.insert(std::make_shared(pos)); - return {*i.first, i.second}; + const auto [n, i] = candidateNodeAt(pos); + if (!i) { + nodes.insert(n); + } + return {n, !i}; } NodePtr Network::findNodeAt(glm::vec3 pos) const { - if (const auto n = nodes.find(std::make_shared(pos)); n != nodes.end()) { + if (const auto n = nodes.find(pos); n != nodes.end()) { return *n; } return {}; @@ -38,7 +41,7 @@ Network::findNodeAt(glm::vec3 pos) const std::pair Network::candidateNodeAt(glm::vec3 pos) const { - if (const auto n = nodes.find(std::make_shared(pos)); n != nodes.end()) { + if (const auto n = nodes.find(pos); n != nodes.end()) { return {*n, true}; } return {std::make_shared(pos), false}; -- cgit v1.2.3