Skip to content

Commit 87c4733

Browse files
committed
feat(cut_header): --sort-by option
1 parent 27de922 commit 87c4733

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

cut_header.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ def compute_top_indirect_cuts(
181181
DG: nx.DiGraph,
182182
target: str,
183183
edge_dominations: dict,
184-
top_n: int = 5,
185184
) -> List[Tuple[str, str, float]]:
186185
"""Compute the top indirect cuts ranked by prevalence of the includer file.
187186
@@ -273,9 +272,7 @@ def compute_top_indirect_cuts(
273272
dominated_edges = edge_dominations[(includer_file, included_file)]
274273
indirect_cuts.append((includer_file, included_file, includer_prevalence, dominated_edges))
275274

276-
# Sort by prevalence (descending) and take top N
277-
indirect_cuts.sort(key=lambda x: x[2], reverse=True)
278-
return indirect_cuts[:top_n]
275+
return indirect_cuts
279276

280277

281278
# From analyze_includes.py in Chromium
@@ -438,6 +435,12 @@ def main():
438435
parser.add_argument("--skips", action="append", default=[], help="Edges to skip when determining cuts.")
439436
parser.add_argument("--verbose", action="store_true", default=False, help="Enable verbose logging.")
440437
parser.add_argument("--top", type=int, default=5, help="Number of top cuts to display (default: 5).")
438+
parser.add_argument(
439+
"--sort-by",
440+
choices=["prevalence", "dominated"],
441+
default="prevalence",
442+
help="Sort results by prevalence (default) or dominated edges count.",
443+
)
441444
args = parser.parse_args()
442445

443446
try:
@@ -564,19 +567,22 @@ def main():
564567
(includer_file, args.target, includer_prevalence, edge_dominations[(includer_file, args.target)])
565568
)
566569

567-
# Sort by prevalence (descending) and take top N
568-
direct_includers.sort(key=lambda x: x[2], reverse=True)
570+
# Sort and take top N
571+
sort_key = (lambda x: x[3]) if args.sort_by == "dominated" else (lambda x: x[2])
572+
direct_includers.sort(key=sort_key, reverse=True)
569573
top_direct = direct_includers[: args.top]
570574

571-
print(f"\nTop {args.top} direct includers (by overall prevalence)")
575+
print(f"\nTop {args.top} direct includers (by {args.sort_by})")
572576
writer = csv.writer(sys.stdout)
573577
for includer_file, included_file, prevalence, dominated_edges in top_direct:
574578
writer.writerow([includer_file, included_file, f"{prevalence:.2f}", dominated_edges])
575579

576-
# Compute top N indirect cuts ranked by prevalence of the includer file
577-
top_indirect = compute_top_indirect_cuts(include_analysis, DG, args.target, edge_dominations, top_n=args.top)
580+
# Compute top N indirect cuts
581+
all_indirect = compute_top_indirect_cuts(include_analysis, DG, args.target, edge_dominations)
582+
all_indirect.sort(key=sort_key, reverse=True)
583+
top_indirect = all_indirect[: args.top]
578584

579-
print(f"\nTop {args.top} indirect cuts (by overall prevalence)")
585+
print(f"\nTop {args.top} indirect cuts (by {args.sort_by})")
580586
for includer_file, included_file, prevalence, dominated_edges in top_indirect:
581587
writer.writerow([includer_file, included_file, f"{prevalence:.2f}", dominated_edges])
582588

0 commit comments

Comments
 (0)