Expand description
Cyclomatic Complexity Calculation from Branch Counts
This module calculates cyclomatic complexity using the Z-coordinate (branch_count) from the 3D spatial representation of CFG blocks.
§Formula
Cyclomatic Complexity M = E - N + 2P where:
- E = number of edges
- N = number of nodes
- P = number of connected components (usually 1 for a function)
Using spatial coordinates: M = 1 + sum(branch_count) for all blocks
§Complexity
- Time: O(n) single pass through blocks
- Space: O(1) constant space
§Example
use geographdb_core::algorithms::complexity::{calculate_cyclomatic_complexity, ComplexityBlock};
let blocks = vec![
ComplexityBlock { id: 0, z: 0.0 }, // Entry (no branches)
ComplexityBlock { id: 1, z: 1.0 }, // If statement (1 branch)
ComplexityBlock { id: 2, z: 0.0 }, // Sequential
ComplexityBlock { id: 3, z: 1.0 }, // Another if (1 branch)
];
let result = calculate_cyclomatic_complexity(&blocks);
assert_eq!(result.cyclomatic_complexity, 3); // 1 + 1 + 1 = 3Structs§
- Block
Complexity - Complexity metrics for a single block
- Complexity
Block - A block for complexity calculation
- Complexity
Result - Result of complexity analysis
Enums§
- Complexity
Rating - Complexity rating based on McCabe number
Functions§
- calculate_
cyclomatic_ complexity - Calculate cyclomatic complexity from CFG blocks
- calculate_
per_ block_ complexity - Calculate complexity for each block individually
- find_
most_ complex_ blocks - Identify the most complex blocks (top N by branch count)
- get_
complexity_ trend - Get complexity trend description
- is_
complexity_ acceptable - Check if complexity is within acceptable limits