Skip to content

Commit

Permalink
cargo fmt (mrhooray#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhooray authored Dec 15, 2022
1 parent ff080e8 commit 2514671
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 135 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ env:
CARGO_TERM_COLOR: always

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.56.0
- stable
steps:
- uses: actions/checkout@v3
- name: Run Tests
run: cargo fmt --all --check
build:
runs-on: ubuntu-latest
strategy:
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width=120
90 changes: 22 additions & 68 deletions src/kdtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ pub enum ErrorKind {
ZeroCapacity,
}

impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::PartialEq>
KdTree<A, T, U>
{
impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::PartialEq> KdTree<A, T, U> {
/// Create a new KD tree, specifying the dimension size of each point
pub fn new(dims: usize) -> Self {
KdTree::with_capacity(dims, 2_usize.pow(4))
Expand Down Expand Up @@ -63,12 +61,7 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
self.size
}

pub fn nearest<F>(
&self,
point: &[A],
num: usize,
distance: &F,
) -> Result<Vec<(A, &T)>, ErrorKind>
pub fn nearest<F>(&self, point: &[A], num: usize, distance: &F) -> Result<Vec<(A, &T)>, ErrorKind>
where
F: Fn(&[A], &[A]) -> A,
{
Expand All @@ -86,17 +79,9 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
element: self,
});
while !pending.is_empty()
&& (evaluated.len() < num
|| (-pending.peek().unwrap().distance <= evaluated.peek().unwrap().distance))
&& (evaluated.len() < num || (-pending.peek().unwrap().distance <= evaluated.peek().unwrap().distance))
{
self.nearest_step(
point,
num,
A::infinity(),
distance,
&mut pending,
&mut evaluated,
);
self.nearest_step(point, num, A::infinity(), distance, &mut pending, &mut evaluated);
}
Ok(evaluated
.into_sorted_vec()
Expand All @@ -123,20 +108,9 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
element: self,
});
while !pending.is_empty() && (-pending.peek().unwrap().distance <= radius) {
self.nearest_step(
point,
self.size,
radius,
distance,
&mut pending,
&mut evaluated,
);
self.nearest_step(point, self.size, radius, distance, &mut pending, &mut evaluated);
}
Ok(evaluated
.into_sorted_vec()
.into_iter()
.map(Into::into)
.collect())
Ok(evaluated.into_sorted_vec().into_iter().map(Into::into).collect())
}

fn nearest_step<'b, F>(
Expand Down Expand Up @@ -170,12 +144,8 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
candidate = curr.left.as_ref().unwrap();
curr = curr.right.as_ref().unwrap();
}
let candidate_to_space = util::distance_to_space(
point,
&*candidate.min_bounds,
&*candidate.max_bounds,
distance,
);
let candidate_to_space =
util::distance_to_space(point, &*candidate.min_bounds, &*candidate.max_bounds, distance);
if candidate_to_space <= evaluated_dist {
pending.push(HeapElement {
distance: candidate_to_space * -A::one(),
Expand Down Expand Up @@ -420,8 +390,7 @@ pub struct NearestIter<
distance: &'a F,
}

impl<'a, 'b, A: Float + Zero + One, T: 'b, U: 'b + AsRef<[A]>, F: 'a> Iterator
for NearestIter<'a, 'b, A, T, U, F>
impl<'a, 'b, A: Float + Zero + One, T: 'b, U: 'b + AsRef<[A]>, F: 'a> Iterator for NearestIter<'a, 'b, A, T, U, F>
where
F: Fn(&[A], &[A]) -> A,
U: PartialEq,
Expand All @@ -434,8 +403,7 @@ where
let distance = self.distance;
let point = self.point;
while !self.pending.is_empty()
&& (self.evaluated.peek().map_or(A::infinity(), |x| -x.distance)
>= -self.pending.peek().unwrap().distance)
&& (self.evaluated.peek().map_or(A::infinity(), |x| -x.distance) >= -self.pending.peek().unwrap().distance)
{
let mut curr = &*self.pending.pop().unwrap().element;
while !curr.is_leaf() {
Expand All @@ -448,22 +416,16 @@ where
curr = curr.right.as_ref().unwrap();
}
self.pending.push(HeapElement {
distance: -distance_to_space(
point,
&*candidate.min_bounds,
&*candidate.max_bounds,
distance,
),
distance: -distance_to_space(point, &*candidate.min_bounds, &*candidate.max_bounds, distance),
element: &**candidate,
});
}
let points = curr.points.as_ref().unwrap().iter();
let bucket = curr.bucket.as_ref().unwrap().iter();
self.evaluated
.extend(points.zip(bucket).map(|(p, d)| HeapElement {
distance: -distance(point, p.as_ref()),
element: d,
}));
self.evaluated.extend(points.zip(bucket).map(|(p, d)| HeapElement {
distance: -distance(point, p.as_ref()),
element: d,
}));
}
self.evaluated.pop().map(|x| (-x.distance, x.element))
}
Expand All @@ -483,8 +445,7 @@ pub struct NearestIterMut<
distance: &'a F,
}

