@@ -93,14 +93,11 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
93
93
. collect ( ) )
94
94
}
95
95
96
- pub fn within < F > ( & self , point : & [ A ] , radius : A , distance : & F ) -> Result < Vec < ( A , & T ) > , ErrorKind >
96
+ #[ inline( always) ]
97
+ fn evaluated_heap < F > ( & self , point : & [ A ] , radius : A , distance : & F ) -> BinaryHeap < HeapElement < A , & T > >
97
98
where
98
99
F : Fn ( & [ A ] , & [ A ] ) -> A ,
99
100
{
100
- self . check_point ( point) ?;
101
- if self . size == 0 {
102
- return Ok ( vec ! [ ] ) ;
103
- }
104
101
let mut pending = BinaryHeap :: new ( ) ;
105
102
let mut evaluated = BinaryHeap :: < HeapElement < A , & T > > :: new ( ) ;
106
103
pending. push ( HeapElement {
@@ -110,6 +107,18 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
110
107
while !pending. is_empty ( ) && ( -pending. peek ( ) . unwrap ( ) . distance <= radius) {
111
108
self . nearest_step ( point, self . size , radius, distance, & mut pending, & mut evaluated) ;
112
109
}
110
+ evaluated
111
+ }
112
+
113
+ pub fn within < F > ( & self , point : & [ A ] , radius : A , distance : & F ) -> Result < Vec < ( A , & T ) > , ErrorKind >
114
+ where
115
+ F : Fn ( & [ A ] , & [ A ] ) -> A ,
116
+ {
117
+ self . check_point ( point) ?;
118
+ if self . size == 0 {
119
+ return Ok ( vec ! [ ] ) ;
120
+ }
121
+ let evaluated = self . evaluated_heap ( point, radius, distance) ;
113
122
Ok ( evaluated. into_sorted_vec ( ) . into_iter ( ) . map ( Into :: into) . collect ( ) )
114
123
}
115
124
@@ -121,15 +130,7 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
121
130
if self . size == 0 {
122
131
return Ok ( vec ! [ ] ) ;
123
132
}
124
- let mut pending = BinaryHeap :: new ( ) ;
125
- let mut evaluated = BinaryHeap :: < HeapElement < A , & T > > :: new ( ) ;
126
- pending. push ( HeapElement {
127
- distance : A :: zero ( ) ,
128
- element : self ,
129
- } ) ;
130
- while !pending. is_empty ( ) && ( -pending. peek ( ) . unwrap ( ) . distance <= radius) {
131
- self . nearest_step ( point, self . size , radius, distance, & mut pending, & mut evaluated) ;
132
- }
133
+ let evaluated = self . evaluated_heap ( point, radius, distance) ;
133
134
Ok ( evaluated. into_iter ( ) . map ( Into :: into) . collect ( ) )
134
135
}
135
136
@@ -141,15 +142,7 @@ impl<A: Float + Zero + One, T: std::cmp::PartialEq, U: AsRef<[A]> + std::cmp::Pa
141
142
if self . size == 0 {
142
143
return Ok ( 0 ) ;
143
144
}
144
- let mut pending = BinaryHeap :: new ( ) ;
145
- let mut evaluated = BinaryHeap :: < HeapElement < A , & T > > :: new ( ) ;
146
- pending. push ( HeapElement {
147
- distance : A :: zero ( ) ,
148
- element : self ,
149
- } ) ;
150
- while !pending. is_empty ( ) && ( -pending. peek ( ) . unwrap ( ) . distance <= radius) {
151
- self . nearest_step ( point, self . size , radius, distance, & mut pending, & mut evaluated) ;
152
- }
145
+ let evaluated = self . evaluated_heap ( point, radius, distance) ;
153
146
Ok ( evaluated. len ( ) )
154
147
}
155
148
0 commit comments