From 23da320fabf12f945deb4a9975ebcc908f97d0c8 Mon Sep 17 00:00:00 2001 From: atksh <31435204+atksh@users.noreply.github.com> Date: Sat, 17 May 2025 19:00:14 +0900 Subject: [PATCH] optimize C++ implementation --- cpp/prtree.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/cpp/prtree.h b/cpp/prtree.h index 8976963..c74aedc 100644 --- a/cpp/prtree.h +++ b/cpp/prtree.h @@ -77,8 +77,10 @@ auto list_list_to_arrays(vec> out_ll) } vec out; out.reserve(sum); - for (const auto &v : out_ll) - out.insert(out.end(), v.begin(), v.end()); + for (auto &v : out_ll) + out.insert(out.end(), + std::make_move_iterator(v.begin()), + std::make_move_iterator(v.end())); return make_tuple( std::move(as_pyarray(out_s)), @@ -104,14 +106,14 @@ static const float REBUILD_THRE = 1.25; #define unlikely(x) (x) #endif -std::string compress(std::string &data) +inline std::string compress(const std::string &data) { std::string output; snappy::Compress(data.data(), data.size(), &output); return output; } -std::string decompress(std::string &data) +inline std::string decompress(const std::string &data) { std::string output; snappy::Uncompress(data.data(), data.size(), &output); @@ -702,25 +704,22 @@ class PRTreeElement template void bfs(const std::function> &)> &func, vec> &flat_tree, const BB target) { - queue que; - auto qpush_if_intersect = [&](const size_t &i) + vec que; + que.reserve(flat_tree.size()); + auto qpush_if_intersect = [&](size_t i) { PRTreeElement &r = flat_tree[i]; - // std::cout << "i " << (long int) i << " : " << (bool) r.leaf << std::endl; if (r(target)) { - // std::cout << " is pushed" << std::endl; - que.emplace(i); + que.push_back(i); } }; - // std::cout << "size: " << flat_tree.size() << std::endl; qpush_if_intersect(0); - while (!que.empty()) + size_t qhead = 0; + while (qhead < que.size()) { - size_t idx = que.front(); - // std::cout << "idx: " << (long int) idx << std::endl; - que.pop(); + size_t idx = que[qhead++]; PRTreeElement &elem = flat_tree[idx]; if (elem.leaf) @@ -733,7 +732,8 @@ void bfs(const std::function> &)> &func for (size_t offset = 0; offset < B; offset++) { size_t jdx = idx * B + offset + 1; - qpush_if_intersect(jdx); + if (jdx < flat_tree.size()) + qpush_if_intersect(jdx); } } }