Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
FL03 committed Jul 15, 2024
1 parent bdd4098 commit 0c06709
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 50 deletions.
45 changes: 0 additions & 45 deletions core/src/ops/absmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,51 +59,6 @@ where
use core::ops::{Add, Rem};
use num::traits::{Num, Signed};

pub trait Floor {
type Output;

fn floor(self) -> Self::Output;
}

pub trait FloorDiv<Rhs = Self> {
type Output;

fn floor_div(self, rhs: Rhs) -> Self::Output;
}

macro_rules! impl_floor_div {
(@float $t:ty) => {
impl<T, O> FloorDiv<T> for $t where $t: ::core::ops::Div<T, Output = O> {
type Output = O;

fn floor_div(self, rhs: T) -> Self::Output {
(self / rhs).floor()
}
}
};
(@impl f32) => {
impl_floor_div!(@float f32);
};
(@impl f64) => {
impl_floor_div!(@float f64);
};
(@impl $t:ty) => {
impl<T, O> FloorDiv<T> for $t where $t: ::core::ops::Div<T, Output = O> {
type Output = O;

fn floor_div(self, rhs: T) -> Self::Output {
self / rhs
}
}
};
($($t:ty),*) => {
$(
impl_floor_div!(@impl $t);
)*
};
}

impl_floor_div!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);

impl<T> PyMod<T> for T
where
Expand Down
84 changes: 84 additions & 0 deletions core/src/ops/arith.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Appellation: arith <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/

pub trait Floor {
type Output;

fn floor(self) -> Self::Output;
}

pub trait FloorDiv<Rhs = Self> {
type Output;

fn floor_div(self, rhs: Rhs) -> Self::Output;
}


macro_rules! impl_floor {
(@base $t:ty) => {
impl Floor for $t {
type Output = $t;

fn floor(self) -> Self::Output {
<$t>::floor(self)
}
}
};
(@impl f32) => {
impl_floor!(@base f32);
};
(@impl f64) => {
impl_floor!(@base f64);
};
(@impl $t:ty) => {
impl Floor for $t {
type Output = $t;

fn floor(self) -> Self::Output {
self
}
}
};
($($t:ty),*) => {
$(
impl_floor!(@impl $t);
)*
};
}

macro_rules! impl_floor_div {
(@float $t:ty) => {
impl<T, O> FloorDiv<T> for $t where $t: ::core::ops::Div<T, Output = O> {
type Output = O;

fn floor_div(self, rhs: T) -> Self::Output {
(self / rhs).floor()
}
}
};
(@impl f32) => {
impl_floor_div!(@float f32);
};
(@impl f64) => {
impl_floor_div!(@float f64);
};
(@impl $t:ty) => {
impl<T, O> FloorDiv<T> for $t where $t: ::core::ops::Div<T, Output = O> {
type Output = O;

fn floor_div(self, rhs: T) -> Self::Output {
self / rhs
}
}
};
($($t:ty),*) => {
$(
impl_floor_div!(@impl $t);
)*
};
}

impl_floor!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);
impl_floor_div!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);
2 changes: 2 additions & 0 deletions core/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
pub use self::prelude::*;

pub mod absmod;
pub mod arith;
pub mod distance;

pub(crate) mod prelude {
pub use super::absmod::*;
pub use super::arith::*;
pub use super::distance::*;
}
4 changes: 2 additions & 2 deletions core/src/pitch/impls/pitch_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Appellation: pitch_ops <impl>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::{Pitch, PitchMod, PitchTy};
use crate::{Pitch, PitchMod, PitchTy, PyMod};
use num::{Num, One, Zero};

macro_rules! impl_interval_method {
Expand Down Expand Up @@ -33,7 +33,7 @@ impl PitchMod for Pitch {
type Output = Self;

fn pitchmod(&self) -> Self::Output {
Self(self.0.pitchmod())
Self(self.0.pymod(Self::MOD))
}
}

Expand Down
9 changes: 6 additions & 3 deletions core/src/pitch/pitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::{PitchTy, Pitches};
use crate::PitchMod;
use crate::PyMod;

/// A [pitch](Pitch) is a discrete tone with an individual frequency that may be
/// classified as a [pitch class](Pitches).
Expand All @@ -13,12 +13,15 @@ use crate::PitchMod;
pub struct Pitch(pub(crate) PitchTy);

impl Pitch {
const MOD: PitchTy = crate::MODULUS;

pub fn new(pitch: impl Into<PitchTy>) -> Self {
Self(pitch.into().pitchmod())
let val: PitchTy = pitch.into();
Self(val.pymod(Self::MOD))
}
/// Returns the absolute value of the remainder of the pitch divided by the modulus.
pub fn abs(&self) -> Self {
self.pitchmod()
Self(self.0.pymod(Self::MOD).abs())
}
/// Returns a new instance of the class representing the given pitch.
pub fn class(&self) -> Pitches {
Expand Down

0 comments on commit 0c06709

Please sign in to comment.