Skip to content

Commit

Permalink
Merge pull request #19 from HarrisonMc555/rearrange-parameters
Browse files Browse the repository at this point in the history
Put dimension parameters first
  • Loading branch information
HarrisonMc555 authored May 21, 2024
2 parents 042046f + 8213b40 commit ab64d6f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
56 changes: 28 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
//!
//! pub fn main() -> Result<(), Error> {
//! // Create an array filled with the same element.
//! let prefilled = Array2D::filled_with(42, 2, 3);
//! let prefilled = Array2D::filled_with(2, 3, 42);
//! assert_eq!(prefilled.num_rows(), 2);
//! assert_eq!(prefilled.num_columns(), 3);
//! assert_eq!(prefilled[(0, 0)], 42);
Expand All @@ -77,7 +77,7 @@
//! // column major order.
//! let column_major = vec![1, 4, 2, 5, 3, 6];
//! let from_column_major =
//! Array2D::from_column_major(&column_major, 2, 3)?;
//! Array2D::from_column_major(2, 3, &column_major)?;
//! assert_eq!(from_column_major.num_rows(), 2);
//! assert_eq!(from_column_major.num_columns(), 3);
//! assert_eq!(from_column_major[(1, 1)], 5);
Expand Down Expand Up @@ -289,7 +289,7 @@ impl<T> Array2D<T> {
/// # use array2d::{Array2D, Error};
/// # fn main() -> Result<(), Error> {
/// let row_major = vec![1, 2, 3, 4, 5, 6];
/// let array = Array2D::from_row_major(&row_major, 2, 3)?;
/// let array = Array2D::from_row_major(2, 3, &row_major)?;
/// assert_eq!(array[(1, 2)], 6);
/// assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
/// # Ok(())
Expand All @@ -299,9 +299,9 @@ impl<T> Array2D<T> {
/// [`Array2D`]: struct.Array2D.html
/// [row major order]: https://en.wikipedia.org/wiki/Row-_and_column-major_order
pub fn from_row_major(
elements: &[T],
num_rows: usize,
num_columns: usize,
elements: &[T],
) -> Result<Self, Error>
where
T: Clone,
Expand Down Expand Up @@ -330,7 +330,7 @@ impl<T> Array2D<T> {
/// # use array2d::{Array2D, Error};
/// # fn main() -> Result<(), Error> {
/// let column_major = vec![1, 4, 2, 5, 3, 6];
/// let array = Array2D::from_column_major(&column_major, 2, 3)?;
/// let array = Array2D::from_column_major(2, 3, &column_major)?;
/// assert_eq!(array[(1, 2)], 6);
/// assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
/// # Ok(())
Expand All @@ -340,9 +340,9 @@ impl<T> Array2D<T> {
/// [`Array2D`]: struct.Array2D.html
/// [column major order]: https://en.wikipedia.org/wiki/Row-_and_column-major_order
pub fn from_column_major(
elements: &[T],
num_rows: usize,
num_columns: usize,
elements: &[T],
) -> Result<Self, Error>
where
T: Clone,
Expand Down Expand Up @@ -373,12 +373,12 @@ impl<T> Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let array = Array2D::filled_with(42, 2, 3);
/// let array = Array2D::filled_with(2, 3, 42);
/// assert_eq!(array.as_rows(), vec![vec![42, 42, 42], vec![42, 42, 42]]);
/// ```
///
/// [`Array2D`]: struct.Array2D.html
pub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Self
pub fn filled_with(num_rows: usize, num_columns: usize, element: T) -> Self
where
T: Clone,
{
Expand All @@ -397,7 +397,7 @@ impl<T> Array2D<T> {
where
T: Clone,
{
Array2D::filled_with(element, num_rows, num_columns)
Array2D::filled_with(num_rows, num_columns, element)
}

/// Creates a new [`Array2D`] with the specified number of rows and columns
Expand All @@ -415,12 +415,12 @@ impl<T> Array2D<T> {
/// counter += 1;
/// tmp
/// };
/// let array = Array2D::filled_by_row_major(increment, 2, 3);
/// let array = Array2D::filled_by_row_major(2, 3, increment);
/// assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
/// ```
///
/// [`Array2D`]: struct.Array2D.html
pub fn filled_by_row_major<F>(mut generator: F, num_rows: usize, num_columns: usize) -> Self
pub fn filled_by_row_major<F>(num_rows: usize, num_columns: usize, mut generator: F) -> Self
where
F: FnMut() -> T,
{
Expand Down Expand Up @@ -448,19 +448,19 @@ impl<T> Array2D<T> {
/// counter += 1;
/// tmp
/// };
/// let array = Array2D::filled_by_column_major(increment, 2, 3);
/// let array = Array2D::filled_by_column_major(2, 3, increment);
/// assert_eq!(array.as_columns(), vec![vec![1, 2], vec![3, 4], vec![5, 6]]);
/// ```
///
/// [`Array2D`]: struct.Array2D.html
pub fn filled_by_column_major<F>(mut generator: F, num_rows: usize, num_columns: usize) -> Self
pub fn filled_by_column_major<F>(num_rows: usize, num_columns: usize, mut generator: F) -> Self
where
F: FnMut() -> T,
T: Clone,
{
let total_len = num_rows * num_columns;
let array_column_major = (0..total_len).map(|_| generator()).collect::<Vec<_>>();
Array2D::from_column_major(&array_column_major, num_rows, num_columns)
Array2D::from_column_major(num_rows, num_columns, &array_column_major)
.expect("Filled by should never fail")
}

Expand All @@ -478,7 +478,7 @@ impl<T> Array2D<T> {
/// # use array2d::{Array2D, Error};
/// # fn main() -> Result<(), Error> {
/// let iterator = 1..;
/// let array = Array2D::from_iter_row_major(iterator, 2, 3)?;
/// let array = Array2D::from_iter_row_major(2, 3, iterator)?;
/// assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
/// # Ok(())
/// # }
Expand All @@ -487,9 +487,9 @@ impl<T> Array2D<T> {
/// [`Array2D`]: struct.Array2D.html
/// [row major order]: https://en.wikipedia.org/wiki/Row-_and_column-major_order
pub fn from_iter_row_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize,
iterator: I,
) -> Result<Self, Error>
where
I: Iterator<Item = T>,
Expand Down Expand Up @@ -520,7 +520,7 @@ impl<T> Array2D<T> {
/// # use array2d::{Array2D, Error};
/// # fn main() -> Result<(), Error> {
/// let iterator = 1..;
/// let array = Array2D::from_iter_column_major(iterator, 2, 3)?;
/// let array = Array2D::from_iter_column_major(2, 3, iterator)?;
/// assert_eq!(array.as_rows(), vec![vec![1, 3, 5], vec![2, 4, 6]]);
/// # Ok(())
/// # }
Expand All @@ -529,17 +529,17 @@ impl<T> Array2D<T> {
/// [`Array2D`]: struct.Array2D.html
/// [column major order]: https://en.wikipedia.org/wiki/Row-_and_column-major_order
pub fn from_iter_column_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize,
iterator: I,
) -> Result<Self, Error>
where
I: Iterator<Item = T>,
T: Clone,
{
let total_len = num_rows * num_columns;
let array_column_major = iterator.take(total_len).collect::<Vec<_>>();
Array2D::from_column_major(&array_column_major, num_rows, num_columns)
Array2D::from_column_major(num_rows, num_columns, &array_column_major)
.map_err(|_| Error::NotEnoughElements)
}

Expand Down Expand Up @@ -577,7 +577,7 @@ impl<T> Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let array = Array2D::filled_with(42, 2, 3);
/// let array = Array2D::filled_with(2, 3, 42);
/// assert_eq!(array.get(0, 0), Some(&42));
/// assert_eq!(array.get(10, 10), None);
/// ```
Expand Down Expand Up @@ -642,7 +642,7 @@ impl<T> Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let mut array = Array2D::filled_with(42, 2, 3);
/// let mut array = Array2D::filled_with(2, 3, 42);
///
/// assert_eq!(array.get_mut(0, 0), Some(&mut 42));
/// assert_eq!(array.get_mut(10, 10), None);
Expand Down Expand Up @@ -727,7 +727,7 @@ impl<T> Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let mut array = Array2D::filled_with(42, 2, 3);
/// let mut array = Array2D::filled_with(2, 3, 42);
///
/// let result = array.set(0, 0, 100);
/// assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -757,7 +757,7 @@ impl<T> Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let mut array = Array2D::filled_with(42, 2, 3);
/// let mut array = Array2D::filled_with(2, 3, 42);
///
/// let result = array.set_row_major(4, 100);
/// assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -787,7 +787,7 @@ impl<T> Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let mut array = Array2D::filled_with(42, 2, 3);
/// let mut array = Array2D::filled_with(2, 3, 42);
///
/// let result = array.set_column_major(4, 100);
/// assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -1241,7 +1241,7 @@ impl<T> Index<(usize, usize)> for Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let array = Array2D::filled_with(42, 2, 3);
/// let array = Array2D::filled_with(2, 3, 42);
/// assert_eq!(array[(0, 0)], 42);
/// ```
///
Expand All @@ -1251,7 +1251,7 @@ impl<T> Index<(usize, usize)> for Array2D<T> {
///
/// ```rust,should_panic
/// # use array2d::Array2D;
/// let array = Array2D::filled_with(42, 2, 3);
/// let array = Array2D::filled_with(2, 3, 42);
/// let element = array[(10, 10)];
/// ```
fn index(&self, (row, column): (usize, usize)) -> &Self::Output {
Expand All @@ -1268,7 +1268,7 @@ impl<T> IndexMut<(usize, usize)> for Array2D<T> {
///
/// ```
/// # use array2d::{Array2D, Error};
/// let mut array = Array2D::filled_with(42, 2, 3);
/// let mut array = Array2D::filled_with(2, 3, 42);
/// array[(0, 0)] = 100;
/// assert_eq!(array[(0, 0)], 100);
/// ```
Expand All @@ -1279,7 +1279,7 @@ impl<T> IndexMut<(usize, usize)> for Array2D<T> {
///
/// ```rust,should_panic
/// # use array2d::Array2D;
/// let mut array = Array2D::filled_with(42, 2, 3);
/// let mut array = Array2D::filled_with(2, 3, 42);
/// array[(10, 10)] = 7;
/// ```
fn index_mut(&mut self, (row, column): (usize, usize)) -> &mut Self::Output {
Expand Down
Loading

0 comments on commit ab64d6f

Please sign in to comment.