impl<'a, 'b, A: Float + Zero + One, T: 'b, U: 'b + AsRef<[A]>, F: 'a> Iterator
for NearestIterMut<'a, 'b, A, T, U, F>
impl<'a, 'b, A: Float + Zero + One, T: 'b, U: 'b + AsRef<[A]>, F: 'a> Iterator for NearestIterMut<'a, 'b, A, T, U, F>
where
F: Fn(&[A], &[A]) -> A,
U: PartialEq,
Expand All @@ -497,8 +458,7 @@ where
let distance = self.distance;
let point = self.point;
while !self.pending.is_empty()
&& (self.evaluated.peek().map_or(A::infinity(), |x| -x.distance)
>= -self.pending.peek().unwrap().distance)
&& (self.evaluated.peek().map_or(A::infinity(), |x| -x.distance) >= -self.pending.peek().unwrap().distance)
{
let mut curr = &mut *self.pending.pop().unwrap().element;
while !curr.is_leaf() {
Expand All @@ -511,22 +471,16 @@ where
curr = curr.right.as_mut().unwrap();
}
self.pending.push(HeapElement {
distance: -distance_to_space(
point,
&*candidate.min_bounds,
&*candidate.max_bounds,
distance,
),
distance: -distance_to_space(point, &*candidate.min_bounds, &*candidate.max_bounds, distance),
element: &mut **candidate,
});
}
let points = curr.points.as_ref().unwrap().iter();
let bucket = curr.bucket.as_mut().unwrap().iter_mut();
self.evaluated
.extend(points.zip(bucket).map(|(p, d)| HeapElement {
distance: -distance(point, p.as_ref()),
element: d,
}));
self.evaluated.extend(points.zip(bucket).map(|(p, d)| HeapElement {
distance: -distance(point, p.as_ref()),
element: d,
}));
}
self.evaluated.pop().map(|x| (-x.distance, x.element))
}
Expand Down
7 changes: 1 addition & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ mod tests {

#[test]
fn test_distance_outside_inf() {
let dis = distance_to_space(
&[0.0, 0.0],
&[1.0, 1.0],
&[INFINITY, INFINITY],
&squared_euclidean,
);
let dis = distance_to_space(&[0.0, 0.0], &[1.0, 1.0], &[INFINITY, INFINITY], &squared_euclidean);
assert_eq!(dis, 2.0);
}

Expand Down
77 changes: 16 additions & 61 deletions tests/kdtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ fn it_works() {
kdtree.add(&POINT_D.0, POINT_D.1).unwrap();

assert_eq!(kdtree.size(), 4);
assert_eq!(
kdtree.nearest(&POINT_A.0, 0, &squared_euclidean).unwrap(),
vec![]
);
assert_eq!(kdtree.nearest(&POINT_A.0, 0, &squared_euclidean).unwrap(), vec![]);
assert_eq!(
kdtree.nearest(&POINT_A.0, 1, &squared_euclidean).unwrap(),
vec![(0f64, &0)]
Expand Down Expand Up @@ -99,10 +96,7 @@ fn it_works_with_vec() {
kdtree.add(vec![3.0; 2], 3).unwrap();

assert_eq!(kdtree.size(), 4);
assert_eq!(
kdtree.nearest(&POINT_A.0, 0, &squared_euclidean).unwrap(),
vec![]
);
assert_eq!(kdtree.nearest(&POINT_A.0, 0, &squared_euclidean).unwrap(), vec![]);
assert_eq!(
kdtree.nearest(&POINT_A.0, 1, &squared_euclidean).unwrap(),
vec![(0f64, &0)]
Expand Down Expand Up @@ -133,25 +127,16 @@ fn it_works_with_vec() {
fn handles_zero_capacity() {
let mut kdtree = KdTree::with_capacity(2, 0);

assert_eq!(
kdtree.add(&POINT_A.0, POINT_A.1),
Err(ErrorKind::ZeroCapacity)
);
assert_eq!(
kdtree.nearest(&POINT_A.0, 1, &squared_euclidean).unwrap(),
vec![]
);
assert_eq!(kdtree.add(&POINT_A.0, POINT_A.1), Err(ErrorKind::ZeroCapacity));
assert_eq!(kdtree.nearest(&POINT_A.0, 1, &squared_euclidean).unwrap(), vec![]);
}

#[test]
fn handles_wrong_dimension() {
let point = ([0f64], 0f64);
let mut kdtree = KdTree::with_capacity(2, 1);

assert_eq!(
kdtree.add(&point.0, point.1),
Err(ErrorKind::WrongDimension)
);
assert_eq!(kdtree.add(&point.0, point.1), Err(ErrorKind::WrongDimension));
assert_eq!(
kdtree.nearest(&point.0, 1, &squared_euclidean),
Err(ErrorKind::WrongDimension)
Expand All @@ -164,14 +149,8 @@ fn handles_non_finite_coordinate() {
let point_b = ([std::f64::INFINITY, std::f64::INFINITY], 0f64);
let mut kdtree = KdTree::with_capacity(2, 1);

assert_eq!(
kdtree.add(&point_a.0, point_a.1),
Err(ErrorKind::NonFiniteCoordinate)
);
assert_eq!(
kdtree.add(&point_b.0, point_b.1),
Err(ErrorKind::NonFiniteCoordinate)
);
assert_eq!(kdtree.add(&point_a.0, point_a.1), Err(ErrorKind::NonFiniteCoordinate));
assert_eq!(kdtree.add(&point_b.0, point_b.1), Err(ErrorKind::NonFiniteCoordinate));
assert_eq!(
kdtree.nearest(&point_a.0, 1, &squared_euclidean),
Err(ErrorKind::NonFiniteCoordinate)
Expand Down Expand Up @@ -245,10 +224,7 @@ fn handles_pending_order() {
vec![(16.0, &4), (36.0, &3), (2401.0, &2), (2601.0, &1)]
);

assert_eq!(
kdtree.within(&[50f64], 1.0, &squared_euclidean).unwrap(),
vec![]
);
assert_eq!(kdtree.within(&[50f64], 1.0, &squared_euclidean).unwrap(), vec![]);
assert_eq!(
kdtree.within(&[50f64], 25.0, &squared_euclidean).unwrap(),
vec![(25.0, &3), (25.0, &4)]
Expand Down Expand Up @@ -332,14 +308,8 @@ fn handles_remove_correctly() {
kdtree.add(&item4.0, item4.1).unwrap();

let num_removed = kdtree.remove(&&item3.0, &item3.1).unwrap();
assert_eq!(
kdtree.size(),
3
);
assert_eq!(
num_removed,
1
);
assert_eq!(kdtree.size(), 3);
assert_eq!(num_removed, 1);
assert_eq!(
kdtree.nearest(&[51f64], 2, &squared_euclidean).unwrap(),
vec![(16.0, &4), (2401.0, &2)]
Expand All @@ -363,19 +333,10 @@ fn handles_remove_multiple_match() {
kdtree.add(&item3.0, item3.1).unwrap();
kdtree.add(&item4.0, item4.1).unwrap();

assert_eq!(
kdtree.size(),
4
);
assert_eq!(kdtree.size(), 4);
let num_removed = kdtree.remove(&&[0f64], &1).unwrap();
assert_eq!(
kdtree.size(),
2
);
assert_eq!(
num_removed,
2
);
assert_eq!(kdtree.size(), 2);
assert_eq!(num_removed, 2);
assert_eq!(
kdtree.nearest(&[45f64], 1, &squared_euclidean).unwrap(),
vec![(0.0, &3)]
Expand All @@ -400,16 +361,10 @@ fn handles_remove_no_match() {
kdtree.add(&item4.0, item4.1).unwrap();

let num_removed = kdtree.remove(&&[1f64], &2).unwrap();
assert_eq!(
kdtree.size(),
4
);
assert_eq!(
num_removed,
0
);
assert_eq!(kdtree.size(), 4);
assert_eq!(num_removed, 0);
assert_eq!(
kdtree.nearest(&[51f64], 2, &squared_euclidean).unwrap(),
vec![(16.0, &4), (36.0, &3)]
);
}
}

0 comments on commit 2514671

Please sign in to comment.