-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace rng with deterministic indexer
- Loading branch information
1 parent
3f0326f
commit b115cc7
Showing
10 changed files
with
88 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use core::{iter, ops}; | ||
|
||
/// Generate an iteration sequence. This provides *fair* iteration when multiple | ||
/// futures need to be polled concurrently. | ||
pub(crate) struct Indexer { | ||
offset: usize, | ||
max: usize, | ||
} | ||
|
||
impl Indexer { | ||
pub(crate) fn new(max: usize) -> Self { | ||
Self { offset: 0, max } | ||
} | ||
|
||
/// Access the current offset. | ||
/// | ||
/// This method should be called before calling `iter`, as `iter` will shift | ||
/// the starting point by one for the next iteration. | ||
pub(crate) fn offset(&self) -> usize { | ||
self.offset | ||
} | ||
|
||
/// Generate a range between `0..max`, incrementing the starting point | ||
/// for the next iteration. | ||
pub(crate) fn iter(&mut self) -> IndexIter { | ||
// Increment the starting point for next time. | ||
let offset = self.offset; | ||
self.offset.wrapping_rem(self.max); | ||
|
||
IndexIter { | ||
iter: (0..self.max), | ||
max: self.max, | ||
offset, | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct IndexIter { | ||
iter: ops::Range<usize>, | ||
offset: usize, | ||
max: usize, | ||
} | ||
|
||
impl Iterator for IndexIter { | ||
type Item = usize; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.iter | ||
.next() | ||
.map(|pos| (pos + self.offset).wrapping_rem(self.max)) | ||
} | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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