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

Add values_mut (fixes #36) and shrink_to_fit (fixes #28). #37

Merged
merged 2 commits into from
Jun 19, 2017
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*~
*.swp
target
Cargo.lock
47 changes: 47 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ impl<V> VecMap<V> {
}
}

/// Trims the `VecMap` of any excess capacity.
Copy link
Contributor

Choose a reason for hiding this comment

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

don't you think "excess capacity" could also describe Nones that may remain at the end of the array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes and I will add a fix for this momentarily!

Copy link
Contributor

Choose a reason for hiding this comment

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

great

///
/// The collection may reserve more space to avoid frequent reallocations.
///
/// # Examples
///
/// ```
/// use vec_map::VecMap;
/// let mut map: VecMap<&str> = VecMap::with_capacity(10);
/// map.shrink_to_fit();
/// assert_eq!(map.capacity(), 0);
/// ```
pub fn shrink_to_fit(&mut self) {
// strip off trailing `None`s
if let Some(idx) = self.v.iter().rposition(Option::is_some) {
self.v.truncate(idx + 1);
} else {
self.v.clear();
}

self.v.shrink_to_fit()
}

/// Returns an iterator visiting all keys in ascending order of the keys.
/// The iterator's element type is `usize`.
pub fn keys(&self) -> Keys<V> {
Expand All @@ -203,6 +226,12 @@ impl<V> VecMap<V> {
Values { iter: self.iter() }
}

/// Returns an iterator visiting all values in ascending order of the keys.
/// The iterator's element type is `&'r mut V`.
pub fn values_mut(&mut self) -> ValuesMut<V> {
ValuesMut { iter_mut: self.iter_mut() }
}

/// Returns an iterator visiting all key-value pairs in ascending order of the keys.
/// The iterator's element type is `(usize, &'r V)`.
///
Expand Down Expand Up @@ -922,6 +951,11 @@ impl<'a, V> Clone for Values<'a, V> {
}
}

/// An iterator over the values of a map.
pub struct ValuesMut<'a, V: 'a> {
iter_mut: IterMut<'a, V>,
}

/// A consuming iterator over the key-value pairs of a map.
pub struct IntoIter<V> {
n: usize,
Expand Down Expand Up @@ -975,6 +1009,19 @@ impl<'a, V> DoubleEndedIterator for Values<'a, V> {
fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back().map(|e| e.1) }
}

impl<'a, V> Iterator for ValuesMut<'a, V> {
type Item = &'a mut V;

fn next(&mut self) -> Option<(&'a mut V)> { self.iter_mut.next().map(|e| e.1) }
fn size_hint(&self) -> (usize, Option<usize>) { self.iter_mut.size_hint() }
}

impl<'a, V> ExactSizeIterator for ValuesMut<'a, V> {}

impl<'a, V> DoubleEndedIterator for ValuesMut<'a, V> {
fn next_back(&mut self) -> Option<&'a mut V> { self.iter_mut.next_back().map(|e| e.1) }
}

impl<V> Iterator for IntoIter<V> {
type Item = (usize, V);

Expand Down