Skip to content

Commit

Permalink
Use std::lower_bound instead of reimplementing binary-search
Browse files Browse the repository at this point in the history
  • Loading branch information
angriman committed Jun 23, 2021
1 parent 63f9aba commit 752ad34
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions include/networkit/algebraic/CSRGeneralMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,22 @@ class CSRGeneralMatrix {
}

/**
* Binary search the sorted columnIdx vector between [@a left, @a right] for
* column @a j.
* Binary search the sorted columnIdx vector between [@a left, @a right]
* for column @a j.
* If @a j is not present, the index that is immediately left of the place
* where @a j would be
* is returned. If
* where @a j would be is returned.
* @param left
* @param right
* @param j
* @return The position of column @a j in columnIdx or the element immediately
* to the left of the place where @a j
* would be.
* to the left of the place where @a j would be.
*/
index binarySearchColumns(index left, index right, index j) const {
assert(sorted());
if (left > right)
return right; // return the index immediately left to j if it would be
// present
index mid = (left + right) / 2;
if (columnIdx[mid] == j) {
return mid;
} else if (columnIdx[mid] > j && mid > 0) {
return binarySearchColumns(left, mid - 1, j);
} else {
return binarySearchColumns(mid + 1, right, j);
}
const auto it = std::lower_bound(columnIdx.begin() + left, columnIdx.begin() + right, j);
if (it == columnIdx.end() || *it != j)
return none;
return it - columnIdx.begin();
}

public:
Expand Down

0 comments on commit 752ad34

Please sign in to comment.