Skip to content

Commit

Permalink
source credits and whitespace curly readability
Browse files Browse the repository at this point in the history
  • Loading branch information
travissanderson-wf committed Aug 2, 2023
1 parent ff2b0fd commit db0c2d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 20 additions & 8 deletions lib/src/r_tree/quickselect.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -65,6 +62,7 @@ void _quickSelectStep<T>(List<T> arr, int k, int left, int right, Comparator<T>
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);
}

Expand All @@ -73,14 +71,24 @@ void _quickSelectStep<T>(List<T> arr, int k, int left, int right, Comparator<T>
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) {
Expand All @@ -90,8 +98,12 @@ void _quickSelectStep<T>(List<T> arr, int k, int left, int right, Comparator<T>
_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;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/r_tree/r_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class RTree<E> {
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<RTreeDatum<E>> items) {
if (items.isEmpty) {
return this;
Expand Down

0 comments on commit db0c2d2

Please sign in to comment.