Example usage for com.google.common.collect Multimap remove

List of usage examples for com.google.common.collect Multimap remove

Introduction

In this page you can find the example usage for com.google.common.collect Multimap remove.

Prototype

boolean remove(@Nullable Object key, @Nullable Object value);

Source Link

Document

Removes a single key-value pair with the key key and the value value from this multimap, if such exists.

Usage

From source file:com.facebook.buck.android.apkmodule.APKModuleGraph.java

/**
 * For each seed target, find its reachable targets and mark them in a multimap as being reachable
 * by that module for later sorting into exclusive and shared targets
 *
 * @return the Multimap containing targets and the seed modules that contain them
 *//*from   ww  w  . j av a 2 s  . c o m*/
private Multimap<BuildTarget, String> mapTargetsToContainingModules() {
    Multimap<BuildTarget, String> targetToContainingApkModuleNameMap = MultimapBuilder.treeKeys()
            .treeSetValues().build();
    for (Map.Entry<String, List<BuildTarget>> seedConfig : getSeedConfigMap().get().entrySet()) {
        String seedModuleName = seedConfig.getKey();
        for (BuildTarget seedTarget : seedConfig.getValue()) {
            targetToContainingApkModuleNameMap.put(seedTarget, seedModuleName);
            new AbstractBreadthFirstTraversal<TargetNode<?>>(targetGraph.get(seedTarget)) {
                @Override
                public ImmutableSet<TargetNode<?>> visit(TargetNode<?> node) {

                    ImmutableSet.Builder<TargetNode<?>> depsBuilder = ImmutableSet.builder();
                    for (BuildTarget depTarget : node.getBuildDeps()) {
                        if (!isInRootModule(depTarget) && !isSeedTarget(depTarget)) {
                            depsBuilder.add(targetGraph.get(depTarget));
                            targetToContainingApkModuleNameMap.put(depTarget, seedModuleName);
                        }
                    }
                    return depsBuilder.build();
                }
            }.start();
        }
    }
    // Now to generate the minimal covers of APKModules for each set of APKModules that contain
    // a buildTarget
    DirectedAcyclicGraph<String> declaredDependencies = getDeclaredDependencyGraph();
    Multimap<BuildTarget, String> targetModuleEntriesToRemove = MultimapBuilder.treeKeys().treeSetValues()
            .build();
    for (BuildTarget key : targetToContainingApkModuleNameMap.keySet()) {
        Collection<String> modulesForTarget = targetToContainingApkModuleNameMap.get(key);
        new AbstractBreadthFirstTraversal<String>(modulesForTarget) {
            @Override
            public Iterable<String> visit(String moduleName) throws RuntimeException {
                Collection<String> dependentModules = declaredDependencies.getIncomingNodesFor(moduleName);
                for (String dependent : dependentModules) {
                    if (modulesForTarget.contains(dependent)) {
                        targetModuleEntriesToRemove.put(key, dependent);
                    }
                }
                return dependentModules;
            }
        }.start();
    }
    for (Map.Entry<BuildTarget, String> entryToRemove : targetModuleEntriesToRemove.entries()) {
        targetToContainingApkModuleNameMap.remove(entryToRemove.getKey(), entryToRemove.getValue());
    }
    return targetToContainingApkModuleNameMap;
}

From source file:com.cloudant.sync.datastore.DatastoreImpl.java

/**
 * Removes revisions present in the datastore from the input map.
 *
 * @param revisions an multimap from document id to set of revisions. The
 *                  map is modified in place for performance consideration.
 *///from w  w w.  j  av  a2s . com
void revsDiffBatch(SQLDatabase db, Multimap<String, String> revisions) throws DatastoreException {

    final String sql = String.format(
            "SELECT docs.docid, revs.revid FROM docs, revs "
                    + "WHERE docs.doc_id = revs.doc_id AND docs.docid IN (%s) AND revs.revid IN (%s) "
                    + "ORDER BY docs.docid",
            DatabaseUtils.makePlaceholders(revisions.keySet().size()),
            DatabaseUtils.makePlaceholders(revisions.size()));

    String[] args = new String[revisions.keySet().size() + revisions.size()];
    String[] keys = revisions.keySet().toArray(new String[revisions.keySet().size()]);
    String[] values = revisions.values().toArray(new String[revisions.size()]);
    System.arraycopy(keys, 0, args, 0, revisions.keySet().size());
    System.arraycopy(values, 0, args, revisions.keySet().size(), revisions.size());

    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, args);
        while (cursor.moveToNext()) {
            String docId = cursor.getString(0);
            String revId = cursor.getString(1);
            revisions.remove(docId, revId);
        }
    } catch (SQLException e) {
        throw new DatastoreException(e);
    } finally {
        DatabaseUtils.closeCursorQuietly(cursor);
    }
}

