Example usage for java.util HashSet removeIf

List of usage examples for java.util HashSet removeIf

Introduction

In this page you can find the example usage for java.util HashSet removeIf.

Prototype

default boolean removeIf(Predicate<? super E> filter) 

Source Link

Document

Removes all of the elements of this collection that satisfy the given predicate.

Usage

From source file:blusunrize.immersiveengineering.api.ApiUtils.java

private static boolean handlePos(Vec3d pos, BlockPos posB, HashMap<BlockPos, Vec3d> halfScanned,
        HashSet<BlockPos> done, Predicate<Triple<BlockPos, Vec3d, Vec3d>> shouldStop,
        HashSet<Triple<BlockPos, Vec3d, Vec3d>> near) {
    final double DELTA_NEAR = .3;
    if (!done.contains(posB)) {
        if (halfScanned.containsKey(posB) && !pos.equals(halfScanned.get(posB))) {
            Triple<BlockPos, Vec3d, Vec3d> added = new ImmutableTriple<>(posB, halfScanned.get(posB), pos);
            boolean stop = shouldStop.test(added);
            done.add(posB);//  w w  w.jav a  2 s. co m
            halfScanned.remove(posB);
            near.removeIf((t) -> t.getLeft().equals(posB));
            if (stop)
                return true;
            for (int i = 0; i < 3; i++) {
                double coord = getDim(pos, i);
                double diff = coord - Math.floor(coord);
                if (diff < DELTA_NEAR)
                    near.add(
                            new ImmutableTriple<>(offsetDim(posB, i, -1), added.getMiddle(), added.getRight()));
                diff = Math.ceil(coord) - coord;
                if (diff < DELTA_NEAR)
                    near.add(new ImmutableTriple<>(offsetDim(posB, i, 1), added.getMiddle(), added.getRight()));
            }
        } else {
            halfScanned.put(posB, pos);
        }
    }
    return false;
}

From source file:com.net2plan.gui.utils.topologyPane.jung.JUNGCanvas.java

/**
 * Default constructor.//from   w w w  . j  a va2s  .  co  m
 *
 * @since 0.2.3
 */
