Skip to content

Commit 1e997d6

Browse files
committed
Implement PartialEq/Eq/Clone/Hash/FromIterator/Extendable for SmallIntMap and Show/Clone for TrieMap and TrieSet
1 parent 92c9705 commit 1e997d6

File tree

2 files changed

+185
-1
lines changed

2 files changed

+185
-1
lines changed

src/libcollections/smallintmap.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use core::mem::replace;
2424
use {Collection, Mutable, Map, MutableMap, MutableSeq};
2525
use {vec, slice};
2626
use vec::Vec;
27+
use hash::Hash;
2728

2829
/// A map optimized for small integer keys.
2930
///
@@ -58,6 +59,7 @@ use vec::Vec;
5859
/// months.clear();
5960
/// assert!(months.is_empty());
6061
/// ```
62+
#[deriving(PartialEq, Eq, Hash)]
6163
pub struct SmallIntMap<T> {
6264
v: Vec<Option<T>>,
6365
}
@@ -151,6 +153,21 @@ impl<V> Default for SmallIntMap<V> {
151153
fn default() -> SmallIntMap<V> { SmallIntMap::new() }
152154
}
153155

156+
impl<V:Clone> Clone for SmallIntMap<V> {
157+
#[inline]
158+
fn clone(&self) -> SmallIntMap<V> {
159+
SmallIntMap { v: self.v.clone() }
160+
}
161+
162+
#[inline]
163+
fn clone_from(&mut self, source: &SmallIntMap<V>) {
164+
self.v.reserve(source.v.len());
165+
for (i, w) in self.v.mut_iter().enumerate() {
166+
*w = source.v[i].clone();
167+
}
168+
}
169+
}
170+
154171
impl<V> SmallIntMap<V> {
155172
/// Create an empty SmallIntMap.
156173
///
@@ -362,6 +379,22 @@ impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
362379
}
363380
}
364381

382+
impl<V> FromIterator<(uint, V)> for SmallIntMap<V> {
383+
fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> SmallIntMap<V> {
384+
let mut map = SmallIntMap::new();
385+
map.extend(iter);
386+
map
387+
}
388+
}
389+
390+
impl<V> Extendable<(uint, V)> for SmallIntMap<V> {
391+
fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) {
392+
for (k, v) in iter {
393+
self.insert(k, v);
394+
}
395+
}
396+
}
397+
365398
macro_rules! iterator {
366399
(impl $name:ident -> $elem:ty, $getter:ident) => {
367400
impl<'a, T> Iterator<$elem> for $name<'a, T> {
@@ -446,6 +479,7 @@ pub type Values<'a, T> =
446479
#[cfg(test)]
447480
mod test_map {
448481
use std::prelude::*;
482+
use std::hash;
449483

450484
use {Map, MutableMap, Mutable};
451485
use super::SmallIntMap;
@@ -698,6 +732,63 @@ mod test_map {
698732
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
699733
assert_eq!(format!("{}", empty), "{}".to_string());
700734
}
735+
736+
#[test]
737+
fn test_clone() {
738+
let mut a = SmallIntMap::new();
739+
740+
a.insert(1, vec!(4i, 5, 6));
741+
a.insert(4, vec!());
742+
a.insert(6, vec!(1, 3));
743+
744+
assert!(a.clone() == a);
745+
}
746+
747+
#[test]
748+
fn test_eq() {
749+
let mut a = SmallIntMap::new();
750+
let mut b = SmallIntMap::new();
751+
752+
assert!(a == b);
753+
assert!(a.insert(0, 5i));
754+
assert!(a != b);
755+
assert!(b.insert(0, 4i));
756+
assert!(a != b);
757+
assert!(a.insert(5, 19));
758+
assert!(a != b);
759+
assert!(!b.insert(0, 5));
760+
assert!(a != b);
761+
assert!(b.insert(5, 19));
762+
assert!(a == b);
763+
}
764+
765+
#[test]
766+
fn test_hash() {
767+
let mut x = SmallIntMap::new();
768+
let mut y = SmallIntMap::new();
769+
770+
assert!(hash::hash(&x) == hash::hash(&y));
771+
x.insert(1, 'a');
772+
x.insert(2, 'b');
773+
x.insert(3, 'c');
774+
775+
y.insert(3, 'c');
776+
y.insert(2, 'b');
777+
y.insert(1, 'a');
778+
779+
assert!(hash::hash(&x) == hash::hash(&y));
780+
}
781+
782+
#[test]
783+
fn test_from_iter() {
784+
let xs = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')];
785+
786+
let map: SmallIntMap<char> = xs.iter().map(|&x| x).collect();
787+
788+
for &(k, v) in xs.iter() {
789+
assert_eq!(map.find(&k), Some(&v));
790+
}
791+
}
701792
}
702793

703794
#[cfg(test)]

src/libcollections/trie.rs

Lines changed: 94 additions & 1 deletion
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::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)