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

index 15e288f10719f14afecebf9f43665c3a783cc33c..278004ed31bdd427b4a56df2f2fcd2b946dc4676 100644 (file)
@@ -144,22 +144,20 @@ where
             where
                 T: CumulativeFrequency,
             {
-                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 }
-                })
+                let obs = this.get(k)?;
+                let y = obs.y();
+                let cc = obs.cc();
+                let c = if k == 0 { cc } else { cc - this[k - 1].cc() };
+                Some(Observation { y, c, cc })
             }
 
-            match this.partition_point(|obs| obs.cc() <= cum_weight) {
-                0 => (None, get(this, 0)),
-                k => (get(this, k - 1), get(this, k)),
-            }
+            let k = this.partition_point(|obs| obs.cc() <= cum_weight);
+            let a = k.checked_sub(1).and_then(|k| get(this, k));
+            let b = get(this, k);
+            (a, b)
         }
 
-        let total_weight = self.last().map_or(0.0, |last| last.cc());
-        let tcw = algorithm.target_cumulative_weight(total_weight, p);
+        let tcw = algorithm.target_cumulative_weight(self.last()?.cc(), p);
         algorithm.percentile(tcw, observations_around(self, tcw))
     }
 }