public JUNGCanvas(IVisualizationCallback callback, TopologyPanel topologyPanel) {
    this.callback = callback;

    transformNetPlanCoordinatesToJungCoordinates = vertex -> {
        final int vlIndex = this.callback.getVisualizationState()
                .getCanvasVisualizationOrderRemovingNonVisible(vertex.getLayer());
        final double interLayerDistanceInNpCoord = currentInterLayerDistanceInNpCoordinates;
        final Point2D basePositionInNetPlanCoord = vertex.getAssociatedNetPlanNode().getXYPositionMap();
        return new Point2D.Double(basePositionInNetPlanCoord.getX(),
                -(basePositionInNetPlanCoord.getY() + (vlIndex * interLayerDistanceInNpCoord)));
    };

    g = new DirectedOrderedSparseMultigraph<>();
    l = new StaticLayout<>(g, transformNetPlanCoordinatesToJungCoordinates);
    vv = new VisualizationViewer<>(l);

    osmStateManager = new OSMStateManager(callback, topologyPanel, this);

    originalEdgeShapeTransformer = new EdgeShape.QuadCurve<>();
    ((EdgeShape.QuadCurve<GUINode, GUILink>) originalEdgeShapeTransformer).setControlOffsetIncrement(10); // how much they separate from the direct line (default is 20)
    //((EdgeShape.QuadCurve<GUINode, GUILink>) originalEdgeShapeTransformer).setEdgeIndexFunction(DefaultParallelEdgeIndexFunction.<GUINode, GUILink>getInstance()); // how much they separate from the direct line (default is 20)
    /* This functions gives an index to the links to show separate (curved): the order among the parallel links (BUT NOW only among the separated ones among them) */
    ((EdgeShape.QuadCurve<GUINode, GUILink>) originalEdgeShapeTransformer)
            .setEdgeIndexFunction(new EdgeIndexFunction<GUINode, GUILink>() {
                public void reset(Graph<GUINode, GUILink> graph, GUILink e) {
                }

                public void reset() {
                }

                public int getIndex(Graph<GUINode, GUILink> graph, GUILink e) {
                    final GUINode u = e.getOriginNode();
                    final GUINode v = e.getDestinationNode();
                    final HashSet<GUILink> commonEdgeSet = new HashSet<>(graph.getInEdges(v));
                    commonEdgeSet.retainAll(graph.getOutEdges(u));
                    commonEdgeSet.removeIf(ee -> !ee.isShownSeparated());
                    int count = 0;
                    for (GUILink other : commonEdgeSet)
                        if (other == e)
                            return count;
                        else
                            count++;
                    throw new RuntimeException();
                }
            });
    /* Customize the graph */
    vv.getRenderContext().setVertexDrawPaintTransformer(n -> n.getDrawPaint());
    vv.getRenderContext().setVertexFillPaintTransformer(n -> n.getFillPaint());
    vv.getRenderContext().setVertexFontTransformer(n -> n.getFont());

    vv.getRenderContext().setVertexIconTransformer(gn -> gn.getIcon());

    vv.getRenderContext().setVertexIncludePredicate(
            guiNodeContext -> callback.getVisualizationState().isVisibleInCanvas(guiNodeContext.element));
    vv.getRenderer().setVertexLabelRenderer(new NodeLabelRenderer());
    vv.setVertexToolTipTransformer(node -> node.getToolTip());

    vv.getRenderContext().setEdgeIncludePredicate(
            context -> callback.getVisualizationState().isVisibleInCanvas(context.element));
    vv.getRenderContext().setEdgeArrowPredicate(
            context -> callback.getVisualizationState().isVisibleInCanvas(context.element)
                    && context.element.getHasArrow());
    vv.getRenderContext().setEdgeArrowStrokeTransformer(i -> i.getArrowStroke());
    vv.getRenderContext()
            .setEdgeArrowTransformer(new ConstantTransformer(ArrowFactory.getNotchedArrow(7, 10, 5)));
    vv.getRenderContext().setEdgeLabelClosenessTransformer(new ConstantDirectionalEdgeValueTransformer(.6, .6));
    vv.getRenderContext().setEdgeStrokeTransformer(i -> i.getEdgeStroke());

    vv.getRenderContext().setEdgeDrawPaintTransformer(e -> e.getEdgeDrawPaint());
    vv.getRenderContext().setArrowDrawPaintTransformer(e -> e.getArrowDrawPaint());
    vv.getRenderContext().setArrowFillPaintTransformer(e -> e.getArrowFillPaint());

    vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.BLUE));
    vv.getRenderer().setEdgeLabelRenderer(new BasicEdgeLabelRenderer<GUINode, GUILink>() {
        public void labelEdge(RenderContext<GUINode, GUILink> rc, Layout<GUINode, GUILink> layout, GUILink e,
                String label) {
            if (callback.getVisualizationState().isCanvasShowLinkLabels())
                super.labelEdge(rc, layout, e, e.getLabel());
        }
    });
    vv.setEdgeToolTipTransformer(link -> link.getToolTip());
    vv.getRenderContext().setEdgeShapeTransformer(
            c -> c.element.isShownSeparated() ? originalEdgeShapeTransformer.transform(c)
                    : new Line2D.Float(0.0f, 0.0f, 1.0f, 0.0f));

    // Background controller
    this.paintableAssociatedToBackgroundImage = null;

    gm = new PluggableGraphMouse();
    vv.setGraphMouse(gm);

    scalingControl = new LayoutScalingControl();
    ITopologyCanvasPlugin scalingPlugin = new ScalingCanvasPlugin(scalingControl, MouseEvent.NOBUTTON);
    addPlugin(scalingPlugin);

    vv.setOpaque(false);
    vv.setBackground(new Color(0, 0, 0, 0));

    this.updateInterLayerDistanceInNpCoordinates(callback.getVisualizationState().getInterLayerSpaceInPixels());

    //        reset();
}