From source file:org.sosy_lab.cpachecker.cfa.postprocessing.global.singleloop.CFASingleLoopTransformation.java

/**
 * Simplify the new graph by removing empty subgraphs and dummy edges.
 *
 * @param pStartNode the start node of the new control flow automaton.
 * @param pNewPredecessorsToPC the mapping of program counter value assignment predecessors to program counter values. Must be mutable.
 * @param pNewSuccessorsToPC the mapping of program counter value assumption successors to program counter values. Must be mutable.
 * @param pGlobalNewToOld//www.  ja va2s  . c o  m
 * @throws InterruptedException if a shutdown has been requested by the registered shutdown notifier.
 */
private void simplify(CFANode pStartNode, Multimap<Integer, CFANode> pNewPredecessorsToPC,
        Map<Integer, CFANode> pNewSuccessorsToPC, BiMap<Integer, CFANode> pImmutableNewSuccessorsToPC,
        Map<CFANode, CFANode> pGlobalNewToOld) throws InterruptedException {
    Map<CFANode, Integer> pcToNewSuccessors = pImmutableNewSuccessorsToPC.inverse();

    for (int replaceablePCValue : new ArrayList<>(pNewPredecessorsToPC.keySet())) {
        this.shutdownNotifier.shutdownIfNecessary();
        CFANode newSuccessor = pNewSuccessorsToPC.get(replaceablePCValue);
        List<CFANode> tailsOfRedundantSubgraph = new ArrayList<>(pNewPredecessorsToPC.get(replaceablePCValue));
        for (CFANode tailOfRedundantSubgraph : tailsOfRedundantSubgraph) {
            Integer precedingPCValue;
            CFAEdge dummyEdge;
            // If a subgraph consists only of a dummy edge, eliminate it completely
            if (tailOfRedundantSubgraph.getNumEnteringEdges() == 1
                    && isDummyEdge(dummyEdge = tailOfRedundantSubgraph.getEnteringEdge(0))
                    && dummyEdge.getPredecessor().getNumEnteringEdges() == 0
                    && (precedingPCValue = pcToNewSuccessors.get(dummyEdge.getPredecessor())) != null) {
                Integer predToRemove = pcToNewSuccessors.get(newSuccessor);
                CFANode removed = pNewSuccessorsToPC.remove(predToRemove);
                assert removed == newSuccessor;
                for (CFANode removedPredecessor : pNewPredecessorsToPC.removeAll(predToRemove)) {
                    pNewPredecessorsToPC.put(precedingPCValue, removedPredecessor);
                }
                pNewPredecessorsToPC.remove(precedingPCValue, tailOfRedundantSubgraph);
                pNewSuccessorsToPC.remove(precedingPCValue);
                pNewSuccessorsToPC.put(precedingPCValue, newSuccessor);
            }
        }
    }
    for (CFAEdge oldDummyEdge : findEdges(DUMMY_EDGE_PREDICATE, pStartNode)) {
        this.shutdownNotifier.shutdownIfNecessary();
        CFANode successor = pGlobalNewToOld.get(oldDummyEdge.getSuccessor());
        for (CFAEdge edge : CFAUtils.enteringEdges(successor).toList()) {
            if (isDummyEdge(edge)) {
                removeFromNodes(edge);
                CFANode predecessor = edge.getPredecessor();
                /*
                 * If the subgraph is entered by a dummy edge adjust the program
                 * counter successor.
                 */
                Integer precedingPCValue;
                if (predecessor.getNumEnteringEdges() == 0
                        && (precedingPCValue = pcToNewSuccessors.get(predecessor)) != null) {
                    pcToNewSuccessors.remove(predecessor);
                    pNewSuccessorsToPC.remove(precedingPCValue);
                    pNewSuccessorsToPC.put(precedingPCValue, edge.getSuccessor());
                } else {
                    /*
                     * If the dummy edge is somewhere in between, replace its
                     * predecessor by its successor in the graph.
                     */
                    for (CFAEdge edgeEnteringPredecessor : CFAUtils.enteringEdges(predecessor).toList()) {
                        removeFromNodes(edgeEnteringPredecessor);
                        edgeEnteringPredecessor = copyCFAEdgeWithNewNodes(edgeEnteringPredecessor,
                                edgeEnteringPredecessor.getPredecessor(), successor, pGlobalNewToOld);
                        addToNodes(edgeEnteringPredecessor);
                    }
                }
            }
        }
    }
}

