Skip to content

Commit 729b4e2

Browse files
committed
Update modtype to 0.7.0
1 parent 5e780e3 commit 729b4e2

File tree

3 files changed

+117
-117
lines changed

3 files changed

+117
-117
lines changed

Cargo.lock

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ordered-float = "=1.0.2"
2121

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

2626
# 英数字などのASCII文字専用の文字列。文字にインデックスでアクセスしたり
2727
# substringを簡単に作ったりできる

src/main.rs

Lines changed: 107 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ type UnitResult = Result<(), Box<dyn std::error::Error>>;
55
fn main() -> UnitResult {
66
run_proconio();
77
run_ordered_float();
8-
// run_modtype()?;
9-
// run_modtype_derive();
8+
run_modtype()?;
109
run_ascii()?;
1110
run_bitset_fixed();
1211
run_permutohedron();
@@ -106,114 +105,112 @@ fn test_ordered_float() {
106105
}
107106

108107
// modtype
109-
// these codes were taken from examples at https://github.com/qryxip/modtype/tree/master/examples
110-
// fn run_modtype() -> UnitResult {
111-
// use modtype::use_modtype;
112-
//
113-
// #[use_modtype]
114-
// type F = modtype::u64::F<1_000_000_007u64>;
115-
//
116-
// let mut a = "13".parse::<F>()?;
117-
// a += F(1_000_000_000);
118-
// assert_eq!(a, F(6));
119-
//
120-
// Ok(())
121-
// }
122-
123-
// #[test]
124-
// fn test_modtype() -> UnitResult {
125-
// run_modtype()
126-
// }
127-
128-
// these codes were taken from examples at https://github.com/qryxip/modtype/blob/master/examples/derive.rs
129-
// fn run_modtype_derive() {
130-
// use modtype::{use_modtype, ConstValue};
131-
// use std::marker::PhantomData;
132-
//
133-
// #[use_modtype]
134-
// type F = F_<17u32>;
135-
//
136-
// #[derive(
137-
// modtype::new,
138-
// modtype::new_unchecked,
139-
// modtype::get,
140-
// Default,
141-
// Clone,
142-
// Copy,
143-
// PartialEq,
144-
// Eq,
145-
// PartialOrd,
146-
// Ord,
147-
// modtype::From,
148-
// modtype::Into,
149-
// modtype::Display,
150-
// modtype::Debug,
151-
// modtype::FromStr,
152-
// modtype::Deref,
153-
// modtype::Neg,
154-
// modtype::Add,
155-
// modtype::AddAssign,
156-
// modtype::Sub,
157-
// modtype::SubAssign,
158-
// modtype::Mul,
159-
// modtype::MulAssign,
160-
// modtype::Div,
161-
// modtype::DivAssign,
162-
// modtype::Rem,
163-
// modtype::RemAssign,
164-
// modtype::Num,
165-
// modtype::Unsigned,
166-
// modtype::Bounded,
167-
// modtype::Zero,
168-
// modtype::One,
169-
// modtype::FromPrimitive,
170-
// modtype::Inv,
171-
// modtype::CheckedNeg,
172-
// modtype::CheckedAdd,
173-
// modtype::CheckedSub,
174-
// modtype::CheckedMul,
175-
// modtype::CheckedDiv,
176-
// modtype::CheckedRem,
177-
// modtype::Pow,
178-
// modtype::Integer,
179-
// )]
180-
// #[modtype(
181-
// modulus = "M::VALUE",
182-
// std = "std",
183-
// num_traits = "num::traits",
184-
// num_integer = "num::integer",
185-
// num_bigint = "num::bigint",
186-
// from(InnerValue, BigUint, BigInt),
187-
// debug(SingleTuple),
188-
// neg(for_ref = true),
189-
// add(for_ref = true),
190-
// add_assign(for_ref = true),
191-
// sub(for_ref = true),
192-
// sub_assign(for_ref = true),
193-
// mul(for_ref = true),
194-
// mul_assign(for_ref = true),
195-
// div(for_ref = true),
196-
// div_assign(for_ref = true),
197-
// rem(for_ref = true),
198-
// rem_assign(for_ref = true),
199-
// inv(for_ref = true),
200-
// pow(for_ref = true)
201-
// )]
202-
// struct F_<M: ConstValue<Value = u32>> {
203-
// #[modtype(value)]
204-
// __value: u32,
205-
// phantom: PhantomData<fn() -> M>,
206-
// }
207-
// assert_eq!(F(7) + F(13), F(3));
208-
// assert_eq!(F(5) - F(11), F(11));
209-
// assert_eq!(F(3), F(4) * F(5));
210-
// assert_eq!(F(3) / F(4), F(5));
211-
// }
212-
213-
// #[test]
214-
// fn test_modtype_derive() {
215-
// run_modtype_derive();
216-
// }
108+
fn run_modtype() -> UnitResult {
109+
use modtype::cartridges::{Additive, AllowFlexibleRhs, Field, ManuallyAdjust, Multiplicative};
110+
use modtype::{use_modtype, Cartridge, ConstValue};
111+
use num::{BigInt, BigRational, CheckedDiv as _};
112+
113+
use std::marker::PhantomData;
114+
115+
{
116+
#[use_modtype]
117+
type F = modtype::F<1_000_000_007u64>;
118+
119+
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
120+
}
121+
{
122+
#[allow(non_snake_case)]
123+
modtype::thread_local::F::with(1_000_000_007u64, |F| {
124+
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
125+
});
126+
}
127+
{
128+
#[allow(non_snake_case)]
129+
let F = modtype::non_static::F::factory(1_000_000_007u64);
130+
131+
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
132+
}
133+
{
134+
#[use_modtype]
135+
type F = modtype::ModType<AllowFlexibleRhs<Field<u64>>, 1_000_000_007u64>;
136+
137+
let mut x = F(1);
138+
x += F(1);
139+
x += 1u64;
140+
x += 1i32;
141+
x += 1f64;
142+
x += BigInt::from(1u32);
143+
x += BigRational::new(BigInt::from(1u32), BigInt::from(1u32));
144+
assert_eq!(x, F(7));
145+
}
146+
{
147+
#[use_modtype]
148+
type Z = modtype::ModType<Multiplicative<u32>, 57u32>;
149+
150+
assert_eq!(Z(56) * Z(56), Z(1));
151+
assert_eq!(Z(1).checked_div(&Z(13)), Some(Z(22))); // 13・22 ≡ 1 (mod 57)
152+
}
153+
{
154+
#[use_modtype]
155+
type Z = modtype::ModType<Additive<u64>, 1_000_000_007u64>;
156+
157+
let mut x = Z(1_000_000_006);
158+
159+
x += Z(1);
160+
assert_eq!(*x.get_mut_unchecked(), 1_000_000_007);
161+
162+
x += Z(u64::max_value() / 2 - 1_000_000_007);
163+
assert_eq!(*x.get_mut_unchecked(), u64::max_value() / 2);
164+
165+
x += Z(1);
166+
assert_eq!(
167+
*x.get_mut_unchecked(),
168+
(u64::max_value() / 2 + 1) % 1_000_000_007,
169+
);
170+
}
171+
{
172+
#[use_modtype]
173+
type Z = modtype::ModType<ManuallyAdjust<u64>, 1_000_000_007u64>;
174+
175+
let mut x = Z(1_000_000_006);
176+
177+
x += Z(u64::max_value() - 1_000_000_006);
178+
assert_eq!(*x.get_mut_unchecked(), u64::max_value());
179+
180+
x.adjust();
181+
assert_eq!(*x.get_mut_unchecked(), u64::max_value() % 1_000_000_007);
182+
}
183+
{
184+
#[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/3673
185+
#[derive(modtype::ModType)]
186+
#[modtype(modulus = "M::VALUE", cartridge = "C")]
187+
struct ModType<C: Cartridge<Target = u64>, M: ConstValue<Value = u64>> {
188+
#[modtype(value)]
189+
value: u64,
190+
phantom: PhantomData<fn() -> (C, M)>,
191+
}
192+
193+
impl<M: ConstValue<Value = u64>> ModType<Field<u64>, M> {
194+
fn new(value: u64) -> Self {
195+
Self {
196+
value,
197+
phantom: PhantomData,
198+
}
199+
}
200+
}
201+
202+
#[use_modtype]
203+
type F = ModType<Field<u64>, 1_000_000_007u64>;
204+
205+
assert_eq!((-F(1)).to_string(), "1000000006");
206+
}
207+
Ok(())
208+
}
209+
210+
#[test]
211+
fn test_modtype() -> UnitResult {
212+
run_modtype()
213+
}
217214

218215
// ascii
219216
fn run_ascii() -> UnitResult {

0 commit comments

Comments
 (0)