From source file:gedi.riboseq.inference.orf.OrfFinder.java

/**
 * Coordinates are in codonsRegion space!
 * @param index//from  w  w  w .jav a2 s. c  om
 * @param sequence
 * @param sg
 * @param codonsRegion
 * @return
 */
public ArrayList<OrfWithCodons> findOrfs(int index, String sequence, SpliceGraph sg,
        ImmutableReferenceGenomicRegion<IntervalTreeSet<Codon>> codonsRegion) {
    SimpleDirectedGraph<Codon> fg = new SimpleDirectedGraph<Codon>("Codongraph");

    //      if (!codonsRegion.getReference().toString().equals("chr4+") || !codonsRegion.getRegion().contains(140_283_087))
    //         return 0;

    LeftMostInFrameAndClearList buff = new LeftMostInFrameAndClearList();

    IntervalTreeSet<Codon> codons = codonsRegion.getData();
    codons.removeIf(c -> c.getTotalActivity() < minCodonActivity);
    if (codons.size() == 0)
        return new ArrayList<OrfWithCodons>();

    // add stop codons for easy orf inference
    HashSet<Codon> stopCodons = new HashSet<Codon>();
    Trie<String> stop = new Trie<String>();
    stop.put("TAG", "TAG");
    stop.put("TGA", "TGA");
    stop.put("TAA", "TAA");
    stop.iterateAhoCorasick(sequence)
            .map(r -> new Codon(new ArrayGenomicRegion(r.getStart(), r.getEnd()), r.getValue()))
            .toCollection(stopCodons);

    for (Intron intr : sg.iterateIntrons().loop()) {
        ArrayGenomicRegion reg = new ArrayGenomicRegion(intr.getStart() - 2, intr.getStart(), intr.getEnd(),
                intr.getEnd() + 1);
        String cod = stop.get(SequenceUtils.extractSequence(reg, sequence));
        if (cod != null)
            stopCodons.add(new Codon(reg, cod));

        reg = new ArrayGenomicRegion(intr.getStart() - 1, intr.getStart(), intr.getEnd(), intr.getEnd() + 2);
        cod = stop.get(SequenceUtils.extractSequence(reg, sequence));
        if (cod != null)
            stopCodons.add(new Codon(reg, cod));
    }
    stopCodons.removeAll(codons);
    codons.addAll(stopCodons);

    ArrayList<OrfWithCodons> re = new ArrayList<OrfWithCodons>();
    HashSet<Codon> usedForAnno = new HashSet<Codon>();

    if (assembleAnnotationFirst) {
        // new: first use annotated transcripts in a greedy fashion
        ArrayList<ImmutableReferenceGenomicRegion<Transcript>> transcripts = annotation.ei(codonsRegion)
                .filter(t -> t.getData().isCoding()).map(t -> codonsRegion.induce(t, "T")).list();

        int acount = 0;
        LinkedList<OrfWithCodons> orfs = new LinkedList<OrfWithCodons>();
        GenomicRegion best;
        HashSet<Codon> aremoved = new HashSet<Codon>();

        do {
            best = null;
            double bestSum = 0;
            for (ImmutableReferenceGenomicRegion<Transcript> tr : transcripts) {
                double[] a = new double[tr.getRegion().getTotalLength()];
                for (Codon c : codons) {
                    if (tr.getRegion().containsUnspliced(c)) {
                        int p = tr.induce(c.getStart());

                        assert a[p] == 0;

                        if (!aremoved.contains(c))
                            a[p] = c.totalActivity;
                        if (c.isStop())
                            a[p] = -1;
                    }
                }
                for (int f = 0; f < 3; f++) {
                    int s = -1;
                    double sum = 0;

                    for (int p = f; p < a.length; p += 3) {
                        if (a[p] == -1) {//stop
                            if (sum > bestSum) {
                                bestSum = sum;
                                best = tr.getRegion().map(new ArrayGenomicRegion(s, p + 3));
                            }
                            s = -1;
                            sum = 0;
                        } else
                            sum += a[p];

                        if (a[p] > 0 && s == -1)
                            s = p;
                    }
                }
            }
            if (best != null) {
                ArrayList<Codon> cods = new ArrayList<>();
                int uniqueCodons = 0;
                double uniqueActivity = 0;
                double totalActivity = 0;

                for (Codon c : codons) {
                    if (best.containsUnspliced(c) && best.induce(c.getStart()) % 3 == 0) {
                        if (aremoved.add(c)) {
                            uniqueActivity += c.totalActivity;
                            uniqueCodons++;
                        }
                        totalActivity += c.totalActivity;
                        if (c.totalActivity > 0)
                            cods.add(c);
                    }
                }
                //                  System.out.println(codonsRegion.map(best));
                if ((uniqueCodons >= minUniqueCodons || uniqueCodons == cods.size())
                        && uniqueActivity > minUniqueActivity && totalActivity > minOrfTotalActivity) {

                    Collections.sort(cods);
                    usedForAnno.addAll(cods);

                    OrfWithCodons orf = new OrfWithCodons(index, 0, acount++, best.toArrayGenomicRegion(), cods,
                            true);
                    orfs.add(orf);
                }

            }
        } while (best != null);

        if (orfs.size() > 1) {

            // they are not necessarily connected!
            LinkedList<OrfWithCodons>[] connected = findConnectedOrfs(orfs);
            orfs.clear();

            for (LinkedList<OrfWithCodons> corfs : connected) {
                for (boolean changed = true; changed && corfs.size() > 1;) {
                    changed = false;

                    if (useEM)
                        inferOverlappingOrfActivitiesEM(corfs);
                    else
                        overlapUniqueCoverage(corfs);

                    Iterator<OrfWithCodons> it = corfs.iterator();
                    while (it.hasNext()) {
                        OrfWithCodons orf = it.next();
                        if (orf.getEstimatedTotalActivity() < minOrfTotalActivity) {
                            it.remove();
                            changed = true;
                        }
                    }
                }

                if (corfs.size() > 1)
                    distributeCodons(corfs);
                orfs.addAll(corfs);
            }
        }
        re.addAll(orfs);
    }

    // as edges only are represented in the splice graph, singleton codons are discarded (which does make sense anyway)
    for (Codon c : codons) {
        if (!c.isStop()) {
            // find unspliced successors (can be more than one, when the successor codon itself is spliced! all of them have the same start!)
            int max = c.getEnd() + maxAminoDist * 3;
            for (Codon n : codons
                    .getIntervalsIntersecting(c.getEnd(), c.getEnd() + maxAminoDist * 3, buff.startAndClear(c))
                    .get()) {
                if (!containsInframeStop(sequence.substring(c.getEnd(), n.getStart())))
                    fg.addInteraction(c, n);
                max = n.getStart() + 2;
            }

            // find all spliced successors for each splice junction that comes before n or maxAminoDist
            sg.forEachIntronStartingBetween(c.getEnd(), max + 1, intron -> {
                for (Codon n : codons.getIntervalsIntersecting(intron.getEnd(),
                        intron.getEnd() + maxAminoDist * 3 - (intron.getStart() - c.getEnd()),
                        buff.startAndClear(c, intron)).get())
                    if (!containsInframeStop(SequenceUtils.extractSequence(new ArrayGenomicRegion(c.getStart(),
                            intron.getStart(), intron.getEnd(), n.getStart()), sequence)))
                        fg.addInteraction(c, n, intron);
            });
        }
    }

    int cc = 1;
    for (SimpleDirectedGraph<Codon> g : fg.getWeaklyConnectedComponents()) {
        if (EI.wrap(g.getSources()).mapToDouble(c -> c.getTotalActivity()).sum() == 0)
            continue;

        // iterate longest paths in g
        LinkedList<Codon> topo = g.getTopologicalOrder();
        HashSet<Codon> remInTopo = new HashSet<Codon>(topo);
        remInTopo.removeIf(c -> !stopCodons.contains(c) && !usedForAnno.contains(c));
        HashSet<Codon> removed = new HashSet<Codon>(remInTopo);

        //         double maxPathScore = 0;

        LinkedList<OrfWithCodons> orfs = new LinkedList<OrfWithCodons>();

        int count = 0;
        while (removed.size() < topo.size()) {
            HashMap<Codon, MutablePair<GenomicRegion, Double>> longestPrefixes = new HashMap<Codon, MutablePair<GenomicRegion, Double>>();
            for (Codon c : topo)
                longestPrefixes.put(c, new MutablePair<GenomicRegion, Double>(c,
                        removed.contains(c) ? 0 : (c.getTotalActivity())));

            Codon longestEnd = null;
            HashMap<Codon, Codon> backtracking = new HashMap<Codon, Codon>();

            for (Codon c : topo) {
                //               if (codonsRegion.map(c).getStart()==100_466_118)
                //                  System.out.println(c);
                //               
                //               if (codonsRegion.map(c).getStart()==100_465_842)
                //                  System.out.println(c);

                double len = longestPrefixes.get(c).Item2;
                for (AdjacencyNode<Codon> n = g.getTargets(c); n != null; n = n.next) {
                    MutablePair<GenomicRegion, Double> pref = longestPrefixes.get(n.node);

                    double nnact = removed.contains(n.node) ? 0 : (n.node.getTotalActivity());
                    if (pref.Item2 <= len + nnact) {
                        pref.set(extendFullPath(longestPrefixes.get(c).Item1, c, n.node, n.getLabel()),
                                len + nnact);
                        backtracking.put(n.node, c);
                    }
                }
                if (longestEnd == null || longestPrefixes.get(longestEnd).Item2 <= len)
                    longestEnd = c;

            }

            // determine longest path by backtracking and mark all codons on the path as removed
            ArrayList<Codon> orfCodons = new ArrayList<Codon>();
            double totalActivity = 0;
            double uniqueActivity = 0;
            int uniqueCodons = 0;
            for (Codon c = longestEnd; c != null; c = backtracking.get(c)) {
                if (removed.add(c) && c.getTotalActivity() > 0) {
                    uniqueCodons++;
                    uniqueActivity += c.getTotalActivity();
                }

                if (c.getTotalActivity() > 0) // to remove dummy stop codons
                    orfCodons.add(c);
                totalActivity += c.getTotalActivity();
            }

            //            System.out.println(codonsRegion.map(longestPrefixes.get(longestEnd).Item1));

            if ((uniqueCodons >= minUniqueCodons || uniqueCodons == orfCodons.size())
                    && uniqueActivity > minUniqueActivity && totalActivity > minOrfTotalActivity) {
                Collections.reverse(orfCodons);

                MutablePair<GenomicRegion, Double> triple = longestPrefixes.get(longestEnd);
                ArrayGenomicRegion region = triple.Item1.toArrayGenomicRegion();
                String lastCodon = SequenceUtils.extractSequence(
                        region.map(
                                new ArrayGenomicRegion(region.getTotalLength() - 3, region.getTotalLength())),
                        sequence);

                OrfWithCodons orf = new OrfWithCodons(index, cc, count++, region, orfCodons,
                        stop.containsKey(lastCodon));
                orfs.add(orf);
            }

            //            maxPathScore = Math.max(maxPathScore,totalActivity);
        }

        if (orfs.size() > 1) {

            // they are not necessarily connected!

            LinkedList<OrfWithCodons>[] connected = findConnectedOrfs(orfs);
            orfs.clear();

            for (LinkedList<OrfWithCodons> corfs : connected) {
                for (boolean changed = true; changed && corfs.size() > 1;) {
                    changed = false;

                    if (useEM)
                        inferOverlappingOrfActivitiesEM(corfs);
                    else
                        overlapUniqueCoverage(corfs);

                    Iterator<OrfWithCodons> it = corfs.iterator();
                    while (it.hasNext()) {
                        OrfWithCodons orf = it.next();
                        if (orf.getEstimatedTotalActivity() < minOrfTotalActivity) {
                            it.remove();
                            changed = true;
                        }
                    }
                }

                if (corfs.size() > 1)
                    distributeCodons(corfs);
                orfs.addAll(corfs);
            }

        }

        re.addAll(orfs);

        cc++;
    }

    return re;

}

