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
use std::ops::{Add, AddAssign, Mul, Range};
use itertools::Itertools;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RowCounts {
counts: Vec<usize>,
}
impl RowCounts {
pub fn zero(num_counts: usize) -> Self {
RowCounts {
counts: vec![0; num_counts],
}
}
pub fn single_count(count: usize, idx: usize, num_counts: usize) -> Self {
let mut cnts = Self::zero(num_counts);
cnts.counts[idx] += count;
cnts
}
pub fn counts(&self) -> &[usize] {
&self.counts
}
pub fn is_feasible(&self, max_rows_left: usize, target_range: Range<usize>) -> bool {
let mut rows_required = 0;
for &c in &self.counts {
if c >= target_range.end {
return false;
}
rows_required += target_range.start.saturating_sub(c);
}
rows_required <= max_rows_left
}
}
impl Add for &RowCounts {
type Output = RowCounts;
fn add(self, other: &RowCounts) -> RowCounts {
RowCounts {
counts: self
.counts
.iter()
.zip_eq(&other.counts)
.map(|(x, y)| x + y)
.collect_vec(),
}
}
}
impl AddAssign<&RowCounts> for RowCounts {
fn add_assign(&mut self, rhs: &RowCounts) {
self.counts
.iter_mut()
.zip_eq(&rhs.counts)
.for_each(|(lhs, inc)| *lhs += *inc);
}
}
impl Mul<usize> for &RowCounts {
type Output = RowCounts;
fn mul(self, multiplier: usize) -> Self::Output {
RowCounts {
counts: self
.counts
.iter()
.map(|&count| count * multiplier)
.collect_vec(),
}
}
}