From source file:org.apache.cassandra.service.StorageService.java

/**
 * Called when an endpoint is removed from the ring. This function checks
 * whether this node becomes responsible for new ranges as a
 * consequence and streams data if needed.
 *
 * This is rather ineffective, but it does not matter so much
 * since this is called very seldom//from  w w w. j av  a  2 s  .c o m
 *
 * @param endpoint the node that left
 */
private void restoreReplicaCount(InetAddress endpoint, final InetAddress notifyEndpoint) {
    final Multimap<InetAddress, String> fetchSources = HashMultimap.create();
    Multimap<String, Map.Entry<InetAddress, Collection<Range>>> rangesToFetch = HashMultimap.create();

    final InetAddress myAddress = FBUtilities.getLocalAddress();

    for (String table : DatabaseDescriptor.getNonSystemTables()) {
        Multimap<Range, InetAddress> changedRanges = getChangedRangesForLeaving(table, endpoint);
        Set<Range> myNewRanges = new HashSet<Range>();
        for (Map.Entry<Range, InetAddress> entry : changedRanges.entries()) {
            if (entry.getValue().equals(myAddress))
                myNewRanges.add(entry.getKey());
        }
        Multimap<InetAddress, Range> sourceRanges = getNewSourceRanges(table, myNewRanges);
        for (Map.Entry<InetAddress, Collection<Range>> entry : sourceRanges.asMap().entrySet()) {
            fetchSources.put(entry.getKey(), table);
            rangesToFetch.put(table, entry);
        }
    }

    for (final String table : rangesToFetch.keySet()) {
        for (Map.Entry<InetAddress, Collection<Range>> entry : rangesToFetch.get(table)) {
            final InetAddress source = entry.getKey();
            Collection<Range> ranges = entry.getValue();
            final Runnable callback = new Runnable() {
                public void run() {
                    synchronized (fetchSources) {
                        fetchSources.remove(source, table);
                        if (fetchSources.isEmpty())
                            sendReplicationNotification(myAddress, notifyEndpoint);
                    }
                }
            };
            if (logger_.isDebugEnabled())
                logger_.debug("Requesting from " + source + " ranges " + StringUtils.join(ranges, ", "));
            StreamIn.requestRanges(source, table, ranges, callback, OperationType.RESTORE_REPLICA_COUNT);
        }
    }
}

From source file:com.axelor.db.JPA.java