From source file:org.apache.sysml.hops.codegen.opt.PlanSelectionFuseCostBased.java

private void createAndAddMultiAggPlans(CPlanMemoTable memo, ArrayList<Hop> roots) {
    //collect full aggregations as initial set of candidates
    HashSet<Long> fullAggs = new HashSet<>();
    Hop.resetVisitStatus(roots);/*from www .  ja  va  2 s  .  co m*/
    for (Hop hop : roots)
        rCollectFullAggregates(hop, fullAggs);
    Hop.resetVisitStatus(roots);

    //remove operators with assigned multi-agg plans
    fullAggs.removeIf(p -> memo.contains(p, TemplateType.MAGG));

    //check applicability for further analysis
    if (fullAggs.size() <= 1)
        return;

    if (LOG.isTraceEnabled()) {
        LOG.trace("Found across-partition ua(RC) aggregations: "
                + Arrays.toString(fullAggs.toArray(new Long[0])));
    }

    //collect information for all candidates 
    //(subsumed aggregations, and inputs to fused operators) 
    List<AggregateInfo> aggInfos = new ArrayList<>();
    for (Long hopID : fullAggs) {
        Hop aggHop = memo.getHopRefs().get(hopID);
        AggregateInfo tmp = new AggregateInfo(aggHop);
        for (int i = 0; i < aggHop.getInput().size(); i++) {
            Hop c = HopRewriteUtils.isMatrixMultiply(aggHop) && i == 0
                    ? aggHop.getInput().get(0).getInput().get(0)
                    : aggHop.getInput().get(i);
            rExtractAggregateInfo(memo, c, tmp, TemplateType.CELL);
        }
        if (tmp._fusedInputs.isEmpty()) {
            if (HopRewriteUtils.isMatrixMultiply(aggHop)) {
                tmp.addFusedInput(aggHop.getInput().get(0).getInput().get(0).getHopID());
                tmp.addFusedInput(aggHop.getInput().get(1).getHopID());
            } else
                tmp.addFusedInput(aggHop.getInput().get(0).getHopID());
        }
        aggInfos.add(tmp);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Extracted across-partition ua(RC) aggregation info: ");
        for (AggregateInfo info : aggInfos)
            LOG.trace(info);
    }

    //sort aggregations by num dependencies to simplify merging
    //clusters of aggregations with parallel dependencies
    aggInfos = aggInfos.stream().sorted(Comparator.comparing(a -> a._inputAggs.size()))
            .collect(Collectors.toList());

    //greedy grouping of multi-agg candidates
    boolean converged = false;
    while (!converged) {
        AggregateInfo merged = null;
        for (int i = 0; i < aggInfos.size(); i++) {
            AggregateInfo current = aggInfos.get(i);
            for (int j = i + 1; j < aggInfos.size(); j++) {
                AggregateInfo that = aggInfos.get(j);
                if (current.isMergable(that)) {
                    merged = current.merge(that);
                    aggInfos.remove(j);
                    j--;
                }
            }
        }
        converged = (merged == null);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Merged across-partition ua(RC) aggregation info: ");
        for (AggregateInfo info : aggInfos)
            LOG.trace(info);
    }

    //construct and add multiagg template plans (w/ max 3 aggregations)
    for (AggregateInfo info : aggInfos) {
        if (info._aggregates.size() <= 1)
            continue;
        Long[] aggs = info._aggregates.keySet().toArray(new Long[0]);
        MemoTableEntry me = new MemoTableEntry(TemplateType.MAGG, aggs[0], aggs[1],
                (aggs.length > 2) ? aggs[2] : -1, aggs.length);
        for (int i = 0; i < aggs.length; i++) {
            memo.add(memo.getHopRefs().get(aggs[i]), me);
            addBestPlan(aggs[i], me);
            if (LOG.isTraceEnabled())
                LOG.trace("Added multiagg* plan: " + aggs[i] + " " + me);

        }
    }
}

