Skip to content

Commit d114dda

Browse files
committed
auto merge of #15963 : nham/rust/moar_15294, r=alexcrichton
Implements PartialEq/Eq/Clone/Hash/FromIterator/Extendable for SmallIntMap and Clone/Show for TrieMap/TrieSet. cc #15294
2 parents 2de3fad + fadbc0b commit d114dda

File tree

2 files changed

+194
-2
lines changed

2 files changed

+194
-2
lines changed

src/libcollections/smallintmap.rs

+100-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use core::mem::replace;
2424
use {Collection, Mutable, Map, MutableMap, MutableSeq};
2525
use {vec, slice};
2626
use vec::Vec;
27+
use hash;
28+
use hash::Hash;
2729

2830
/// A map optimized for small integer keys.
2931
///
@@ -58,6 +60,7 @@ use vec::Vec;
5860
/// months.clear();
5961
/// assert!(months.is_empty());
6062
/// ```
63+
#[deriving(PartialEq, Eq)]
6164
pub struct SmallIntMap<T> {
6265
v: Vec<Option<T>>,
6366
}
@@ -151,6 +154,27 @@ impl<V> Default for SmallIntMap<V> {
151154
fn default() -> SmallIntMap<V> { SmallIntMap::new() }
152155
}
153156

157+
impl<V:Clone> Clone for SmallIntMap<V> {
158+
#[inline]
159+
fn clone(&self) -> SmallIntMap<V> {
160+
SmallIntMap { v: self.v.clone() }
161+
}
162+
163+
#[inline]
164+
fn clone_from(&mut self, source: &SmallIntMap<V>) {
165+
self.v.reserve(source.v.len());
166+
for (i, w) in self.v.mut_iter().enumerate() {
167+
*w = source.v[i].clone();
168+
}
169+
}
170+
}
171+
172+
impl <S: hash::Writer, T: Hash<S>> Hash<S> for SmallIntMap<T> {
173+
fn hash(&self, state: &mut S) {
174+
self.v.hash(state)
175+
}
176+
}
177+
154178
impl<V> SmallIntMap<V> {
155179
/// Create an empty SmallIntMap.
156180
///
@@ -362,6 +386,22 @@ impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
362386
}
363387
}
364388

