Skip to content

Commit 23da320

Browse files
committed
optimize C++ implementation
1 parent 4624469 commit 23da320

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

cpp/prtree.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ auto list_list_to_arrays(vec<vec<T>> out_ll)
7777
}
7878
vec<T> out;
7979
out.reserve(sum);
80-
for (const auto &v : out_ll)
81-
out.insert(out.end(), v.begin(), v.end());
80+
for (auto &v : out_ll)
81+
out.insert(out.end(),
82+
std::make_move_iterator(v.begin()),
83+
std::make_move_iterator(v.end()));
8284

8385
return make_tuple(
8486
std::move(as_pyarray(out_s)),
@@ -104,14 +106,14 @@ static const float REBUILD_THRE = 1.25;
104106
#define unlikely(x) (x)
105107
#endif
106108

107-
std::string compress(std::string &data)
109+
inline std::string compress(const std::string &data)
108110
{
109111
std::string output;
110112
snappy::Compress(data.data(), data.size(), &output);
111113
return output;
112114
}
113115

114-
std::string decompress(std::string &data)
116+
inline std::string decompress(const std::string &data)
115117
{
116118
std::string output;
117119
snappy::Uncompress(data.data(), data.size(), &output);
@@ -702,25 +704,22 @@ class PRTreeElement
702704
template <class T, int B = 6, int D = 2>
703705
void bfs(const std::function<void(std::unique_ptr<PRTreeLeaf<T, B, D>> &)> &func, vec<PRTreeElement<T, B, D>> &flat_tree, const BB<D> target)
704706
{
705-
queue<size_t> que;
706-
auto qpush_if_intersect = [&](const size_t &i)
707+
vec<size_t> que;
708+
que.reserve(flat_tree.size());
709+
auto qpush_if_intersect = [&](size_t i)
707710
{
708711
PRTreeElement<T, B, D> &r = flat_tree[i];
709-
// std::cout << "i " << (long int) i << " : " << (bool) r.leaf << std::endl;
710712
if (r(target))
711713
{
712-
// std::cout << " is pushed" << std::endl;
713-
que.emplace(i);
714+
que.push_back(i);
714715
}
715716
};
716717

717-
// std::cout << "size: " << flat_tree.size() << std::endl;
718718
qpush_if_intersect(0);
719-
while (!que.empty())
719+
size_t qhead = 0;
720+
while (qhead < que.size())
720721
{
721-
size_t idx = que.front();
722-
// std::cout << "idx: " << (long int) idx << std::endl;
723-
que.pop();
722+
size_t idx = que[qhead++];
724723
PRTreeElement<T, B, D> &elem = flat_tree[idx];
725724

726725
if (elem.leaf)
@@ -733,7 +732,8 @@ void bfs(const std::function<void(std::unique_ptr<PRTreeLeaf<T, B, D>> &)> &func
733732
for (size_t offset = 0; offset < B; offset++)
734733
{
735734
size_t jdx = idx * B + offset + 1;
736-
qpush_if_intersect(jdx);
735+
if (jdx < flat_tree.size())
736+
qpush_if_intersect(jdx);
737737
}
738738
}
739739
}

0 commit comments

Comments
 (0)