From source file:org.apache.sysml.hops.codegen.template.PlanSelectionFuseCostBased.java

private void createAndAddMultiAggPlans(CPlanMemoTable memo, ArrayList<Hop> roots) {
    //collect full aggregations as initial set of candidates
    HashSet<Long> fullAggs = new HashSet<Long>();
    Hop.resetVisitStatus(roots);//  w ww .j  a  v  a2 s  .  com
    for (Hop hop : roots)
        rCollectFullAggregates(hop, fullAggs);
    Hop.resetVisitStatus(roots);

    //remove operators with assigned multi-agg plans
    fullAggs.removeIf(p -> memo.contains(p, TemplateType.MultiAggTpl));

    //check applicability for further analysis
    if (fullAggs.size() <= 1)
        return;

    if (LOG.isTraceEnabled()) {
        LOG.trace("Found across-partition ua(RC) aggregations: "
                + Arrays.toString(fullAggs.toArray(new Long[0])));
    }

    //collect information for all candidates 
    //(subsumed aggregations, and inputs to fused operators) 
    List<AggregateInfo> aggInfos = new ArrayList<AggregateInfo>();
    for (Long hopID : fullAggs) {
        Hop aggHop = memo._hopRefs.get(hopID);
        AggregateInfo tmp = new AggregateInfo(aggHop);
        for (int i = 0; i < aggHop.getInput().size(); i++) {
            Hop c = HopRewriteUtils.isMatrixMultiply(aggHop) && i == 0
                    ? aggHop.getInput().get(0).getInput().get(0)
                    : aggHop.getInput().get(i);
            rExtractAggregateInfo(memo, c, tmp, TemplateType.CellTpl);
        }
        if (tmp._fusedInputs.isEmpty()) {
            if (HopRewriteUtils.isMatrixMultiply(aggHop)) {
                tmp.addFusedInput(aggHop.getInput().get(0).getInput().get(0).getHopID());
                tmp.addFusedInput(aggHop.getInput().get(1).getHopID());
            } else
                tmp.addFusedInput(aggHop.getInput().get(0).getHopID());
        }
        aggInfos.add(tmp);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Extracted across-partition ua(RC) aggregation info: ");
        for (AggregateInfo info : aggInfos)
            LOG.trace(info);
    }

    //sort aggregations by num dependencies to simplify merging
    //clusters of aggregations with parallel dependencies
    aggInfos = aggInfos.stream().sorted(Comparator.comparing(a -> a._inputAggs.size()))
            .collect(Collectors.toList());

    //greedy grouping of multi-agg candidates
    boolean converged = false;
    while (!converged) {
        AggregateInfo merged = null;
        for (int i = 0; i < aggInfos.size(); i++) {
            AggregateInfo current = aggInfos.get(i);
            for (int j = i + 1; j < aggInfos.size(); j++) {
                AggregateInfo that = aggInfos.get(j);
                if (current.isMergable(that)) {
                    merged = current.merge(that);
                    aggInfos.remove(j);
                    j--;
                }
            }
        }
        converged = (merged == null);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Merged across-partition ua(RC) aggregation info: ");
        for (AggregateInfo info : aggInfos)
            LOG.trace(info);
    }

    //construct and add multiagg template plans (w/ max 3 aggregations)
    for (AggregateInfo info : aggInfos) {
        if (info._aggregates.size() <= 1)
            continue;
        Long[] aggs = info._aggregates.keySet().toArray(new Long[0]);
        MemoTableEntry me = new MemoTableEntry(TemplateType.MultiAggTpl, aggs[0], aggs[1],
                (aggs.length > 2) ? aggs[2] : -1);
        for (int i = 0; i < aggs.length; i++) {
            memo.add(memo._hopRefs.get(aggs[i]), me);
            addBestPlan(aggs[i], me);
            if (LOG.isTraceEnabled())
                LOG.trace("Added multiagg* plan: " + aggs[i] + " " + me);

        }
    }
}