Example usage for java.util TreeSet retainAll

List of usage examples for java.util TreeSet retainAll

Introduction

In this page you can find the example usage for java.util TreeSet retainAll.

Prototype

boolean retainAll(Collection<?> c);

Source Link

Document

Retains only the elements in this set that are contained in the specified collection (optional operation).

Usage

From source file:ch.unil.genescore.pathway.GeneSetLibrary.java

License:asdf

public void computeApproxPathwayCorrelation() {

    DenseMatrix corMat = new DenseMatrix(geneSets_.size(), geneSets_.size());
    for (int i = 0; i < geneSets_.size(); i++) {
        GeneSet leftSet = geneSets_.get(i);
        double leftSize = leftSet.genes_.size();
        for (int j = 0; j < geneSets_.size(); j++) {
            GeneSet rightSet = geneSets_.get(j);
            double rightSize = rightSet.genes_.size();
            HashSet<Gene> unpackedMetaGenes = new HashSet<Gene>();
            HashSet<Gene> allRightGenes = new HashSet<Gene>();
            if (null != rightSet.getMetaGenes())
                for (MetaGene mg : rightSet.getMetaGenes()) {
                    unpackedMetaGenes.addAll(mg.getGenes());
                }//from w w w .  j  a  v a2  s . c  o  m

            allRightGenes.addAll(unpackedMetaGenes);
            allRightGenes.addAll(rightSet.genes_);
            allRightGenes.removeAll(rightSet.getMetaGenes());

            HashSet<Gene> copiedLeftGenes = new HashSet<Gene>(leftSet.genes_);
            copiedLeftGenes.retainAll(allRightGenes);
            double count = copiedLeftGenes.size();
            if (null != leftSet.getMetaGenes())
                for (MetaGene mg : leftSet.getMetaGenes()) {
                    TreeSet<Gene> mgSetCopy = new TreeSet<Gene>(mg.getGenes());
                    mgSetCopy.retainAll(allRightGenes);
                    if (!mgSetCopy.isEmpty()) {
                        count++;
                    }
                }
            double corr = count / Math.sqrt(leftSize * rightSize);
            corMat.set(i, j, corr);
            //corMat.set(j, i, corr);
        }
    }
    pathwayCorMat_ = corMat;
}

From source file:eu.numberfour.n4js.npmexporter.ui.NpmExportWizard.java

@Override
public void init(IWorkbench targetWorkbench, IStructuredSelection currentSelection) {

    // this.selection = currentSelection;

    List<?> selectedResources = IDE.computeSelectedResources(currentSelection);

    // Find all projects
    Set<IProject> projects = selectedResources.stream().filter(m -> m instanceof IResource)
            .map(m -> ((IResource) m).getProject()).filter(p -> p.isOpen()) // only open projects
            .collect(Collectors.toSet());
    // make the behavior predictable by ordering:
    TreeSet<IProject> sortedProjects = Sets
            .<IProject>newTreeSet((a, b) -> a.getName().compareToIgnoreCase(b.getName()));
    sortedProjects.addAll(projects);//  w  ww  .j  av  a 2  s.  c  om

    // 0) turn into IN4JSProject and give and process further.
    // a) find out which projects fulfill the npm-"exportable"-contract
    // b) give back a list to the user what to export,
    // c) disable things not fullfilling the contract.
    // d) take choosing from the list and pass to exporter in non-ui package.

    // 0)
    List<IN4JSEclipseProject> rawN4jsProjects = Lists.newArrayList();
    iP2in4jsP = HashBiMap.create();
    for (IProject iProject : sortedProjects) {
        IN4JSEclipseProject mappedIn4jsProject = map2In4js(iProject);
        if (mappedIn4jsProject != null) {
            rawN4jsProjects.add(mappedIn4jsProject);

            iP2in4jsP.put(iProject, mappedIn4jsProject);
        }
    }

    // remove all filtered out Non-N4JS-projects.
    sortedProjects.retainAll(iP2in4jsP.keySet());

    // if (!selectedResources.isEmpty()) {
    // this.selection = new StructuredSelection(sortedProjects.toArray());
    // }

    setWindowTitle("N4JS to npm Export");
    setNeedsProgressMonitor(true);

    // exportPage = new ExportSelectionPage("Export Page", rawN4jsProjects, labelProvider);
    exportPage = new ExportSelectionPage("Export Page", newArrayList(sortedProjects));
    if (runTools())
        toolRunnerPage = new NpmToolRunnerPage("npm Execution Page");
    comparePage = new PackageJsonComparePage("Compare package.json Page");

    pageListener = new IPageChangedListener() {

        @Override
        public void pageChanged(PageChangedEvent event) {
            if (event.getSelectedPage() == comparePage) {
                udpatePackagJasonComparison();
            }
        }
    };
}

