Safe Rust, simple, fast, space-efficient accumulator for computing median
Find a file
2022-09-20 19:02:56 +02:00
benches Initial commit 2022-09-20 19:02:56 +02:00
src Initial commit 2022-09-20 19:02:56 +02:00
.gitignore Initial commit 2022-09-20 19:02:56 +02:00
Cargo.toml Initial commit 2022-09-20 19:02:56 +02:00
LICENSE Initial commit 2022-09-20 19:02:56 +02:00
README.md Initial commit 2022-09-20 19:02:56 +02:00
rustfmt.toml Initial commit 2022-09-20 19:02:56 +02:00

Median accumulator

Simple, space-efficient algorithm to compute the median of an accumulation of elements.

  • Push-only (no running window)
  • Median is updated at each push (getting the median is always O(1))
  • Space-efficient: O(D) space, D being the number of different samples, not the total number of samples
  • Time-efficient: push is O(log(N))
  • Generic: T: Clone + Ord
  • Tested
  • No unsafe, no deps except std

Faster than other implementations if there are samples having the same value. If this is not your case, you should use another implementation.

Use

use median_accumulator::*;

let mut acc = MedianAcc::new();

assert_eq!(acc.get_median(), None);
acc.push(7);
assert_eq!(acc.get_median(), Some(MedianResult::One(7)));
acc.push(5);
assert_eq!(acc.get_median(), Some(MedianResult::Two(5, 7)));
acc.push(7);
assert_eq!(acc.get_median(), Some(MedianResult::One(7)));

If you ever encounter an unreachable panic, please file an issue or send me an e-mail.

License

CopyLeft 2022 Pascal Engélibert

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.