Skip to content

Commit 4f99feb

Browse files
committed
add docs
1 parent b4ff259 commit 4f99feb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/data_structures/linear_rmq.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
//! # Linear Range Minimum Query
22
3+
/// # Example
4+
/// ```
5+
/// use programming_team_code_rust::data_structures::linear_rmq::LinearRMQ;
6+
///
7+
/// let a = [1, 0, 2, 0, 3];
8+
/// let rmq = LinearRMQ::new(&a, |&x, &y| x.lt(&y)); // lt -> right-most min
9+
/// // le -> left-most min
10+
/// // gt -> right-most max
11+
/// // ge -> left-most max
12+
/// assert_eq!(rmq.query_idx(0..5), 3);
13+
/// assert_eq!(*rmq.query(1..5), 0);
14+
/// ```
315
pub struct LinearRMQ<T, F> {
416
a: Vec<T>,
517
cmp: F,
@@ -8,6 +20,11 @@ pub struct LinearRMQ<T, F> {
820
}
921

1022
impl<T: Clone, F: Fn(&T, &T) -> bool> LinearRMQ<T, F> {
23+
/// Create a new LinearRMQ instance
24+
///
25+
/// # Complexity (n = a.len())
26+
/// - Time: O(n)
27+
/// - Space: O(n)
1128
pub fn new(a: &[T], cmp: F) -> Self {
1229
let mut head = vec![0; a.len() + 1];
1330
let mut t = vec![(0, 0); a.len()];
@@ -43,6 +60,11 @@ impl<T: Clone, F: Fn(&T, &T) -> bool> LinearRMQ<T, F> {
4360
}
4461
}
4562

63+
/// Gets the index of min/max of range
64+
///
65+
/// # Complexity
66+
/// - Time: O(1)
67+
/// - Space: O(1)
4668
pub fn query_idx(&self, range: std::ops::Range<usize>) -> usize {
4769
assert!(!range.is_empty());
4870
let (mut le, mut ri) = (range.start, range.end - 1);
@@ -70,6 +92,11 @@ impl<T: Clone, F: Fn(&T, &T) -> bool> LinearRMQ<T, F> {
7092
}
7193
}
7294

95+
/// Gets the min/max of range
96+
///
97+
/// # Complexity
98+
/// - Time: O(1)
99+
/// - Space: O(1)
73100
pub fn query(&self, range: std::ops::Range<usize>) -> &T {
74101
&self.a[self.query_idx(range)]
75102
}

0 commit comments

Comments
 (0)