Skip to content

Instantly share code, notes, and snippets.

@mzaks
Created June 8, 2020 09:35
Show Gist options
  • Select an option

  • Save mzaks/f0e8f19237945fcb1597f849168ef6fe to your computer and use it in GitHub Desktop.

Select an option

Save mzaks/f0e8f19237945fcb1597f849168ef6fe to your computer and use it in GitHub Desktop.
Find first index for eytzinger
pub fn first_index_for_eytzinger<T>(arr: &[T], value: &T) -> Option<usize> where T: PartialOrd {
let mut index = 0;
let count = arr.len();
while index < count {
let candidate = unsafe{ arr.get_unchecked(index) };
if value == candidate {
return Some(index)
}
index = index * 2 + 1 + ((candidate < value) as usize);
}
None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment