Example usage for java.util Deque removeLast

List of usage examples for java.util Deque removeLast

Introduction

In this page you can find the example usage for java.util Deque removeLast.

Prototype

E removeLast();

Source Link

Document

Retrieves and removes the last element of this deque.

Usage

From source file:org.wso2.carbon.uuf.internal.core.create.DependencyTreeParser.java

public static Result parse(List<String> dependencyTreeLines) {
    // Flattened dependencies map.
    // key   = component name
    // value = all dependencies of the 'key'
    SetMultimap<String, String> flattenedDependencies = HashMultimap.create();
    // Leveled dependencies list.
    // index       = dependency level, index 0 == root component's dependencies
    // List.get(i) = set of dependencies in level i
    List<Set<ComponentData>> leveledDependencies = new ArrayList<>(6);

    int previousLevel = 0;
    String previousComponentName = null;
    Deque<Pair<String, List<String>>> parentNodesStack = new LinkedList<>();

    for (int i = 0; i < dependencyTreeLines.size(); i++) {
        String line = dependencyTreeLines.get(i);
        int level = countLevel(line);
        int jump = (level - previousLevel);
        ComponentData currentComponent = getComponentData(line);

        if (level < leveledDependencies.size()) {
            leveledDependencies.get(level).add(currentComponent);
        } else {/*from  w  w w . ja  va  2 s  . co  m*/
            Set<ComponentData> set = new HashSet<>();
            set.add(currentComponent);
            leveledDependencies.add(level, set);
        }

        if (i == 0) {
            // Very first leaf dependency.
            previousComponentName = currentComponent.name;
            continue;
        }
        if (jump < 0) {
            // Dependency level decreased, so remove entries from the stack.
            for (int j = Math.abs(jump); j > 0; j--) {
                Pair<String, List<String>> entry = parentNodesStack.removeLast();
                flattenedDependencies.putAll(entry.getKey(), entry.getValue());
            }
        } else if (jump > 0) { // jump == 1
            // Dependency level increased, so add an entry to the stack.
            parentNodesStack.add(new ImmutablePair<>(previousComponentName, new ArrayList<>(3)));
        }
        // (jump == 0): Same dependency level, no need to change the stack.

        // Add current component name to all parent nodes as a dependency.
        for (Pair<String, List<String>> entry : parentNodesStack) {
            entry.getValue().add(currentComponent.name);
        }

        previousLevel = level;
        previousComponentName = currentComponent.name;
    }
    // If there is any remaining stack elements, add them to the flattenedDependencies.
    for (Pair<String, List<String>> entry : parentNodesStack) {
        flattenedDependencies.putAll(entry.getKey(), entry.getValue());
    }

    return new Result(flattenedDependencies, leveledDependencies);
}

From source file:ro.hasna.ts.math.ml.distance.DynamicTimeWarpingDistance.java

/**
 * <p>/*from www .ja  v  a2  s. c  o  m*/
 * Reference:
 * Daniel Lemire (2008)
 * <i>Faster Sequential Search with a Two-Pass Dynamic-Time-Warping Lower Bound</i>
 * </p>
 *
 * @param v      the vector
 * @param n      the length of the vector
 * @param radius the Sakoe-Chiba band radius
 * @return time series envelope
 */
protected Envelope computeLemireEnvelope(double[] v, int n, int radius) {
    int w = 2 * radius + 1;
    double[] upper = new double[n];
    double[] lower = new double[n];
    int i;

    Deque<Integer> upperList = new LinkedList<>();
    Deque<Integer> lowerList = new LinkedList<>();
    upperList.addLast(0);
    lowerList.addLast(0);
    for (i = 1; i < n; i++) {
        if (i >= w) {
            upper[i - w] = v[upperList.getFirst()];
            lower[i - w] = v[lowerList.getFirst()];
        }
        if (v[i] > v[i - 1]) {
            upperList.removeLast();
            while (!upperList.isEmpty() && v[i] > v[upperList.getLast()]) {
                upperList.removeLast();
            }
        } else {
            lowerList.removeLast();
            while (!lowerList.isEmpty() && v[i] < v[lowerList.getLast()]) {
                lowerList.removeLast();
            }
        }
        upperList.addLast(i);
        lowerList.addLast(i);
        if (i == 2 * w + upperList.getFirst()) {
            upperList.removeFirst();
        } else if (i == 2 * w + lowerList.getFirst()) {
            lowerList.removeFirst();
        }
    }

    for (i = n; i < n + w; i++) {
        upper[i - w] = v[upperList.getFirst()];
        lower[i - w] = v[lowerList.getFirst()];
        if (i - upperList.getFirst() >= 2 * w) {
            upperList.removeFirst();
        }
        if (i - lowerList.getFirst() >= 2 * w) {
            lowerList.removeFirst();
        }
    }

    return new Envelope(lower, upper);
}

From source file:uniol.apt.adt.automaton.FiniteAutomatonUtility.java

