Ejemplo n.º 1
0
  public Set<Position> getAllMatches(
      Playfield<E> haystack, Playfield<E> needle, Matcher<E, E> matcher) {
    Set<Position> results = new HashSet<Position>();

    IntegerElement haystackWidth = haystack.getMaxX().subtract(haystack.getMinX()).succ();
    IntegerElement haystackHeight = haystack.getMaxY().subtract(haystack.getMinY()).succ();
    IntegerElement needleWidth = needle.getMaxX().subtract(needle.getMinX()).succ();
    IntegerElement needleHeight = needle.getMaxY().subtract(needle.getMinY()).succ();

    if (needleWidth.compareTo(haystackWidth) > 0 || needleHeight.compareTo(haystackHeight) > 0) {
      // thing being sought is larger than the thing seeking in, so, no
      return results;
    }

    IntegerElement xSpan = haystackWidth.subtract(needleWidth).succ();
    IntegerElement ySpan = haystackHeight.subtract(needleHeight).succ();

    IntegerElement x, y;
    for (x = IntegerElement.ZERO; x.compareTo(xSpan) < 0; x = x.succ()) {
      for (y = IntegerElement.ZERO; y.compareTo(ySpan) < 0; y = y.succ()) {
        if (isMatchAt(haystack, needle, matcher, x, y)) {
          results.add(new Position(x, y));
        } else {
        }
      }
    }
    return results;
  }
Ejemplo n.º 2
0
  public List<Rule> findAllRules() {
    IntegerElement x, y;
    ArrayList<Rule> rules = new ArrayList<Rule>();

    for (x = (IntegerElement) getMinX(); x.compareTo(getMaxX()) <= 0; x = x.succ()) {
      for (y = (IntegerElement) getMinY(); y.compareTo(getMaxY()) <= 0; y = y.succ()) {
        boolean escaped = (!y.isZero() && get(x, y.pred()).getChar() != ' ');
        if (escaped) continue;
        if (get(x, y).getChar() == '(') {
          // System.out.println("Found a rule start at " + x + ", " + y);
          IntegerElement x2 = x;
          CharacterElement g = get(x2, y);
          while (g.getChar() != ')' && x2.compareTo(getMaxX()) <= 0) {
            x2 = x2.succ();
            g = get(x2, y);
          }
          if (g.getChar() != ')') continue;
          // System.out.println("Found a rule from " + x + "," + y + " to " + x2);
          CharacterElement w = get(x2.pred(), y);
          if (w.getChar() == ' ') w = null;
          Rule r = new Rule(this, x.succ(), y.succ(), (x2.subtract(x)).pred(), w);
          rules.add(r);
        }
      }
    }

    return rules;
  }