Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 21 additions & 29 deletions src/algebraic_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use crate::{
interval_arithmetic::DyadicFractionInterval,
polynomial::Polynomial,
traits::{AlwaysExactDiv, AlwaysExactDivAssign, CeilLog2, ExactDiv, ExactDivAssign, FloorLog2},
traits::{AlwaysExactDiv, AlwaysExactDivAssign, CeilLog2, ExactDiv, ExactDivAssign,
ExactDivAssignError, FloorLog2},
util::{DebugAsDisplay, Sign},
};
use num_bigint::{BigInt, BigUint};
Expand Down Expand Up @@ -95,7 +96,7 @@ pub struct RealAlgebraicNumber {

impl fmt::Debug for RealAlgebraicNumber {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
debug_real_algebraic_number(&self.data(), f, "RealAlgebraicNumber")
debug_real_algebraic_number(self.data(), f, "RealAlgebraicNumber")
}
}

Expand Down Expand Up @@ -251,7 +252,7 @@ impl<'a> IntervalAndSignChanges<'a> {
if let Some(sign_changes) = *get_sign_changes(self) {
sign_changes
} else {
let at = get_interval_bound(&self.interval);
let at = get_interval_bound(self.interval);
let sign_changes =
sign_changes_at(primitive_sturm_sequence, ValueOrInfinity::Value(&at));
*get_sign_changes(self) = Some(sign_changes);
Expand Down Expand Up @@ -297,27 +298,22 @@ impl<'a> IntervalAndSignChanges<'a> {
impl Deref for IntervalAndSignChanges<'_> {
type Target = DyadicFractionInterval;
fn deref(&self) -> &DyadicFractionInterval {
&self.interval
self.interval
}
}

impl DerefMut for IntervalAndSignChanges<'_> {
fn deref_mut(&mut self) -> &mut DyadicFractionInterval {
&mut self.interval
self.interval
}
}

fn distance(a: usize, b: usize) -> usize {
if a < b {
b - a
} else {
a - b
}
b.abs_diff(a)
}

#[derive(Debug)]
struct IntervalShrinker<'a> {
minimal_polynomial: &'a Polynomial<BigInt>,
primitive_sturm_sequence: Cow<'a, [Polynomial<BigInt>]>,
interval: IntervalAndSignChanges<'a>,
}
Expand All @@ -331,12 +327,10 @@ enum IntervalShrinkResult {
impl<'a> IntervalShrinker<'a> {
#[inline]
fn with_primitive_sturm_sequence(
minimal_polynomial: &'a Polynomial<BigInt>,
primitive_sturm_sequence: Cow<'a, [Polynomial<BigInt>]>,
interval: &'a mut DyadicFractionInterval,
) -> Self {
IntervalShrinker {
minimal_polynomial,
primitive_sturm_sequence,
interval: IntervalAndSignChanges::new(interval),
}
Expand All @@ -346,7 +340,6 @@ impl<'a> IntervalShrinker<'a> {
interval: &'a mut DyadicFractionInterval,
) -> Self {
Self::with_primitive_sturm_sequence(
minimal_polynomial,
Cow::Owned(minimal_polynomial.to_primitive_sturm_sequence()),
interval,
)
Expand Down Expand Up @@ -481,7 +474,7 @@ impl RealAlgebraicNumber {
pub fn interval(&self) -> &DyadicFractionInterval {
&self.data().interval
}
fn interval_shrinker(&mut self) -> IntervalShrinker {
fn interval_shrinker(&mut self) -> IntervalShrinker<'_> {
let RealAlgebraicNumberData {
minimal_polynomial,
interval,
Expand Down Expand Up @@ -609,7 +602,7 @@ impl RealAlgebraicNumber {
}
/// shrinks the interval till it doesn't contain zero
#[must_use]
fn remove_zero_from_interval(&mut self) -> Option<(Sign, IntervalShrinker)> {
fn remove_zero_from_interval(&mut self) -> Option<(Sign, IntervalShrinker<'_>)> {
let sign = match self.cmp_with_zero() {
Ordering::Equal => return None,
Ordering::Less => Sign::Negative,
Expand Down Expand Up @@ -956,9 +949,8 @@ struct ResultFactor {
}

impl ResultFactor {
fn factor(&self) -> Cow<Polynomial<BigInt>> {
self.primitive_sturm_sequence
.get(0)
fn factor(&self) -> Cow<'_, Polynomial<BigInt>> {
self.primitive_sturm_sequence.first()
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(Polynomial::zero()))
}
Expand Down Expand Up @@ -1192,7 +1184,7 @@ impl Add<RealAlgebraicNumber> for &'_ RealAlgebraicNumber {
}
}

impl<'a, 'b> Add<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
impl Add<&RealAlgebraicNumber> for &RealAlgebraicNumber {
type Output = RealAlgebraicNumber;
fn add(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber {
self.clone().add(rhs)
Expand Down Expand Up @@ -1260,7 +1252,7 @@ impl Sub<RealAlgebraicNumber> for &'_ RealAlgebraicNumber {
}
}

impl<'a, 'b> Sub<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
impl Sub<&RealAlgebraicNumber> for &RealAlgebraicNumber {
type Output = RealAlgebraicNumber;
fn sub(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber {
self.clone().sub(rhs)
Expand Down Expand Up @@ -1432,30 +1424,30 @@ impl Mul<RealAlgebraicNumber> for &'_ RealAlgebraicNumber {
}
}

impl<'a, 'b> Mul<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
impl Mul<&RealAlgebraicNumber> for &RealAlgebraicNumber {
type Output = RealAlgebraicNumber;
fn mul(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber {
self.clone().mul(rhs)
}
}

impl ExactDivAssign for RealAlgebraicNumber {
fn checked_exact_div_assign(&mut self, rhs: RealAlgebraicNumber) -> Result<(), ()> {
fn checked_exact_div_assign(&mut self, rhs: RealAlgebraicNumber) -> Result<(), ExactDivAssignError> {
self.checked_exact_div_assign(&rhs)
}
}

impl ExactDivAssign<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {
fn checked_exact_div_assign(&mut self, rhs: &RealAlgebraicNumber) -> Result<(), ()> {
self.mul_assign(&rhs.checked_recip().ok_or(())?);
fn checked_exact_div_assign(&mut self, rhs: &RealAlgebraicNumber) -> Result<(), ExactDivAssignError> {
self.mul_assign(&rhs.checked_recip().ok_or(ExactDivAssignError)?);
Ok(())
}
}

impl AlwaysExactDiv<RealAlgebraicNumber> for RealAlgebraicNumber {}
impl AlwaysExactDiv<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {}
impl AlwaysExactDiv<RealAlgebraicNumber> for &'_ RealAlgebraicNumber {}
impl<'a, 'b> AlwaysExactDiv<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {}
impl AlwaysExactDiv<&RealAlgebraicNumber> for &RealAlgebraicNumber {}
impl AlwaysExactDivAssign<RealAlgebraicNumber> for RealAlgebraicNumber {}
impl AlwaysExactDivAssign<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {}

Expand All @@ -1482,7 +1474,7 @@ impl ExactDiv<RealAlgebraicNumber> for &'_ RealAlgebraicNumber {
}
}

impl<'a, 'b> ExactDiv<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
impl ExactDiv<&RealAlgebraicNumber> for &RealAlgebraicNumber {
type Output = RealAlgebraicNumber;
fn checked_exact_div(self, rhs: &RealAlgebraicNumber) -> Option<RealAlgebraicNumber> {
self.clone().checked_exact_div(rhs)
Expand Down Expand Up @@ -1522,7 +1514,7 @@ impl Div<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {
}
}

impl<'a, 'b> Div<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
impl Div<&RealAlgebraicNumber> for &RealAlgebraicNumber {
type Output = RealAlgebraicNumber;
fn div(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber {
self.exact_div(rhs)
Expand Down Expand Up @@ -1564,7 +1556,7 @@ impl Rem<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {
}
}

impl<'a, 'b> Rem<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
impl Rem<&RealAlgebraicNumber> for &RealAlgebraicNumber {
type Output = RealAlgebraicNumber;
fn rem(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber {
self.clone().rem(rhs)
Expand Down
30 changes: 10 additions & 20 deletions src/array2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ mod private {
pub(crate) trait SealedData {}

impl<T> SealedData for Vec<T> {}
impl<'a, T> SealedData for &'a [T] {}
impl<'a, T> SealedData for &'a mut [T] {}
impl<T> SealedData for &[T] {}
impl<T> SealedData for &mut [T] {}
}

pub(crate) trait Array2DData:
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<Data: Array2DData> Array2DBase<Data> {
&self,
x_bound: XB,
y_bound: YB,
) -> Array2DSlice<Data::Element> {
) -> Array2DSlice<'_, Data::Element> {
let Array2DSliceData {
x_size,
y_size,
Expand All @@ -294,7 +294,7 @@ impl<Data: Array2DData> Array2DBase<Data> {
&mut self,
x_bound: XB,
y_bound: YB,
) -> Array2DMutSlice<Data::Element>
) -> Array2DMutSlice<'_, Data::Element>
where
Data: BorrowMut<[<Data as Array2DData>::Element]>,
{
Expand All @@ -313,14 +313,14 @@ impl<Data: Array2DData> Array2DBase<Data> {
pub(crate) fn positions(&self) -> Positions {
Positions::new(self.x_size, self.y_size)
}
pub(crate) fn iter(&self) -> Iter<Data::Element> {
pub(crate) fn iter(&self) -> Iter<'_, Data::Element> {
Iter {
positions: self.positions(),
stride: self.stride(),
data: self.data.borrow(),
}
}
pub(crate) fn iter_mut(&mut self) -> IterMut<Data::Element>
pub(crate) fn iter_mut(&mut self) -> IterMut<'_, Data::Element>
where
Data: BorrowMut<[<Data as Array2DData>::Element]>,
{
Expand Down Expand Up @@ -382,10 +382,10 @@ impl<T> Array2DBase<Vec<T>> {
self.data
}
pub(crate) fn data(&self) -> &[T] {
&*self.data
&self.data
}
pub(crate) fn data_mut(&mut self) -> &mut [T] {
&mut *self.data
&mut self.data
}
}

Expand Down Expand Up @@ -498,6 +498,7 @@ pub(crate) type Array2DMutSlice<'a, T> = Array2DBase<&'a mut [T]>;

/// column-major 2D positions iterator
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[derive(Default)]
pub(crate) struct Positions {
x: usize,
y: usize,
Expand All @@ -506,17 +507,6 @@ pub(crate) struct Positions {
after_rev_y: usize,
}

impl Default for Positions {
fn default() -> Self {
Self {
x: 0,
y: 0,
y_size: 0,
rev_x: 0,
after_rev_y: 0,
}
}
}

impl Positions {
pub(crate) fn new(x_size: usize, y_size: usize) -> Self {
Expand Down Expand Up @@ -869,7 +859,7 @@ mod tests {
#[test]
fn test_iter_mut() {
let mut array = Array2DOwned::new_with_positions(2, 3, |x, y| x * 10 + y);
let expected_list: Vec<_> = array.data().iter().copied().collect();
let expected_list: Vec<_> = array.data().to_vec();
for operation_mask in 0..(1 << expected_list.len()) {
println!();
let mut expected = expected_list.iter().copied();
Expand Down
Loading