diff --git a/lib/src/r_tree/quickselect.dart b/lib/src/r_tree/quickselect.dart index f471a3c..29e4718 100644 --- a/lib/src/r_tree/quickselect.dart +++ b/lib/src/r_tree/quickselect.dart @@ -1,8 +1,5 @@ part of r_tree; - -// Copyright (c) 2021 Ilya Zverev, (c) 2018 Vladimir Agafonkin. // Port of https://github.com/mourner/quickselect. -// Use of this code is governed by an ISC license, see the LICENSE file. // sort an array so that items come in groups of n unsorted items, with groups sorted between each other; // combines selection algorithm with binary divide & conquer approach @@ -65,6 +62,7 @@ void _quickSelectStep(List arr, int k, int left, int right, Comparator final sd = 0.5 * sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1); final newLeft = max(left, (k - m * s / n + sd).floor()); final newRight = min(right, (k + (n - m) * s / n + sd).floor()); + _quickSelectStep(arr, k, newLeft, newRight, compare); } @@ -73,14 +71,24 @@ void _quickSelectStep(List arr, int k, int left, int right, Comparator var j = right; _swap(arr, left, k); - if (compare(arr[right], t) > 0) _swap(arr, left, right); + + if (compare(arr[right], t) > 0) { + _swap(arr, left, right); + } while (i < j) { _swap(arr, i, j); + i++; j--; - while (compare(arr[i], t) < 0) i++; - while (compare(arr[j], t) > 0) j--; + + while (compare(arr[i], t) < 0) { + i++; + } + + while (compare(arr[j], t) > 0) { + j--; + } } if (compare(arr[left], t) == 0) { @@ -90,8 +98,12 @@ void _quickSelectStep(List arr, int k, int left, int right, Comparator _swap(arr, j, right); } - if (j <= k) left = j + 1; - if (k <= j) right = j - 1; + if (j <= k) { + left = j + 1; + } + if (k <= j) { + right = j - 1; + } } } diff --git a/lib/src/r_tree/r_tree.dart b/lib/src/r_tree/r_tree.dart index 404e286..263fd3a 100644 --- a/lib/src/r_tree/r_tree.dart +++ b/lib/src/r_tree/r_tree.dart @@ -62,7 +62,8 @@ class RTree { return _root.search(searchRect, shouldInclude); } - /// Bulk adds all [items] to the rtree + /// Bulk adds all [items] to the rtree. This implementation draws heavily from + /// https://github.com/mourner/rbush and https://github.com/Zverik/dart_rbush. load(List> items) { if (items.isEmpty) { return this;