예제 #1
0
  /**
   * Depth-first search on the dependency graph. The correctness of this algorithm follows from the
   * invariant that a function is only added to the output after all its dependencies have been
   * added.
   *
   * @param n name of the function to be visited
   * @param refs adjacency map
   * @param marked already visited nodes
   * @param stack stack of current nodes
   * @param out output list
   * @param equiv equivalence classes
   */
  private void visit(
      final Name n,
      final Map<Name, Set<Name>> refs,
      final Set<Name> marked,
      final Stack<Name> stack,
      final List<Name> out,
      final Map<Name, Set<Name>> equiv) {
    if (marked.add(n)) { // name wasn't marked
      stack.push(n);

      for (final Name name : refs.get(n)) {
        visit(name, refs, marked, stack, out, equiv);
      }

      out.add(stack.pop());
    } else {
      final int pos = stack.lastIndexOf(n);
      if (pos >= 0) {
        final Set<Name> eqClass = new HashSet<Name>();
        for (final Name eq : stack.subList(pos, stack.size())) {
          eqClass.add(eq);
          final Set<Name> old = equiv.get(eq);
          if (old != null) {
            eqClass.addAll(old);
          }
        }
        for (final Name nm : eqClass) {
          equiv.put(nm, eqClass);
        }
      }
    }
  }
예제 #2
0
 private String listItem(ParserInput parserInput, String raw) {
   Stack listOpenStack = retrieveStack(parserInput, LIST_OPEN_STACK);
   Stack listCloseStack = retrieveStack(parserInput, LIST_CLOSE_STACK);
   StringBuffer output = new StringBuffer();
   // build a stack of html tags based on current values passed to lexer
   Stack currentOpenStack = tagsToStack(raw);
   String currentItemOpenTag = (String) currentOpenStack.peek();
   String currentItemCloseTag = (String) listCloseHash.get(currentItemOpenTag);
   // if list was previously open to a greater depth, close the old list
   while (listOpenStack.size() > currentOpenStack.size()) {
     listOpenStack.pop();
     output.append(listCloseStack.pop());
   }
   if (this.listStackEquals(currentOpenStack, listOpenStack)) {
     // if continuing same list close previous list item & open new item
     output.append(listCloseStack.pop());
     listCloseStack.push(currentItemCloseTag);
     output.append(currentItemOpenTag);
   } else {
     // look for differences in the old list stack and the new list stack
     int pos = 0;
     while (pos < listOpenStack.size()) {
       if (!this.listStackEquals(
           currentOpenStack.subList(0, pos + 1), listOpenStack.subList(0, pos + 1))) {
         break;
       }
       pos++;
     }
     // if any differences found process them
     while (listOpenStack.size() > pos) {
       listOpenStack.pop();
       output.append(listCloseStack.pop());
     }
     // continue processing differences
     for (int i = pos; i < currentOpenStack.size(); i++) {
       String openTag = (String) currentOpenStack.elementAt(i);
       String closeTag = (String) listCloseHash.get(openTag);
       listOpenStack.push(openTag);
       listCloseStack.push(closeTag);
       output.append(openTag);
     }
   }
   updateStack(parserInput, listOpenStack, LIST_OPEN_STACK);
   updateStack(parserInput, listCloseStack, LIST_CLOSE_STACK);
   return output.toString();
 }
예제 #3
0
 public void execute(ExecutionContext context) {
   Stack<Object> stack = context.getStack();
   int n = ((Number) stack.pop()).intValue();
   if (n > 0) {
     int size = stack.size();
     // Need to copy to a new list to avoid ConcurrentModificationException
     List<Object> copy = new java.util.ArrayList<Object>(stack.subList(size - n, size));
     stack.addAll(copy);
   }
 }
예제 #4
0
 private void readVector(Stack<String> stack) {
   int start = getListStart(stack);
   int end = getListEnd(stack);
   List<String> subList = stack.subList(start, end);
   for (String item : subList) {
     if (item.equals(LIST_START) || item.equals(LIST_END)) continue;
     list.add(new FieldElement("", item));
   }
   subList.clear();
 }
예제 #5
0
 private void readScalarList(Stack<String> stack) {
   int start = 1;
   int end = stack.size() - 1;
   List<String> subList = stack.subList(start, end);
   for (String item : subList) {
     if (item.equals(LIST_START) || item.equals(LIST_END)) continue;
     list.add(new FieldElement("", item));
   }
   subList.clear();
 }
  @Test
  public void testStackPartialEq() {

    Stack<String> s1 = new Stack<String>();
    Stack<String> s2 = new Stack<String>();

    for (String s : MapLinkGeoJsonParseContext.Data_Feature.getPath()) {
      s1.push(s);
    }
    for (String s : MapLinkGeoJsonParseContext.Data_Feature.getPath()) {
      s2.push(new String(s));
    }

    s2.push("[#]");

    assertTrue(s1.equals(s2.subList(0, s1.size())));
    assertTrue(!s1.equals(s2));
  }
예제 #7
0
  private static <T> void processDependencyMap(
      T selected,
      Stack<T> callStack,
      Map<T, Set<T>> dependencies,
      Set<T> processed,
      Processor<T> processor,
      SubMonitor subMonitor)
      throws CoreException, CircularDependencyException {
    if (processed.contains(selected)) return;

    // Check for cycles
    int stackIndex = callStack.indexOf(selected);
    if (stackIndex != -1) {
      List<T> subList = callStack.subList(stackIndex, callStack.size());
      List<T> cycle = new ArrayList<T>(subList.size() + 1);
      cycle.addAll(subList);
      cycle.add(selected);

      throw new CircularDependencyException(cycle);
    }
    try {
      callStack.push(selected);

      Set<T> selectedDeps = dependencies.get(selected);

      // Process dependencies first
      if (selectedDeps != null) {
        for (T dep : selectedDeps) {
          processDependencyMap(dep, callStack, dependencies, processed, processor, subMonitor);
        }
      }

      // Process the selection
      processor.process(selected, subMonitor.newChild(1));
      processed.add(selected);
    } finally {
      callStack.pop();
    }
  }
 /** @return The collection of persistence units that we have parsed */
 public List<PersistenceUnitImpl> getPersistenceUnits() {
   return persistenceUnits.subList(0, persistenceUnits.size());
 }