static private List<String> findAcceptedWord(DeterministicFiniteAutomaton dfa) {
    Set<DFAState> statesSeen = new HashSet<>();
    LinkedList<String> word = new LinkedList<>();
    Deque<Pair<DFAState, Iterator<Symbol>>> trace = new LinkedList<>();
    DFAState initial = dfa.getInitialState();
    trace.add(new Pair<>(initial, initial.getDefinedSymbols().iterator()));

    while (!trace.isEmpty()) {
        InterrupterRegistry.throwIfInterruptRequestedForCurrentThread();
        Pair<DFAState, Iterator<Symbol>> pair = trace.peekLast();
        if (!pair.getSecond().hasNext()) {
            trace.removeLast();
            word.pollLast();//from ww w  .  j a  v a  2s  .c o m
        } else {
            Symbol symbol = pair.getSecond().next();
            DFAState nextState = pair.getFirst().getFollowingState(symbol);

            // Only follow this state if we haven't followed it yet before
            if (statesSeen.add(nextState)) {
                trace.add(new Pair<>(nextState, nextState.getDefinedSymbols().iterator()));
                word.add(symbol.getEvent());

                if (nextState.isFinalState())
                    return word;
            }
        }
    }

    return null;
}

From source file:uniol.apt.adt.automaton.FiniteAutomatonUtility.java

/**
 * Find a word whose prefixes (including the word) conform to a given predicate and which itself also conforms
 * to a second predicate.//from  w w  w.  ja  v  a  2  s  . c  o m
 *
 * This method uses a depth-first search. A breath-first search would use more memory.
 *
 * @param a The automaton whose accepted words should get checked.
 * @param prefixPredicate The predicate to check the prefixes.
 * @param wordPredicate The predicate to check the words.
 * @return A word which conforms to the predicates.
 */
static public List<String> findPredicateWord(FiniteAutomaton a, Predicate<List<String>> prefixPredicate,
        Predicate<List<String>> wordPredicate) {
    MinimalDeterministicFiniteAutomaton dfa = minimizeInternal(a);
    Deque<Pair<DFAState, Iterator<Symbol>>> trace = new ArrayDeque<>();
    LinkedList<String> word = new LinkedList<>();
    DFAState initial = dfa.getInitialState();
    DFAState sinkState = findSinkState(dfa);
    trace.add(new Pair<>(initial, initial.getDefinedSymbols().iterator()));

    while (!trace.isEmpty()) {
        Pair<DFAState, Iterator<Symbol>> pair = trace.peekLast();
        if (!pair.getSecond().hasNext()) {
            trace.removeLast();
            word.pollLast();
        } else {
            Symbol symbol = pair.getSecond().next();
            DFAState nextState = pair.getFirst().getFollowingState(symbol);
            if (!nextState.equals(sinkState)) {
                word.add(symbol.getEvent());

                List<String> roWord = ListUtils.unmodifiableList(word);

                if (prefixPredicate.evaluate(roWord)) {
                    trace.addLast(new Pair<>(nextState, nextState.getDefinedSymbols().iterator()));

                    if (nextState.isFinalState() && wordPredicate.evaluate(roWord))
                        return word;
                } else {
                    word.removeLast();
                }
            }
        }
    }

    return null;
}

From source file:uniol.apt.analysis.isomorphism.IsomorphismLogicComplex.java

/**
 * Isomorphism-check for current state/*from  w  w  w  . j  a v a 2s  . c om*/
 *
 * @param state current state
 * @return true if node-pair m, n in state s is isomorphic.
 */
private boolean doMatch(ExtendedState state) {
    // Statal-tree, that stores all states
    Deque<ExtendedState> states = new LinkedList<>();

    // Add current state to list of states
    states.addLast(state);
    int depth = 0;

    while (!states.isEmpty()) {
        ExtendedState s = states.getLast();
        depth = s.depth;

        if (depth > 0) {
            core1.put(s.n, s.m);
            core2.put(s.m, s.n);
        }
        if (depth == numNodes) {
            break;
        }
        if (!s.active) {
            computeTerminalSets(depth, s);
        }

        Pair<State, State> node = computeP(depth, s);

        State n, m;
        //boolean that is true, if (m, n) is an isomorphic pair
        boolean goodState = false;
        s.active = true;
        for (; node != null; node = computeP(depth, s)) {
            InterrupterRegistry.throwIfInterruptRequestedForCurrentThread();
            n = node.getFirst();
            m = node.getSecond();

            //if (m,n) is an isomorphic pair
            if (isFeasible(n, m)) {
                goodState = true;

                //increment depth because of new isomorphic pair and
                //set new pair (n,m) as current pair in new state
                ExtendedState news = new ExtendedState(s, depth + 1, n, m);

                if (!in1.containsKey(n)) {
                    in1.put(n, depth + 1);
                    news.numin1++;
                }
                if (!out1.containsKey(n)) {
                    out1.put(n, depth + 1);
                    news.numout1++;
                }
                if (!in2.containsKey(m)) {
                    in2.put(m, depth + 1);
                    news.numin2++;
                }
                if (!out2.containsKey(m)) {
                    out2.put(m, depth + 1);
                    news.numout2++;
                }

                //Add new state to state-tree
                states.addLast(news);

                break;
            }
        }

        //Discard current state if no isomorphic pair has been found
        if (!goodState) {
            rollback(depth, s.n, s.m);
            ExtendedState last = states.removeLast();
            assert last == s;
        }

    }

    //Set final state, to get pairs of isomorphic nodes.
    this.finalState = states;
    for (ExtendedState ext : states) {
        if (ext.depth > 0)
            isomorphism.put(ext.n, ext.m);
    }

    return (depth == numNodes);
}