Java Code Examples for java.util.ArrayDeque#remove()

The following examples show how to use java.util.ArrayDeque#remove() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * iterator.remove() removes current element
 */
public void testIteratorRemove() {
    final ArrayDeque q = new ArrayDeque();
    final Random rng = new Random();
    for (int iters = 0; iters < 100; ++iters) {
        int max = rng.nextInt(5) + 2;
        int split = rng.nextInt(max - 1) + 1;
        for (int j = 1; j <= max; ++j)
            q.add(new Integer(j));
        Iterator it = q.iterator();
        for (int j = 1; j <= split; ++j)
            assertEquals(it.next(), new Integer(j));
        it.remove();
        assertEquals(it.next(), new Integer(split + 1));
        for (int j = 1; j <= split; ++j)
            q.remove(new Integer(j));
        it = q.iterator();
        for (int j = split + 1; j <= max; ++j) {
            assertEquals(it.next(), new Integer(j));
            it.remove();
        }
        assertFalse(it.hasNext());
        assertTrue(q.isEmpty());
    }
}
 
Example 2
/**
 * Descending iterator ordering is reverse FIFO
 */
public void testDescendingIteratorOrdering() {
    final ArrayDeque q = new ArrayDeque();
    for (int iters = 0; iters < 100; ++iters) {
        q.add(new Integer(3));
        q.add(new Integer(2));
        q.add(new Integer(1));
        int k = 0;
        for (Iterator it = q.descendingIterator(); it.hasNext();) {
            assertEquals(++k, it.next());
        }

        assertEquals(3, k);
        q.remove();
        q.remove();
        q.remove();
    }
}
 
Example 3
/**
 * descendingIterator.remove() removes current element
 */
public void testDescendingIteratorRemove() {
    final ArrayDeque q = new ArrayDeque();
    final Random rng = new Random();
    for (int iters = 0; iters < 100; ++iters) {
        int max = rng.nextInt(5) + 2;
        int split = rng.nextInt(max - 1) + 1;
        for (int j = max; j >= 1; --j)
            q.add(new Integer(j));
        Iterator it = q.descendingIterator();
        for (int j = 1; j <= split; ++j)
            assertEquals(it.next(), new Integer(j));
        it.remove();
        assertEquals(it.next(), new Integer(split + 1));
        for (int j = 1; j <= split; ++j)
            q.remove(new Integer(j));
        it = q.descendingIterator();
        for (int j = split + 1; j <= max; ++j) {
            assertEquals(it.next(), new Integer(j));
            it.remove();
        }
        assertFalse(it.hasNext());
        assertTrue(q.isEmpty());
    }
}
 
Example 4
Source Project: j2objc   File: ArrayDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * iterator.remove() removes current element
 */
public void testIteratorRemove() {
    final ArrayDeque q = new ArrayDeque();
    final Random rng = new Random();
    for (int iters = 0; iters < 100; ++iters) {
        int max = rng.nextInt(5) + 2;
        int split = rng.nextInt(max - 1) + 1;
        for (int j = 1; j <= max; ++j)
            q.add(new Integer(j));
        Iterator it = q.iterator();
        for (int j = 1; j <= split; ++j)
            assertEquals(it.next(), new Integer(j));
        it.remove();
        assertEquals(it.next(), new Integer(split + 1));
        for (int j = 1; j <= split; ++j)
            q.remove(new Integer(j));
        it = q.iterator();
        for (int j = split + 1; j <= max; ++j) {
            assertEquals(it.next(), new Integer(j));
            it.remove();
        }
        assertFalse(it.hasNext());
        assertTrue(q.isEmpty());
    }
}
 
Example 5
Source Project: j2objc   File: ArrayDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Descending iterator ordering is reverse FIFO
 */
public void testDescendingIteratorOrdering() {
    final ArrayDeque q = new ArrayDeque();
    for (int iters = 0; iters < 100; ++iters) {
        q.add(new Integer(3));
        q.add(new Integer(2));
        q.add(new Integer(1));
        int k = 0;
        for (Iterator it = q.descendingIterator(); it.hasNext();) {
            assertEquals(++k, it.next());
        }

        assertEquals(3, k);
        q.remove();
        q.remove();
        q.remove();
    }
}
 
Example 6
Source Project: j2objc   File: ArrayDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * descendingIterator.remove() removes current element
 */
