Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement index-sliced key-value iterators #104

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use std::hash::Hasher;
use std::iter::FromIterator;
use std::collections::hash_map::RandomState;
use std::ops::RangeFull;
use std::ops::RangeBounds;

use std::cmp::{max, Ordering};
use std::fmt;
use std::mem::{replace};
use std::marker::PhantomData;

use util::{third, ptrdistance, enumerate};
use util::{third, ptrdistance, enumerate, SliceWith};
use equivalent::Equivalent;
use {
Bucket,
Expand Down Expand Up @@ -931,6 +932,48 @@ impl<K, V, S> IndexMap<K, V, S>
}
}

/// Return a sliced iterator over the key-value pairs of the map, in their order
///
/// ***Panics*** if the range is out of bounds.
pub fn iter_slice<I>(&self, range: I) -> Iter<K, V>
where I: RangeBounds<usize>
{
Iter {
iter: self.core.entries.slice(range).iter()
}
}

/// Return a sliced iterator over the key-value pairs of the map, in their order
///
/// ***Panics*** if the range is out of bounds.
pub fn iter_slice_mut<I>(&mut self, range: I) -> IterMut<K, V>
where I: RangeBounds<usize>
{
IterMut {
iter: self.core.entries.slice_mut(range).iter_mut()
}
}


/// Return a sliced iterator over the values of the map, in their order
///
/// ***Panics*** if the range is out of bounds.
pub fn values_slice<I>(&self, range: I) -> Values<K, V>
where I: RangeBounds<usize>
{
Values { iter: self.core.entries.slice(range).iter() }
}

/// Return a sliced iterator over mutable references to the the values of the map,
/// in their order
///
/// ***Panics*** if the range is out of bounds.
pub fn values_slice_mut<I>(&mut self, range: I) -> ValuesMut<K, V>
where I: RangeBounds<usize>
{
ValuesMut { iter: self.core.entries.slice_mut(range).iter_mut() }
}

/// Return `true` if an equivalent to `key` exists in the map.
///
/// Computes in **O(1)** time (average).
Expand Down
163 changes: 163 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

use std::iter::Enumerate;
use std::mem::size_of;
use std::ops::Bound;
use std::ops::RangeBounds;
use std::ops::{Range, RangeFrom, RangeInclusive};

pub fn third<A, B, C>(t: (A, B, C)) -> C { t.2 }

Expand All @@ -15,3 +18,163 @@ pub fn ptrdistance<T>(a: *const T, b: *const T) -> usize {
debug_assert!(a as usize <= b as usize);
(b as usize - a as usize) / size_of::<T>()
}

// Trait for calling &slice[r] for range r on either shared or mutable slices
pub trait SliceWithRange {
fn slice_range(self, r: Range<usize>) -> Self;
fn slice_rangefrom(self, r: RangeFrom<usize>) -> Self;
fn slice_rangeincl(self, r: RangeInclusive<usize>) -> Self;
}

impl<'a, T> SliceWithRange for &'a [T] {
fn slice_range(self, r: Range<usize>) -> Self { &self[r] }
fn slice_rangefrom(self, r: RangeFrom<usize>) -> Self { &self[r] }
fn slice_rangeincl(self, r: RangeInclusive<usize>) -> Self { &self[r] }
}

impl<'a, T> SliceWithRange for &'a mut [T] {
fn slice_range(self, r: Range<usize>) -> Self { &mut self[r] }
fn slice_rangefrom(self, r: RangeFrom<usize>) -> Self { &mut self[r] }
fn slice_rangeincl(self, r: RangeInclusive<usize>) -> Self { &mut self[r] }
}

/// Fluent API for slicing with RangeBounds on vectors and slices
pub trait SliceWith {
fn slice<I>(&self, r: I) -> &Self where I: RangeBounds<usize>;
fn slice_mut<I>(&mut self, r: I) -> &mut Self where I: RangeBounds<usize>;
}

impl<T> SliceWith for [T] {
fn slice<I>(&self, r: I) -> &Self where I: RangeBounds<usize>
{ slice_by(self, r) }

fn slice_mut<I>(&mut self, r: I) -> &mut Self where I: RangeBounds<usize>
{ slice_by(self, r) }
}