389+
impl<V> FromIterator<(uint, V)> for SmallIntMap<V> {
390+
fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> SmallIntMap<V> {
391+
let mut map = SmallIntMap::new();
392+
map.extend(iter);
393+
map
394+
}
395+
}
396+
397+
impl<V> Extendable<(uint, V)> for SmallIntMap<V> {
398+
fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) {
399+
for (k, v) in iter {
400+
self.insert(k, v);
401+
}
402+
}
403+
}
404+
365405
macro_rules! iterator {
366406
(impl $name:ident -> $elem:ty, $getter:ident) => {
367407
impl<'a, T> Iterator<$elem> for $name<'a, T> {
@@ -446,8 +486,10 @@ pub type Values<'a, T> =
446486
#[cfg(test)]
447487
mod test_map {
448488
use std::prelude::*;
489+
use vec::Vec;
490+
use hash;
449491

450-
use {Map, MutableMap, Mutable};
492+
use {Map, MutableMap, Mutable, MutableSeq};
451493
use super::SmallIntMap;
452494

453495
#[test]
@@ -698,6 +740,63 @@ mod test_map {
698740
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
699741
assert_eq!(format!("{}", empty), "{}".to_string());
700742
}
743+
744+
#[test]
745+
fn test_clone() {
746+
let mut a = SmallIntMap::new();
747+
748+
a.insert(1, 'x');
749+
a.insert(4, 'y');
750+
a.insert(6, 'z');
751+
752+
assert!(a.clone() == a);
753+
}
754+
755+
#[test]
756+
fn test_eq() {
757+
let mut a = SmallIntMap::new();
758+
let mut b = SmallIntMap::new();
759+
760+
assert!(a == b);
761+
assert!(a.insert(0, 5i));
762+
assert!(a != b);
763+
assert!(b.insert(0, 4i));
764+
assert!(a != b);
765+
assert!(a.insert(5, 19));
766+
assert!(a != b);
767+
assert!(!b.insert(0, 5));
768+
assert!(a != b);
769+
assert!(b.insert(5, 19));
770+
assert!(a == b);
771+
}
772+
773+
#[test]
774+
fn test_hash() {
775+
let mut x = SmallIntMap::new();
776+
let mut y = SmallIntMap::new();
777+
778+
assert!(hash::hash(&x) == hash::hash(&y));
779+
x.insert(1, 'a');
780+
x.insert(2, 'b');
781+
x.insert(3, 'c');
782+
783+
y.insert(3, 'c');
784+
y.insert(2, 'b');
785+
y.insert(1, 'a');
786+
787+
assert!(hash::hash(&x) == hash::hash(&y));
788+
}
789+
790+
#[test]
791+
fn test_from_iter() {
792+
let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
793+
794+
let map: SmallIntMap<char> = xs.iter().map(|&x| x).collect();
795+
796+
for &(k, v) in xs.iter() {
797+
assert_eq!(map.find(&k), Some(&v));
798+
}
799+
}
701800
}
702801

703802
#[cfg(test)]

src/libcollections/trie.rs

+94-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use core::prelude::*;
1515

1616
use alloc::boxed::Box;
1717
use core::default::Default;
18+
use core::fmt;
19+
use core::fmt::Show;
1820
use core::mem::zeroed;
1921
use core::mem;
2022
use core::uint;
@@ -31,6 +33,7 @@ static SIZE: uint = 1 << SHIFT;
3133
static MASK: uint = SIZE - 1;
3234
static NUM_CHUNKS: uint = uint::BITS / SHIFT;
3335

36+
#[deriving(Clone)]
3437
enum Child<T> {
3538
Internal(Box<TrieNode<T>>),
3639
External(uint, T),
@@ -75,6 +78,7 @@ enum Child<T> {
7578
/// map.clear();
7679
/// assert!(map.is_empty());
7780
/// ```
81+
#[deriving(Clone)]
7882
pub struct TrieMap<T> {
7983
root: TrieNode<T>,
8084
length: uint
@@ -89,6 +93,19 @@ impl<T: PartialEq> PartialEq for TrieMap<T> {
8993

9094
impl<T: Eq> Eq for TrieMap<T> {}
9195

96+
impl<T: Show> Show for TrieMap<T> {
97+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98+
try!(write!(f, "{{"));
99+
100+
for (i, (k, v)) in self.iter().enumerate() {
101+
if i != 0 { try!(write!(f, ", ")); }
102+
try!(write!(f, "{}: {}", k, *v));
103+
}
104+
105+
write!(f, "}}")
106+
}
107+
}
108+
92109
impl<T> Collection for TrieMap<T> {
93110
/// Return the number of elements in the map.
94111
#[inline]
@@ -500,11 +517,24 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
500517
/// set.clear();
501518
/// assert!(set.is_empty());
502519
/// ```
503-
#[deriving(Hash, PartialEq, Eq)]
520+
#[deriving(Clone, Hash, PartialEq, Eq)]
504521
pub struct TrieSet {
505522
map: TrieMap<()>
506523
}
507524

525+
impl Show for TrieSet {
526+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
527+
try!(write!(f, "{{"));
528+
529+
for (i, x) in self.iter().enumerate() {
530+
if i != 0 { try!(write!(f, ", ")); }
531+
try!(write!(f, "{}", x));
532+
}
533+
534+
write!(f, "}}")
535+
}
536+
}
537+
508538
impl Collection for TrieSet {
509539
/// Return the number of elements in the set.
510540
#[inline]
@@ -673,6 +703,19 @@ struct TrieNode<T> {
673703
children: [Child<T>, ..SIZE]
674704
}
675705

706+
impl<T:Clone> Clone for TrieNode<T> {
707+
#[inline]
708+
fn clone(&self) -> TrieNode<T> {
709+
let ch = &self.children;
710+
TrieNode {
711+
count: self.count,
712+
children: [ch[0].clone(), ch[1].clone(), ch[2].clone(), ch[3].clone(),
713+
ch[4].clone(), ch[5].clone(), ch[6].clone(), ch[7].clone(),
714+
ch[8].clone(), ch[9].clone(), ch[10].clone(), ch[11].clone(),
715+
ch[12].clone(), ch[13].clone(), ch[14].clone(), ch[15].clone()]}
716+
}
717+
}
718+
676719
impl<T> TrieNode<T> {
677720
#[inline]
678721
fn new() -> TrieNode<T> {
@@ -1237,6 +1280,17 @@ mod test_map {
12371280
assert!(m_upper.iter().all(|(_, &x)| x == 0));
12381281
}
12391282

1283+
#[test]
1284+
fn test_clone() {
1285+
let mut a = TrieMap::new();
1286+
1287+
a.insert(1, 'a');
1288+
a.insert(2, 'b');
1289+
a.insert(3, 'c');
1290+
1291+
assert!(a.clone() == a);
1292+
}
1293+
12401294
#[test]
12411295
fn test_eq() {
12421296
let mut a = TrieMap::new();
@@ -1271,6 +1325,20 @@ mod test_map {
12711325

12721326
assert!(hash::hash(&x) == hash::hash(&y));
12731327
}
1328+
1329+
#[test]
1330+
fn test_show() {
1331+
let mut map = TrieMap::new();
1332+
let empty: TrieMap<uint> = TrieMap::new();
1333+
1334+
map.insert(1, 'a');
1335+
map.insert(2, 'b');
1336+
1337+
let map_str = format!("{}", map);
1338+
1339+
assert!(map_str == "{1: a, 2: b}".to_string());
1340+
assert_eq!(format!("{}", empty), "{}".to_string());
1341+
}
12741342
}
12751343

12761344
#[cfg(test)]
@@ -1420,4 +1488,29 @@ mod test_set {
14201488
assert!(set.contains(x));
14211489
}
14221490
}
1491+
1492+
#[test]
1493+
fn test_show() {
1494+
let mut set = TrieSet::new();
1495+
let empty = TrieSet::new();
1496+
1497+
set.insert(1);
1498+
set.insert(2);
1499+
1500+
let set_str = format!("{}", set);
1501+
1502+
assert!(set_str == "{1, 2}".to_string());
1503+
assert_eq!(format!("{}", empty), "{}".to_string());
1504+
}
1505+
1506+
#[test]
1507+
fn test_clone() {
1508+
let mut a = TrieSet::new();
1509+
1510+
a.insert(1);
1511+
a.insert(2);
1512+
a.insert(3);
1513+
1514+
assert!(a.clone() == a);
1515+
}
14231516
}

0 commit comments

Comments
 (0)