Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
nightly fmt
  • Loading branch information
Kwonunn committed Feb 2, 2026
commit e750399492acfe52c9cdde1b9a42bd0dd8b9dcf0
24 changes: 14 additions & 10 deletions src/finite_bounds.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
use num_traits::Float;

pub enum Bound<F> {
pub enum Bound<F>
{
Included(F),
Excluded(F),
}

/// A version of std::ops::RangeBounds that only implements a..b and a..=b ranges.
pub trait FiniteBounds<F> {
pub trait FiniteBounds<F>
{
fn start_bound(&self) -> F;
fn end_bound(&self) -> Bound<F>;
}

impl<F> FiniteBounds<F> for std::ops::Range<F>
where
F: Float,
where F: Float
{
fn start_bound(&self) -> F {
fn start_bound(&self) -> F
{
self.start
}

fn end_bound(&self) -> Bound<F> {
fn end_bound(&self) -> Bound<F>
{
Bound::Excluded(self.end)
}
}

impl<F> FiniteBounds<F> for std::ops::RangeInclusive<F>
where
F: Float,
where F: Float
{
fn start_bound(&self) -> F {
fn start_bound(&self) -> F
{
*self.start()
}

fn end_bound(&self) -> Bound<F> {
fn end_bound(&self) -> Bound<F>
{
Bound::Included(*self.end())
}
}
21 changes: 11 additions & 10 deletions src/linspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ use num_traits::Float;
/// An iterator of a sequence of evenly spaced floats.
///
/// Iterator element type is `F`.
pub struct Linspace<F> {
pub struct Linspace<F>
{
start: F,
step: F,
index: usize,
len: usize,
}

impl<F> Iterator for Linspace<F>
where
F: Float,
where F: Float
{
type Item = F;

#[inline]
fn next(&mut self) -> Option<F> {
fn next(&mut self) -> Option<F>
{
if self.index >= self.len {
None
} else {
Expand All @@ -40,18 +41,19 @@ where
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
fn size_hint(&self) -> (usize, Option<usize>)
{
let n = self.len - self.index;
(n, Some(n))
}
}

impl<F> DoubleEndedIterator for Linspace<F>
where
F: Float,
where F: Float
{
#[inline]
fn next_back(&mut self) -> Option<F> {
fn next_back(&mut self) -> Option<F>
{
if self.index >= self.len {
None
} else {
Expand Down Expand Up @@ -111,8 +113,7 @@ where
/// **Panics** if converting `((b - a) / step).ceil()` to type `F` fails.
#[inline]
pub fn range<F>(a: F, b: F, step: F) -> Linspace<F>
where
F: Float,
where F: Float
{
let len = b - a;
let steps = F::ceil(len / step);
Expand Down
30 changes: 18 additions & 12 deletions src/logspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use num_traits::Float;
/// An iterator of a sequence of logarithmically spaced number.
///
/// Iterator element type is `F`.
pub struct Logspace<F> {
pub struct Logspace<F>
{
sign: F,
base: F,
start: F,
Expand All @@ -24,13 +25,13 @@ pub struct Logspace<F> {
}

impl<F> Iterator for Logspace<F>
where
F: Float,
where F: Float
{
type Item = F;

#[inline]
fn next(&mut self) -> Option<F> {
fn next(&mut self) -> Option<F>
{
if self.index >= self.len {
None
} else {
Expand All @@ -43,18 +44,19 @@ where
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
fn size_hint(&self) -> (usize, Option<usize>)
{
let n = self.len - self.index;
(n, Some(n))
}
}

impl<F> DoubleEndedIterator for Logspace<F>
where
F: Float,
where F: Float
{
#[inline]
fn next_back(&mut self) -> Option<F> {
fn next_back(&mut self) -> Option<F>
{
if self.index >= self.len {
None
} else {
Expand Down Expand Up @@ -107,12 +109,14 @@ where
}

#[cfg(test)]
mod tests {
mod tests
{
use super::logspace;

#[test]
#[cfg(feature = "approx")]
fn valid() {
fn valid()
{
use crate::{arr1, Array1};
use approx::assert_abs_diff_eq;

Expand All @@ -130,7 +134,8 @@ mod tests {
}

#[test]
fn iter_forward() {
fn iter_forward()
{
let mut iter = logspace(10.0f64, 0.0..=3.0, 4);

assert!(iter.size_hint() == (4, Some(4)));
Expand All @@ -145,7 +150,8 @@ mod tests {
}

#[test]
fn iter_backward() {
fn iter_backward()
{
let mut iter = logspace(10.0f64, 0.0..=3.0, 4);

assert!(iter.size_hint() == (4, Some(4)));
Expand Down