]> pintos-os.org Git - pspp/commitdiff
work on percentiles
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 20 Oct 2026 02:12:04 +0000 (19:12 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 20 Oct 2026 02:12:04 +0000 (19:12 -0700)
rust/pspp/src/percentiles.rs

index 7a8d3ef63ac37b20e500308b927b5e511fec0f5a..15e288f10719f14afecebf9f43665c3a783cc33c 100644 (file)
@@ -140,34 +140,21 @@ where
         where
             T: CumulativeFrequency,
         {
-            fn get<T>(this: &[T], k: usize) -> Observation
+            fn get<T>(this: &[T], k: usize) -> Option<Observation>
             where
                 T: CumulativeFrequency,
             {
-                let y = this[k].y();
-                let cc = this[k].cc();
-                let c = if k == 0 { cc } else { cc - this[k - 1].cc() };
-                Observation { y, c, cc }
+                this.get(k).map(|obs| {
+                    let y = obs.y();
+                    let cc = obs.cc();
+                    let c = if k == 0 { cc } else { cc - this[k - 1].cc() };
+                    Observation { y, c, cc }
+                })
             }
 
-            match this
-                .partition_point(|obs| obs.cc() <= cum_weight)
-                .checked_sub(1)
-            {
-                Some(k) => {
-                    if k + 1 >= this.len() {
-                        (Some(get(this, k)), None)
-                    } else {
-                        (Some(get(this, k)), Some(get(this, k + 1)))
-                    }
-                }
-                None => {
-                    if this.is_empty() {
-                        (None, None)
-                    } else {
-                        (None, Some(get(this, 0)))
-                    }
-                }
+            match this.partition_point(|obs| obs.cc() <= cum_weight) {
+                0 => (None, get(this, 0)),
+                k => (get(this, k - 1), get(this, k)),
             }
         }