@SuppressWarnings("all")
private static <T extends Model> T _edit(Class<T> klass, Map<String, Object> values, Set<Model> visited,
        Multimap<String, Long> edited) {

    if (values == null)
        return null;

    Mapper mapper = Mapper.of(klass);/*from  www . ja v a 2s.  c  om*/
    Long id = null;
    T bean = null;

    try {
        id = Long.valueOf(values.get("id").toString());
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException(e);
    } catch (NullPointerException e) {
    }

    if (id == null || id <= 0) {
        id = null;
        try {
            bean = klass.newInstance();
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    } else {
        bean = JPA.em().find(klass, id);
        if (bean == null) {
            throw new OptimisticLockException(new StaleObjectStateException(klass.getName(), id));
        }
    }

    // optimistic concurrency check
    Integer beanVersion = (Integer) values.get("version");
    boolean beanChanged = false;

    if (visited.contains(bean) && beanVersion == null) {
        return bean;
    }

    visited.add(bean);

    // don't update reference objects
    if (id != null && (beanVersion == null || edited.containsEntry(klass.getName(), id))) {
        return bean;
    }
    if (id != null) {
        edited.put(klass.getName(), id);
    }

    for (String name : values.keySet()) {

        Property p = mapper.getProperty(name);
        if (p == null || p.isPrimary() || p.isVersion() || mapper.getSetter(name) == null)
            continue;

        Object value = values.get(name);
        Class<Model> target = (Class<Model>) p.getTarget();

        if (p.isCollection()) {

            Collection items = new ArrayList();
            if (Set.class.isAssignableFrom(p.getJavaType()))
                items = new HashSet();

            if (value instanceof Collection) {
                for (Object val : (Collection) value) {
                    if (val instanceof Map) {
                        if (p.getMappedBy() != null) {
                            if (val instanceof ImmutableMap)
                                val = Maps.newHashMap((Map) val);
                            ((Map) val).remove(p.getMappedBy());
                        }
                        Model item = _edit(target, (Map) val, visited, edited);
                        items.add(p.setAssociation(item, bean));
                    } else if (val instanceof Number) {
                        items.add(JPA.find(target, Long.parseLong(val.toString())));
                    }
                }
            }

            Object old = mapper.get(bean, name);
            if (old instanceof Collection) {
                boolean changed = ((Collection) old).size() != items.size();
                if (!changed) {
                    for (Object item : items) {
                        if (!((Collection) old).contains(item)) {
                            changed = true;
                            break;
                        }
                    }
                }
                if (changed) {
                    if (p.isOrphan()) {
                        for (Object item : (Collection) old) {
                            if (!items.contains(item)) {
                                p.setAssociation(item, null);
                            }
                        }
                    }
                    p.clear(bean);
                    p.addAll(bean, items);
                    beanChanged = true;
                }
                continue;
            }
            if (p.getType() == PropertyType.MANY_TO_MANY && p.getMappedBy() != null) {
                p.addAll(bean, items);
            }
            value = items;
        } else if (value instanceof Map) {
            value = _edit(target, (Map) value, visited, edited);
        }
        Object oldValue = mapper.set(bean, name, value);
        if (p.valueChanged(bean, oldValue)) {
            beanChanged = true;
        }
    }

    if (beanChanged) {
        checkVersion(bean, beanVersion);
    } else if (id != null) {
        edited.remove(klass.getName(), id);
    }

    return bean;
}

From source file:moa2014.MOA2014.java

public static void principal(int[][] matrizatual) {

    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int counter = 1;

    while (!open_list.isEmpty()) {
        System.out.println("Arvores processadas: " + counter);
        Iterator iterator = open_list.keySet().iterator();
        /*//from   www  .ja v a2 s . com
        Iterator iterator2 = open_list.keySet().iterator();
                
        while (iterator2.hasNext()) {
          Integer key = (Integer) iterator2.next();
          System.out.println("key : " + key + " value :" + open_list.get(key));
        }
                
        Scanner scanner = new Scanner( System.in );
        String input = scanner.nextLine();
        */
        counter++;
        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        System.out.println("Altura: " + altura);
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            System.out.println("Arvores processadas: " + counter);
            System.out.println("Custo: " + estadomenor.getCusto());
            break;
        }
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {

                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {

                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {

                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {

                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }

}

From source file:moa2014.MOAH12014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {

        Iterator iterator = open_list.keySet().iterator();
        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }/*from  ww w .j a  v a 2s  .com*/
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:moa2014.MOAH32014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {
        Iterator iterator = open_list.keySet().iterator();
        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }/*ww  w  .j av  a  2  s .com*/
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:moa2014.MOAH22014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {
        Iterator iterator = open_list.keySet().iterator();

        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }//from   w  w  w. ja  v  a  2  s .c o  m
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:moa2014.MOAH42014.java

public static int principal(int[][] matrizatual, double c1, double c2, double c3) {
    long startTime = System.currentTimeMillis();
    Multimap<Double, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    double h1 = c1 * h1(matrizatual);
    double h2 = c2 * h2(matrizatual);
    double h3 = c3 * h3(matrizatual);
    double difmatrizatual = h1 + h2 + h3;

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {

        Iterator iterator = open_list.keySet().iterator();

        Double key = (Double) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();

        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = h1(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }//from  www  . jav  a2  s .  c o  m
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}