1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::cmp::Ordering;
use std::convert::{Infallible, TryFrom};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::time::Duration;
use getset::{CopyGetters, Getters, Setters};
use serde::{Deserialize, Serialize};
use crate::model::sample::{Sample, SampleIter};
#[derive(
Serialize, Deserialize, Getters, CopyGetters, Setters, Debug, Clone, PartialEq, Eq, Hash,
)]
pub struct Problem {
#[get = "pub"]
id: ProblemId,
#[get = "pub"]
name: String,
#[get = "pub"]
url_name: String,
#[serde(with = "humantime_serde")]
#[get_copy = "pub"]
time_limit: Option<Duration>,
#[get_copy = "pub"]
memory_limit: Option<Byte>,
#[get_copy = "pub"]
compare: Compare,
#[set = "pub"]
samples: Vec<Sample>,
}
impl Problem {
pub fn new(
id: impl Into<ProblemId>,
name: impl Into<String>,
url_name: impl Into<String>,
time_limit: Option<Duration>,
memory_limit: Option<Byte>,
compare: Compare,
samples: Vec<Sample>,
) -> Self {
Self {
id: id.into(),
name: name.into(),
url_name: url_name.into(),
time_limit,
memory_limit,
compare,
samples,
}
}
pub fn take_samples(self, sample_name: &Option<String>) -> SampleIter {
if let Some(sample_name) = sample_name {
self.samples
.into_iter()
.filter(|sample| sample.name() == sample_name)
.collect::<Vec<_>>()
.into()
} else {
self.samples.into()
}
}
}
impl Default for Problem {
fn default() -> Self {
Self::new(
"C",
"Linear Approximation",
"arc100_a",
Some(Duration::from_secs(2)),
Some("1024 MB".parse().unwrap()),
Compare::Default,
vec![],
)
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
pub struct ProblemId(String);
impl ProblemId {
pub fn normalize(&self) -> String {
self.0.to_uppercase()
}
}
impl PartialEq<ProblemId> for ProblemId {
fn eq(&self, other: &ProblemId) -> bool {
self.normalize() == other.normalize()
}
}
impl PartialOrd for ProblemId {
fn partial_cmp(&self, other: &ProblemId) -> Option<Ordering> {
Some(self.normalize().cmp(&other.normalize()))
}
}
impl Ord for ProblemId {
fn cmp(&self, other: &Self) -> Ordering {
self.normalize().cmp(&other.normalize())
}
}
impl Hash for ProblemId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.normalize().hash(state);
}
}
impl<T: Into<String>> From<T> for ProblemId {
fn from(id: T) -> Self {
Self(id.into())
}
}
impl FromStr for ProblemId {
type Err = Infallible;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(Self::from(s))
}
}
impl AsRef<str> for ProblemId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for ProblemId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.normalize())
}
}
#[derive(
Serialize,
Deserialize,
EnumString,
EnumVariantNames,
IntoStaticStr,
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum Compare {
Default,
}
impl Compare {
pub fn compare(self, a: &str, b: &str) -> bool {
match self {
Self::Default => Self::compare_default(a, b),
}
}
fn compare_default(a: &str, b: &str) -> bool {
a.trim_end() == b.trim_end()
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[serde(try_from = "String", into = "String")]
pub struct Byte(u64);
impl FromStr for Byte {
type Err = &'static str;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(Self(bytefmt::parse(s)?))
}
}
impl TryFrom<String> for Byte {
type Error = &'static str;
fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
Self::from_str(&s)
}
}
impl From<Byte> for String {
fn from(byte: Byte) -> Self {
bytefmt::format_to(byte.0, bytefmt::Unit::MB)
}
}
impl fmt::Display for Byte {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&String::from(*self))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_problem_take_sapmles() {
let samples = vec![
Sample::new("name 1", "5", "0"),
Sample::new("name 2", "5", "0"),
];
let problem = Problem {
id: "A".into(),
name: "Problem A".into(),
url_name: "test_contest_a".into(),
time_limit: Some(Duration::from_secs(2)),
memory_limit: Some("1024 KB".parse().unwrap()),
compare: Compare::Default,
samples: samples.clone(),
};
let tests = &[
(Some(String::from("name 2")), vec![&samples[1]]),
(None, vec![&samples[0], &samples[1]]),
];
for (sample_name, expected) in tests {
let actual = &problem
.clone()
.take_samples(sample_name)
.collect::<Vec<_>>();
assert_eq!(actual.len(), expected.len());
let is_all_equal = actual
.iter()
.zip(expected)
.all(|(a, b)| a.as_ref().unwrap() == *b);
assert!(is_all_equal);
}
}
#[test]
fn problem_id_eq() {
assert_eq!(ProblemId::from("A"), ProblemId::from("A"));
assert_eq!(ProblemId::from("a"), ProblemId::from("A"));
}
#[test]
fn test_problem_id_display() {
assert_eq!(&ProblemId::from("A").to_string(), "A");
assert_eq!(&ProblemId::from("a").to_string(), "A");
}
#[test]
fn test_compare() {
let tests = &[
(Compare::Default, "hoge", "hoge", true),
(Compare::Default, "hoge", "hoge ", true),
(Compare::Default, "hoge", "hoge\n", true),
(Compare::Default, "hoge", " hoge", false),
(Compare::Default, "hoge", "\nhoge", false),
];
for (compare, a, b, expected) in tests {
let actual = compare.compare(a, b);
assert_eq!(actual, *expected);
}
}
#[test]
fn test_byte_try_from() -> anyhow::Result<()> {
assert_eq!(
Byte::try_from(String::from("1024KB")).unwrap(),
Byte(1024 * 1000)
);
assert_eq!(
Byte::try_from(String::from("1.2MB")).unwrap(),
Byte(1200 * 1000)
);
Ok(())
}
#[test]
fn test_byte_display() {
assert_eq!(&Byte(1024 * 1000).to_string(), "1.02 MB");
assert_eq!(&Byte(2000 * 1000).to_string(), "2 MB");
assert_eq!(&Byte(10 * 1000 * 1000).to_string(), "10 MB");
}
}