public void testDescendingIteratorRemove() {
    final ArrayDeque q = new ArrayDeque();
    final Random rng = new Random();
    for (int iters = 0; iters < 100; ++iters) {
        int max = rng.nextInt(5) + 2;
        int split = rng.nextInt(max - 1) + 1;
        for (int j = max; j >= 1; --j)
            q.add(new Integer(j));
        Iterator it = q.descendingIterator();
        for (int j = 1; j <= split; ++j)
            assertEquals(it.next(), new Integer(j));
        it.remove();
        assertEquals(it.next(), new Integer(split + 1));
        for (int j = 1; j <= split; ++j)
            q.remove(new Integer(j));
        it = q.descendingIterator();
        for (int j = split + 1; j <= max; ++j) {
            assertEquals(it.next(), new Integer(j));
            it.remove();
        }
        assertFalse(it.hasNext());
        assertTrue(q.isEmpty());
    }
}
 
Example 7
Source Project: bazel   File: PhonyTarget.java    License: Apache License 2.0 6 votes vote down vote up
public void visitExplicitInputs(
    ImmutableSortedMap<PathFragment, PhonyTarget> phonyTargetsMap,
    Consumer<ImmutableList<PathFragment>> consumer) {
  consumer.accept(directExplicitInputs);

  ArrayDeque<PathFragment> queue = new ArrayDeque<>(phonyNames);
  Set<PathFragment> visited = Sets.newHashSet();
  while (!queue.isEmpty()) {
    PathFragment fragment = queue.remove();
    if (visited.add(fragment)) {
      PhonyTarget phonyTarget = Preconditions.checkNotNull(phonyTargetsMap.get(fragment));
      consumer.accept(phonyTarget.getDirectExplicitInputs());
      queue.addAll(phonyTarget.getPhonyNames());
    }
  }
}
 
Example 8
/**
 * remove() removes next element, or throws NSEE if empty
 */
public void testRemove() {
    ArrayDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.remove());
    }
    try {
        q.remove();
        shouldThrow();
    } catch (NoSuchElementException success) {}
}
 
Example 9
Source Project: requery   File: SchemaModifier.java    License: Apache License 2.0 5 votes vote down vote up
private ArrayList<Type<?>> sortTypes() {
    // sort the types in table creation order to avoid referencing not created table via a
    // reference (could also add constraints at the end but SQLite doesn't support that)
    ArrayDeque<Type<?>> queue = new ArrayDeque<>(model.getTypes());
    ArrayList<Type<?>> sorted = new ArrayList<>();
    while (!queue.isEmpty()) {
        Type<?> type = queue.poll();

        if (type.isView()) {
            continue;
        }

        Set<Type<?>> referencing = referencedTypesOf(type);
        for (Type<?> referenced : referencing) {
            Set<Type<?>> backReferences = referencedTypesOf(referenced);
            if (backReferences.contains(type)) {
                throw new CircularReferenceException("circular reference detected between "
                    + type.getName() + " and " + referenced.getName());
            }
        }
        if (referencing.isEmpty() || sorted.containsAll(referencing)) {
            sorted.add(type);
            queue.remove(type);
        } else {
            queue.offer(type); // put back
        }
    }
    return sorted;
}
 
Example 10
Source Project: j2objc   File: ArrayDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * remove() removes next element, or throws NSEE if empty
 */
public void testRemove() {
    ArrayDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.remove());
    }
    try {
        q.remove();
        shouldThrow();
    } catch (NoSuchElementException success) {}
}
 
Example 11
Source Project: bazel   File: NinjaPhonyTargetsUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * For the given phony NinjaTarget, return a list of all phony NinjaTargets, composing its subtree
 * (direct and transitive inputs). The list is ordered from leaves to their dependents; for any
 * node all its direct and transitive inputs are preceding it in the list.
 *
 * <p>Function does DFS starting from the NinjaTarget, with two phases: in initial processing: 1)
 * if the target was already computed, nothing happens 2) the target is checked for cycle and
 * marked in cycleProtection set, its phony inputs are queued (put in the beginning of the queue)
 * for initial processing 3) the target is queued after its inputs for post-processing in
 * post-processing, the target is recorded into resulting list; all its inputs should have been
 * already written to that list on the previous steps
 */
