Skip to content

Commit 0ef8375

Browse files
committed
auto merge of #7768 : sfackler/rust/containers, r=huonw
See #4989. I didn't add Persistent{Set,Map} since the only persistent data structure is fun_treemap and its functionality is currently too limited to build a trait out of.
2 parents 247ad45 + 0e882f2 commit 0ef8375

13 files changed

+114
-91
lines changed

src/libextra/bitv.rs

+34-32
Original file line numberDiff line numberDiff line change
@@ -718,38 +718,6 @@ impl Set<uint> for BitvSet {
718718
*value < self.bitv.storage.len() * uint::bits && self.bitv.get(*value)
719719
}
720720

721-
fn insert(&mut self, value: uint) -> bool {
722-
if self.contains(&value) {
723-
return false;
724-
}
725-
let nbits = self.capacity();
726-
if value >= nbits {
727-
let newsize = num::max(value, nbits * 2) / uint::bits + 1;
728-
assert!(newsize > self.bitv.storage.len());
729-
self.bitv.storage.grow(newsize, &0);
730-
}
731-
self.size += 1;
732-
self.bitv.set(value, true);
733-
return true;
734-
}
735-
736-
fn remove(&mut self, value: &uint) -> bool {
737-
if !self.contains(value) {
738-
return false;
739-
}
740-
self.size -= 1;
741-
self.bitv.set(*value, false);
742-
743-
// Attempt to truncate our storage
744-
let mut i = self.bitv.storage.len();
745-
while i > 1 && self.bitv.storage[i - 1] == 0 {
746-
i -= 1;
747-
}
748-
self.bitv.storage.truncate(i);
749-
750-
return true;
751-
}
752-
753721
fn is_disjoint(&self, other: &BitvSet) -> bool {
754722
for self.intersection(other) |_| {
755723
return false;
@@ -816,6 +784,40 @@ impl Set<uint> for BitvSet {
816784
}
817785
}
818786

787+
impl MutableSet<uint> for BitvSet {
788+
fn insert(&mut self, value: uint) -> bool {
789+
if self.contains(&value) {
790+
return false;
791+
}
792+
let nbits = self.capacity();
793+
if value >= nbits {
794+
let newsize = num::max(value, nbits * 2) / uint::bits + 1;
795+
assert!(newsize > self.bitv.storage.len());
796+
self.bitv.storage.grow(newsize, &0);
797+
}
798+
self.size += 1;
799+
self.bitv.set(value, true);
800+
return true;
801+
}
802+
803+
fn remove(&mut self, value: &uint) -> bool {
804+
if !self.contains(value) {
805+
return false;
806+
}
807+
self.size -= 1;
808+
self.bitv.set(*value, false);
809+
810+
// Attempt to truncate our storage
811+
let mut i = self.bitv.storage.len();
812+
while i > 1 && self.bitv.storage[i - 1] == 0 {
813+
i -= 1;
814+
}
815+
self.bitv.storage.truncate(i);
816+
817+
return true;
818+
}
819+
}
820+
819821
impl BitvSet {
820822
/// Visits each of the words that the two bit vectors (self and other)
821823
/// both have in common. The three yielded arguments are (bit location,

src/libextra/smallintmap.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818

1919
use std::cmp;
20-
use std::container::{Container, Mutable, Map, Set};
21-
use std::iterator::*;
20+
use std::iterator::{Iterator,IteratorUtil,ZipIterator,Counter,EnumerateIterator,FilterMapIterator};
2221
use std::uint;
2322
use std::util::replace;
2423
use std::vec::{VecIterator,VecMutIterator,VecRevIterator,VecMutRevIterator};
@@ -68,7 +67,9 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
6867
None
6968
}
7069
}
70+
}
7171

72+
impl<V> MutableMap<uint, V> for SmallIntMap<V> {
7273
/// Return a mutable reference to the value corresponding to the key
7374
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
7475
if *key < self.v.len() {
@@ -349,14 +350,6 @@ impl Set<uint> for SmallIntSet {
349350
/// Return true if the set contains a value
350351
fn contains(&self, value: &uint) -> bool { self.map.contains_key(value) }
351352

352-
/// Add a value to the set. Return true if the value was not already
353-
/// present in the set.
354-
fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
355-
356-
/// Remove a value from the set. Return true if the value was
357-
/// present in the set.
358-
fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
359-
360353
/// Return true if the set has no elements in common with `other`.
361354
/// This is equivalent to checking for an empty uintersection.
362355
fn is_disjoint(&self, other: &SmallIntSet) -> bool {
@@ -412,6 +405,16 @@ impl Set<uint> for SmallIntSet {
412405
}
413406
}
414407

408+
impl MutableSet<uint> for SmallIntSet {
409+
/// Add a value to the set. Return true if the value was not already
410+
/// present in the set.
411+
fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
412+
413+
/// Remove a value from the set. Return true if the value was
414+
/// present in the set.
415+
fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
416+
}
417+
415418
impl SmallIntSet {
416419
/// Create an empty SmallIntSet
417420
pub fn new() -> SmallIntSet { SmallIntSet{map: SmallIntMap::new()} }

src/libextra/treemap.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
124124
}
125125
}
126126
}
127+
}
127128

