Skip to content

Update modtype #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2019
Merged
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
15 changes: 9 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ ordered-float = "=1.0.2"

# 剰余関連。普通の整数型などと同じ感覚で扱うだけで自動的にmodを取ってくれる
# 答えの整数をMで割った余りが要求される設問で便利
modtype = "=0.6.0"
modtype = "=0.7.0"

# 英数字などのASCII文字専用の文字列。文字にインデックスでアクセスしたり
# substringを簡単に作ったりできる
226 changes: 111 additions & 115 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -5,8 +5,7 @@ type UnitResult = Result<(), Box<dyn std::error::Error>>;
fn main() -> UnitResult {
run_proconio();
run_ordered_float();
// run_modtype()?;
// run_modtype_derive();
run_modtype()?;
run_ascii()?;
run_bitset_fixed();
run_permutohedron();
@@ -71,9 +70,8 @@ fn run_ordered_float() {
use std::hash::{Hash, Hasher};

let mut v = [
8.20, -5.83, -0.21, 3.44, -7.12, 3.39, -0.72, -1.07, 9.36, NAN,
5.16, -2.81, 1.02, -8.67, 5.77, -1.24, 0.44, 9.91, -7.06, INFINITY,
-3.93, 5.82, 9.64, -8.04, -4.53,
8.20, -5.83, -0.21, 3.44, -7.12, 3.39, -0.72, -1.07, 9.36, NAN, 5.16, -2.81, 1.02, -8.67,
5.77, -1.24, 0.44, 9.91, -7.06, INFINITY, -3.93, 5.82, 9.64, -8.04, -4.53,
]
.iter()
.map(|&n| OrderedFloat(n))
@@ -106,114 +104,112 @@ fn test_ordered_float() {
}

// modtype
// these codes were taken from examples at https://github.com/qryxip/modtype/tree/master/examples
// fn run_modtype() -> UnitResult {
// use modtype::use_modtype;
//
// #[use_modtype]
// type F = modtype::u64::F<1_000_000_007u64>;
//
// let mut a = "13".parse::<F>()?;
// a += F(1_000_000_000);
// assert_eq!(a, F(6));
//
// Ok(())
// }

// #[test]
// fn test_modtype() -> UnitResult {
// run_modtype()
// }

// these codes were taken from examples at https://github.com/qryxip/modtype/blob/master/examples/derive.rs
// fn run_modtype_derive() {
// use modtype::{use_modtype, ConstValue};
// use std::marker::PhantomData;
//
// #[use_modtype]
// type F = F_<17u32>;
//
// #[derive(
// modtype::new,
// modtype::new_unchecked,
// modtype::get,
// Default,
// Clone,
// Copy,
// PartialEq,
// Eq,
// PartialOrd,
// Ord,
// modtype::From,
// modtype::Into,
// modtype::Display,
// modtype::Debug,
// modtype::FromStr,
// modtype::Deref,
// modtype::Neg,
// modtype::Add,
// modtype::AddAssign,
// modtype::Sub,
// modtype::SubAssign,
// modtype::Mul,
// modtype::MulAssign,
// modtype::Div,
// modtype::DivAssign,
// modtype::Rem,
// modtype::RemAssign,
// modtype::Num,
// modtype::Unsigned,
// modtype::Bounded,
// modtype::Zero,
// modtype::One,
// modtype::FromPrimitive,
// modtype::Inv,
// modtype::CheckedNeg,
// modtype::CheckedAdd,
// modtype::CheckedSub,
// modtype::CheckedMul,
// modtype::CheckedDiv,
// modtype::CheckedRem,
// modtype::Pow,
// modtype::Integer,
// )]
// #[modtype(
// modulus = "M::VALUE",
// std = "std",
// num_traits = "num::traits",
// num_integer = "num::integer",
// num_bigint = "num::bigint",
// from(InnerValue, BigUint, BigInt),
// debug(SingleTuple),
// neg(for_ref = true),
// add(for_ref = true),
// add_assign(for_ref = true),
// sub(for_ref = true),
// sub_assign(for_ref = true),
// mul(for_ref = true),
// mul_assign(for_ref = true),
// div(for_ref = true),
// div_assign(for_ref = true),
// rem(for_ref = true),
// rem_assign(for_ref = true),
// inv(for_ref = true),
// pow(for_ref = true)
// )]
// struct F_<M: ConstValue<Value = u32>> {
// #[modtype(value)]
// __value: u32,
// phantom: PhantomData<fn() -> M>,
// }
// assert_eq!(F(7) + F(13), F(3));
// assert_eq!(F(5) - F(11), F(11));
// assert_eq!(F(3), F(4) * F(5));
// assert_eq!(F(3) / F(4), F(5));
// }

// #[test]
// fn test_modtype_derive() {
// run_modtype_derive();
// }
fn run_modtype() -> UnitResult {
use modtype::cartridges::{Additive, AllowFlexibleRhs, Field, ManuallyAdjust, Multiplicative};
use modtype::{use_modtype, Cartridge, ConstValue};
use num::{BigInt, BigRational, CheckedDiv as _};

use std::marker::PhantomData;

{
#[use_modtype]
type F = modtype::F<1_000_000_007u64>;

assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
}
{
#[allow(non_snake_case)]
modtype::thread_local::F::with(1_000_000_007u64, |F| {
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
});
}
{
#[allow(non_snake_case)]
let F = modtype::non_static::F::factory(1_000_000_007u64);

assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
}
{
#[use_modtype]
type F = modtype::ModType<AllowFlexibleRhs<Field<u64>>, 1_000_000_007u64>;

let mut x = F(1);
x += F(1);
x += 1u64;
x += 1i32;
x += 1f64;
x += BigInt::from(1u32);
x += BigRational::new(BigInt::from(1u32), BigInt::from(1u32));
assert_eq!(x, F(7));
}
{
#[use_modtype]
type Z = modtype::ModType<Multiplicative<u32>, 57u32>;

assert_eq!(Z(56) * Z(56), Z(1));
assert_eq!(Z(1).checked_div(&Z(13)), Some(Z(22))); // 13・22 ≡ 1 (mod 57)
}
{
#[use_modtype]
type Z = modtype::ModType<Additive<u64>, 1_000_000_007u64>;

let mut x = Z(1_000_000_006);

x += Z(1);
assert_eq!(*x.get_mut_unchecked(), 1_000_000_007);

x += Z(u64::max_value() / 2 - 1_000_000_007);
assert_eq!(*x.get_mut_unchecked(), u64::max_value() / 2);

x += Z(1);
assert_eq!(
*x.get_mut_unchecked(),
(u64::max_value() / 2 + 1) % 1_000_000_007,
);
}
{
#[use_modtype]
type Z = modtype::ModType<ManuallyAdjust<u64>, 1_000_000_007u64>;

let mut x = Z(1_000_000_006);

x += Z(u64::max_value() - 1_000_000_006);
assert_eq!(*x.get_mut_unchecked(), u64::max_value());

x.adjust();
assert_eq!(*x.get_mut_unchecked(), u64::max_value() % 1_000_000_007);
}
{
#[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/3673
#[derive(modtype::ModType)]
#[modtype(modulus = "M::VALUE", cartridge = "C")]
struct ModType<C: Cartridge<Target = u64>, M: ConstValue<Value = u64>> {
#[modtype(value)]
value: u64,
phantom: PhantomData<fn() -> (C, M)>,
}

impl<M: ConstValue<Value = u64>> ModType<Field<u64>, M> {
fn new(value: u64) -> Self {
Self {
value,
phantom: PhantomData,
}
}
}

#[use_modtype]
type F = ModType<Field<u64>, 1_000_000_007u64>;

assert_eq!((-F(1)).to_string(), "1000000006");
}
Ok(())
}

#[test]
fn test_modtype() -> UnitResult {
run_modtype()
}

// ascii
fn run_ascii() -> UnitResult {
@@ -226,9 +222,9 @@ fn run_ascii() -> UnitResult {
let (i0, _) = ix.next().ok_or_else(|| "got none")?;
let (i1, _) = ix.next().ok_or_else(|| "got none")?;

assert_eq!(s[..i0].as_str(), "2019");
assert_eq!(s[..i0].as_str(), "2019");
assert_eq!(s[i0 + 1..i1].as_str(), "07");
assert_eq!(s[i1 + 1..].as_str(), "01");
assert_eq!(s[i1 + 1..].as_str(), "01");

// split is not available in ascii 0.9.1
// https://github.com/tomprogrammer/rust-ascii/issues/62