Skip to content

Commit 06b7980

Browse files
standardize how we templatize (#91)
* convert * converted RMQ * fix * fix * nit * actually let's do fnmut * Revert "actually let's do fnmut" This reverts commit 946ebfd. --------- Co-authored-by: Luke Videckis <lukevideckis@gmail.com> Co-authored-by: Cameron Custer <73217097+cameroncuster@users.noreply.github.com>
1 parent ba3f9c6 commit 06b7980

File tree

11 files changed

+28
-31
lines changed

11 files changed

+28
-31
lines changed

examples/data_structures/rmq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
a: [usize; n],
1111
}
1212

13-
let rmq = RMQ::new(&a, std::cmp::min);
13+
let rmq = RMQ::new(&a, |&x, &y| std::cmp::min(x, y));
1414
for _ in 0..q {
1515
input! {
1616
le: usize,

examples/data_structures/seg_tree_aizu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
n: usize,
99
q: usize,
1010
}
11-
let mut st = SegTree::<usize>::new(n, |x, y| x + y, 0);
11+
let mut st = SegTree::new(n, |x, y| x + y, 0);
1212
for _ in 0..q {
1313
input! {
1414
t: u8,

examples/data_structures/seg_tree_yosupo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
})
2222
.collect::<Vec<(u64, u64)>>();
2323

24-
let mut seg_tree = SegTree::<(u64, u64)>::build_on_array(
24+
let mut seg_tree = SegTree::build_on_array(
2525
&a,
2626
|x, y| (x.0 * y.0 % MOD, (y.0 * x.1 + y.1) % MOD),
2727
(1, 0),

examples/graphs/hld_path_composite_yosupo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ fn main() {
3939
input_a[hld.tin[i]] = elem;
4040
}
4141

42-
let mut st_forwards = SegTree::<(u64, u64)>::build_on_array(
42+
let mut st_forwards = SegTree::build_on_array(
4343
&input_a,
4444
|x, y| (x.0 * y.0 % MOD, (y.0 * x.1 + y.1) % MOD),
4545
(1, 0),
4646
);
47-
let mut st_backwards = SegTree::<(u64, u64)>::build_on_array(
47+
let mut st_backwards = SegTree::build_on_array(
4848
&input_a,
4949
|x, y| (x.0 * y.0 % MOD, (x.0 * y.1 + x.1) % MOD),
5050
(1, 0),

examples/monotonic/mono_st.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414

1515
let rmq = RMQ::new(
1616
&(0..n).map(|i| (i, i)).collect::<Vec<_>>(),
17-
|(min_i1, max_i1), (min_i2, max_i2)| {
17+
|&(min_i1, max_i1), &(min_i2, max_i2)| {
1818
let min_idx = if a[min_i1] < a[min_i2] {
1919
min_i1
2020
} else {

examples/strings/suf_ary_find_substr_many_aizu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
}
2727

2828
let suf_ary = SufAry::new(&s, 255);
29-
let rmq = RMQ::new(&suf_ary.sa, std::cmp::min);
29+
let rmq = RMQ::new(&suf_ary.sa, |&x, &y| std::cmp::min(x, y));
3030

3131
for i in 0..num_queries_find_substr {
3232
let idx = rmq.query(suf_ary.find_substr(length[i]..length[i + 1]));

src/data_structures/rmq.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/// use programming_team_code_rust::data_structures::rmq::RMQ;
66
///
77
/// let a = [1, 3, 2, 4, 5];
8-
/// let rmq1 = RMQ::new(&a, std::cmp::min);
8+
/// let rmq1 = RMQ::new(&a, |&x, &y| std::cmp::min(x, y));
99
/// assert_eq!(rmq1.query(0..5), 1);
1010
/// assert_eq!(rmq1.query(1..4), 2);
1111
///
1212
/// let outside_var = 5;
13-
/// let rmq2 = RMQ::new(&a, |x, y| if x + outside_var < y + outside_var { x } else { y });
13+
/// let rmq2 = RMQ::new(&a, |&x, &y| if x + outside_var < y + outside_var { x } else { y });
1414
/// assert_eq!(rmq2.query(0..5), 1);
1515
/// assert_eq!(rmq2.query(1..4), 2);
1616
/// ```
@@ -19,19 +19,19 @@ pub struct RMQ<T, F> {
1919
op: F,
2020
}
2121

22-
impl<T: Copy, F: Fn(T, T) -> T> RMQ<T, F> {
22+
impl<T: Clone, F: Fn(&T, &T) -> T> RMQ<T, F> {
2323
/// Create a new RMQ instance
2424
///
2525
/// # Complexity (n = a.len())
2626
/// - Time: O(n log n)
2727
/// - Space: O(n log n)
2828
pub fn new(a: &[T], op: F) -> Self {
29-
let mut t = vec![a.to_owned(); 1];
29+
let mut t = vec![a.to_vec(); 1];
3030
let mut i = 0;
3131
while (2 << i) <= a.len() {
3232
t.push(
3333
(0..t[i].len() - (1 << i))
34-
.map(|j| op(t[i][j], t[i][j + (1 << i)]))
34+
.map(|j| op(&t[i][j], &t[i][j + (1 << i)]))
3535
.collect(),
3636
);
3737
i += 1;
@@ -46,6 +46,6 @@ impl<T: Copy, F: Fn(T, T) -> T> RMQ<T, F> {
4646
/// - Space: O(1)
4747
pub fn query(&self, range: std::ops::Range<usize>) -> T {
4848
let lg = range.len().ilog2() as usize;
49-
(self.op)(self.t[lg][range.start], self.t[lg][range.end - (1 << lg)])
49+
(self.op)(&self.t[lg][range.start], &self.t[lg][range.end - (1 << lg)])
5050
}
5151
}

src/data_structures/seg_tree.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
/// ```
55
/// use programming_team_code_rust::data_structures::seg_tree::SegTree;
66
///
7-
/// let mut st = SegTree::<usize>::new(3, |&x, &y| x + y, 0);
7+
/// let mut st = SegTree::new(3, |&x, &y| x + y, 0);
88
/// st.set(1, 2);
99
/// st.set(2, 3);
1010
/// assert_eq!(st.query(0..3), 5);
1111
/// ```
12-
pub struct SegTree<T> {
12+
pub struct SegTree<T, F> {
1313
n: usize,
1414
/// associative operation
15-
pub op: fn(&T, &T) -> T,
15+
pub op: F,
1616
/// identity element
1717
pub unit: T,
1818
tree: Vec<T>,
1919
}
2020

21-
impl<T: Clone> SegTree<T> {
21+
impl<T: Clone, F: Fn(&T, &T) -> T> SegTree<T, F> {
2222
/// Creates a new segment tree with n elements
2323
///
2424
/// # Complexity
2525
/// - Time: O(n)
2626
/// - Space: O(n)
27-
pub fn new(n: usize, op: fn(&T, &T) -> T, unit: T) -> Self {
27+
pub fn new(n: usize, op: F, unit: T) -> Self {
2828
Self {
2929
n,
3030
op,
@@ -38,7 +38,7 @@ impl<T: Clone> SegTree<T> {
3838
/// # Complexity
3939
/// - Time: O(n)
4040
/// - Space: O(n)
41-
pub fn build_on_array(a: &[T], op: fn(&T, &T) -> T, unit: T) -> Self {
41+
pub fn build_on_array(a: &[T], op: F, unit: T) -> Self {
4242
let n = a.len();
4343
let mut tree = vec![unit.clone(); n];
4444
tree.extend(a.to_vec());

src/graphs/lca.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::data_structures::rmq::RMQ;
44
use crate::graphs::dfs_order::get_dfs_preorder;
55

6-
type OpType = fn((usize, usize), (usize, usize)) -> (usize, usize);
6+
type OpType = fn(&(usize, usize), &(usize, usize)) -> (usize, usize);
77

88
/// # Example
99
/// ```
@@ -53,11 +53,8 @@ impl LCA {
5353
tin,
5454
p,
5555
rmq: RMQ::new(
56-
&order
57-
.iter()
58-
.map(|&u| (d[u], u))
59-
.collect::<Vec<(usize, usize)>>(),
60-
std::cmp::min,
56+
&order.iter().map(|&u| (d[u], u)).collect::<Vec<_>>(),
57+
|&x, &y| std::cmp::min(x, y),
6158
),
6259
}
6360
}

src/helpers/compress.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/// - Space: O(n)
2020
pub fn compress<T: Ord>(a: &[T]) -> (Vec<usize>, usize) {
2121
let n = a.len();
22-
let mut idx = (0..n).collect::<Vec<usize>>();
22+
let mut idx = (0..n).collect::<Vec<_>>();
2323
idx.sort_by_key(|&i| &a[i]);
2424
let mut compressed = vec![0; n];
2525
let mut max_val = 0;

0 commit comments

Comments
 (0)