Skip to content

Commit 368e805

Browse files
authored
Merge pull request #6 from qryxip/update-modtype
Update modtype
2 parents 5e780e3 + 7490928 commit 368e805

File tree

3 files changed

+121
-122
lines changed

3 files changed

+121
-122
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: 111 additions & 115 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();
@@ -71,9 +70,8 @@ fn run_ordered_float() {
7170
use std::hash::{Hash, Hasher};
7271

7372
let mut v = [
74-
8.20, -5.83, -0.21, 3.44, -7.12, 3.39, -0.72, -1.07, 9.36, NAN,
75-
5.16, -2.81, 1.02, -8.67, 5.77, -1.24, 0.44, 9.91, -7.06, INFINITY,
76-
-3.93, 5.82, 9.64, -8.04, -4.53,
73+
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,
74+
5.77, -1.24, 0.44, 9.91, -7.06, INFINITY, -3.93, 5.82, 9.64, -8.04, -4.53,
7775
]
7876
.iter()
7977
.map(|&n| OrderedFloat(n))
@@ -106,114 +104,112 @@ fn test_ordered_float() {
106104
}
107105

108106
// 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-
// }
107+
fn run_modtype() -> UnitResult {
108+
use modtype::cartridges::{Additive, AllowFlexibleRhs, Field, ManuallyAdjust, Multiplicative};
109+
use modtype::{use_modtype, Cartridge, ConstValue};
110+
use num::{BigInt, BigRational, CheckedDiv as _};
111+
112+
use std::marker::PhantomData;
113+
114+
{
115+
#[use_modtype]
116+
type F = modtype::F<1_000_000_007u64>;
117+
118+
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
119+
}
120+
{
121+
#[allow(non_snake_case)]
122+
modtype::thread_local::F::with(1_000_000_007u64, |F| {
123+
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
124+
});
125+
}
126+
{
127+
#[allow(non_snake_case)]
128+
let F = modtype::non_static::F::factory(1_000_000_007u64);
129+
130+
assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
131+
}
132+
{
133+
#[use_modtype]
134+
type F = modtype::ModType<AllowFlexibleRhs<Field<u64>>, 1_000_000_007u64>;
135+
136+
let mut x = F(1);
137+
x += F(1);
138+
x += 1u64;
139+
x += 1i32;
140+
x += 1f64;
141+
x += BigInt::from(1u32);
142+
x += BigRational::new(BigInt::from(1u32), BigInt::from(1u32));
143+
assert_eq!(x, F(7));
144+
}
145+
{
146+
#[use_modtype]
147+
type Z = modtype::ModType<Multiplicative<u32>, 57u32>;
148+
149+
assert_eq!(Z(56) * Z(56), Z(1));
150+
assert_eq!(Z(1).checked_div(&Z(13)), Some(Z(22))); // 13・22 ≡ 1 (mod 57)
151+
}
152+
{
153+
#[use_modtype]
154+
type Z = modtype::ModType<Additive<u64>, 1_000_000_007u64>;
155+
156+
let mut x = Z(1_000_000_006);
157+
158+
x += Z(1);
159+
assert_eq!(*x.get_mut_unchecked(), 1_000_000_007);
160+
161+
x += Z(u64::max_value() / 2 - 1_000_000_007);
162+
assert_eq!(*x.get_mut_unchecked(), u64::max_value() / 2);
163+
164+
x += Z(1);
165+
assert_eq!(
166+
*x.get_mut_unchecked(),
167+
(u64::max_value() / 2 + 1) % 1_000_000_007,
168+
);
169+
}
170+
{
171+
#[use_modtype]
172+
type Z = modtype::ModType<ManuallyAdjust<u64>, 1_000_000_007u64>;
173+
174+
let mut x = Z(1_000_000_006);
175+
176+
x += Z(u64::max_value() - 1_000_000_006);
177+
assert_eq!(*x.get_mut_unchecked(), u64::max_value());
178+
179+
x.adjust();
180+
assert_eq!(*x.get_mut_unchecked(), u64::max_value() % 1_000_000_007);
181+
}
182+
{
183+
#[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/3673
184+
#[derive(modtype::ModType)]
185+
#[modtype(modulus = "M::VALUE", cartridge = "C")]
186+
struct ModType<C: Cartridge<Target = u64>, M: ConstValue<Value = u64>> {
187+
#[modtype(value)]
188+
value: u64,
189+
phantom: PhantomData<fn() -> (C, M)>,
190+
}
191+
192+
impl<M: ConstValue<Value = u64>> ModType<Field<u64>, M> {
193+
fn new(value: u64) -> Self {
194+
Self {
195+
value,
196+
phantom: PhantomData,
197+
}
198+
}
199+
}
200+
201+
#[use_modtype]
202+
type F = ModType<Field<u64>, 1_000_000_007u64>;
203+
204+
assert_eq!((-F(1)).to_string(), "1000000006");
205+
}
206+
Ok(())
207+
}
208+
209+
#[test]
210+
fn test_modtype() -> UnitResult {
211+
run_modtype()
212+
}
217213

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

229-
assert_eq!(s[..i0].as_str(), "2019");
225+
assert_eq!(s[..i0].as_str(), "2019");
230226
assert_eq!(s[i0 + 1..i1].as_str(), "07");
231-
assert_eq!(s[i1 + 1..].as_str(), "01");
227+
assert_eq!(s[i1 + 1..].as_str(), "01");
232228

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

0 commit comments

Comments
 (0)