#!/usr/bin/env python3
"""策略之间的边集互补性：各策略独有的新边有多少。"""
import os, subprocess, sys, tempfile, collections
AFLBIN, OUTDIR, SEED = sys.argv[1], sys.argv[2], sys.argv[3]
def edges(path):
    with tempfile.NamedTemporaryFile(suffix=".map", delete=False) as t: mp=t.name
    try:
        subprocess.run(["afl-showmap","-t","5000","-m","none","-q","-o",mp,"--",AFLBIN,path],
                       stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,
                       env={**os.environ,"AFL_MAP_SIZE":"65536"})
        with open(mp) as f: return {int(l.split(":")[0]) for l in f if ":" in l}
    finally: os.unlink(mp)
base=edges(SEED)
uni=collections.defaultdict(set); cnt=collections.Counter()
for fn in sorted(os.listdir(OUTDIR)):
    if fn.endswith(".hints"): continue
    tag = fn.split("-",1)[1] if "-" in fn else "nominal"
    uni[tag] |= (edges(os.path.join(OUTDIR,fn)) - base); cnt[tag]+=1
tags=sorted(uni)
allu=set().union(*uni.values()) if uni else set()
print(f"种子边={len(base)}  全部产出合计新边={len(allu)}")
for t in tags:
    others=set().union(*[uni[o] for o in tags if o!=t]) if len(tags)>1 else set()
    only=uni[t]-others
    print(f"  {t:<16} 产出={cnt[t]:>4}  新边={len(uni[t]):>4}  独有新边={len(only):>4}  边/产出={len(uni[t])/cnt[t]:>5.2f}")