From source file:mondrian.olap.Util.java

/**
 * Returns the intersection of two sorted sets. Does not modify either set.
 *
 * <p>Optimized for the case that both sets are {@link ArraySortedSet}.</p>
 *
 * @param set1 First set/*  w  w w. j  a v a  2 s .c o  m*/
 * @param set2 Second set
 * @return Intersection of the sets
 */
public static <E extends Comparable> SortedSet<E> intersect(SortedSet<E> set1, SortedSet<E> set2) {
    if (set1.isEmpty()) {
        return set1;
    }
    if (set2.isEmpty()) {
        return set2;
    }
    if (!(set1 instanceof ArraySortedSet) || !(set2 instanceof ArraySortedSet)) {
        final TreeSet<E> set = new TreeSet<E>(set1);
        set.retainAll(set2);
        return set;
    }
    final Comparable<?>[] result = new Comparable[Math.min(set1.size(), set2.size())];
    final Iterator<E> it1 = set1.iterator();
    final Iterator<E> it2 = set2.iterator();
    int i = 0;
    E e1 = it1.next();
    E e2 = it2.next();
    for (;;) {
        final int compare = e1.compareTo(e2);
        if (compare == 0) {
            result[i++] = e1;
            if (!it1.hasNext() || !it2.hasNext()) {
                break;
            }
            e1 = it1.next();
            e2 = it2.next();
        } else if (compare == 1) {
            if (!it2.hasNext()) {
                break;
            }
            e2 = it2.next();
        } else {
            if (!it1.hasNext()) {
                break;
            }
            e1 = it1.next();
        }
    }
    return new ArraySortedSet(result, 0, i);
}

From source file:com.datatorrent.stram.StreamingContainerManager.java

/**
 * Compute checkpoints required for a given operator instance to be recovered.
 * This is done by looking at checkpoints available for downstream dependencies first,
 * and then selecting the most recent available checkpoint that is smaller than downstream.
 *
 * @param operator Operator instance for which to find recovery checkpoint
 * @param ctx      Context into which to collect traversal info
 *//*from   w w w  .  j  av  a  2 s.co  m*/
