Skip to content
Snippets Groups Projects
Forked from agnumpde / dune-tectonic
146 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
index-in-sorted-range.hh 614 B
#ifndef DUNE_TECTONIC_INDEX_IN_SORTED_RANGE_HH
#define DUNE_TECTONIC_INDEX_IN_SORTED_RANGE_HH

#include <algorithm>

// returns v.size() if value does not exist
template <typename T>
size_t indexInSortedRange(std::vector<T> const &v, T value) {
  size_t const specialReturnValue = v.size();

  auto const b = std::begin(v);
  auto const e = std::end(v);
  auto const lb = std::lower_bound(b, e, value);

  if (lb == e) // all elements are strictly smaller
    return specialReturnValue;

  if (value < *lb) // value falls between to elements
    return specialReturnValue;

  return std::distance(b, lb);
}
#endif