diff --git a/include/kdbush.hpp b/include/kdbush.hpp index d343b2b..be68ec5 100644 --- a/include/kdbush.hpp +++ b/include/kdbush.hpp @@ -94,6 +94,8 @@ class KDBush { const TIndex right, const std::uint8_t axis) { + if (points.empty()) return; + if (right - left <= nodeSize) { for (auto i = left; i <= right; i++) { const TNumber x = std::get<0>(points[i]); @@ -125,6 +127,8 @@ class KDBush { const TIndex right, const std::uint8_t axis) { + if (points.empty()) return; + const TNumber r2 = r * r; if (right - left <= nodeSize) { diff --git a/test.cpp b/test.cpp index f31a1ab..b9d6e54 100644 --- a/test.cpp +++ b/test.cpp @@ -45,6 +45,12 @@ static void testRadius() { static void testEmpty() { auto emptyPoints = std::vector{}; kdbush::KDBush index(emptyPoints); + // test no crash when using empty index + TIds result; + index.within(50, 50, 20, [&result](const auto id) { result.push_back(id); }); + assert(result.empty()); + index.range(20, 30, 50, 70, [&result](const auto id) { result.push_back(id); }); + assert(result.empty()); } int main() {