Skip to content

Commit

Permalink
Assorted updates
Browse files Browse the repository at this point in the history
  • Loading branch information
slightlyoutofphase committed Apr 2, 2021
1 parent 413c5dc commit 8a9668c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository = "https://github.com/slightlyoutofphase/staticstep"
documentation = "https://docs.rs/staticstep/"
license = "MIT OR Apache-2.0"
readme = "README.md"
version = "0.2.0"
version = "0.2.1"
keywords = ["iterator", "iterators", "step", "range", "ranges"]
categories = ["data-structures", "algorithms"]
edition = "2018"
Expand Down
32 changes: 16 additions & 16 deletions benches/step_by_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,79 @@ use staticstep::*;

#[bench]
fn inc_by_exclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (0..32768).inc_by::<16>() {
for i in (0usize..32768usize).inc_by::<16usize>() {
j += black_box(i);
}
});
}

#[bench]
fn inc_by_inclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (0..=32768).inc_by::<16>() {
for i in (0usize..=32768usize).inc_by::<16usize>() {
j += black_box(i);
}
});
}

#[bench]
fn dec_by_exclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (32768..0).dec_by::<16>() {
for i in (32768usize..0usize).dec_by::<16usize>() {
j += black_box(i);
}
});
}

#[bench]
fn dec_by_inclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (32768..=0).dec_by::<16>() {
for i in (32768usize..=0usize).dec_by::<16usize>() {
j += black_box(i);
}
});
}

#[bench]
fn step_by_inc_exclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (0..32768).step_by(16) {
for i in (0usize..32768usize).step_by(16usize) {
j += black_box(i);
}
});
}

#[bench]
fn step_by_inc_inclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (0..=32768).step_by(16) {
for i in (0usize..=32768usize).step_by(16usize) {
j += black_box(i);
}
});
}

#[bench]
fn step_by_dec_exclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (16..32784).step_by(16).rev() {
for i in (16usize..32784usize).step_by(16usize).rev() {
j += black_box(i);
}
});
}

#[bench]
fn step_by_dec_inclusive(b: &mut Bencher) {
let mut j = 0;
let mut j = 0usize;
b.iter(|| {
for i in (0..=32768).rev().step_by(16) {
for i in (0usize..=32768usize).rev().step_by(16usize) {
j += black_box(i);
}
});
Expand Down
35 changes: 13 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ impl<T: Copy + Default + Step, const STEP: usize> IncBy<T, STEP> {
impl<T: Copy + Default + Step, const STEP: usize> DecBy<T, STEP> {
#[inline(always)]
fn new<R: RangeBounds<T>>(bounds: R) -> DecBy<T, STEP> {
let start = match bounds.start_bound() {
Included(&idx) => idx,
Excluded(&idx) => idx,
Unbounded => Default::default(),
};
let end = match bounds.end_bound() {
let start = match bounds.end_bound() {
Included(&idx) => idx,
Excluded(&idx) => Step::forward(idx, 1),
Unbounded => Step::forward(Default::default(), 1),
};
let end = match bounds.start_bound() {
Included(&idx) => Step::forward(idx, STEP),
Excluded(&idx) => Step::forward(idx, STEP + 1),
Unbounded => Default::default(),
};
DecBy { start, end }
Expand All @@ -60,14 +60,10 @@ impl<T: Copy + Default + Step, const STEP: usize> Iterator for IncBy<T, STEP> {

#[inline(always)]
fn next(&mut self) -> Option<T> {
if let Some(remaining) = Step::backward_checked(self.end, STEP) {
if remaining >= self.start {
let res = Some(self.start);
self.start = Step::forward(self.start, STEP);
res
} else {
None
}
if self.start <= Step::backward(self.end, STEP) {
let res = Some(self.start);
self.start = Step::forward(self.start, STEP);
res
} else {
None
}
Expand All @@ -79,14 +75,9 @@ impl<T: Copy + Default + Step, const STEP: usize> Iterator for DecBy<T, STEP> {

#[inline(always)]
fn next(&mut self) -> Option<T> {
if let Some(remaining) = Step::backward_checked(self.start, STEP) {
if self.start >= self.end {
let res = Some(self.start);
self.start = remaining;
res
} else {
None
}
if Step::forward(self.start, STEP) <= self.end {
self.end = Step::backward(self.end, STEP);
Some(self.end)
} else {
None
}
Expand Down
60 changes: 56 additions & 4 deletions tests/test_staticstep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ fn inc_by_exclusive() {
assert_eq!(r.next(), None);
}

#[test]
fn inc_by_exclusive_unbound_start() {
let mut r = (..64).inc_by::<16>();
assert_eq!(r.next(), Some(0));
assert_eq!(r.next(), Some(16));
assert_eq!(r.next(), Some(32));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), None);
}

#[test]
fn inc_by_inclusive() {
let mut r = (0..=64).inc_by::<16>();
Expand All @@ -22,8 +32,40 @@ fn inc_by_inclusive() {
}

#[test]
fn dec_by_exclusive() {
let mut r = (64usize..0usize).dec_by::<16usize>();
fn inc_by_inclusive_unbound_start() {
let mut r = (..=64).inc_by::<16>();
assert_eq!(r.next(), Some(0));
assert_eq!(r.next(), Some(16));
assert_eq!(r.next(), Some(32));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), Some(64));
assert_eq!(r.next(), None);
}

#[test]
fn dec_by_exclusive_usize() {
let mut r = (64usize..0usize).dec_by::<16>();
assert_eq!(r.next(), Some(64));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), Some(32));
assert_eq!(r.next(), Some(16));
assert_eq!(r.next(), None);
}

#[test]
fn dec_by_inclusive_usize() {
let mut r = (64usize..=0usize).dec_by::<16>();
assert_eq!(r.next(), Some(64));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), Some(32));
assert_eq!(r.next(), Some(16));
assert_eq!(r.next(), Some(0));
assert_eq!(r.next(), None);
}

#[test]
fn dec_by_exclusive_isize() {
let mut r = (64isize..0isize).dec_by::<16>();
assert_eq!(r.next(), Some(64));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), Some(32));
Expand All @@ -32,8 +74,8 @@ fn dec_by_exclusive() {
}

#[test]
fn dec_by_inclusive() {
let mut r = (64..=0).dec_by::<16>();
fn dec_by_inclusive_isize() {
let mut r = (64isize..=0isize).dec_by::<16>();
assert_eq!(r.next(), Some(64));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), Some(32));
Expand All @@ -42,6 +84,16 @@ fn dec_by_inclusive() {
assert_eq!(r.next(), None);
}

#[test]
fn dec_by_exclusive_unbound_end() {
let mut r = (64isize..).dec_by::<16>();
assert_eq!(r.next(), Some(64));
assert_eq!(r.next(), Some(48));
assert_eq!(r.next(), Some(32));
assert_eq!(r.next(), Some(16));
assert_eq!(r.next(), None);
}

#[test]
fn inc_by_exclusive_wrong() {
let mut r = (64..0).inc_by::<16>();
Expand Down

0 comments on commit 8a9668c

Please sign in to comment.