From b2ec71fc277ca590bb8bd26d3f7762d6406860e5 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 18 Jan 2014 07:33:41 -0500 Subject: [PATCH] hashmap: port to Vec --- src/libstd/hashmap.rs | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 7f19105cdc8..13a03d32525 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -66,8 +66,9 @@ use rand::Rng; use rand; use uint; use util::replace; -use vec::{ImmutableVector, MutableVector, OwnedVector}; -use vec; +use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems}; +use vec_ng; +use vec_ng::Vec; static INITIAL_CAPACITY: uint = 32u; // 2^5 @@ -90,7 +91,7 @@ pub struct HashMap { priv k1: u64, priv resize_at: uint, priv size: uint, - priv buckets: ~[Option>], + priv buckets: Vec>> } // We could rewrite FoundEntry to have type Option<&Bucket> @@ -151,7 +152,7 @@ impl HashMap { -> SearchResult { let mut ret = TableFull; self.bucket_sequence(hash, |i| { - match self.buckets[i] { + match self.buckets.as_slice()[i] { Some(ref bkt) if bkt.hash == hash && *k == bkt.key => { ret = FoundEntry(i); false }, @@ -169,7 +170,7 @@ impl HashMap { -> SearchResult { let mut ret = TableFull; self.bucket_sequence(hash, |i| { - match self.buckets[i] { + match self.buckets.as_slice()[i] { Some(ref bkt) if bkt.hash == hash && k.equiv(&bkt.key) => { ret = FoundEntry(i); false }, @@ -194,7 +195,7 @@ impl HashMap { self.resize_at = resize_at(new_capacity); let old_buckets = replace(&mut self.buckets, - vec::from_fn(new_capacity, |_| None)); + Vec::from_fn(new_capacity, |_| None)); self.size = 0; for bucket in old_buckets.move_iter() { @@ -213,7 +214,7 @@ impl HashMap { #[inline] fn value_for_bucket<'a>(&'a self, idx: uint) -> &'a V { - match self.buckets[idx] { + match self.buckets.as_slice()[idx] { Some(ref bkt) => &bkt.value, None => fail!("HashMap::find: internal logic error"), } @@ -221,7 +222,7 @@ impl HashMap { #[inline] fn mut_value_for_bucket<'a>(&'a mut self, idx: uint) -> &'a mut V { - match self.buckets[idx] { + match self.buckets.as_mut_slice()[idx] { Some(ref mut bkt) => &mut bkt.value, None => unreachable!() } @@ -234,13 +235,12 @@ impl HashMap { match self.bucket_for_key_with_hash(hash, &k) { TableFull => { fail!("Internal logic error"); } FoundHole(idx) => { - self.buckets[idx] = Some(Bucket{hash: hash, key: k, - value: v}); + self.buckets.as_mut_slice()[idx] = Some(Bucket{hash: hash, key: k, value: v}); self.size += 1; None } FoundEntry(idx) => { - match self.buckets[idx] { + match self.buckets.as_mut_slice()[idx] { None => { fail!("insert_internal: Internal logic error") } Some(ref mut b) => { b.hash = hash; @@ -273,7 +273,7 @@ impl HashMap { }; let len_buckets = self.buckets.len(); - let bucket = self.buckets[idx].take(); + let bucket = self.buckets.as_mut_slice()[idx].take(); let value = bucket.map(|bucket| bucket.value); @@ -281,8 +281,8 @@ impl HashMap { what our new size is ahead of time before we start insertions */ let size = self.size - 1; idx = self.next_bucket(idx, len_buckets); - while self.buckets[idx].is_some() { - let bucket = self.buckets[idx].take(); + while self.buckets.as_slice()[idx].is_some() { + let bucket = self.buckets.as_mut_slice()[idx].take(); self.insert_opt_bucket(bucket); idx = self.next_bucket(idx, len_buckets); } @@ -300,7 +300,7 @@ impl Container for HashMap { impl Mutable for HashMap { /// Clear the map, removing all key-value pairs. fn clear(&mut self) { - for bkt in self.buckets.mut_iter() { + for bkt in self.buckets.as_mut_slice().mut_iter() { *bkt = None; } self.size = 0; @@ -380,7 +380,7 @@ impl HashMap { k0: k0, k1: k1, resize_at: resize_at(cap), size: 0, - buckets: vec::from_fn(cap, |_| None) + buckets: Vec::from_fn(cap, |_| None) } } @@ -455,7 +455,7 @@ impl HashMap { FoundEntry(idx) => { found(&k, self.mut_value_for_bucket(idx), a); idx } FoundHole(idx) => { let v = not_found(&k, a); - self.buckets[idx] = Some(Bucket{hash: hash, key: k, value: v}); + self.buckets.as_mut_slice()[idx] = Some(Bucket{hash: hash, key: k, value: v}); self.size += 1; idx } @@ -541,14 +541,14 @@ impl HashMap { /// An iterator visiting all key-value pairs in arbitrary order. /// Iterator element type is (&'a K, &'a V). pub fn iter<'a>(&'a self) -> Entries<'a, K, V> { - Entries { iter: self.buckets.iter() } + Entries { iter: self.buckets.as_slice().iter() } } /// An iterator visiting all key-value pairs in arbitrary order, /// with mutable references to the values. /// Iterator element type is (&'a K, &'a mut V). pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> { - MutEntries { iter: self.buckets.mut_iter() } + MutEntries { iter: self.buckets.as_mut_slice().mut_iter() } } /// Creates a consuming iterator, that is, one that moves each key-value @@ -599,17 +599,17 @@ impl Clone for HashMap { /// HashMap iterator #[deriving(Clone)] pub struct Entries<'a, K, V> { - priv iter: vec::Items<'a, Option>>, + priv iter: Items<'a, Option>>, } /// HashMap mutable values iterator pub struct MutEntries<'a, K, V> { - priv iter: vec::MutItems<'a, Option>>, + priv iter: MutItems<'a, Option>>, } /// HashMap move iterator pub struct MoveEntries { - priv iter: vec::MoveItems>>, + priv iter: vec_ng::MoveItems>>, } /// HashMap keys iterator @@ -623,12 +623,12 @@ pub type Values<'a, K, V> = /// HashSet iterator #[deriving(Clone)] pub struct SetItems<'a, K> { - priv iter: vec::Items<'a, Option>>, + priv iter: Items<'a, Option>>, } /// HashSet move iterator pub struct SetMoveItems { - priv iter: vec::MoveItems>>, + priv iter: vec_ng::MoveItems>>, } impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> { @@ -807,7 +807,7 @@ impl HashSet { /// An iterator visiting all elements in arbitrary order. /// Iterator element type is &'a T. pub fn iter<'a>(&'a self) -> SetItems<'a, T> { - SetItems { iter: self.map.buckets.iter() } + SetItems { iter: self.map.buckets.as_slice().iter() } } /// Creates a consuming iterator, that is, one that moves each value out