Search in sources :

Example 91 with ArrayDeque

use of java.util.ArrayDeque in project gradle by gradle.

the class DefaultTaskExecutionPlan method determineExecutionPlan.

public void determineExecutionPlan() {
    List<TaskInfoInVisitingSegment> nodeQueue = Lists.newArrayList(Iterables.transform(entryTasks, new Function<TaskInfo, TaskInfoInVisitingSegment>() {

        int index;

        public TaskInfoInVisitingSegment apply(TaskInfo taskInfo) {
            return new TaskInfoInVisitingSegment(taskInfo, index++);
        }
    }));
    int visitingSegmentCounter = nodeQueue.size();
    HashMultimap<TaskInfo, Integer> visitingNodes = HashMultimap.create();
    Deque<GraphEdge> walkedShouldRunAfterEdges = new ArrayDeque<GraphEdge>();
    Deque<TaskInfo> path = new ArrayDeque<TaskInfo>();
    HashMap<TaskInfo, Integer> planBeforeVisiting = new HashMap<TaskInfo, Integer>();
    while (!nodeQueue.isEmpty()) {
        TaskInfoInVisitingSegment taskInfoInVisitingSegment = nodeQueue.get(0);
        int currentSegment = taskInfoInVisitingSegment.visitingSegment;
        TaskInfo taskNode = taskInfoInVisitingSegment.taskInfo;
        if (taskNode.isIncludeInGraph() || executionPlan.containsKey(taskNode.getTask())) {
            nodeQueue.remove(0);
            visitingNodes.remove(taskNode, currentSegment);
            maybeRemoveProcessedShouldRunAfterEdge(walkedShouldRunAfterEdges, taskNode);
            continue;
        }
        boolean alreadyVisited = visitingNodes.containsKey(taskNode);
        visitingNodes.put(taskNode, currentSegment);
        if (!alreadyVisited) {
            // Have not seen this task before - add its dependencies to the head of the queue and leave this
            // task in the queue
            recordEdgeIfArrivedViaShouldRunAfter(walkedShouldRunAfterEdges, path, taskNode);
            removeShouldRunAfterSuccessorsIfTheyImposeACycle(visitingNodes, taskInfoInVisitingSegment);
            takePlanSnapshotIfCanBeRestoredToCurrentTask(planBeforeVisiting, taskNode);
            ArrayList<TaskInfo> successors = new ArrayList<TaskInfo>();
            addAllSuccessorsInReverseOrder(taskNode, successors);
            for (TaskInfo successor : successors) {
                if (visitingNodes.containsEntry(successor, currentSegment)) {
                    if (!walkedShouldRunAfterEdges.isEmpty()) {
                        // remove the last walked should run after edge and restore state from before walking it
                        GraphEdge toBeRemoved = walkedShouldRunAfterEdges.pop();
                        toBeRemoved.from.removeShouldRunAfterSuccessor(toBeRemoved.to);
                        restorePath(path, toBeRemoved);
                        restoreQueue(nodeQueue, visitingNodes, toBeRemoved);
                        restoreExecutionPlan(planBeforeVisiting, toBeRemoved);
                        break;
                    } else {
                        onOrderingCycle();
                    }
                }
                nodeQueue.add(0, new TaskInfoInVisitingSegment(successor, currentSegment));
            }
            path.push(taskNode);
        } else {
            // Have visited this task's dependencies - add it to the end of the plan
            nodeQueue.remove(0);
            maybeRemoveProcessedShouldRunAfterEdge(walkedShouldRunAfterEdges, taskNode);
            visitingNodes.remove(taskNode, currentSegment);
            path.pop();
            executionPlan.put(taskNode.getTask(), taskNode);
            Project project = taskNode.getTask().getProject();
            projectLocks.put(project, getOrCreateProjectLock(project));
            TaskMutationInfo taskMutationInfo = getOrCreateMutationsOf(taskNode);
            for (TaskInfo dependency : taskNode.getDependencySuccessors()) {
                getOrCreateMutationsOf(dependency).consumingTasks.add(taskNode);
                taskMutationInfo.consumesOutputOf.add(dependency);
            }
            // Add any finalizers to the queue
            ArrayList<TaskInfo> finalizerTasks = new ArrayList<TaskInfo>();
            addAllReversed(finalizerTasks, taskNode.getFinalizers());
            for (TaskInfo finalizer : finalizerTasks) {
                if (!visitingNodes.containsKey(finalizer)) {
                    nodeQueue.add(finalizerTaskPosition(finalizer, nodeQueue), new TaskInfoInVisitingSegment(finalizer, visitingSegmentCounter++));
                }
            }
        }
    }
    executionQueue.clear();
    executionQueue.addAll(executionPlan.values());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) Function(com.google.common.base.Function) Project(org.gradle.api.Project)

Example 92 with ArrayDeque

use of java.util.ArrayDeque in project gradle by gradle.

the class DefaultTaskDependency method visitDependencies.

@Override
public void visitDependencies(TaskDependencyResolveContext context) {
    Set<Object> mutableValues = getMutableValues();
    if (mutableValues.isEmpty() && immutableValues.isEmpty()) {
        return;
    }
    Deque<Object> queue = new ArrayDeque<Object>(mutableValues.size() + immutableValues.size());
    queue.addAll(immutableValues);
    queue.addAll(mutableValues);
    while (!queue.isEmpty()) {
        Object dependency = queue.removeFirst();
        if (dependency instanceof Buildable) {
            context.add(dependency);
        } else if (dependency instanceof Task) {
            context.add(dependency);
        } else if (dependency instanceof TaskDependency) {
            context.add(dependency);
        } else if (dependency instanceof Closure) {
            Closure closure = (Closure) dependency;
            Object closureResult = closure.call(context.getTask());
            if (closureResult != null) {
                queue.addFirst(closureResult);
            }
        } else if (dependency instanceof RealizableTaskCollection) {
            RealizableTaskCollection realizableTaskCollection = (RealizableTaskCollection) dependency;
            realizableTaskCollection.realizeRuleTaskTypes();
            addAllFirst(queue, realizableTaskCollection.toArray());
        } else if (dependency instanceof List) {
            List<?> list = (List) dependency;
            if (list instanceof RandomAccess) {
                for (int i = list.size() - 1; i >= 0; i--) {
                    queue.addFirst(list.get(i));
                }
            } else {
                ListIterator<?> iterator = list.listIterator(list.size());
                while (iterator.hasPrevious()) {
                    Object item = iterator.previous();
                    queue.addFirst(item);
                }
            }
        } else if (dependency instanceof Iterable) {
            Iterable<?> iterable = (Iterable) dependency;
            addAllFirst(queue, toArray(iterable, Object.class));
        } else if (dependency instanceof Map) {
            Map<?, ?> map = (Map) dependency;
            addAllFirst(queue, map.values().toArray());
        } else if (dependency instanceof Object[]) {
            Object[] array = (Object[]) dependency;
            addAllFirst(queue, array);
        } else if (dependency instanceof Callable) {
            Callable callable = (Callable) dependency;
            Object callableResult = uncheckedCall(callable);
            if (callableResult != null) {
                queue.addFirst(callableResult);
            }
        } else if (resolver != null && dependency instanceof TaskReference) {
            context.add(resolver.resolveTask((TaskReference) dependency));
        } else if (resolver != null && dependency instanceof CharSequence) {
            context.add(resolver.resolveTask(dependency.toString()));
        } else if (dependency instanceof TaskDependencyContainer) {
            ((TaskDependencyContainer) dependency).visitDependencies(context);
        } else if (dependency instanceof ProviderInternal) {
            ProviderInternal providerInternal = (ProviderInternal) dependency;
            if (providerInternal.getType() == null || providerInternal.getType().equals(Provider.class)) {
                queue.addFirst(providerInternal.get());
                continue;
            }
            List<String> formats = new ArrayList<String>();
            formats.add("A RegularFileProperty");
            formats.add("A DirectoryProperty");
            throw new UnsupportedNotationException(dependency, String.format("Cannot convert Provider %s to a task.", dependency), null, formats);
        } else {
            List<String> formats = new ArrayList<String>();
            if (resolver != null) {
                formats.add("A String or CharSequence task name or path");
                formats.add("A TaskReference instance");
            }
            formats.add("A Task instance");
            formats.add("A Buildable instance");
            formats.add("A TaskDependency instance");
            formats.add("A RegularFileProperty or DirectoryProperty instance");
            formats.add("A Provider instance that returns any of these types");
            formats.add("A Closure instance that returns any of these types");
            formats.add("A Callable instance that returns any of these types");
            formats.add("An Iterable, Collection, Map or array instance that contains any of these types");
            throw new UnsupportedNotationException(dependency, String.format("Cannot convert %s to a task.", dependency), null, formats);
        }
    }
}
Also used : TaskDependency(org.gradle.api.tasks.TaskDependency) Task(org.gradle.api.Task) Closure(groovy.lang.Closure) RandomAccess(java.util.RandomAccess) ListIterator(java.util.ListIterator) ArrayDeque(java.util.ArrayDeque) Callable(java.util.concurrent.Callable) TaskReference(org.gradle.api.tasks.TaskReference) ProviderInternal(org.gradle.api.internal.provider.ProviderInternal) UnsupportedNotationException(org.gradle.internal.typeconversion.UnsupportedNotationException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Buildable(org.gradle.api.Buildable)

Example 93 with ArrayDeque

use of java.util.ArrayDeque in project Leetcode by fishercoder1534.

the class _445 method popIntoStack.

private Deque<Integer> popIntoStack(ListNode head) {
    ListNode tmp = head;
    Deque<Integer> stack = new ArrayDeque<>();
    while (tmp != null) {
        stack.push(tmp.val);
        tmp = tmp.next;
    }
    return stack;
}
Also used : ListNode(com.fishercoder.common.classes.ListNode) ArrayDeque(java.util.ArrayDeque)

Example 94 with ArrayDeque

use of java.util.ArrayDeque in project Leetcode by fishercoder1534.

the class _144 method preorderTraversal_iterative.

public List<Integer> preorderTraversal_iterative(TreeNode root) {
    List<Integer> list = new ArrayList();
    if (root == null) {
        return list;
    }
    Deque<TreeNode> stack = new ArrayDeque<>();
    stack.push(root);
    while (!stack.isEmpty()) {
        TreeNode curr = stack.pop();
        list.add(curr.val);
        /**
         *We push right nodes onto the stack first, since they'll be popped out later than
         * the left nodes, to meet the preorder: root -> left -> right.
         */
        if (curr.right != null) {
            stack.push(curr.right);
        }
        if (curr.left != null) {
            stack.push(curr.left);
        }
    }
    return list;
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque)

Example 95 with ArrayDeque

use of java.util.ArrayDeque in project suite by stupidsing.

the class InstructionAnalyzer method analyzeFrames.

private void analyzeFrames(List<Instruction> instructions) {
    Deque<AnalyzedFrame> analyzedFrames = new ArrayDeque<>();
    // assumes every FRAME-BEGIN has a ASSIGN-THUNK referencing it.
    for (int ip = 0; ip < instructions.size(); ip++) {
        Instruction insn = instructions.get(ip);
        if (insn.insn == Insn.FRAMEBEGIN____)
            analyzedFrames.push(new AnalyzedFrame(ip));
        AnalyzedFrame frame = !analyzedFrames.isEmpty() ? analyzedFrames.peek() : null;
        frameByIp.add(frame);
        if (insn.insn == Insn.FRAMEEND______)
            analyzedFrames.pop();
    }
}
Also used : Instruction(suite.instructionexecutor.InstructionUtil.Instruction) ArrayDeque(java.util.ArrayDeque)

Aggregations

ArrayDeque (java.util.ArrayDeque)217 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)36 IOException (java.io.IOException)27 HashMap (java.util.HashMap)23 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)17 Deque (java.util.Deque)11 Iterator (java.util.Iterator)10 NoSuchElementException (java.util.NoSuchElementException)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 File (java.io.File)7 Path (java.nio.file.Path)7 Random (java.util.Random)7 ByteBuffer (java.nio.ByteBuffer)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpFields (org.eclipse.jetty.http.HttpFields)5 Name (com.github.anba.es6draft.ast.scope.Name)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4