private static List<NinjaTarget> topoOrderSubGraph(
    ImmutableSortedMap<PathFragment, NinjaTarget> phonyTargets,
    Set<NinjaTarget> alreadyVisited,
    NinjaTarget target)
    throws GenericParsingException {
  Set<NinjaTarget> cycleProtection = Sets.newHashSet();
  List<NinjaTarget> fragment = Lists.newArrayList();
  ArrayDeque<Pair<NinjaTarget, Boolean>> queue = new ArrayDeque<>();
  queue.add(Pair.of(target, true));
  while (!queue.isEmpty()) {
    Pair<NinjaTarget, Boolean> pair = queue.remove();
    NinjaTarget currentTarget = pair.getFirst();
    if (pair.getSecond()) {
      // Initial processing: checking all the phony inputs of the current target.
      if (alreadyVisited.contains(currentTarget)) {
        continue;
      }
      if (!cycleProtection.add(currentTarget)) {
        throw new GenericParsingException(
            String.format(
                "Detected a dependency cycle involving the phony target '%s'",
                Iterables.getOnlyElement(currentTarget.getAllOutputs())));
      }
      // Adding <phony-inputs-of-current-target> for initial processing in front of
      // <current-target>
      // for post-processing into the queue.
      queue.addFirst(Pair.of(currentTarget, false));
      for (PathFragment input : currentTarget.getAllInputs()) {
        NinjaTarget phonyInput = phonyTargets.get(input);
        if (phonyInput != null) {
          queue.addFirst(Pair.of(phonyInput, true));
        }
      }
    } else {
      // Post processing: all inputs should have been processed and added to fragment.
      cycleProtection.remove(currentTarget);
      alreadyVisited.add(currentTarget);
      fragment.add(currentTarget);
    }
  }
  return fragment;
}
 
Example 12
Source Project: bazel   File: NinjaActionsHelper.java    License: Apache License 2.0 4 votes vote down vote up
void createNinjaActions() throws GenericParsingException {
  // Traverse the action graph starting from the targets, specified by the user.
  // Only create the required actions.
  Set<PathFragment> visitedPaths = Sets.newHashSet();
  Set<NinjaTarget> visitedTargets = Sets.newHashSet();
  visitedPaths.addAll(pathsToBuild);
  ArrayDeque<PathFragment> queue = new ArrayDeque<>(pathsToBuild);
  Consumer<Collection<PathFragment>> enqueuer =
      paths -> {
        for (PathFragment input : paths) {
          if (visitedPaths.add(input)) {
            queue.add(input);
          }
        }
      };
  while (!queue.isEmpty()) {
    PathFragment fragment = queue.remove();
    NinjaTarget target = targetsMap.get(fragment);
    if (target != null) {
      // If the output is already created by a symlink action created from specifying that
      // file in output_root_inputs attribute of the ninja_graph rule, do not create other
      // actions that output that same file, since that will result in an action conflict.
      if (!outputRootInputsSymlinks.contains(fragment)) {
        if (visitedTargets.add(target)) {
          createNinjaAction(target);
        }
        enqueuer.accept(target.getAllInputs());
      } else {
        // Sanity check that the Ninja action we're skipping (because its outputs are already
        // being symlinked using output_root_inputs) has only symlink outputs specified in
        // output_root_inputs. Otherwise we might skip some other outputs.
        List<PathFragment> outputsInOutputRootInputsSymlinks = new ArrayList<>();
        List<PathFragment> outputsNotInOutputRootInputsSymlinks = new ArrayList<>();
        for (PathFragment output : target.getAllOutputs()) {
          if (outputRootInputsSymlinks.contains(output)) {
            outputsInOutputRootInputsSymlinks.add(output);
          } else {
            outputsNotInOutputRootInputsSymlinks.add(output);
          }
        }
        if (!outputsNotInOutputRootInputsSymlinks.isEmpty()) {
          throw new GenericParsingException(
              "Ninja target "
                  + target.getRuleName()
                  + " has "
                  + "outputs in output_root_inputs and other outputs not in output_root_inputs:\n"
                  + "Outputs in output_root_inputs:\n  "
                  + Joiner.on("  \n").join(outputsInOutputRootInputsSymlinks)
                  + "\nOutputs not in output_root_inputs:\n  "
                  + Joiner.on("  \n").join(outputsNotInOutputRootInputsSymlinks));
        }
      }
    } else {
      PhonyTarget phonyTarget = phonyTargets.get(fragment);
      // Phony target can be null, if the path in neither regular or phony target,
      // but the source file.
      if (phonyTarget != null) {
        phonyTarget.visitExplicitInputs(phonyTargets, enqueuer::accept);
      }
    }
  }
}