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 Apr 19, 2021
1 parent b5e8484 commit 89744e7
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions include/networkit/algebraic/CSRGeneralMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#ifndef NETWORKIT_ALGEBRAIC_CSR_GENERAL_MATRIX_HPP_
#define NETWORKIT_ALGEBRAIC_CSR_GENERAL_MATRIX_HPP_

#include <omp.h>
#include <algorithm>
#include <numeric>
#include <omp.h>
#include <vector>

#include <networkit/Globals.hpp>
Expand All @@ -32,7 +33,6 @@ namespace NetworKit {
*/
template <class ValueType>
class CSRGeneralMatrix {
private:
std::vector<index> rowIdx;
std::vector<index> columnIdx;
std::vector<ValueType> nonZeros;
Expand Down Expand Up @@ -84,31 +84,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 89744e7

Please sign in to comment.