Skip to content

Commit 1bf6b15

Browse files
committed
Document reasoning behind not using function pointers
1 parent 8664d3b commit 1bf6b15

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/arq_tree.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ where
115115
}
116116

117117
pub trait ArqSpec {
118+
/// Type of data representing an endomorphism.
119+
// Note that while a Fn(M) -> M may seem like a more natural representation
120+
// for an endomorphism, compositions would then have to delegate to each of
121+
// their parts. This representation is more efficient.
118122
type F;
123+
/// Type of monoid elements.
119124
type M;
120125
/// Require for all f,g,a: apply(compose(f, g), a) = apply(f, apply(g, a))
121126
fn compose(f: &Self::F, g: &Self::F) -> Self::F;
@@ -129,13 +134,13 @@ pub trait ArqSpec {
129134

130135
/// In this example, we want to support range sum queries and range constant
131136
/// assignments. Note that constant assignment f_c(a) = c is not a endomorphism
132-
/// on integers because f_c(a+b) = c != 2*c = f_c(a) + f_c(b). In intuitive
137+
/// on (i64, +) because f_c(a+b) = c != 2*c = f_c(a) + f_c(b). In intuitive
133138
/// terms, the problem is that the internal nodes of the tree should really be
134139
/// set to a multiple of c, corresponding to the subtree size. So let's augment
135140
/// the monoid type with size information, using the 2D vector (a_i,1) instead
136-
/// of a_i. Now check that f_c((a, s)) = (c*s, s) is indeed an endomorphism:
137-
/// f_c((a,s)+(b,t)) = f_c((a+b,s+t)) = (c*(s+t),s+t) = (c*s,s)+(c*t,t) =
138-
/// f_c((a,s)) + f_c((b,t)).
141+
/// of a_i. Now check that f_c((a, s)) = (c*s, s) is indeed an endomorphism on
142+
/// vector addition: f_c((a,s)+(b,t)) = f_c((a+b,s+t)) = (c*(s+t),s+t)
143+
/// = (c*s,s)+(c*t,t) = f_c((a,s)) + f_c((b,t)).
139144
///
140145
/// # Panics
141146
///
@@ -186,7 +191,7 @@ impl ArqSpec for AssignMin {
186191
#[cfg(test)]
187192
mod test {
188193
use super::*;
189-
194+
190195
#[test]
191196
fn test_range_sum() {
192197
let mut arq = ArqTree::<AssignSum>::new(vec![(0, 1); 10]);
@@ -202,7 +207,7 @@ mod test {
202207
#[test]
203208
fn test_rmq() {
204209
let mut arq = ArqTree::<AssignMin>::new(vec![0; 10]);
205-
210+
206211
assert_eq!(arq.query(0, 9), 0);
207212

208213
arq.modify(2, 4, &-5);

src/string_proc.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,37 @@ impl<'a> Matcher<'a> {
5353
}
5454

5555
/// Manacher's algorithm for computing palindrome substrings in linear time.
56-
/// len[2*i] = odd length of palindrome centred at text[i].
57-
/// len[2*i+1] = even length of palindrome centred at text[i+0.5].
56+
/// pal[2*i] = odd length of palindrome centred at text[i].
57+
/// pal[2*i+1] = even length of palindrome centred at text[i+0.5].
5858
///
5959
/// # Panics
6060
///
6161
/// Panics if text is empty.
6262
pub fn palindromes(text: &[u8]) -> Vec<usize> {
63-
let mut len = Vec::with_capacity(2 * text.len() - 1);
64-
len.push(1);
65-
while len.len() < len.capacity() {
66-
let i = len.len() - 1;
67-
let max_len = ::std::cmp::min(i + 1, len.capacity() - i);
68-
while len[i] < max_len && text[(i - len[i] - 1) / 2] == text[(i + len[i] + 1) / 2] {
69-
len[i] += 2;
63+
let mut pal = Vec::with_capacity(2 * text.len() - 1); // only mutable var!
64+
pal.push(1);
65+
while pal.len() < pal.capacity() {
66+
let i = pal.len() - 1;
67+
let max_len = ::std::cmp::min(i + 1, pal.capacity() - i);
68+
while pal[i] < max_len && text[(i - pal[i] - 1) / 2] == text[(i + pal[i] + 1) / 2] {
69+
pal[i] += 2;
7070
}
71-
if len[i] < 2 {
72-
let a = 1 - len[i];
73-
len.push(a);
71+
if pal[i] < 2 {
72+
let a = 1 - pal[i];
73+
pal.push(a);
7474
} else {
7575
for d in 1.. {
76-
let (a, b) = (len[i - d], len[i] - d);
76+
let (a, b) = (pal[i - d], pal[i] - d);
7777
if a < b {
78-
len.push(a);
78+
pal.push(a);
7979
} else {
80-
len.push(b);
80+
pal.push(b);
8181
break;
8282
}
8383
}
8484
}
8585
}
86-
len
86+
pal
8787
}
8888

8989
#[cfg(test)]

0 commit comments

Comments
 (0)