Java Collections.binarySearch Examples

Java Collections.binarySearch - 30 examples found. These are the top rated real world Java examples of java.util.Collections.binarySearch extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 @Override
 public void databaseChanged(DatabaseChangeEvent e) {
   synchronized (set) {
     int pos;
     switch (e.getType()) {
       case ADDED_ENTRY:
         pos = -Collections.binarySearch(set, e.getEntry(), comp) - 1;
         set.add(pos, e.getEntry());
         // addEntry(e.getEntry());
         // set.add(e.getEntry());
         // changed = true;
         // Collections.sort(set, comp);
         break;
       case REMOVED_ENTRY:
         set.remove(e.getEntry());
         changed = true;
         break;
       case CHANGED_ENTRY:
         // Entry changed. Resort list:
         // Collections.sort(set, comp);
         pos = Collections.binarySearch(set, e.getEntry(), comp);
         int posOld = set.indexOf(e.getEntry());
         if (pos < 0) {
           set.remove(posOld);
           set.add(-pos - 1, e.getEntry());
         }
         // changed = true;
         break;
     }
   }
 }
  public boolean insert(Record r, String tableName) {
    int tableSearchIndex = Collections.binarySearch(this.tableNames, tableName);
    if (tableSearchIndex < 0) {
      // make new table if necessary
      tableSearchIndex = -tableSearchIndex - 1;
      this.tableNames.add(tableSearchIndex, tableName);

      this.rowStorage.add(tableSearchIndex, new ArrayList<Record>());
      this.columnIDNumbers.add(tableSearchIndex, new ArrayList<IDNumber>());
      this.columnClientNames.add(tableSearchIndex, new ArrayList<ClientName>());
      this.columnPhoneNumbers.add(tableSearchIndex, new ArrayList<PhoneNumber>());

      this.inMemoryRow.add(tableSearchIndex, new ArrayList<Boolean>());
      this.inMemoryColumnIDN.add(tableSearchIndex, new ArrayList<Boolean>());
      this.inMemoryColumnCN.add(tableSearchIndex, new ArrayList<Boolean>());
      this.inMemoryColumnPN.add(tableSearchIndex, new ArrayList<Boolean>());
    }
    int binSearchIndex = -Collections.binarySearch(this.rowStorage.get(tableSearchIndex), r) - 1;
    if (binSearchIndex < 0) {
      // skip records with same ID
      return false;
    }
    this.rowStorage.get(tableSearchIndex).add(binSearchIndex, r);
    this.inMemoryRow.get(tableSearchIndex).add(binSearchIndex, new Boolean(false));

    this.columnIDNumbers.get(tableSearchIndex).add(binSearchIndex, r.id);
    this.inMemoryColumnIDN.get(tableSearchIndex).add(binSearchIndex, new Boolean(false));
    this.columnClientNames.get(tableSearchIndex).add(binSearchIndex, r.clientName);
    this.inMemoryColumnCN.get(tableSearchIndex).add(binSearchIndex, new Boolean(false));
    this.columnPhoneNumbers.get(tableSearchIndex).add(binSearchIndex, r.phoneNumber);
    this.inMemoryColumnPN.get(tableSearchIndex).add(binSearchIndex, new Boolean(false));
    return true;
  }
  @Override
  public boolean process() {
    uniqueRows = new ArrayList<K>(new HashSet<K>(rows));
    Collections.sort(uniqueRows);
    uniqueCols = new ArrayList<J>(new HashSet<J>(cols));
    Collections.sort(uniqueCols);

    final List<Assignment> assignments = new ArrayList<Assignment>(costs.length);
    for (int i = 0; i < costs.length; i++) {
      final K rowObj = rows.get(i);
      final J colObj = cols.get(i);
      final int r = Collections.binarySearch(uniqueRows, rowObj);
      final int c = Collections.binarySearch(uniqueCols, colObj);
      assignments.add(new Assignment(r, c, costs[i]));
    }
    Collections.sort(assignments);

    // Test we do not have duplicates.
    Assignment previousAssgn = assignments.get(0);
    for (int i = 1; i < assignments.size(); i++) {
      final Assignment assgn = assignments.get(i);
      if (assgn.equals(previousAssgn)) {
        errorMessage = BASE_ERROR_MESSAGE + "Found duplicate assignment at index: " + assgn + ".";
        return false;
      }
      previousAssgn = assgn;
    }

    final int nRows = uniqueRows.size();
    final int nCols = uniqueCols.size();
    final int[] kk = new int[costs.length];
    final int[] number = new int[nRows];
    final double[] cc = new double[costs.length];

    Assignment a = assignments.get(0);
    kk[0] = a.c;
    cc[0] = a.cost;
    int currentRow = a.r;
    int nOfEl = 0;
    for (int i = 1; i < assignments.size(); i++) {
      a = assignments.get(i);

      kk[i] = a.c;
      cc[i] = a.cost;
      nOfEl++;

      if (a.r != currentRow) {
        number[currentRow] = nOfEl;
        nOfEl = 0;
        currentRow = a.r;
      }
    }
    number[currentRow] = nOfEl + 1;

    scm = new SparseCostMatrix(cc, kk, number, nCols);

    alternativeCost = computeAlternativeCosts();

    return true;
  }
 public static void main(String[] args) {
   List numL = Arrays.asList(5, 10, -5, -10);
   Collections.sort(numL);
   int v1 = Collections.binarySearch(numL, 5);
   int v2 = Collections.binarySearch(numL, 4);
   System.out.println(v1 + v2);
 }
