package Sequenic.T2ext.Maintenance; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import Sequenic.T2.Coverage.BasicPath; import Sequenic.T2.Seq.Trace; /** * This class takes a set of traces and reduces it. * * * @author Jeiel Schalkwijk * */ public class Reduce { /** * Greedily selects the non-redundant traces. * * A trace is redundant if it only covers paths that are also covered * by selected traces. * * The end result is a smaller collection of traces that, collectively, * covers the same prime paths as the original collection of paths. * * If dst is null, redundant traces are removed from traces using the iterator's remove operation. * The method returns the total number of unique paths covered by the traces. * * If dst is not null, non-redundant traces are added to dst. * The method returns the total number of redundant traces, so that: * traces.size = dst.size + return value. * * By calling the method again as (dst.iterator(),null) the number of unique paths can be obtained. * * @param traces - the original collection of traces, as an iterator * @param dst - If not null, non-redundant traces are added to this. * @return See description */ public static int minimal (Iterator traces, Collection dst) { HashSet totCoverage = new HashSet(); if(dst==null) { while(traces.hasNext()) if(!totCoverage.addAll(traces.next().coverage())) traces.remove(); return totCoverage.size(); } else { int removed = 0; while(traces.hasNext()) { Trace next = traces.next(); if(totCoverage.addAll(next.coverage())) dst.add(next); else removed++; } return removed; } } /** Greedily selects traces with unique coverage sets. * * A coverage set is unique if none of the selected traces cover the exact same set. * * If dst is non-null, the selected traces are added to it and the method returns * the number of non-selected traces * * Otherwise, non-selected traces are removed using the iterators remove method and * the method returns 0. * * @param traces * @param dst * @return */ public static int unique (Iterator traces, Collection dst) { HashSet> uniqueSets = new HashSet>(); if(dst==null) { while(traces.hasNext()) if(!uniqueSets.add(traces.next().coverage())) traces.remove(); return 0; } else { int removed = 0; while(traces.hasNext()) { Trace next = traces.next(); if(uniqueSets.add(next.coverage())) dst.add(next); else removed++; } return removed; } } }