129+
impl<K: TotalOrd, V> MutableMap<K, V> for TreeMap<K, V> {
128130
/// Return a mutable reference to the value corresponding to the key
129131
#[inline]
130132
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> {
@@ -293,16 +295,6 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
293295
self.map.contains_key(value)
294296
}
295297

296-
/// Add a value to the set. Return true if the value was not already
297-
/// present in the set.
298-
#[inline]
299-
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
300-
301-
/// Remove a value from the set. Return true if the value was
302-
/// present in the set.
303-
#[inline]
304-
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
305-
306298
/// Return true if the set has no elements in common with `other`.
307299
/// This is equivalent to checking for an empty intersection.
308300
fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
@@ -475,6 +467,18 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
475467
}
476468
}
477469

470+
impl<T: TotalOrd> MutableSet<T> for TreeSet<T> {
471+
/// Add a value to the set. Return true if the value was not already
472+
/// present in the set.
473+
#[inline]
474+
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
475+
476+
/// Remove a value from the set. Return true if the value was
477+
/// present in the set.
478+
#[inline]
479+
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
480+
}
481+
478482
impl<T: TotalOrd> TreeSet<T> {
479483
/// Create an empty TreeSet
480484
#[inline]

src/libstd/container.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ pub trait Mutable: Container {
3030

3131
/// A map is a key-value store where values may be looked up by their keys. This
3232
/// trait provides basic operations to operate on these stores.
33-
pub trait Map<K, V>: Mutable {
33+
pub trait Map<K, V>: Container {
3434
/// Return true if the map contains a value for the specified key
3535
fn contains_key(&self, key: &K) -> bool;
3636

3737
/// Return a reference to the value corresponding to the key
3838
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
39+
}
3940

40-
/// Return a mutable reference to the value corresponding to the key
41-
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
42-
41+
/// This trait provides basic operations to modify the contents of a map.
42+
pub trait MutableMap<K, V>: Map<K, V> + Mutable {
4343
/// Insert a key-value pair into the map. An existing value for a
4444
/// key is replaced by the new value. Return true if the key did
4545
/// not already exist in the map.
@@ -56,23 +56,18 @@ pub trait Map<K, V>: Mutable {
5656
/// Removes a key from the map, returning the value at the key if the key
5757
/// was previously in the map.
5858
fn pop(&mut self, k: &K) -> Option<V>;
59+
60+
/// Return a mutable reference to the value corresponding to the key
61+
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
5962
}
6063

6164
/// A set is a group of objects which are each distinct from one another. This
62-
/// trait represents actions which can be performed on sets to manipulate and
63-
/// iterate over them.
64-
pub trait Set<T>: Mutable {
65+
/// trait represents actions which can be performed on sets to iterate over
66+
/// them.
67+
pub trait Set<T>: Container {
6568
/// Return true if the set contains a value
6669
fn contains(&self, value: &T) -> bool;
6770

68-
/// Add a value to the set. Return true if the value was not already
69-
/// present in the set.
70-
fn insert(&mut self, value: T) -> bool;
71-
72-
/// Remove a value from the set. Return true if the value was
73-
/// present in the set.
74-
fn remove(&mut self, value: &T) -> bool;
75-
7671
/// Return true if the set has no elements in common with `other`.
7772
/// This is equivalent to checking for an empty intersection.
7873
fn is_disjoint(&self, other: &Self) -> bool;
@@ -95,3 +90,15 @@ pub trait Set<T>: Mutable {
9590
/// Visit the values representing the union
9691
fn union(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
9792
}
93+
94+
/// This trait represents actions which can be performed on sets to mutate
95+
/// them.
96+
pub trait MutableSet<T>: Set<T> + Mutable {
97+
/// Add a value to the set. Return true if the value was not already
98+
/// present in the set.
99+
fn insert(&mut self, value: T) -> bool;
100+
101+
/// Remove a value from the set. Return true if the value was
102+
/// present in the set.
103+
fn remove(&mut self, value: &T) -> bool;
104+
}

src/libstd/gc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ with destructors.
3939
*/
4040

4141
use cast;
42-
use container::{Map, Set};
42+
use container::{Set, MutableSet};
4343
use io;
4444
use libc::{uintptr_t};
4545
use option::{None, Option, Some};

src/libstd/hashmap.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
#[mutable_doc];
1717

18-
use container::{Container, Mutable, Map, Set};
18+
use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
1919
use cmp::{Eq, Equiv};
2020
use hash::Hash;
2121
use iterator::{Iterator, IteratorUtil};
@@ -316,7 +316,9 @@ impl<K:Hash + Eq,V> Map<K, V> for HashMap<K, V> {
316316
TableFull | FoundHole(_) => None,
317317
}
318318
}
319+
}
319320

321+
impl<K:Hash + Eq,V> MutableMap<K, V> for HashMap<K, V> {
320322
/// Return a mutable reference to the value corresponding to the key
321323
fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
322324
let idx = match self.bucket_for_key(k) {
@@ -640,14 +642,6 @@ impl<T:Hash + Eq> Set<T> for HashSet<T> {
640642
/// Return true if the set contains a value
641643
fn contains(&self, value: &T) -> bool { self.map.contains_key(value) }
642644

643-
/// Add a value to the set. Return true if the value was not already
644-
/// present in the set.
645-
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
646-
647-
/// Remove a value from the set. Return true if the value was
648-
/// present in the set.
649-
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
650-
651645
/// Return true if the set has no elements in common with `other`.
652646
/// This is equivalent to checking for an empty intersection.
653647
fn is_disjoint(&self, other: &HashSet<T>) -> bool {
@@ -688,6 +682,16 @@ impl<T:Hash + Eq> Set<T> for HashSet<T> {
688682
}
689683
}
690684

685+
impl<T:Hash + Eq> MutableSet<T> for HashSet<T> {
686+
/// Add a value to the set. Return true if the value was not already
687+
/// present in the set.
688+
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
689+
690+
/// Remove a value from the set. Return true if the value was
691+
/// present in the set.
692+
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
693+
}
694+
691695
impl<T:Hash + Eq> HashSet<T> {
692696
/// Create an empty HashSet
693697
pub fn new() -> HashSet<T> {

src/libstd/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use io::{print, println};
4545
pub use clone::{Clone, DeepClone};
4646
pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
4747
pub use char::Char;
48-
pub use container::{Container, Mutable, Map, Set};
48+
pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
4949
pub use hash::Hash;
5050
pub use iter::Times;
5151
pub use iterator::{Iterator, IteratorUtil, DoubleEndedIterator, DoubleEndedIteratorUtil};

src/libstd/task/spawn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use prelude::*;
7777
use cast::transmute;
7878
use cast;
7979
use cell::Cell;
80-
use container::Map;
80+
use container::MutableMap;
8181
use comm::{Chan, GenericChan};
8282
use hashmap::HashSet;
8383
use task::local_data_priv::{local_get, local_set, OldHandle};

src/libstd/to_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<A:ToStr> ToStr for @[A] {
182182
mod tests {
183183
use hashmap::HashMap;
184184
use hashmap::HashSet;
185-
use container::{Set, Map};
185+
use container::{Set, Map, MutableSet, MutableMap};
186186
#[test]
187187
fn test_simple_types() {
188188
assert_eq!(1i.to_str(), ~"1");

src/libstd/trie.rs

+2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ impl<T> Map<uint, T> for TrieMap<T> {
7878
idx += 1;
7979
}
8080
}
81+
}
8182

83+
impl<T> MutableMap<uint, T> for TrieMap<T> {
8284
/// Return a mutable reference to the value corresponding to the key
8385
#[inline]
8486
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> {

src/test/bench/core-map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn timed(label: &str, f: &fn()) {
2727
io::println(fmt!(" %s: %f", label, end - start));
2828
}
2929

30-
fn ascending<M: Map<uint, uint>>(map: &mut M, n_keys: uint) {
30+
fn ascending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
3131
io::println(" Ascending integers:");
3232

3333
do timed("insert") {
@@ -49,7 +49,7 @@ fn ascending<M: Map<uint, uint>>(map: &mut M, n_keys: uint) {
4949
}
5050
}
5151

52-
fn descending<M: Map<uint, uint>>(map: &mut M, n_keys: uint) {
52+
fn descending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
5353
io::println(" Descending integers:");
5454

5555
do timed("insert") {
@@ -71,7 +71,7 @@ fn descending<M: Map<uint, uint>>(map: &mut M, n_keys: uint) {
7171
}
7272
}
7373

74-
fn vector<M: Map<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) {
74+
fn vector<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) {
7575

7676
do timed("insert") {
7777
for uint::range(0, n_keys) |i| {

src/test/bench/core-set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn timed(result: &mut float, op: &fn()) {
3636
}
3737

3838
impl Results {
39-
pub fn bench_int<T:Set<uint>,
39+
pub fn bench_int<T:MutableSet<uint>,
4040
R: rand::Rng>(
4141
&mut self,
4242
rng: &mut R,
@@ -79,7 +79,7 @@ impl Results {
7979
}
8080
}
8181

82-
pub fn bench_str<T:Set<~str>,
82+
pub fn bench_str<T:MutableSet<~str>,
8383
R:rand::Rng>(
8484
&mut self,
8585
rng: &mut R,

0 commit comments

Comments
 (0)