1
Fork 0

run rustfmt on libstd/collections/hash folder

This commit is contained in:
Srinivas Reddy Thatiparthy 2016-09-27 00:25:47 +05:30
parent c2769285ad
commit 45f2b6abb5
No known key found for this signature in database
GPG key ID: 091C58F4BFC36571
4 changed files with 438 additions and 279 deletions

View file

@ -15,17 +15,17 @@ extern crate test;
use self::test::Bencher; use self::test::Bencher;
#[bench] #[bench]
fn new_drop(b : &mut Bencher) { fn new_drop(b: &mut Bencher) {
use super::map::HashMap; use super::map::HashMap;
b.iter(|| { b.iter(|| {
let m : HashMap<i32, i32> = HashMap::new(); let m: HashMap<i32, i32> = HashMap::new();
assert_eq!(m.len(), 0); assert_eq!(m.len(), 0);
}) })
} }
#[bench] #[bench]
fn new_insert_drop(b : &mut Bencher) { fn new_insert_drop(b: &mut Bencher) {
use super::map::HashMap; use super::map::HashMap;
b.iter(|| { b.iter(|| {

View file

@ -20,19 +20,8 @@ use mem::{self, replace};
use ops::{Deref, Index}; use ops::{Deref, Index};
use rand::{self, Rng}; use rand::{self, Rng};
use super::table::{ use super::table::{self, Bucket, EmptyBucket, FullBucket, FullBucketMut, RawTable, SafeHash};
self, use super::table::BucketState::{Empty, Full};
Bucket,
EmptyBucket,
FullBucket,
FullBucketMut,
RawTable,
SafeHash
};
use super::table::BucketState::{
Empty,
Full,
};
const INITIAL_LOG2_CAP: usize = 5; const INITIAL_LOG2_CAP: usize = 5;
const INITIAL_CAPACITY: usize = 1 << INITIAL_LOG2_CAP; // 2^5 const INITIAL_CAPACITY: usize = 1 << INITIAL_LOG2_CAP; // 2^5
@ -348,12 +337,9 @@ pub struct HashMap<K, V, S = RandomState> {
/// Search for a pre-hashed key. /// Search for a pre-hashed key.
#[inline] #[inline]
fn search_hashed<K, V, M, F>(table: M, fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) -> InternalEntry<K, V, M>
hash: SafeHash, where M: Deref<Target = RawTable<K, V>>,
mut is_match: F) F: FnMut(&K) -> bool
-> InternalEntry<K, V, M> where
M: Deref<Target=RawTable<K, V>>,
F: FnMut(&K) -> bool,
{ {
// This is the only function where capacity can be zero. To avoid // This is the only function where capacity can be zero. To avoid
// undefined behavior when Bucket::new gets the raw bucket in this // undefined behavior when Bucket::new gets the raw bucket in this
@ -375,7 +361,7 @@ fn search_hashed<K, V, M, F>(table: M,
elem: NoElem(bucket), elem: NoElem(bucket),
}; };
} }
Full(bucket) => bucket Full(bucket) => bucket,
}; };
let robin_ib = full.index() as isize - full.displacement() as isize; let robin_ib = full.index() as isize - full.displacement() as isize;
@ -394,9 +380,7 @@ fn search_hashed<K, V, M, F>(table: M,
if hash == full.hash() { if hash == full.hash() {
// If the key doesn't match, it can't be this one.. // If the key doesn't match, it can't be this one..
if is_match(full.read().0) { if is_match(full.read().0) {
return InternalEntry::Occupied { return InternalEntry::Occupied { elem: full };
elem: full
};
} }
} }
@ -409,13 +393,13 @@ fn pop_internal<K, V>(starting_bucket: FullBucketMut<K, V>) -> (K, V) {
let (empty, retkey, retval) = starting_bucket.take(); let (empty, retkey, retval) = starting_bucket.take();
let mut gap = match empty.gap_peek() { let mut gap = match empty.gap_peek() {
Some(b) => b, Some(b) => b,
None => return (retkey, retval) None => return (retkey, retval),
}; };
while gap.full().displacement() != 0 { while gap.full().displacement() != 0 {
gap = match gap.shift() { gap = match gap.shift() {
Some(b) => b, Some(b) => b,
None => break None => break,
}; };
} }
@ -429,11 +413,11 @@ fn pop_internal<K, V>(starting_bucket: FullBucketMut<K, V>) -> (K, V) {
/// ///
/// `hash`, `k`, and `v` are the elements to "robin hood" into the hashtable. /// `hash`, `k`, and `v` are the elements to "robin hood" into the hashtable.
fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>, fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>,
mut ib: usize, mut ib: usize,
mut hash: SafeHash, mut hash: SafeHash,
mut key: K, mut key: K,
mut val: V) mut val: V)
-> &'a mut V { -> &'a mut V {
let starting_index = bucket.index(); let starting_index = bucket.index();
let size = bucket.table().size(); let size = bucket.table().size();
// Save the *starting point*. // Save the *starting point*.
@ -465,8 +449,8 @@ fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>,
// FullBucketMut, into just one FullBucketMut. The "table" // FullBucketMut, into just one FullBucketMut. The "table"
// refers to the inner FullBucketMut in this context. // refers to the inner FullBucketMut in this context.
return bucket.into_table().into_mut_refs().1; return bucket.into_table().into_mut_refs().1;
}, }
Full(bucket) => bucket Full(bucket) => bucket,
}; };
let probe_ib = full_bucket.index() - full_bucket.displacement(); let probe_ib = full_bucket.index() - full_bucket.displacement();
@ -483,9 +467,12 @@ fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>,
} }
impl<K, V, S> HashMap<K, V, S> impl<K, V, S> HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher where K: Eq + Hash,
S: BuildHasher
{ {
fn make_hash<X: ?Sized>(&self, x: &X) -> SafeHash where X: Hash { fn make_hash<X: ?Sized>(&self, x: &X) -> SafeHash
where X: Hash
{
table::make_hash(&self.hash_builder, x) table::make_hash(&self.hash_builder, x)
} }
@ -494,7 +481,8 @@ impl<K, V, S> HashMap<K, V, S>
/// search_hashed. /// search_hashed.
#[inline] #[inline]
fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> InternalEntry<K, V, &'a RawTable<K, V>> fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> InternalEntry<K, V, &'a RawTable<K, V>>
where K: Borrow<Q>, Q: Eq + Hash where K: Borrow<Q>,
Q: Eq + Hash
{ {
let hash = self.make_hash(q); let hash = self.make_hash(q);
search_hashed(&self.table, hash, |k| q.eq(k.borrow())) search_hashed(&self.table, hash, |k| q.eq(k.borrow()))
@ -502,7 +490,8 @@ impl<K, V, S> HashMap<K, V, S>
#[inline] #[inline]
fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> InternalEntry<K, V, &'a mut RawTable<K, V>> fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> InternalEntry<K, V, &'a mut RawTable<K, V>>
where K: Borrow<Q>, Q: Eq + Hash where K: Borrow<Q>,
Q: Eq + Hash
{ {
let hash = self.make_hash(q); let hash = self.make_hash(q);
search_hashed(&mut self.table, hash, |k| q.eq(k.borrow())) search_hashed(&mut self.table, hash, |k| q.eq(k.borrow()))
@ -522,7 +511,7 @@ impl<K, V, S> HashMap<K, V, S>
empty.put(hash, k, v); empty.put(hash, k, v);
return; return;
} }
Full(b) => b.into_bucket() Full(b) => b.into_bucket(),
}; };
buckets.next(); buckets.next();
} }
@ -561,7 +550,8 @@ impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
} }
impl<K, V, S> HashMap<K, V, S> impl<K, V, S> HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher where K: Eq + Hash,
S: BuildHasher
{ {
/// Creates an empty `HashMap` which will use the given hash builder to hash /// Creates an empty `HashMap` which will use the given hash builder to hash
/// keys. /// keys.
@ -613,8 +603,7 @@ impl<K, V, S> HashMap<K, V, S>
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
-> HashMap<K, V, S> {
let resize_policy = DefaultResizePolicy::new(); let resize_policy = DefaultResizePolicy::new();
let min_cap = max(INITIAL_CAPACITY, resize_policy.min_capacity(capacity)); let min_cap = max(INITIAL_CAPACITY, resize_policy.min_capacity(capacity));
let internal_cap = min_cap.checked_next_power_of_two().expect("capacity overflow"); let internal_cap = min_cap.checked_next_power_of_two().expect("capacity overflow");
@ -752,7 +741,7 @@ impl<K, V, S> HashMap<K, V, S>
} }
b.into_bucket() b.into_bucket()
} }
Empty(b) => b.into_bucket() Empty(b) => b.into_bucket(),
}; };
bucket.next(); bucket.next();
} }
@ -806,16 +795,12 @@ impl<K, V, S> HashMap<K, V, S>
fn insert_hashed_nocheck(&mut self, hash: SafeHash, k: K, v: V) -> Option<V> { fn insert_hashed_nocheck(&mut self, hash: SafeHash, k: K, v: V) -> Option<V> {
let entry = search_hashed(&mut self.table, hash, |key| *key == k).into_entry(k); let entry = search_hashed(&mut self.table, hash, |key| *key == k).into_entry(k);
match entry { match entry {
Some(Occupied(mut elem)) => { Some(Occupied(mut elem)) => Some(elem.insert(v)),
Some(elem.insert(v))
}
Some(Vacant(elem)) => { Some(Vacant(elem)) => {
elem.insert(v); elem.insert(v);
None None
} }
None => { None => unreachable!(),
unreachable!()
}
} }
} }
@ -979,7 +964,9 @@ impl<K, V, S> HashMap<K, V, S>
/// assert_eq!(a.len(), 1); /// assert_eq!(a.len(), 1);
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize { self.table.size() } pub fn len(&self) -> usize {
self.table.size()
}
/// Returns true if the map contains no elements. /// Returns true if the map contains no elements.
/// ///
@ -995,7 +982,9 @@ impl<K, V, S> HashMap<K, V, S>
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool { self.len() == 0 } pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Clears the map, returning all key-value pairs as an iterator. Keeps the /// Clears the map, returning all key-value pairs as an iterator. Keeps the
/// allocated memory for reuse. /// allocated memory for reuse.
@ -1019,9 +1008,7 @@ impl<K, V, S> HashMap<K, V, S>
#[inline] #[inline]
#[stable(feature = "drain", since = "1.6.0")] #[stable(feature = "drain", since = "1.6.0")]
pub fn drain(&mut self) -> Drain<K, V> { pub fn drain(&mut self) -> Drain<K, V> {
Drain { Drain { inner: self.table.drain() }
inner: self.table.drain(),
}
} }
/// Clears the map, removing all key-value pairs. Keeps the allocated memory /// Clears the map, removing all key-value pairs. Keeps the allocated memory
@ -1064,7 +1051,8 @@ impl<K, V, S> HashMap<K, V, S>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq where K: Borrow<Q>,
Q: Hash + Eq
{ {
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1) self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1)
} }
@ -1090,7 +1078,8 @@ impl<K, V, S> HashMap<K, V, S>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq where K: Borrow<Q>,
Q: Hash + Eq
{ {
self.search(k).into_occupied_bucket().is_some() self.search(k).into_occupied_bucket().is_some()
} }
@ -1118,7 +1107,8 @@ impl<K, V, S> HashMap<K, V, S>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq where K: Borrow<Q>,
Q: Hash + Eq
{ {
self.search_mut(k).into_occupied_bucket().map(|bucket| bucket.into_mut_refs().1) self.search_mut(k).into_occupied_bucket().map(|bucket| bucket.into_mut_refs().1)
} }
@ -1176,10 +1166,11 @@ impl<K, V, S> HashMap<K, V, S>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq where K: Borrow<Q>,
Q: Hash + Eq
{ {
if self.table.size() == 0 { if self.table.size() == 0 {
return None return None;
} }
self.search_mut(k).into_occupied_bucket().map(|bucket| pop_internal(bucket).1) self.search_mut(k).into_occupied_bucket().map(|bucket| pop_internal(bucket).1)
@ -1188,25 +1179,32 @@ impl<K, V, S> HashMap<K, V, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> PartialEq for HashMap<K, V, S> impl<K, V, S> PartialEq for HashMap<K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher where K: Eq + Hash,
V: PartialEq,
S: BuildHasher
{ {
fn eq(&self, other: &HashMap<K, V, S>) -> bool { fn eq(&self, other: &HashMap<K, V, S>) -> bool {
if self.len() != other.len() { return false; } if self.len() != other.len() {
return false;
}
self.iter().all(|(key, value)| self.iter().all(|(key, value)| other.get(key).map_or(false, |v| *value == *v))
other.get(key).map_or(false, |v| *value == *v)
)
} }
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> Eq for HashMap<K, V, S> impl<K, V, S> Eq for HashMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher where K: Eq + Hash,
{} V: Eq,
S: BuildHasher
{
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> Debug for HashMap<K, V, S> impl<K, V, S> Debug for HashMap<K, V, S>
where K: Eq + Hash + Debug, V: Debug, S: BuildHasher where K: Eq + Hash + Debug,
V: Debug,
S: BuildHasher
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self.iter()).finish() f.debug_map().entries(self.iter()).finish()
@ -1216,7 +1214,7 @@ impl<K, V, S> Debug for HashMap<K, V, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> Default for HashMap<K, V, S> impl<K, V, S> Default for HashMap<K, V, S>
where K: Eq + Hash, where K: Eq + Hash,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
/// Creates an empty `HashMap<K, V, S>`, with the `Default` value for the hasher. /// Creates an empty `HashMap<K, V, S>`, with the `Default` value for the hasher.
fn default() -> HashMap<K, V, S> { fn default() -> HashMap<K, V, S> {
@ -1228,7 +1226,7 @@ impl<K, V, S> Default for HashMap<K, V, S>
impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S> impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
where K: Eq + Hash + Borrow<Q>, where K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash, Q: Eq + Hash,
S: BuildHasher, S: BuildHasher
{ {
type Output = V; type Output = V;
@ -1241,79 +1239,71 @@ impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
/// HashMap iterator. /// HashMap iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, K: 'a, V: 'a> { pub struct Iter<'a, K: 'a, V: 'a> {
inner: table::Iter<'a, K, V> inner: table::Iter<'a, K, V>,
} }
// FIXME(#19839) Remove in favor of `#[derive(Clone)]` // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> Clone for Iter<'a, K, V> { impl<'a, K, V> Clone for Iter<'a, K, V> {
fn clone(&self) -> Iter<'a, K, V> { fn clone(&self) -> Iter<'a, K, V> {
Iter { Iter { inner: self.inner.clone() }
inner: self.inner.clone()
}
} }
} }
/// HashMap mutable values iterator. /// HashMap mutable values iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, K: 'a, V: 'a> { pub struct IterMut<'a, K: 'a, V: 'a> {
inner: table::IterMut<'a, K, V> inner: table::IterMut<'a, K, V>,
} }
/// HashMap move iterator. /// HashMap move iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<K, V> { pub struct IntoIter<K, V> {
inner: table::IntoIter<K, V> inner: table::IntoIter<K, V>,
} }
/// HashMap keys iterator. /// HashMap keys iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Keys<'a, K: 'a, V: 'a> { pub struct Keys<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V> inner: Iter<'a, K, V>,
} }
// FIXME(#19839) Remove in favor of `#[derive(Clone)]` // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> Clone for Keys<'a, K, V> { impl<'a, K, V> Clone for Keys<'a, K, V> {
fn clone(&self) -> Keys<'a, K, V> { fn clone(&self) -> Keys<'a, K, V> {
Keys { Keys { inner: self.inner.clone() }
inner: self.inner.clone()
}
} }
} }
/// HashMap values iterator. /// HashMap values iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Values<'a, K: 'a, V: 'a> { pub struct Values<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V> inner: Iter<'a, K, V>,
} }
// FIXME(#19839) Remove in favor of `#[derive(Clone)]` // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> Clone for Values<'a, K, V> { impl<'a, K, V> Clone for Values<'a, K, V> {
fn clone(&self) -> Values<'a, K, V> { fn clone(&self) -> Values<'a, K, V> {
Values { Values { inner: self.inner.clone() }
inner: self.inner.clone()
}
} }
} }
/// HashMap drain iterator. /// HashMap drain iterator.
#[stable(feature = "drain", since = "1.6.0")] #[stable(feature = "drain", since = "1.6.0")]
pub struct Drain<'a, K: 'a, V: 'a> { pub struct Drain<'a, K: 'a, V: 'a> {
inner: table::Drain<'a, K, V> inner: table::Drain<'a, K, V>,
} }
/// Mutable HashMap values iterator. /// Mutable HashMap values iterator.
#[stable(feature = "map_values_mut", since = "1.10.0")] #[stable(feature = "map_values_mut", since = "1.10.0")]
pub struct ValuesMut<'a, K: 'a, V: 'a> { pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V> inner: IterMut<'a, K, V>,
} }
enum InternalEntry<K, V, M> { enum InternalEntry<K, V, M> {
Occupied { Occupied { elem: FullBucket<K, V, M> },
elem: FullBucket<K, V, M>,
},
Vacant { Vacant {
hash: SafeHash, hash: SafeHash,
elem: VacantEntryState<K, V, M>, elem: VacantEntryState<K, V, M>,
@ -1338,7 +1328,7 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
InternalEntry::Occupied { elem } => { InternalEntry::Occupied { elem } => {
Some(Occupied(OccupiedEntry { Some(Occupied(OccupiedEntry {
key: Some(key), key: Some(key),
elem: elem elem: elem,
})) }))
} }
InternalEntry::Vacant { hash, elem } => { InternalEntry::Vacant { hash, elem } => {
@ -1348,7 +1338,7 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
elem: elem, elem: elem,
})) }))
} }
InternalEntry::TableIsEmpty => None InternalEntry::TableIsEmpty => None,
} }
} }
} }
@ -1362,27 +1352,29 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
pub enum Entry<'a, K: 'a, V: 'a> { pub enum Entry<'a, K: 'a, V: 'a> {
/// An occupied Entry. /// An occupied Entry.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
Occupied( Occupied(#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "rust1", since = "1.0.0")] OccupiedEntry<'a, K, V> OccupiedEntry<'a, K, V>),
),
/// A vacant Entry. /// A vacant Entry.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
Vacant( Vacant(#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "rust1", since = "1.0.0")] VacantEntry<'a, K, V> VacantEntry<'a, K, V>),
),
} }
#[stable(feature= "debug_hash_map", since = "1.12.0")] #[stable(feature= "debug_hash_map", since = "1.12.0")]
impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for Entry<'a, K, V> { impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for Entry<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Vacant(ref v) => f.debug_tuple("Entry") Vacant(ref v) => {
.field(v) f.debug_tuple("Entry")
.finish(), .field(v)
Occupied(ref o) => f.debug_tuple("Entry") .finish()
.field(o) }
.finish(), Occupied(ref o) => {
f.debug_tuple("Entry")
.field(o)
.finish()
}
} }
} }
} }
@ -1401,9 +1393,9 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for OccupiedEntry<'a, K, V> { impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for OccupiedEntry<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("OccupiedEntry") f.debug_struct("OccupiedEntry")
.field("key", self.key()) .field("key", self.key())
.field("value", self.get()) .field("value", self.get())
.finish() .finish()
} }
} }
@ -1422,8 +1414,8 @@ pub struct VacantEntry<'a, K: 'a, V: 'a> {
impl<'a, K: 'a + Debug, V: 'a> Debug for VacantEntry<'a, K, V> { impl<'a, K: 'a + Debug, V: 'a> Debug for VacantEntry<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("VacantEntry") f.debug_tuple("VacantEntry")
.field(self.key()) .field(self.key())
.finish() .finish()
} }
} }
@ -1438,7 +1430,8 @@ enum VacantEntryState<K, V, M> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher where K: Eq + Hash,
S: BuildHasher
{ {
type Item = (&'a K, &'a V); type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>; type IntoIter = Iter<'a, K, V>;
@ -1450,7 +1443,8 @@ impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher where K: Eq + Hash,
S: BuildHasher
{ {
type Item = (&'a K, &'a mut V); type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>; type IntoIter = IterMut<'a, K, V>;
@ -1462,7 +1456,8 @@ impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> IntoIterator for HashMap<K, V, S> impl<K, V, S> IntoIterator for HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher where K: Eq + Hash,
S: BuildHasher
{ {
type Item = (K, V); type Item = (K, V);
type IntoIter = IntoIter<K, V>; type IntoIter = IntoIter<K, V>;
@ -1485,9 +1480,7 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S>
/// let vec: Vec<(&str, isize)> = map.into_iter().collect(); /// let vec: Vec<(&str, isize)> = map.into_iter().collect();
/// ``` /// ```
fn into_iter(self) -> IntoIter<K, V> { fn into_iter(self) -> IntoIter<K, V> {
IntoIter { IntoIter { inner: self.table.into_iter() }
inner: self.table.into_iter()
}
} }
} }
@ -1495,12 +1488,21 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S>
impl<'a, K, V> Iterator for Iter<'a, K, V> { impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V); type Item = (&'a K, &'a V);
#[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(&'a K, &'a V)> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> { impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
@ -1510,12 +1512,21 @@ impl<'a, K, V> FusedIterator for Iter<'a, K, V> {}
impl<'a, K, V> Iterator for IterMut<'a, K, V> { impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V); type Item = (&'a K, &'a mut V);
#[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> { impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {} impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
@ -1524,12 +1535,21 @@ impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
impl<K, V> Iterator for IntoIter<K, V> { impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V); type Item = (K, V);
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next().map(|(_, k, v)| (k, v)) } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(K, V)> {
self.inner.next().map(|(_, k, v)| (k, v))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V> ExactSizeIterator for IntoIter<K, V> { impl<K, V> ExactSizeIterator for IntoIter<K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<K, V> FusedIterator for IntoIter<K, V> {} impl<K, V> FusedIterator for IntoIter<K, V> {}
@ -1538,12 +1558,21 @@ impl<K, V> FusedIterator for IntoIter<K, V> {}
impl<'a, K, V> Iterator for Keys<'a, K, V> { impl<'a, K, V> Iterator for Keys<'a, K, V> {
type Item = &'a K; type Item = &'a K;
#[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next().map(|(k, _)| k) } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(&'a K)> {
self.inner.next().map(|(k, _)| k)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> { impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K, V> FusedIterator for Keys<'a, K, V> {} impl<'a, K, V> FusedIterator for Keys<'a, K, V> {}
@ -1552,12 +1581,21 @@ impl<'a, K, V> FusedIterator for Keys<'a, K, V> {}
impl<'a, K, V> Iterator for Values<'a, K, V> { impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V; type Item = &'a V;
#[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next().map(|(_, v)| v) } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(&'a V)> {
self.inner.next().map(|(_, v)| v)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> { impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K, V> FusedIterator for Values<'a, K, V> {} impl<'a, K, V> FusedIterator for Values<'a, K, V> {}
@ -1566,12 +1604,21 @@ impl<'a, K, V> FusedIterator for Values<'a, K, V> {}
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V; type Item = &'a mut V;
#[inline] fn next(&mut self) -> Option<(&'a mut V)> { self.inner.next().map(|(_, v)| v) } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(&'a mut V)> {
self.inner.next().map(|(_, v)| v)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "map_values_mut", since = "1.10.0")] #[stable(feature = "map_values_mut", since = "1.10.0")]
impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> { impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K, V> FusedIterator for ValuesMut<'a, K, V> {} impl<'a, K, V> FusedIterator for ValuesMut<'a, K, V> {}
@ -1580,12 +1627,21 @@ impl<'a, K, V> FusedIterator for ValuesMut<'a, K, V> {}
impl<'a, K, V> Iterator for Drain<'a, K, V> { impl<'a, K, V> Iterator for Drain<'a, K, V> {
type Item = (K, V); type Item = (K, V);
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next().map(|(_, k, v)| (k, v)) } #[inline]
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } fn next(&mut self) -> Option<(K, V)> {
self.inner.next().map(|(_, k, v)| (k, v))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> { impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
#[inline] fn len(&self) -> usize { self.inner.len() } #[inline]
fn len(&self) -> usize {
self.inner.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K, V> FusedIterator for Drain<'a, K, V> {} impl<'a, K, V> FusedIterator for Drain<'a, K, V> {}
@ -1880,21 +1936,18 @@ impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(self, value: V) -> &'a mut V { pub fn insert(self, value: V) -> &'a mut V {
match self.elem { match self.elem {
NeqElem(bucket, ib) => { NeqElem(bucket, ib) => robin_hood(bucket, ib, self.hash, self.key, value),
robin_hood(bucket, ib, self.hash, self.key, value) NoElem(bucket) => bucket.put(self.hash, self.key, value).into_mut_refs().1,
}
NoElem(bucket) => {
bucket.put(self.hash, self.key, value).into_mut_refs().1
}
} }
} }
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default where K: Eq + Hash,
S: BuildHasher + Default
{ {
fn from_iter<T: IntoIterator<Item=(K, V)>>(iter: T) -> HashMap<K, V, S> { fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> HashMap<K, V, S> {
let iterator = iter.into_iter(); let iterator = iter.into_iter();
let lower = iterator.size_hint().0; let lower = iterator.size_hint().0;
let mut map = HashMap::with_capacity_and_hasher(lower, Default::default()); let mut map = HashMap::with_capacity_and_hasher(lower, Default::default());
@ -1905,9 +1958,10 @@ impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S> impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher where K: Eq + Hash,
S: BuildHasher
{ {
fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
for (k, v) in iter { for (k, v) in iter {
self.insert(k, v); self.insert(k, v);
} }
@ -1916,9 +1970,11 @@ impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
#[stable(feature = "hash_extend_copy", since = "1.4.0")] #[stable(feature = "hash_extend_copy", since = "1.4.0")]
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S> impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
where K: Eq + Hash + Copy, V: Copy, S: BuildHasher where K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher
{ {
fn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
self.extend(iter.into_iter().map(|(&key, &value)| (key, value))); self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
} }
} }
@ -1960,7 +2016,8 @@ impl RandomState {
/// let s = RandomState::new(); /// let s = RandomState::new();
/// ``` /// ```
#[inline] #[inline]
#[allow(deprecated)] // rand #[allow(deprecated)]
// rand
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
pub fn new() -> RandomState { pub fn new() -> RandomState {
// Historically this function did not cache keys from the OS and instead // Historically this function did not cache keys from the OS and instead
@ -1987,9 +2044,7 @@ impl RandomState {
(r.gen(), r.gen()) (r.gen(), r.gen())
}); });
KEYS.with(|&(k0, k1)| { KEYS.with(|&(k0, k1)| RandomState { k0: k0, k1: k1 })
RandomState { k0: k0, k1: k1 }
})
} }
} }
@ -2035,7 +2090,9 @@ impl Default for RandomState {
} }
impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S> impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
where K: Eq + Hash + Borrow<Q>, S: BuildHasher, Q: Eq + Hash where K: Eq + Hash + Borrow<Q>,
S: BuildHasher,
Q: Eq + Hash
{ {
type Key = K; type Key = K;
@ -2045,7 +2102,7 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
fn take(&mut self, key: &Q) -> Option<K> { fn take(&mut self, key: &Q) -> Option<K> {
if self.table.size() == 0 { if self.table.size() == 0 {
return None return None;
} }
self.search_mut(key).into_occupied_bucket().map(|bucket| pop_internal(bucket).0) self.search_mut(key).into_occupied_bucket().map(|bucket| pop_internal(bucket).0)
@ -2069,18 +2126,40 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
#[allow(dead_code)] #[allow(dead_code)]
fn assert_covariance() { fn assert_covariance() {
fn map_key<'new>(v: HashMap<&'static str, u8>) -> HashMap<&'new str, u8> { v } fn map_key<'new>(v: HashMap<&'static str, u8>) -> HashMap<&'new str, u8> {
fn map_val<'new>(v: HashMap<u8, &'static str>) -> HashMap<u8, &'new str> { v } v
fn iter_key<'a, 'new>(v: Iter<'a, &'static str, u8>) -> Iter<'a, &'new str, u8> { v } }
fn iter_val<'a, 'new>(v: Iter<'a, u8, &'static str>) -> Iter<'a, u8, &'new str> { v } fn map_val<'new>(v: HashMap<u8, &'static str>) -> HashMap<u8, &'new str> {
fn into_iter_key<'new>(v: IntoIter<&'static str, u8>) -> IntoIter<&'new str, u8> { v } v
fn into_iter_val<'new>(v: IntoIter<u8, &'static str>) -> IntoIter<u8, &'new str> { v } }
fn keys_key<'a, 'new>(v: Keys<'a, &'static str, u8>) -> Keys<'a, &'new str, u8> { v } fn iter_key<'a, 'new>(v: Iter<'a, &'static str, u8>) -> Iter<'a, &'new str, u8> {
fn keys_val<'a, 'new>(v: Keys<'a, u8, &'static str>) -> Keys<'a, u8, &'new str> { v } v
fn values_key<'a, 'new>(v: Values<'a, &'static str, u8>) -> Values<'a, &'new str, u8> { v } }
fn values_val<'a, 'new>(v: Values<'a, u8, &'static str>) -> Values<'a, u8, &'new str> { v } fn iter_val<'a, 'new>(v: Iter<'a, u8, &'static str>) -> Iter<'a, u8, &'new str> {
v
}
fn into_iter_key<'new>(v: IntoIter<&'static str, u8>) -> IntoIter<&'new str, u8> {
v
}
fn into_iter_val<'new>(v: IntoIter<u8, &'static str>) -> IntoIter<u8, &'new str> {
v
}
fn keys_key<'a, 'new>(v: Keys<'a, &'static str, u8>) -> Keys<'a, &'new str, u8> {
v
}
fn keys_val<'a, 'new>(v: Keys<'a, u8, &'static str>) -> Keys<'a, u8, &'new str> {
v
}
fn values_key<'a, 'new>(v: Values<'a, &'static str, u8>) -> Values<'a, &'new str, u8> {
v
}
fn values_val<'a, 'new>(v: Values<'a, u8, &'static str>) -> Values<'a, u8, &'new str> {
v
}
fn drain<'new>(d: Drain<'static, &'static str, &'static str>) fn drain<'new>(d: Drain<'static, &'static str, &'static str>)
-> Drain<'new, &'new str, &'new str> { d } -> Drain<'new, &'new str, &'new str> {
d
}
} }
#[cfg(test)] #[cfg(test)]
@ -2145,7 +2224,7 @@ mod test_map {
#[derive(Hash, PartialEq, Eq)] #[derive(Hash, PartialEq, Eq)]
struct Dropable { struct Dropable {
k: usize k: usize,
} }
impl Dropable { impl Dropable {
@ -2189,7 +2268,7 @@ mod test_map {
for i in 0..100 { for i in 0..100 {
let d1 = Dropable::new(i); let d1 = Dropable::new(i);
let d2 = Dropable::new(i+100); let d2 = Dropable::new(i + 100);
m.insert(d1, d2); m.insert(d1, d2);
} }
@ -2248,7 +2327,7 @@ mod test_map {
for i in 0..100 { for i in 0..100 {
let d1 = Dropable::new(i); let d1 = Dropable::new(i);
let d2 = Dropable::new(i+100); let d2 = Dropable::new(i + 100);
hm.insert(d1, d2); hm.insert(d1, d2);
} }
@ -2276,13 +2355,13 @@ mod test_map {
for _ in half.by_ref() {} for _ in half.by_ref() {}
DROP_VECTOR.with(|v| { DROP_VECTOR.with(|v| {
let nk = (0..100).filter(|&i| { let nk = (0..100)
v.borrow()[i] == 1 .filter(|&i| v.borrow()[i] == 1)
}).count(); .count();
let nv = (0..100).filter(|&i| { let nv = (0..100)
v.borrow()[i+100] == 1 .filter(|&i| v.borrow()[i + 100] == 1)
}).count(); .count();
assert_eq!(nk, 50); assert_eq!(nk, 50);
assert_eq!(nv, 50); assert_eq!(nv, 50);
@ -2339,12 +2418,12 @@ mod test_map {
for i in 1..1001 { for i in 1..1001 {
assert!(m.insert(i, i).is_none()); assert!(m.insert(i, i).is_none());
for j in 1..i+1 { for j in 1..i + 1 {
let r = m.get(&j); let r = m.get(&j);
assert_eq!(r, Some(&j)); assert_eq!(r, Some(&j));
} }
for j in i+1..1001 { for j in i + 1..1001 {
let r = m.get(&j); let r = m.get(&j);
assert_eq!(r, None); assert_eq!(r, None);
} }
@ -2358,11 +2437,11 @@ mod test_map {
for i in 1..1001 { for i in 1..1001 {
assert!(m.remove(&i).is_some()); assert!(m.remove(&i).is_some());
for j in 1..i+1 { for j in 1..i + 1 {
assert!(!m.contains_key(&j)); assert!(!m.contains_key(&j));
} }
for j in i+1..1001 { for j in i + 1..1001 {
assert!(m.contains_key(&j)); assert!(m.contains_key(&j));
} }
} }
@ -2398,7 +2477,8 @@ mod test_map {
assert!(m.insert(5, 14).is_none()); assert!(m.insert(5, 14).is_none());
let new = 100; let new = 100;
match m.get_mut(&5) { match m.get_mut(&5) {
None => panic!(), Some(x) => *x = new None => panic!(),
Some(x) => *x = new,
} }
assert_eq!(m.get(&5), Some(&new)); assert_eq!(m.get(&5), Some(&new));
} }
@ -2517,7 +2597,7 @@ mod test_map {
m.insert(1, 2); m.insert(1, 2);
match m.get(&1) { match m.get(&1) {
None => panic!(), None => panic!(),
Some(v) => assert_eq!(*v, 2) Some(v) => assert_eq!(*v, 2),
} }
} }
@ -2680,7 +2760,7 @@ mod test_map {
fn test_size_hint() { fn test_size_hint() {
let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
let map: HashMap<_, _> = xs.iter().cloned().collect(); let map: HashMap<_, _> = xs.iter().cloned().collect();
let mut iter = map.iter(); let mut iter = map.iter();
@ -2693,7 +2773,7 @@ mod test_map {
fn test_iter_len() { fn test_iter_len() {
let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
let map: HashMap<_, _> = xs.iter().cloned().collect(); let map: HashMap<_, _> = xs.iter().cloned().collect();
let mut iter = map.iter(); let mut iter = map.iter();
@ -2706,7 +2786,7 @@ mod test_map {
fn test_mut_size_hint() { fn test_mut_size_hint() {
let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
let mut map: HashMap<_, _> = xs.iter().cloned().collect(); let mut map: HashMap<_, _> = xs.iter().cloned().collect();
let mut iter = map.iter_mut(); let mut iter = map.iter_mut();
@ -2719,7 +2799,7 @@ mod test_map {
fn test_iter_mut_len() { fn test_iter_mut_len() {
let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
let mut map: HashMap<_, _> = xs.iter().cloned().collect(); let mut map: HashMap<_, _> = xs.iter().cloned().collect();
let mut iter = map.iter_mut(); let mut iter = map.iter_mut();
@ -2752,7 +2832,7 @@ mod test_map {
} }
#[test] #[test]
fn test_entry(){ fn test_entry() {
let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)]; let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
let mut map: HashMap<_, _> = xs.iter().cloned().collect(); let mut map: HashMap<_, _> = xs.iter().cloned().collect();
@ -2826,11 +2906,11 @@ mod test_map {
for i in 0..1000 { for i in 0..1000 {
let x = rng.gen_range(-10, 10); let x = rng.gen_range(-10, 10);
match m.entry(x) { match m.entry(x) {
Vacant(_) => {}, Vacant(_) => {}
Occupied(e) => { Occupied(e) => {
println!("{}: remove {}", i, x); println!("{}: remove {}", i, x);
e.remove(); e.remove();
}, }
} }
check(&m); check(&m);
@ -2908,7 +2988,7 @@ mod test_map {
Vacant(e) => { Vacant(e) => {
assert_eq!(key, *e.key()); assert_eq!(key, *e.key());
e.insert(value.clone()); e.insert(value.clone());
}, }
} }
assert_eq!(a.len(), 1); assert_eq!(a.len(), 1);
assert_eq!(a[key], value); assert_eq!(a[key], value);

View file

@ -101,7 +101,7 @@ use super::map::{self, HashMap, Keys, RandomState};
#[derive(Clone)] #[derive(Clone)]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct HashSet<T, S = RandomState> { pub struct HashSet<T, S = RandomState> {
map: HashMap<T, (), S> map: HashMap<T, (), S>,
} }
impl<T: Hash + Eq> HashSet<T, RandomState> { impl<T: Hash + Eq> HashSet<T, RandomState> {
@ -136,7 +136,8 @@ impl<T: Hash + Eq> HashSet<T, RandomState> {
} }
impl<T, S> HashSet<T, S> impl<T, S> HashSet<T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
/// Creates a new empty hash set which will use the given hasher to hash /// Creates a new empty hash set which will use the given hasher to hash
/// keys. /// keys.
@ -184,8 +185,7 @@ impl<T, S> HashSet<T, S>
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> {
-> HashSet<T, S> {
HashSet { map: HashMap::with_capacity_and_hasher(capacity, hasher) } HashSet { map: HashMap::with_capacity_and_hasher(capacity, hasher) }
} }
@ -323,8 +323,9 @@ impl<T, S> HashSet<T, S>
/// assert_eq!(diff1, [1, 4].iter().cloned().collect()); /// assert_eq!(diff1, [1, 4].iter().cloned().collect());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, S>) pub fn symmetric_difference<'a>(&'a self,
-> SymmetricDifference<'a, T, S> { other: &'a HashSet<T, S>)
-> SymmetricDifference<'a, T, S> {
SymmetricDifference { iter: self.difference(other).chain(other.difference(self)) } SymmetricDifference { iter: self.difference(other).chain(other.difference(self)) }
} }
@ -388,7 +389,9 @@ impl<T, S> HashSet<T, S>
/// assert_eq!(v.len(), 1); /// assert_eq!(v.len(), 1);
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize { self.map.len() } pub fn len(&self) -> usize {
self.map.len()
}
/// Returns true if the set contains no elements. /// Returns true if the set contains no elements.
/// ///
@ -403,7 +406,9 @@ impl<T, S> HashSet<T, S>
/// assert!(!v.is_empty()); /// assert!(!v.is_empty());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool { self.map.is_empty() } pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
/// Clears the set, returning all elements in an iterator. /// Clears the set, returning all elements in an iterator.
#[inline] #[inline]
@ -425,7 +430,9 @@ impl<T, S> HashSet<T, S>
/// assert!(v.is_empty()); /// assert!(v.is_empty());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self) { self.map.clear() } pub fn clear(&mut self) {
self.map.clear()
}
/// Returns `true` if the set contains a value. /// Returns `true` if the set contains a value.
/// ///
@ -444,7 +451,8 @@ impl<T, S> HashSet<T, S>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq where T: Borrow<Q>,
Q: Hash + Eq
{ {
self.map.contains_key(value) self.map.contains_key(value)
} }
@ -456,7 +464,8 @@ impl<T, S> HashSet<T, S>
/// the value type. /// the value type.
#[stable(feature = "set_recovery", since = "1.9.0")] #[stable(feature = "set_recovery", since = "1.9.0")]
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T> pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q>, Q: Hash + Eq where T: Borrow<Q>,
Q: Hash + Eq
{ {
Recover::get(&self.map, value) Recover::get(&self.map, value)
} }
@ -547,7 +556,9 @@ impl<T, S> HashSet<T, S>
/// assert_eq!(set.len(), 1); /// assert_eq!(set.len(), 1);
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()).is_none() } pub fn insert(&mut self, value: T) -> bool {
self.map.insert(value, ()).is_none()
}
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given /// Adds a value to the set, replacing the existing value, if any, that is equal to the given
/// one. Returns the replaced value. /// one. Returns the replaced value.
@ -576,7 +587,8 @@ impl<T, S> HashSet<T, S>
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq where T: Borrow<Q>,
Q: Hash + Eq
{ {
self.map.remove(value).is_some() self.map.remove(value).is_some()
} }
@ -588,7 +600,8 @@ impl<T, S> HashSet<T, S>
/// the value type. /// the value type.
#[stable(feature = "set_recovery", since = "1.9.0")] #[stable(feature = "set_recovery", since = "1.9.0")]
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T> pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where T: Borrow<Q>, Q: Hash + Eq where T: Borrow<Q>,
Q: Hash + Eq
{ {
Recover::take(&mut self.map, value) Recover::take(&mut self.map, value)
} }
@ -596,10 +609,13 @@ impl<T, S> HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T, S> PartialEq for HashSet<T, S> impl<T, S> PartialEq for HashSet<T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
fn eq(&self, other: &HashSet<T, S>) -> bool { fn eq(&self, other: &HashSet<T, S>) -> bool {
if self.len() != other.len() { return false; } if self.len() != other.len() {
return false;
}
self.iter().all(|key| other.contains(key)) self.iter().all(|key| other.contains(key))
} }
@ -607,8 +623,10 @@ impl<T, S> PartialEq for HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T, S> Eq for HashSet<T, S> impl<T, S> Eq for HashSet<T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
{} S: BuildHasher
{
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T, S> fmt::Debug for HashSet<T, S> impl<T, S> fmt::Debug for HashSet<T, S>
@ -623,9 +641,9 @@ impl<T, S> fmt::Debug for HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T, S> FromIterator<T> for HashSet<T, S> impl<T, S> FromIterator<T> for HashSet<T, S>
where T: Eq + Hash, where T: Eq + Hash,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> HashSet<T, S> { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> HashSet<T, S> {
let iterator = iter.into_iter(); let iterator = iter.into_iter();
let lower = iterator.size_hint().0; let lower = iterator.size_hint().0;
let mut set = HashSet::with_capacity_and_hasher(lower, Default::default()); let mut set = HashSet::with_capacity_and_hasher(lower, Default::default());
@ -637,9 +655,9 @@ impl<T, S> FromIterator<T> for HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T, S> Extend<T> for HashSet<T, S> impl<T, S> Extend<T> for HashSet<T, S>
where T: Eq + Hash, where T: Eq + Hash,
S: BuildHasher, S: BuildHasher
{ {
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for k in iter { for k in iter {
self.insert(k); self.insert(k);
} }
@ -649,9 +667,9 @@ impl<T, S> Extend<T> for HashSet<T, S>
#[stable(feature = "hash_extend_copy", since = "1.4.0")] #[stable(feature = "hash_extend_copy", since = "1.4.0")]
impl<'a, T, S> Extend<&'a T> for HashSet<T, S> impl<'a, T, S> Extend<&'a T> for HashSet<T, S>
where T: 'a + Eq + Hash + Copy, where T: 'a + Eq + Hash + Copy,
S: BuildHasher, S: BuildHasher
{ {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned()); self.extend(iter.into_iter().cloned());
} }
} }
@ -659,7 +677,7 @@ impl<'a, T, S> Extend<&'a T> for HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T, S> Default for HashSet<T, S> impl<T, S> Default for HashSet<T, S>
where T: Eq + Hash, where T: Eq + Hash,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
/// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher. /// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher.
fn default() -> HashSet<T, S> { fn default() -> HashSet<T, S> {
@ -670,7 +688,7 @@ impl<T, S> Default for HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, T, S> BitOr<&'b HashSet<T, S>> for &'a HashSet<T, S> impl<'a, 'b, T, S> BitOr<&'b HashSet<T, S>> for &'a HashSet<T, S>
where T: Eq + Hash + Clone, where T: Eq + Hash + Clone,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
type Output = HashSet<T, S>; type Output = HashSet<T, S>;
@ -702,7 +720,7 @@ impl<'a, 'b, T, S> BitOr<&'b HashSet<T, S>> for &'a HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, T, S> BitAnd<&'b HashSet<T, S>> for &'a HashSet<T, S> impl<'a, 'b, T, S> BitAnd<&'b HashSet<T, S>> for &'a HashSet<T, S>
where T: Eq + Hash + Clone, where T: Eq + Hash + Clone,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
type Output = HashSet<T, S>; type Output = HashSet<T, S>;
@ -734,7 +752,7 @@ impl<'a, 'b, T, S> BitAnd<&'b HashSet<T, S>> for &'a HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, T, S> BitXor<&'b HashSet<T, S>> for &'a HashSet<T, S> impl<'a, 'b, T, S> BitXor<&'b HashSet<T, S>> for &'a HashSet<T, S>
where T: Eq + Hash + Clone, where T: Eq + Hash + Clone,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
type Output = HashSet<T, S>; type Output = HashSet<T, S>;
@ -766,7 +784,7 @@ impl<'a, 'b, T, S> BitXor<&'b HashSet<T, S>> for &'a HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S> impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S>
where T: Eq + Hash + Clone, where T: Eq + Hash + Clone,
S: BuildHasher + Default, S: BuildHasher + Default
{ {
type Output = HashSet<T, S>; type Output = HashSet<T, S>;
@ -798,13 +816,13 @@ impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S>
/// HashSet iterator /// HashSet iterator
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, K: 'a> { pub struct Iter<'a, K: 'a> {
iter: Keys<'a, K, ()> iter: Keys<'a, K, ()>,
} }
/// HashSet move iterator /// HashSet move iterator
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<K> { pub struct IntoIter<K> {
iter: map::IntoIter<K, ()> iter: map::IntoIter<K, ()>,
} }
/// HashSet drain iterator /// HashSet drain iterator
@ -834,18 +852,19 @@ pub struct Difference<'a, T: 'a, S: 'a> {
/// Symmetric difference iterator. /// Symmetric difference iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct SymmetricDifference<'a, T: 'a, S: 'a> { pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>> iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
} }
/// Set union iterator. /// Set union iterator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Union<'a, T: 'a, S: 'a> { pub struct Union<'a, T: 'a, S: 'a> {
iter: Chain<Iter<'a, T>, Difference<'a, T, S>> iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> IntoIterator for &'a HashSet<T, S> impl<'a, T, S> IntoIterator for &'a HashSet<T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
type Item = &'a T; type Item = &'a T;
type IntoIter = Iter<'a, T>; type IntoIter = Iter<'a, T>;
@ -890,18 +909,26 @@ impl<T, S> IntoIterator for HashSet<T, S>
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K> Clone for Iter<'a, K> { impl<'a, K> Clone for Iter<'a, K> {
fn clone(&self) -> Iter<'a, K> { Iter { iter: self.iter.clone() } } fn clone(&self) -> Iter<'a, K> {
Iter { iter: self.iter.clone() }
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K> Iterator for Iter<'a, K> { impl<'a, K> Iterator for Iter<'a, K> {
type Item = &'a K; type Item = &'a K;
fn next(&mut self) -> Option<&'a K> { self.iter.next() } fn next(&mut self) -> Option<&'a K> {
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K> ExactSizeIterator for Iter<'a, K> { impl<'a, K> ExactSizeIterator for Iter<'a, K> {
fn len(&self) -> usize { self.iter.len() } fn len(&self) -> usize {
self.iter.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K> FusedIterator for Iter<'a, K> {} impl<'a, K> FusedIterator for Iter<'a, K> {}
@ -910,12 +937,18 @@ impl<'a, K> FusedIterator for Iter<'a, K> {}
impl<K> Iterator for IntoIter<K> { impl<K> Iterator for IntoIter<K> {
type Item = K; type Item = K;
fn next(&mut self) -> Option<K> { self.iter.next().map(|(k, _)| k) } fn next(&mut self) -> Option<K> {
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } self.iter.next().map(|(k, _)| k)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<K> ExactSizeIterator for IntoIter<K> { impl<K> ExactSizeIterator for IntoIter<K> {
fn len(&self) -> usize { self.iter.len() } fn len(&self) -> usize {
self.iter.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<K> FusedIterator for IntoIter<K> {} impl<K> FusedIterator for IntoIter<K> {}
@ -924,12 +957,18 @@ impl<K> FusedIterator for IntoIter<K> {}
impl<'a, K> Iterator for Drain<'a, K> { impl<'a, K> Iterator for Drain<'a, K> {
type Item = K; type Item = K;
fn next(&mut self) -> Option<K> { self.iter.next().map(|(k, _)| k) } fn next(&mut self) -> Option<K> {
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } self.iter.next().map(|(k, _)| k)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K> ExactSizeIterator for Drain<'a, K> { impl<'a, K> ExactSizeIterator for Drain<'a, K> {
fn len(&self) -> usize { self.iter.len() } fn len(&self) -> usize {
self.iter.len()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, K> FusedIterator for Drain<'a, K> {} impl<'a, K> FusedIterator for Drain<'a, K> {}
@ -943,7 +982,8 @@ impl<'a, T, S> Clone for Intersection<'a, T, S> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Iterator for Intersection<'a, T, S> impl<'a, T, S> Iterator for Intersection<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
type Item = &'a T; type Item = &'a T;
@ -951,9 +991,11 @@ impl<'a, T, S> Iterator for Intersection<'a, T, S>
loop { loop {
match self.iter.next() { match self.iter.next() {
None => return None, None => return None,
Some(elt) => if self.other.contains(elt) { Some(elt) => {
return Some(elt) if self.other.contains(elt) {
}, return Some(elt);
}
}
} }
} }
} }
@ -966,8 +1008,10 @@ impl<'a, T, S> Iterator for Intersection<'a, T, S>
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, T, S> FusedIterator for Intersection<'a, T, S> impl<'a, T, S> FusedIterator for Intersection<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
{} S: BuildHasher
{
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Clone for Difference<'a, T, S> { impl<'a, T, S> Clone for Difference<'a, T, S> {
@ -978,7 +1022,8 @@ impl<'a, T, S> Clone for Difference<'a, T, S> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Iterator for Difference<'a, T, S> impl<'a, T, S> Iterator for Difference<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
type Item = &'a T; type Item = &'a T;
@ -986,9 +1031,11 @@ impl<'a, T, S> Iterator for Difference<'a, T, S>
loop { loop {
match self.iter.next() { match self.iter.next() {
None => return None, None => return None,
Some(elt) => if !self.other.contains(elt) { Some(elt) => {
return Some(elt) if !self.other.contains(elt) {
}, return Some(elt);
}
}
} }
} }
} }
@ -1001,8 +1048,10 @@ impl<'a, T, S> Iterator for Difference<'a, T, S>
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, T, S> FusedIterator for Difference<'a, T, S> impl<'a, T, S> FusedIterator for Difference<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
{} S: BuildHasher
{
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> { impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> {
@ -1013,53 +1062,85 @@ impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S> impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
type Item = &'a T; type Item = &'a T;
fn next(&mut self) -> Option<&'a T> { self.iter.next() } fn next(&mut self) -> Option<&'a T> {
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, T, S> FusedIterator for SymmetricDifference<'a, T, S> impl<'a, T, S> FusedIterator for SymmetricDifference<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
{} S: BuildHasher
{
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Clone for Union<'a, T, S> { impl<'a, T, S> Clone for Union<'a, T, S> {
fn clone(&self) -> Union<'a, T, S> { Union { iter: self.iter.clone() } } fn clone(&self) -> Union<'a, T, S> {
Union { iter: self.iter.clone() }
}
} }
#[unstable(feature = "fused", issue = "35602")] #[unstable(feature = "fused", issue = "35602")]
impl<'a, T, S> FusedIterator for Union<'a, T, S> impl<'a, T, S> FusedIterator for Union<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
{} S: BuildHasher
{
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, S> Iterator for Union<'a, T, S> impl<'a, T, S> Iterator for Union<'a, T, S>
where T: Eq + Hash, S: BuildHasher where T: Eq + Hash,
S: BuildHasher
{ {
type Item = &'a T; type Item = &'a T;
fn next(&mut self) -> Option<&'a T> { self.iter.next() } fn next(&mut self) -> Option<&'a T> {
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
} }
#[allow(dead_code)] #[allow(dead_code)]
fn assert_covariance() { fn assert_covariance() {
fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'new str> { v } fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'new str> {
fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> { v } v
fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> { v } }
fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> {
v
}
fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> {
v
}
fn difference<'a, 'new>(v: Difference<'a, &'static str, RandomState>) fn difference<'a, 'new>(v: Difference<'a, &'static str, RandomState>)
-> Difference<'a, &'new str, RandomState> { v } -> Difference<'a, &'new str, RandomState> {
v
}
fn symmetric_difference<'a, 'new>(v: SymmetricDifference<'a, &'static str, RandomState>) fn symmetric_difference<'a, 'new>(v: SymmetricDifference<'a, &'static str, RandomState>)
-> SymmetricDifference<'a, &'new str, RandomState> { v } -> SymmetricDifference<'a, &'new str, RandomState> {
v
}
fn intersection<'a, 'new>(v: Intersection<'a, &'static str, RandomState>) fn intersection<'a, 'new>(v: Intersection<'a, &'static str, RandomState>)
-> Intersection<'a, &'new str, RandomState> { v } -> Intersection<'a, &'new str, RandomState> {
v
}
fn union<'a, 'new>(v: Union<'a, &'static str, RandomState>) fn union<'a, 'new>(v: Union<'a, &'static str, RandomState>)
-> Union<'a, &'new str, RandomState> { v } -> Union<'a, &'new str, RandomState> {
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d } v
}
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> {
d
}
} }
#[cfg(test)] #[cfg(test)]
@ -1346,7 +1427,9 @@ mod test_set {
assert_eq!(last_i, 49); assert_eq!(last_i, 49);
} }
for _ in &s { panic!("s should be empty!"); } for _ in &s {
panic!("s should be empty!");
}
// reset to try again. // reset to try again.
s.extend(1..100); s.extend(1..100);

View file

@ -449,10 +449,10 @@ impl<'t, K, V> FullBucket<K, V, &'t mut RawTable<K, V>> {
unsafe { unsafe {
*self.raw.hash = EMPTY_BUCKET; *self.raw.hash = EMPTY_BUCKET;
(EmptyBucket { (EmptyBucket {
raw: self.raw, raw: self.raw,
idx: self.idx, idx: self.idx,
table: self.table, table: self.table,
}, },
ptr::read(self.raw.key), ptr::read(self.raw.key),
ptr::read(self.raw.val)) ptr::read(self.raw.val))
} }
@ -644,13 +644,13 @@ impl<K, V> RawTable<K, V> {
// One check for overflow that covers calculation and rounding of size. // One check for overflow that covers calculation and rounding of size.
let size_of_bucket = size_of::<u64>() let size_of_bucket = size_of::<u64>()
.checked_add(size_of::<K>()) .checked_add(size_of::<K>())
.unwrap() .unwrap()
.checked_add(size_of::<V>()) .checked_add(size_of::<V>())
.unwrap(); .unwrap();
assert!(size >= assert!(size >=
capacity.checked_mul(size_of_bucket) capacity.checked_mul(size_of_bucket)
.expect("capacity overflow"), .expect("capacity overflow"),
"capacity overflow"); "capacity overflow");
let buffer = allocate(size, malloc_alignment); let buffer = allocate(size, malloc_alignment);
@ -673,10 +673,8 @@ impl<K, V> RawTable<K, V> {
let keys_size = self.capacity * size_of::<K>(); let keys_size = self.capacity * size_of::<K>();
let buffer = *self.hashes as *const u8; let buffer = *self.hashes as *const u8;
let (keys_offset, vals_offset, oflo) = calculate_offsets(hashes_size, let (keys_offset, vals_offset, oflo) =
keys_size, calculate_offsets(hashes_size, keys_size, align_of::<K>(), align_of::<V>());
align_of::<K>(),
align_of::<V>());
debug_assert!(!oflo, "capacity overflow"); debug_assert!(!oflo, "capacity overflow");
unsafe { unsafe {
RawBucket { RawBucket {
@ -991,9 +989,7 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
} }
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> { impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
fn len(&self) -> usize { fn len(&self) -> usize {
unsafe { unsafe { (**self.table).size() }
(**self.table).size()
}
} }
} }