fn slice_by<S, I>(v: S, range: I) -> S
where S: SliceWithRange, I: RangeBounds<usize>
{
let start = match range.start_bound() {
Bound::Unbounded => 0,
Bound::Included(&i) => i,
Bound::Excluded(&i) => check_exclusive_to_inclusive_start(i),
};
match range.end_bound() {
Bound::Excluded(&i) => v.slice_range(start..i),
Bound::Included(&i) => v.slice_rangeincl(start..=i),
Bound::Unbounded => v.slice_rangefrom(start..),
}
}

/// Compute i + 1 but check for overflow
#[inline]
fn check_exclusive_to_inclusive_start(i: usize) -> usize {
// Range exclusive from usize max is always out of bounds.
// Exclusive from usize max to unbounded is a well-formed range (!0, infinity)
// but always out of bounds in slices which have len <= !0 anyway.
//
// Range exclusive from usize max to Included/Excluded usize value is
// an ill-formed range because it does not have start <= end, but we
// don't make the difference here.
//
// start at i + 1 but panic for oob on overflow
let (iplus1, overflow) = i.overflowing_add(1);
if overflow {
panic!(concat!("Range out of bounds: exclusive from ",
stringify!(std::usize::MAX)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That stringify! prints the tokens, not the constant value -- std :: usize :: MAX. You could use a runtime formatting string for panic! -- this is a cold case anyway. Or just say, "exclusive from usize::MAX".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printing the tokens was intended, but the whitespace isn't pretty. I guess I can just use the simple option of writing usize::MAX :)

}
iplus1
}

#[cfg(test)]
mod tests {
use std::ops::Bound;
use std::ops::Range;
use util::enumerate;
use util::ptrdistance;
use util::slice_by;

const LEN: usize = 15;

fn mkarray() -> [usize; LEN] {
let mut arr = [0; LEN];
for (i, elt) in enumerate(&mut arr) {
*elt = i as usize;
}
arr
}

fn assert_range(start: &[usize], slice: &[usize], r: Range<usize>) {
assert_eq!(slice.len(), r.len());
if r.len() > 0 {
assert_eq!(slice[0], r.start);
}
assert_eq!(r.start, ptrdistance(start.as_ptr(), slice.as_ptr()));
}

#[test]
fn range() {
let data = &mkarray()[..];
let s = slice_by(data, ..);
assert_range(data, s, 0..LEN);

for i in 0..LEN {
for j in i..LEN {
let s = slice_by(data, i..j);
assert_range(data, s, i..j);

if j + 1 < LEN {
let s = slice_by(data, i..=j);
assert_range(data, s, i..j + 1);
}
}

let s = slice_by(data, i..);
assert_range(data, s, i..LEN);

let s = slice_by(data, ..i);
assert_range(data, s, 0..i);
}
}

#[test]
fn bound() {
let data = &mkarray()[..];
let s = slice_by(data, (Bound::Excluded(1), Bound::Unbounded));
assert_range(data, s, 2..LEN);

let s = slice_by(data, (Bound::Excluded(1), Bound::Excluded(2)));
assert_range(data, s, 2..2);

let s = slice_by(data, (Bound::Excluded(1), Bound::Excluded(3)));
assert_range(data, s, 2..3);

let s = slice_by(data, (Bound::Excluded(1), Bound::Included(1)));
assert_range(data, s, 2..2);

let s = slice_by(data, (Bound::Excluded(1), Bound::Included(2)));
assert_range(data, s, 2..3);
}

#[test]
#[should_panic="out of bounds"]
fn exclusive_start_at_the_end() {
let data = &mut mkarray()[..];
slice_by(data, (Bound::Excluded(!0), Bound::Unbounded));
}

#[test]
#[should_panic]
fn exclusive_start_before_end() {
let data = &mkarray()[..];
slice_by(data, (Bound::Excluded(1), Bound::Excluded(1)));
}

#[test]
#[should_panic]
fn inclusive_end_at_length() {
let data = &mkarray()[..];
slice_by(data, 0..=LEN);
}
}