Created
June 8, 2020 09:35
-
-
Save mzaks/f0e8f19237945fcb1597f849168ef6fe to your computer and use it in GitHub Desktop.
Find first index for eytzinger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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