1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
std::size_t lower_bound( const std::vector<int>& seq, std::size_t start,
std::size_t end, int value )
{
if( start == end ) return start ; // empty sequence
else if( (end-start) == 1U ) return seq[start] < value ? end : start ;
const auto mid = start + (end-start) / 2 ;
if( seq[mid] < value ) return lower_bound( seq, mid+1, end, value ) ;
else return lower_bound( seq, start, mid, value ) ;
}
std::size_t lower_bound( const std::vector<int>& seq, int value )
{ return lower_bound( seq, 0, seq.size(), value ) ; }
std::size_t upper_bound( const std::vector<int>& seq, int value )
{
if( value == std::numeric_limits<int>::max() ) return seq.size() ;
else return lower_bound( seq, 0, seq.size(), value+1 ) ;
}
|