Use width_lim instead of bucket_lim

This commit is contained in:
Julien Cretin
2020-11-17 10:16:39 +01:00
parent fcc9484510
commit bbb73c77a8
2 changed files with 11 additions and 19 deletions

View File

@@ -33,12 +33,7 @@ impl Histogram {
/// ///
/// The bucket of `item` is the highest power of two, lower or equal to `item`. If `item` is /// The bucket of `item` is the highest power of two, lower or equal to `item`. If `item` is
/// zero, then its bucket is also zero. /// zero, then its bucket is also zero.
///
/// # Panics
///
/// Panics if the item is too big, i.e. it uses its most significant bit.
pub fn add(&mut self, item: usize) { pub fn add(&mut self, item: usize) {
assert!(item <= usize::max_value() / 2);
*self.buckets.entry(get_bucket(item)).or_insert(0) += 1; *self.buckets.entry(get_bucket(item)).or_insert(0) += 1;
} }
@@ -49,15 +44,12 @@ impl Histogram {
} }
} }
/// Returns one past the highest non-empty bucket. /// Returns the bit-width of one past the highest non-empty bucket.
/// ///
/// In other words, all non-empty buckets of the histogram are smaller than the returned bucket. /// In other words, all non-empty buckets of the histogram have a bit-width smaller than the
pub fn bucket_lim(&self) -> usize { /// returned width.
match self.buckets.keys().max() { pub fn width_lim(&self) -> usize {
None => 0, self.buckets.keys().max().map_or(0, |&x| num_bits(x) + 1)
Some(0) => 1,
Some(x) => 2 * x,
}
} }
/// Returns the count of a bucket. /// Returns the count of a bucket.

View File

@@ -17,7 +17,6 @@
//! This is not used during actual fuzzing, only when replaying a corpus to compute statistics. //! This is not used during actual fuzzing, only when replaying a corpus to compute statistics.
use crate::histogram::{bucket_from_width, Histogram}; use crate::histogram::{bucket_from_width, Histogram};
use crate::num_bits;
use std::collections::HashMap; use std::collections::HashMap;
use strum::{Display, EnumIter, EnumString, IntoEnumIterator}; use strum::{Display, EnumIter, EnumString, IntoEnumIterator};
@@ -110,13 +109,14 @@ impl Stats {
self.stats.get(&key).and_then(|h| h.get(bucket)) self.stats.get(&key).and_then(|h| h.get(bucket))
} }
/// Returns one past the highest non-empty bucket. /// Returns the bit-width of one past the highest non-empty bucket.
/// ///
/// In other words, all non-empty buckets of the histogram are smaller than the returned bucket. /// In other words, all non-empty buckets of the histogram have a bit-width smaller than the
fn bucket_lim(&self) -> usize { /// returned width.
fn width_lim(&self) -> usize {
self.stats self.stats
.values() .values()
.map(|h| h.bucket_lim()) .map(|h| h.width_lim())
.max() .max()
.unwrap_or(0) .unwrap_or(0)
} }
@@ -125,10 +125,10 @@ impl Stats {
impl std::fmt::Display for Stats { impl std::fmt::Display for Stats {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let mut matrix: Vec<Vec<String>> = Vec::new(); let mut matrix: Vec<Vec<String>> = Vec::new();
let bits = self.width_lim();
let mut header = Vec::new(); let mut header = Vec::new();
header.push(String::new()); header.push(String::new());
let bits = num_bits(self.bucket_lim());
for width in 0..bits { for width in 0..bits {
header.push(format!(" {}", bucket_from_width(width))); header.push(format!(" {}", bucket_from_width(width)));
} }