Example #5
0
    private void search(File currentDirectory) {
      FileTreeIterator iter = new FileTreeIterator(currentDirectory);
      while (iter.hasNext()) {
        File file = iter.next();
        boolean accept = fileFilter.accept(file);
        synchronized (FileList.this) {
          if (searchID == searchCounter.get()) {
            if (accept) {

              int i =
                  searchConstraints == null
                      ? Collections.binarySearch(fileList, file)
                      : Collections.binarySearch(fileList, file, searchConstraints);
              if (i >= 0) {
                // already exists? great
              } else {
                fileList.add(-i - 1, file);
              }
            }
          } else {
            return;
          }
        }
      }
    }
Example #6
0
  private void parseFile(String filename) throws IOException {
    BufferedReader fileIn = new BufferedReader(new FileReader(filename));
    String line;
    // some persistent loop variables
    int tempo = 120;
    int key = 0;
    int timesig = 4;
    int measure = 0;
    while ((line = fileIn.readLine()) != null) {
      // we assume that listOfNotes is sorted.
      if (line.charAt(0) == '#' || line.equals("")) continue;
      String[] lineTokens = line.split(" ");

      switch (lineTokens[0]) {
        case "tempo":
          tempo = Integer.valueOf(lineTokens[1]);
          continue;
        case "key":
          key = Integer.valueOf(lineTokens[1]);
          continue;
        case "timesig":
          timesig = Integer.valueOf(lineTokens[1]);
          // put timesig into the global list
          Vector<Integer> measureSig = new Vector<Integer>(2);
          measureSig.add(measure);
          measureSig.add(timesig);
          this.timesigChanges.add(
              measureSig); // note that this always preserves order, as measure is monotone
                           // increasing
          continue;
        case "measure":
          measure = Integer.valueOf(lineTokens[1]) - 1;
          continue;
      }
      // now assuming the note start duration format
      String noteVal = keyTransform(lineTokens[0], key);
      double start = Double.valueOf(lineTokens[1]) + measure * timesig;
      double end = start + Double.valueOf(lineTokens[2]);
      // create two new NoteBeat nodes for start and end in case we need them
      NoteBeat startNote = new NoteBeat(start, tempo);
      NoteBeat endNote = new NoteBeat(end, tempo);
      // look for the start and the end in the list of preexisting ones
      int startPosition = Collections.binarySearch(this.listOfBeats, startNote);
      // process them
      if (startPosition < 0) { // insert new node into arraylist
        startNote.addNoteOn(noteVal);
        this.listOfBeats.add(-startPosition - 1, startNote);
      } else { // add this note to the noteOn list in pre-existing node
        this.listOfBeats.get(startPosition).addNoteOn(noteVal);
      }

      int endPosition = Collections.binarySearch(this.listOfBeats, endNote);
      if (endPosition < 0) {
        endNote.addNoteOff(noteVal);
        this.listOfBeats.add(-endPosition - 1, endNote);
      } else {
        this.listOfBeats.get(endPosition).addNoteOff(noteVal);
      }
    }
  }