Skip to content

Commit

Permalink
Put dimension parameters first
Browse files Browse the repository at this point in the history
There are several methods that can be used to create a new
`Array2D`. In the past, these have typically had the "source"
(iterator, generator function, etc.) first, followed by the
dimensions. However, this can make the source code somewhat awkward
if, for example, you use a closure that ends up being moderately
large. These functions now put the dimensions first, which makes the
"source" parameter last.
  • Loading branch information
HarrisonMc555 committed May 21, 2024
1 parent d979964 commit 6103137
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 @@ -275,7 +275,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 @@ -285,9 +285,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 @@ -316,7 +316,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 @@ -326,9 +326,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 @@ -359,12 +359,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 @@ -383,7 +383,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 @@ -401,12 +401,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 @@ -434,19 +434,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 @@ -464,7 +464,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 @@ -473,9 +473,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 @@ -506,7 +506,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 @@ -515,17 +515,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 @@ -563,7 +563,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 @@ -628,7 +628,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 @@ -713,7 +713,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 @@ -743,7 +743,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 @@ -773,7 +773,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 @@ -1221,7 +1221,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 @@ -1231,7 +1231,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 @@ -1248,7 +1248,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 @@ -1259,7 +1259,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 6103137

Please sign in to comment.