Skip to content

Commit

Permalink
Merge pull request #32 from Shir0kamii/rotate
Browse files Browse the repository at this point in the history
Implement rotation methods
  • Loading branch information
becheran authored Jul 28, 2023
2 parents ff9a7e8 + 5b1944e commit 33f512f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
26 changes: 26 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ fn criterion_benchmark(c: &mut Criterion) {
criterion::BatchSize::SmallInput,
)
});

// Rotation
c.bench_function("grid_rotate_left", |b| {
let grid = init_grid();
b.iter_batched(
|| grid.clone(),
|g| g.rotate_left(),
criterion::BatchSize::SmallInput,
)
});
c.bench_function("grid_rotate_right", |b| {
let grid = init_grid();
b.iter_batched(
|| grid.clone(),
|g| g.rotate_right(),
criterion::BatchSize::SmallInput,
)
});
c.bench_function("grid_rotate_half", |b| {
let grid = init_grid();
b.iter_batched(
|| grid.clone(),
|g| g.rotate_half(),
criterion::BatchSize::SmallInput,
)
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
76 changes: 76 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,82 @@ impl<T> Grid<T> {
}
}

/// Rotate the grid 90° counter-clockwise.
///
/// # Examples
///
/// ```
/// use grid::*;
/// let grid = grid![[1,2,3][4,5,6]];
/// assert_eq!(grid.rotate_left(), grid![[3,6][2,5][1,4]]);
/// ```
#[must_use]
pub fn rotate_left(&self) -> Grid<T>
where
T: Clone,
{
let mut data = Vec::with_capacity(self.data.len());
for c in (0..self.cols).rev() {
for r in 0..self.rows {
data.push(self[r][c].clone());
}
}
Grid {
data,
cols: self.rows,
rows: self.cols,
}
}

/// Rotate the grid 90° clockwise.
///
/// # Examples
///
/// ```
/// use grid::*;
/// let grid = grid![[1,2,3][4,5,6]];
/// assert_eq!(grid.rotate_right(), grid![[4,1][5,2][6,3]]);
/// ```
#[must_use]
pub fn rotate_right(&self) -> Grid<T>
where
T: Clone,
{
let mut data = Vec::with_capacity(self.data.len());
for c in 0..self.cols {
for r in (0..self.rows).rev() {
data.push(self[r][c].clone());
}
}
Grid {
data,
cols: self.rows,
rows: self.cols,
}
}

/// Rotate the grid 180°.
///
/// # Examples
///
/// ```
/// use grid::*;
/// let grid = grid![[1,2,3][4,5,6]];
/// assert_eq!(grid.rotate_half(), grid![[6,5,4][3,2,1]]);
/// ```
#[must_use]
pub fn rotate_half(&self) -> Grid<T>
where
T: Clone,
{
let data: Vec<_> = self.data.iter().rev().cloned().collect();
Grid {
data,
cols: self.cols,
rows: self.rows,
}
}

/// Fills the grid with elements by cloning `value`.
///
/// # Examples
Expand Down

0 comments on commit 33f512f

Please sign in to comment.