Last active
September 25, 2020 01:02
-
-
Save MikuroXina/83592e39038b34070a219903e517a54c to your computer and use it in GitHub Desktop.
Iterator for Binary Indexed Tree
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
| struct BITIter { | |
| start: i64, | |
| end: i64 | |
| } | |
| impl BITIter { | |
| fn new(start: usize, end: usize) -> Self { | |
| BITIter { start: start as i64, end: end as i64 } | |
| } | |
| } | |
| impl std::iter::Iterator for BITIter { | |
| type Item = usize; | |
| fn next(&mut self) -> Option<Self::Item> { | |
| let ret = self.start; | |
| if ret <= 0 || self.end <= ret { | |
| return None; | |
| } | |
| self.start += least_significant_bit(self.start); | |
| Some(ret as usize) | |
| } | |
| } | |
| impl std::iter::DoubleEndedIterator for BITIter { | |
| fn next_back(&mut self) -> Option<Self::Item> { | |
| let ret = self.end; | |
| if ret <= self.start { | |
| return None; | |
| } | |
| self.end -= least_significant_bit(self.end); | |
| Some(ret as usize) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment