From 34d25673f9c8ed01a74c388363ed16522c965050 Mon Sep 17 00:00:00 2001 From: autumnal Date: Fri, 26 Nov 2021 11:08:40 +0100 Subject: [PATCH] solve #20: clean up trait usage in `kd_tree` + replace `core::ops::*` by `num::traits::Num` + remove unnecessary trait bounds like `Sized` + break long comment lines --- kd_tree/src/lib.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/kd_tree/src/lib.rs b/kd_tree/src/lib.rs index 5a861aa..72318d6 100644 --- a/kd_tree/src/lib.rs +++ b/kd_tree/src/lib.rs @@ -2,14 +2,13 @@ #![allow(clippy::too_many_arguments)] use core::cmp::Ordering; use core::fmt::Debug; -use core::ops::{Add, Mul, Sub}; -use num::traits::Zero; +use num::traits::Num; -pub struct Tree { +pub struct Tree { pub nodes: [Node; SIZE], } -impl +impl Tree { pub const fn new(nodes: [Node; SIZE]) -> Tree { @@ -18,17 +17,8 @@ impl - + Add - + Mul - + Zero - + Copy - + Debug - + Default, - V: Sized, + T: PartialOrd + Num + Copy + Debug, + V, const SIZE: usize, const DIM: usize, const MAX_LEVEL: usize, @@ -41,7 +31,8 @@ impl< .expect("This can not fail, expect if the Tree got no nodes: SIZE == 0"); let mut index = 0usize; let mut level = 0usize; - // Store which child nodes where visited for the Node on the index/level and if it was already compared + // Store which child nodes where visited for the Node on the index/level and if it was + // already compared let mut visited: [(Visited, bool); MAX_LEVEL] = [(Visited::None, false); MAX_LEVEL]; //Initialise best node/distance with root node let mut best_distance: T = euclid(&node.val, point); @@ -52,7 +43,8 @@ impl< self.search_down(point, &mut node, &mut index, &mut level, &mut visited); // Go up until we either reach the top or we go down by one - // Should we go down by one, we will need to go to the best fit leaf of the current subtree + // Should we go down by one, we will need to go to the best fit leaf of the current + // subtree self.search_up( point, &mut node, @@ -242,7 +234,7 @@ pub struct Node { v: V, } -impl Node { +impl Node { pub fn val(&self) -> &[T; DIM] { &self.val } @@ -258,7 +250,7 @@ impl Node { pub fn euclid(left: &[T; SIZE], right: &[T; SIZE]) -> T where - T: Default + Add + Sub + Mul + Zero + Copy + Debug, + T: Num + Copy + Debug, { left.iter() .zip(right.iter())