Example #1
0
  public static void recSort(ArrayList<Integer> arr, int left, int right) {

    if (left >= right) return;

    int mid = (left + right) / 2;

    recSort(arr, left, mid);
    recSort(arr, mid + 1, right);

    // combine

    ArrayList<Integer> copy = new ArrayList<Integer>();

    // MERGE
    int a = left;
    int b = mid + 1;

    while (a <= mid && b <= right) {
      int cmp;
      cmp = arr.get(a) - arr.get(b);

      if (cmp < 0) copy.add(arr.get(a++));
      else copy.add(arr.get(b++));
    }

    while (a <= mid) copy.add(arr.get(a++));
    while (b <= right) copy.add(arr.get(b++));

    // Put back
    int c = 0;
    for (int i = left; i <= right; i++) arr.set(i, copy.get(c++));
  }
Example #2
0
  private void fixTabs(int tabSize) {

    int rowIndex = 0;

    for (StringBuilder row1 : rows) {
      String row = row1.toString();
      StringBuilder newRow = new StringBuilder();

      char[] chars = row.toCharArray();
      for (char c : chars) {
        if (c == '\t') {
          int spacesLeft = tabSize - newRow.length() % tabSize;
          if (DEBUG) {
            System.out.println("Found tab. Spaces left: " + spacesLeft);
          }
          String spaces = StringUtils.repeatString(" ", spacesLeft);
          newRow.append(spaces);
        } else {
          String character = Character.toString(c);
          newRow.append(character);
        }
      }
      rows.set(rowIndex, newRow);
      rowIndex++;
    }
  }
Example #3
0
 /**
  * Adds an Item to the Kit
  *
  * @param item The Item to be added to the Kit
  */
 public void addItem(GraphicItem item) {
   for (int i = 0; i < items.size(); i++) {
     if (items.get(i) == null) {
       items.set(i, item);
       return;
     }
   }
   items.add(item);
 }
 private ArrayList<Integer> findRanges() {
   int rowsPerRange = (int) Math.ceil((grid.size()) / (double) threads);
   ArrayList<Integer> ranges = new ArrayList<Integer>();
   int startRow = 0, endRow = rowsPerRange;
   for (int i = 0; i < threads; i++) {
     ranges.add(startRow);
     ranges.add(endRow);
     startRow = endRow;
     ;
     endRow += rowsPerRange;
   }
   ranges.set(ranges.size() - 1, Math.min(grid.size(), ranges.get(ranges.size() - 1)));
   System.out.println(ranges);
   return ranges;
 }
Example #5
0
 public void replaceHumanColorCodes() {
   int height = getHeight();
   for (int y = 0; y < height; y++) {
     String row = rows.get(y).toString();
     for (String humanCode : humanColorCodes.keySet()) {
       String hexCode = humanColorCodes.get(humanCode);
       if (hexCode != null) {
         humanCode = "c" + humanCode;
         hexCode = "c" + hexCode;
         row = row.replaceAll(humanCode, hexCode);
         rows.set(
             y, new StringBuilder(row)); // TODO: this is not the most efficient way to do this
         row = rows.get(y).toString();
       }
     }
   }
 }
Example #6
0
 public void setRow(int y, StringBuilder row) {
   if (y > getHeight() || row.length() != getWidth())
     throw new IllegalArgumentException("setRow out of bounds or string wrong size");
   rows.set(y, row);
 }
Example #7
0
  public void initialiseWithLines(ArrayList<StringBuilder> lines, ProcessingOptions options)
      throws UnsupportedEncodingException {

    // remove blank rows at the bottom
    boolean done = false;
    int i;
    for (i = lines.size() - 1; i >= 0 && !done; i--) {
      StringBuilder row = lines.get(i);
      if (!StringUtils.isBlank(row.toString())) done = true;
    }
    rows = new ArrayList<StringBuilder>(lines.subList(0, i + 2));

    if (options != null) fixTabs(options.getTabSize());
    else fixTabs(ProcessingOptions.DEFAULT_TAB_SIZE);

    // make all lines of equal length
    // add blank outline around the buffer to prevent fill glitch
    // convert tabs to spaces (or remove them if setting is 0)

    int blankBorderSize = 2;

    int maxLength = 0;
    int index = 0;

    String encoding = null;
    if (options != null) encoding = options.getCharacterEncoding();

    Iterator<StringBuilder> it = rows.iterator();
    while (it.hasNext()) {
      String row = it.next().toString();
      if (encoding != null) {
        byte[] bytes = row.getBytes();
        row = new String(bytes, encoding);
      }
      if (row.length() > maxLength) maxLength = row.length();
      rows.set(index, new StringBuilder(row));
      index++;
    }

    it = rows.iterator();
    ArrayList<StringBuilder> newRows = new ArrayList<StringBuilder>();
    // TODO: make the following depend on blankBorderSize

    StringBuilder topBottomRow =
        new StringBuilder(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2));

    newRows.add(topBottomRow);
    newRows.add(topBottomRow);
    while (it.hasNext()) {
      StringBuilder row = it.next();

      if (row.length() < maxLength) {
        String borderString = StringUtils.repeatString(" ", blankBorderSize);
        StringBuilder newRow = new StringBuilder();

        newRow.append(borderString);
        newRow.append(row);
        newRow.append(StringUtils.repeatString(" ", maxLength - row.length()));
        newRow.append(borderString);

        newRows.add(newRow);
      } else { // TODO: why is the following line like that?
        newRows.add(new StringBuilder("  ").append(row).append("  "));
      }
    }
    // TODO: make the following depend on blankBorderSize
    newRows.add(topBottomRow);
    newRows.add(topBottomRow);
    rows = newRows;

    replaceBullets();
    replaceHumanColorCodes();
  }
Example #8
0
 /**
  * Sets the Item at the given index
  *
  * @param index The given index
  * @param item The given Item
  */
 public void setItem(int index, GraphicItem item) {
   items.set(index, item);
 }