pub trait BinarySearchable<I, V>{
// Required methods
fn get_val(&mut self, index: &I) -> V;
fn found_result(
&mut self,
val: &V,
lower_bound: &I,
mid_point: &I,
upper_bound: &I,
) -> bool;
fn set_next_bounds(
&mut self,
val: &V,
lower_bound: &mut I,
mid_point: &I,
upper_bound: &mut I,
) -> bool;
// Provided method
fn binary_search(
&mut self,
lower_bound: I,
upper_bound: I,
) -> Option<(I, V)> { ... }
}Expand description
For example usage, see https://github.com/glennhartmann/aoc19 day_14.rs.
Required Methods§
Sourcefn get_val(&mut self, index: &I) -> V
fn get_val(&mut self, index: &I) -> V
Gets the value at the given index. Called at with the midpoint at the beginning of each loop. Can mutate internal state.
Sourcefn found_result(
&mut self,
val: &V,
lower_bound: &I,
mid_point: &I,
upper_bound: &I,
) -> bool
fn found_result( &mut self, val: &V, lower_bound: &I, mid_point: &I, upper_bound: &I, ) -> bool
Called immediately after retrieving the val at the midpoint using get_val(). Returns
whether the result has been found. Returning true will exit the loop, returning val and its
index. Can mutate internal state.
Sourcefn set_next_bounds(
&mut self,
val: &V,
lower_bound: &mut I,
mid_point: &I,
upper_bound: &mut I,
) -> bool
fn set_next_bounds( &mut self, val: &V, lower_bound: &mut I, mid_point: &I, upper_bound: &mut I, ) -> bool
Called immediately after found_result(), provided that found_result() returned false. The
purpose is to mutate lower_bound and upper_bound. The return value indicates whether to
abort the search in failure. Returning true will result in the loop exiting and returning
None. Can mutate internal state.