Skip to content

Commit

Permalink
Add finalize function
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 committed Jul 2, 2020
1 parent 0e3287b commit f42e470
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 76 deletions.
95 changes: 25 additions & 70 deletions src/metrics/halstead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,28 @@ pub struct HalsteadMaps<'a> {
}

impl<'a> HalsteadMaps<'a> {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
HalsteadMaps {
operators: FxHashMap::default(),
operands: FxHashMap::default(),
}
}

pub fn merge(&mut self, other: &HalsteadMaps<'a>) {
pub(crate) fn merge(&mut self, other: &HalsteadMaps<'a>) {
for (k, v) in other.operators.iter() {
*self.operators.entry(*k).or_insert(0) += v;
}
for (k, v) in other.operands.iter() {
*self.operands.entry(*k).or_insert(0) += v;
}
}

pub(crate) fn finalize(&self, stats: &mut Stats) {
stats.u_operators = self.operators.len() as u64;
stats.operators = self.operators.values().sum::<u64>();
stats.u_operands = self.operands.len() as u64;
stats.operands = self.operands.values().sum::<u64>();
}
}

impl Serialize for Stats {
Expand Down Expand Up @@ -114,13 +121,7 @@ impl fmt::Display for Stats {
}

impl Stats {
/// Merges a second `Halstead` metric suite into the first one
pub fn merge(&mut self, other: &Stats) {
self.u_operators += other.u_operators;
self.operators += other.operators;
self.u_operands += other.u_operands;
self.operands += other.operands;
}
pub(crate) fn merge(&mut self, _other: &Stats) {}

/// Returns `η1`, the number of distinct operators
#[inline(always)]
Expand Down Expand Up @@ -213,13 +214,7 @@ pub trait Halstead
where
Self: Checker,
{
fn compute<'a>(
_node: &Node<'a>,
_code: &'a [u8],
_stats: &mut Stats,
_halstead_maps: &mut HalsteadMaps<'a>,
) {
}
fn compute<'a>(_node: &Node<'a>, _code: &'a [u8], _halstead_maps: &mut HalsteadMaps<'a>) {}
}

#[inline(always)]
Expand All @@ -231,101 +226,61 @@ fn get_id<'a>(node: &Node<'a>, code: &'a [u8]) -> &'a [u8] {
fn compute_halstead<'a, T: Getter>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
match T::get_op_type(&node) {
HalsteadType::Operator => {
*halstead_maps.operators.entry(node.kind_id()).or_insert(0) += 1;
stats.u_operators = halstead_maps.operators.len() as u64;
stats.operators += 1;
}
HalsteadType::Operand => {
*halstead_maps
.operands
.entry(get_id(node, code))
.or_insert(0) += 1;
stats.u_operands = halstead_maps.operands.len() as u64;
stats.operands += 1;
}
_ => {}
}
}

impl Halstead for PythonCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

impl Halstead for MozjsCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

impl Halstead for JavascriptCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

impl Halstead for TypescriptCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

impl Halstead for TsxCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

impl Halstead for RustCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

impl Halstead for CppCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
halstead_maps: &mut HalsteadMaps<'a>,
) {
compute_halstead::<Self>(node, code, stats, halstead_maps);
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
compute_halstead::<Self>(node, code, halstead_maps);
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ fn finalize<'a>(state_stack: &mut Vec<State<'a>>, diff_level: usize) {
}
let state = state_stack.pop().unwrap();
let last_state = state_stack.last_mut().unwrap();
state
.halstead_maps
.finalize(&mut last_state.space.metrics.halstead);
last_state.space.metrics.merge(&state.space.metrics);
last_state.halstead_maps.merge(&state.halstead_maps);
last_state.space.spaces.push(state.space);
Expand Down Expand Up @@ -234,12 +237,7 @@ pub fn metrics<'a, T: ParserTrait>(parser: &'a T, path: &'a PathBuf) -> Option<F
if let Some(state) = state_stack.last_mut() {
let last = &mut state.space;
T::Cyclomatic::compute(&node, &mut last.metrics.cyclomatic);
T::Halstead::compute(
&node,
code,
&mut last.metrics.halstead,
&mut state.halstead_maps,
);
T::Halstead::compute(&node, code, &mut state.halstead_maps);
T::Loc::compute(&node, &mut last.metrics.loc, func_space, unit);
T::Nom::compute(&node, &mut last.metrics.nom);
T::Mi::compute(
Expand Down

0 comments on commit f42e470

Please sign in to comment.