public void updateRecoveryCheckpoints(PTOperator operator, UpdateCheckpointsContext ctx) {
    if (operator.getRecoveryCheckpoint().windowId < ctx.committedWindowId.longValue()) {
        ctx.committedWindowId.setValue(operator.getRecoveryCheckpoint().windowId);
    }

    if (operator.getState() == PTOperator.State.ACTIVE && (ctx.currentTms
            - operator.stats.lastWindowIdChangeTms) > operator.stats.windowProcessingTimeoutMillis) {
        // if the checkpoint is ahead, then it is not blocked but waiting for activation (state-less recovery, at-most-once)
        if (ctx.committedWindowId.longValue() >= operator.getRecoveryCheckpoint().windowId) {
            LOG.debug("Marking operator {} blocked committed window {}, recovery window {}", operator,
                    Codec.getStringWindowId(ctx.committedWindowId.longValue()),
                    Codec.getStringWindowId(operator.getRecoveryCheckpoint().windowId));
            ctx.blocked.add(operator);
        }
    }

    // the most recent checkpoint eligible for recovery based on downstream state
    Checkpoint maxCheckpoint = Checkpoint.INITIAL_CHECKPOINT;

    Set<OperatorMeta> checkpointGroup = ctx.checkpointGroups.get(operator.getOperatorMeta());
    if (checkpointGroup == null) {
        checkpointGroup = Collections.singleton(operator.getOperatorMeta());
    }
    // find intersection of checkpoints that group can collectively move to
    TreeSet<Checkpoint> commonCheckpoints = new TreeSet<>(new Checkpoint.CheckpointComparator());
    synchronized (operator.checkpoints) {
        commonCheckpoints.addAll(operator.checkpoints);
    }
    Set<PTOperator> groupOpers = new HashSet<>(checkpointGroup.size());
    boolean pendingDeploy = operator.getState() == PTOperator.State.PENDING_DEPLOY;
    if (checkpointGroup.size() > 1) {
        for (OperatorMeta om : checkpointGroup) {
            Collection<PTOperator> operators = plan.getAllOperators(om);
            for (PTOperator groupOper : operators) {
                synchronized (groupOper.checkpoints) {
                    commonCheckpoints.retainAll(groupOper.checkpoints);
                }
                // visit all downstream operators of the group
                ctx.visited.add(groupOper);
                groupOpers.add(groupOper);
                pendingDeploy |= operator.getState() == PTOperator.State.PENDING_DEPLOY;
            }
        }
        // highest common checkpoint
        if (!commonCheckpoints.isEmpty()) {
            maxCheckpoint = commonCheckpoints.last();
        }
    } else {
        // without logical grouping, treat partitions as independent
        // this is especially important for parallel partitioning
        ctx.visited.add(operator);
        groupOpers.add(operator);
        maxCheckpoint = operator.getRecentCheckpoint();
        if (ctx.recovery && maxCheckpoint.windowId == Stateless.WINDOW_ID && operator.isOperatorStateLess()) {
            long currentWindowId = WindowGenerator.getWindowId(ctx.currentTms, this.vars.windowStartMillis,
                    this.getLogicalPlan().getValue(LogicalPlan.STREAMING_WINDOW_SIZE_MILLIS));
            maxCheckpoint = new Checkpoint(currentWindowId, 0, 0);
        }
    }

    // DFS downstream operators
    for (PTOperator groupOper : groupOpers) {
        for (PTOperator.PTOutput out : groupOper.getOutputs()) {
            for (PTOperator.PTInput sink : out.sinks) {
                PTOperator sinkOperator = sink.target;
                if (groupOpers.contains(sinkOperator)) {
                    continue; // downstream operator within group
                }
                if (!ctx.visited.contains(sinkOperator)) {
                    // downstream traversal
                    updateRecoveryCheckpoints(sinkOperator, ctx);
                }
                // recovery window id cannot move backwards
                // when dynamically adding new operators
                if (sinkOperator.getRecoveryCheckpoint().windowId >= operator
                        .getRecoveryCheckpoint().windowId) {
                    maxCheckpoint = Checkpoint.min(maxCheckpoint, sinkOperator.getRecoveryCheckpoint());
                }

                if (ctx.blocked.contains(sinkOperator)) {
                    if (sinkOperator.stats.getCurrentWindowId() == operator.stats.getCurrentWindowId()) {
                        // downstream operator is blocked by this operator
                        ctx.blocked.remove(sinkOperator);
                    }
                }
            }
        }
    }

    // find the common checkpoint that is <= downstream recovery checkpoint
    if (!commonCheckpoints.contains(maxCheckpoint)) {
        if (!commonCheckpoints.isEmpty()) {
            maxCheckpoint = Objects.firstNonNull(commonCheckpoints.floor(maxCheckpoint), maxCheckpoint);
        }
    }

    for (PTOperator groupOper : groupOpers) {
        // checkpoint frozen during deployment
        if (!pendingDeploy || ctx.recovery) {
            // remove previous checkpoints
            Checkpoint c1 = Checkpoint.INITIAL_CHECKPOINT;
            LinkedList<Checkpoint> checkpoints = groupOper.checkpoints;
            synchronized (checkpoints) {
                if (!checkpoints.isEmpty() && (checkpoints.getFirst()).windowId <= maxCheckpoint.windowId) {
                    c1 = checkpoints.getFirst();
                    Checkpoint c2;
                    while (checkpoints.size() > 1
                            && ((c2 = checkpoints.get(1)).windowId) <= maxCheckpoint.windowId) {
                        checkpoints.removeFirst();
                        //LOG.debug("Checkpoint to delete: operator={} windowId={}", operator.getName(), c1);
                        this.purgeCheckpoints.add(new Pair<PTOperator, Long>(groupOper, c1.windowId));
                        c1 = c2;
                    }
                } else {
                    if (ctx.recovery && checkpoints.isEmpty() && groupOper.isOperatorStateLess()) {
                        LOG.debug("Adding checkpoint for stateless operator {} {}", groupOper,
                                Codec.getStringWindowId(maxCheckpoint.windowId));
                        c1 = groupOper.addCheckpoint(maxCheckpoint.windowId, this.vars.windowStartMillis);
                    }
                }
            }
            //LOG.debug("Operator {} checkpoints: commit {} recent {}", new Object[] {operator.getName(), c1, operator.checkpoints});
            groupOper.setRecoveryCheckpoint(c1);
        } else {
            LOG.debug("Skipping checkpoint update {} during {}", groupOper, groupOper.getState());
        }
    }

}