Skip to content

Commit 66bc977

Browse files
felix-clarkLukeMathWalker
authored andcommitted
most clippy warnings and a bugfix (#59)
1 parent a681d56 commit 66bc977

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

src/entropy.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ where
133133
where
134134
A: Float,
135135
{
136-
if self.len() == 0 {
136+
if self.is_empty() {
137137
Err(EmptyInput)
138138
} else {
139139
let entropy = -self
@@ -154,7 +154,7 @@ where
154154
A: Float,
155155
S2: Data<Elem = A>,
156156
{
157-
if self.len() == 0 {
157+
if self.is_empty() {
158158
return Err(MultiInputError::EmptyInput);
159159
}
160160
if self.shape() != q.shape() {
@@ -187,7 +187,7 @@ where
187187
S2: Data<Elem = A>,
188188
A: Float,
189189
{
190-
if self.len() == 0 {
190+
if self.is_empty() {
191191
return Err(MultiInputError::EmptyInput);
192192
}
193193
if self.shape() != q.shape() {

src/histogram/bins.rs

+40
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ impl<A: Ord> Edges<A> {
123123
self.edges.len()
124124
}
125125

126+
/// Returns `true` if `self` contains no edges.
127+
///
128+
/// # Example:
129+
///
130+
/// ```
131+
/// use ndarray_stats::histogram::Edges;
132+
/// use noisy_float::types::{N64, n64};
133+
///
134+
/// let edges = Edges::<N64>::from(vec![]);
135+
/// assert_eq!(edges.is_empty(), true);
136+
/// let edges = Edges::from(vec![n64(0.), n64(2.), n64(5.)]);
137+
/// assert_eq!(edges.is_empty(), false);
138+
/// ```
139+
pub fn is_empty(&self) -> bool {
140+
self.edges.is_empty()
141+
}
142+
126143
/// Borrow an immutable reference to the edges as a 1-dimensional
127144
/// array view.
128145
///
@@ -240,6 +257,29 @@ impl<A: Ord> Bins<A> {
240257
}
241258
}
242259

260+
/// Returns `true` if the number of bins is zero, or in other words, if the
261+
/// number of edges is 0 or 1.
262+
///
263+
/// # Example:
264+
///
265+
/// ```
266+
/// use ndarray_stats::histogram::{Edges, Bins};
267+
/// use noisy_float::types::{N64, n64};
268+
///
269+
/// let edges = Edges::<N64>::from(vec![]);
270+
/// let bins = Bins::new(edges);
271+
/// assert_eq!(bins.is_empty(), true);
272+
/// let edges = Edges::from(vec![n64(0.)]);
273+
/// let bins = Bins::new(edges);
274+
/// assert_eq!(bins.is_empty(), true);
275+
/// let edges = Edges::from(vec![n64(0.), n64(1.), n64(3.)]);
276+
/// let bins = Bins::new(edges);
277+
/// assert_eq!(bins.is_empty(), false);
278+
/// ```
279+
pub fn is_empty(&self) -> bool {
280+
self.len() == 0
281+
}
282+
243283
/// Given `value`, it returns:
244284
/// - `Some(i)`, if the `i`-th bin in `self` contains `value`;
245285
/// - `None`, if `value` does not belong to any of the bins in `self`.

src/histogram/strategies.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ where
171171
fn build(&self) -> Bins<T> {
172172
let n_bins = self.n_bins();
173173
let mut edges: Vec<T> = vec![];
174-
for i in 0..(n_bins + 1) {
174+
for i in 0..=n_bins {
175175
let edge = self.min.clone() + T::from_usize(i).unwrap() * self.bin_width.clone();
176176
edges.push(edge);
177177
}
@@ -185,7 +185,7 @@ where
185185
max_edge = max_edge + self.bin_width.clone();
186186
n_bins += 1;
187187
}
188-
return n_bins;
188+
n_bins
189189
}
190190

191191
fn bin_width(&self) -> T {
@@ -361,8 +361,7 @@ where
361361
{
362362
fn compute_bin_width(n_bins: usize, iqr: T) -> T {
363363
let denominator = (n_bins as f64).powf(1. / 3.);
364-
let bin_width = T::from_usize(2).unwrap() * iqr / T::from_f64(denominator).unwrap();
365-
bin_width
364+
T::from_usize(2).unwrap() * iqr / T::from_f64(denominator).unwrap()
366365
}
367366

368367
/// The bin width (or bin length) according to the fitted strategy.
@@ -449,8 +448,7 @@ where
449448
T: Ord + Clone + FromPrimitive + NumOps + Zero,
450449
{
451450
let range = max.clone() - min.clone();
452-
let bin_width = range / T::from_usize(n_bins).unwrap();
453-
bin_width
451+
range / T::from_usize(n_bins).unwrap()
454452
}
455453

456454
#[cfg(test)]

src/maybe_nan/impl_not_none.rs

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ impl<T: PartialEq> PartialEq for NotNone<T> {
3535
fn eq(&self, other: &Self) -> bool {
3636
self.deref().eq(other)
3737
}
38-
fn ne(&self, other: &Self) -> bool {
39-
self.deref().eq(other)
40-
}
4138
}
4239

4340
impl<T: Ord> Ord for NotNone<T> {

src/maybe_nan/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub trait MaybeNan: Sized {
4343
///
4444
/// This modifies the input view by moving elements as necessary.
4545
fn remove_nan_mut<A: MaybeNan>(mut view: ArrayViewMut1<'_, A>) -> ArrayViewMut1<'_, A> {
46-
if view.len() == 0 {
46+
if view.is_empty() {
4747
return view.slice_move(s![..0]);
4848
}
4949
let mut i = 0;

src/summary_statistics/means.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ where
288288
{
289289
let n_elements =
290290
A::from_usize(a.len()).expect("Converting number of elements to `A` must not fail");
291-
let order = order as i32;
291+
let order = i32::from(order);
292292

293293
// When k=0, we are raising each element to the 0th power
294294
// No need to waste CPU cycles going through the array

0 commit comments

Comments
 (0)