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 3dfe1146f48bdac8cae6a0080e2c3316a6163e88
33 changes: 15 additions & 18 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ use rawpointer::PointerExt;
///
/// ## Constructor methods for one-dimensional arrays.
impl<S, A> ArrayBase<S, Ix1>
where
S: DataOwned<Elem = A>,
where S: DataOwned<Elem = A>
{
/// Create a one-dimensional array from a vector (no copying needed).
///
Expand All @@ -56,7 +55,8 @@ where
///
/// let array = Array::from_vec(vec![1., 2., 3., 4.]);
/// ```
pub fn from_vec(v: Vec<A>) -> Self {
pub fn from_vec(v: Vec<A>) -> Self
{
if mem::size_of::<A>() == 0 {
assert!(v.len() <= isize::MAX as usize, "Length must fit in `isize`.",);
}
Expand All @@ -73,7 +73,8 @@ where
/// let array = Array::from_iter(0..10);
/// ```
#[allow(clippy::should_implement_trait)]
pub fn from_iter<I: IntoIterator<Item = A>>(iterable: I) -> Self {
pub fn from_iter<I: IntoIterator<Item = A>>(iterable: I) -> Self
{
Self::from_vec(iterable.into_iter().collect())
}

Expand Down Expand Up @@ -116,8 +117,7 @@ where
/// ```
#[cfg(feature = "std")]
pub fn range(start: A, end: A, step: A) -> Self
where
A: Float,
where A: Float
{
Self::from(to_vec(linspace::range(start, end, step)))
}
Expand Down Expand Up @@ -181,17 +181,15 @@ where
/// ```
#[cfg(feature = "std")]
pub fn geomspace(start: A, end: A, n: usize) -> Option<Self>
where
A: Float,
where A: Float
{
Some(Self::from(to_vec(geomspace::geomspace(start, end, n)?)))
}
}

/// ## Constructor methods for two-dimensional arrays.
impl<S, A> ArrayBase<S, Ix2>
where
S: DataOwned<Elem = A>,
where S: DataOwned<Elem = A>
{
/// Create an identity matrix of size `n` (square 2D array).
///
Expand Down Expand Up @@ -473,14 +471,14 @@ where
/// );
/// ```
pub fn from_shape_vec<Sh>(shape: Sh, v: Vec<A>) -> Result<Self, ShapeError>
where
Sh: Into<StrideShape<D>>,
where Sh: Into<StrideShape<D>>
{
// eliminate the type parameter Sh as soon as possible
Self::from_shape_vec_impl(shape.into(), v)
}

fn from_shape_vec_impl(shape: StrideShape<D>, v: Vec<A>) -> Result<Self, ShapeError> {
fn from_shape_vec_impl(shape: StrideShape<D>, v: Vec<A>) -> Result<Self, ShapeError>
{
let dim = shape.dim;
let is_custom = shape.strides.is_custom();
dimension::can_index_slice_with_strides(&v, &dim, &shape.strides, dimension::CanIndexCheckMode::OwnedMutable)?;
Expand Down Expand Up @@ -516,16 +514,16 @@ where
/// 5. The strides must not allow any element to be referenced by two different
/// indices.
pub unsafe fn from_shape_vec_unchecked<Sh>(shape: Sh, v: Vec<A>) -> Self
where
Sh: Into<StrideShape<D>>,
where Sh: Into<StrideShape<D>>
{
let shape = shape.into();
let dim = shape.dim;
let strides = shape.strides.strides_for_dim(&dim);
Self::from_vec_dim_stride_unchecked(dim, strides, v)
}

unsafe fn from_vec_dim_stride_unchecked(dim: D, strides: D, mut v: Vec<A>) -> Self {
unsafe fn from_vec_dim_stride_unchecked(dim: D, strides: D, mut v: Vec<A>) -> Self
{
// debug check for issues that indicates wrong use of this constructor
debug_assert!(dimension::can_index_slice(&v, &dim, &strides, CanIndexCheckMode::OwnedMutable).is_ok());

Expand Down Expand Up @@ -598,8 +596,7 @@ where
/// # let _ = shift_by_two;
/// ```
pub fn uninit<Sh>(shape: Sh) -> ArrayBase<S::MaybeUninit, D>
where
Sh: ShapeBuilder<Dim = D>,
where Sh: ShapeBuilder<Dim = D>
{
unsafe {
let shape = shape.into_shape_with_order();
Expand Down
31 changes: 15 additions & 16 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 @@ -82,12 +84,10 @@ where
F: Float,
{
let (a, b, num_steps) = match (range.start_bound(), range.end_bound()) {
(Bound::Included(a), Bound::Included(b)) => {
(*a, *b, F::from(n - 1).expect("Converting number of steps to `A` must not fail."))
}
(Bound::Included(a), Bound::Excluded(b)) => {
(*a, *b, F::from(n).expect("Converting number of steps to `A` must not fail."))
}
(Bound::Included(a), Bound::Included(b)) =>
(*a, *b, F::from(n - 1).expect("Converting number of steps to `A` must not fail.")),
(Bound::Included(a), Bound::Excluded(b)) =>
(*a, *b, F::from(n).expect("Converting number of steps to `A` must not fail.")),
_ => panic!("Only a..b and a..=b ranges are supported."),
};

Expand Down Expand Up @@ -116,8 +116,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
14 changes: 6 additions & 8 deletions src/logspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// except according to those terms.
#![cfg(feature = "std")]

use std::ops::{Bound, RangeBounds};
use num_traits::Float;
use std::ops::{Bound, RangeBounds};

/// An iterator of a sequence of logarithmically spaced number.
///
Expand Down Expand Up @@ -82,17 +82,15 @@ impl<F> ExactSizeIterator for Logspace<F> where Logspace<F>: Iterator {}
/// **Panics** if converting `n - 1` to type `F` fails.
#[inline]
pub fn logspace<R, F>(base: F, range: R, n: usize) -> Logspace<F>
where
where
R: RangeBounds<F>,
F: Float,
{
let (a, b, num_steps) = match (range.start_bound(), range.end_bound()) {
(Bound::Included(a), Bound::Included(b)) => {
(*a, *b, F::from(n - 1).expect("Converting number of steps to `A` must not fail."))
}
(Bound::Included(a), Bound::Excluded(b)) => {
(*a, *b, F::from(n).expect("Converting number of steps to `A` must not fail."))
}
(Bound::Included(a), Bound::Included(b)) =>
(*a, *b, F::from(n - 1).expect("Converting number of steps to `A` must not fail.")),
(Bound::Included(a), Bound::Excluded(b)) =>
(*a, *b, F::from(n).expect("Converting number of steps to `A` must not fail.")),
_ => panic!("Only a..b and a..=b ranges are supported."),
};

Expand Down