pub fn binary_search<T: Measure + PartialEnum, U, V, I: AsRef<Item<AnyRange<T>, V>>>(
	items: &[I],
	element: &U,
	connected: bool,
) -> Option<usize>
where
	U: RangePartialOrd<T>,
{
	if items.is_empty()
		|| element
			.range_partial_cmp(items[0].as_ref().key())
			.unwrap_or(RangeOrdering::Before(false))
			.is_before(connected)
	{
		None
	} else {
		let mut i = 0;
		let mut j = items.len() - 1;

		if !element
			.range_partial_cmp(items[j].as_ref().key())
			.unwrap_or(RangeOrdering::After(false))
			.is_before(connected)
		{
			return Some(j);
		}

		// invariants:
		// vec[i].as_ref().key() < range
		// vec[j].as_ref().key() >= range
		// j > i

		while j - i > 1 {
			let k = (i + j) / 2;

			if let Some(ord) = element.range_partial_cmp(items[k].as_ref().key()) {
				if ord.is_before(connected) {
					j = k;
				} else {
					i = k;
				}
			} else {
				return None; // FIXME: that's bad. Maybe we should expect a total order.
			}
		}

		Some(i)
	}
}
/// Search for the index of the gratest item less/below or equal/including the given element.
///
/// If `connected` is `true`, then it will search for the gratest item less/below or equal/including **or connected to** the given element.
Search for the index of the gratest item less/below or equal/including the given element.
If `connected` is `true`, then it will search for the gratest item less/below or equal/including **or connected to** the given element.
Search for the index of the gratest item less / below or equal / including the given element . If ` connected ` is ` true ` then it will search for the gratest item less / below or equal / including ** or connected to ** the given element .
pub fn binary_search<T: Measure + PartialEnum, U, V, I: AsRef<Item<AnyRange<T>, V>>>(
	items: &[I],
	element: &U,
	connected: bool,
) -> Option<usize>
where
	U: RangePartialOrd<T>,
{
	if items.is_empty()
		|| element
			.range_partial_cmp(items[0].as_ref().key())
			.unwrap_or(RangeOrdering::Before(false))
			.is_before(connected)
	{
		None
	} else {
		let mut i = 0;
		let mut j = items.len() - 1;
		if !element
			.range_partial_cmp(items[j].as_ref().key())
			.unwrap_or(RangeOrdering::After(false))
			.is_before(connected)
		{
			return Some(j);
		}
		// invariants:
		// vec[i].as_ref().key() < range
		// vec[j].as_ref().key() >= range
		// j > i
		while j - i > 1 {
			let k = (i + j) / 2;
			if let Some(ord) = element.range_partial_cmp(items[k].as_ref().key()) {
				if ord.is_before(connected) {
					j = k;
				} else {
					i = k;
				}
			} else {
				return None; // FIXME: that's bad. Maybe we should expect a total order.
			}
		}
		Some(i)
	}
}
pub fn binary_search < T : Measure + PartialEnum , U , V , I : AsRef < Item < AnyRange < T > , V > > > ( items : & [ I ] , element : & U , connected : bool , ) -> Option < usize > where U : RangePartialOrd < T > , { if items . is_empty ( ) || element . range_partial_cmp ( items [ 0 ] . as_ref ( ) . key ( ) ) . unwrap_or ( RangeOrdering :: Before ( false ) ) . is_before ( connected ) { None } else { let mut i = 0 ; let mut j = items . len ( ) - 1 ; if ! element . range_partial_cmp ( items [ j ] . as_ref ( ) . key ( ) ) . unwrap_or ( RangeOrdering :: After ( false ) ) . is_before ( connected ) { return Some ( j ) ; } // invariants: // vec[i].as_ref().key() < range // vec[j].as_ref().key() >= range // j > i while j - i > 1 { let k = ( i + j ) / 2 ; if let Some ( ord ) = element . range_partial_cmp ( items [ k ] . as_ref ( ) . key ( ) ) { if ord . is_before ( connected ) { j = k ; } else { i = k ; } } else { return None ; // FIXME: that's bad. Maybe we should expect a total order. } } Some ( i ) } }
Search for the index of the gratest item less/below or equal/including the given element.
Search for the index of the gratest item less / below or equal / including the given element .
items
&[I]
element
&U
connected
bool
items
&[I]
element
&U
connected
bool