Skip to content

Commit 21ac985

Browse files
committed
collections: Remove all collections traits
As part of the collections reform RFC, this commit removes all collections traits in favor of inherent methods on collections themselves. All methods should continue to be available on all collections. This is a breaking change with all of the collections traits being removed and no longer being in the prelude. In order to update old code you should move the trait implementations to inherent implementations directly on the type itself. Note that some traits had default methods which will also need to be implemented to maintain backwards compatibility. [breaking-change] cc #18424
1 parent 1442235 commit 21ac985

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2462
-1569
lines changed

src/libcollections/bench.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::prelude::*;
12+
use std::rand;
13+
use std::rand::Rng;
14+
use test::Bencher;
15+
16+
pub fn insert_rand_n<M>(n: uint, map: &mut M, b: &mut Bencher,
17+
insert: |&mut M, uint|,
18+
remove: |&mut M, uint|) {
19+
// setup
20+
let mut rng = rand::weak_rng();
21+
22+
for _ in range(0, n) {
23+
insert(map, rng.gen::<uint>() % n);
24+
}
25+
26+
// measure
27+
b.iter(|| {
28+
let k = rng.gen::<uint>() % n;
29+
insert(map, k);
30+
remove(map, k);
31+
})
32+
}
33+
34+
pub fn insert_seq_n<M>(n: uint, map: &mut M, b: &mut Bencher,
35+
insert: |&mut M, uint|,
36+
remove: |&mut M, uint|) {
37+
// setup
38+
for i in range(0u, n) {
39+
insert(map, i * 2);
40+
}
41+
42+
// measure
43+
let mut i = 1;
44+
b.iter(|| {
45+
insert(map, i);
46+
remove(map, i);
47+
i = (i + 2) % n;
48+
})
49+
}
50+
51+
pub fn find_rand_n<M, T>(n: uint, map: &mut M, b: &mut Bencher,
52+
insert: |&mut M, uint|,
53+
find: |&M, uint| -> T) {
54+
// setup
55+
let mut rng = rand::weak_rng();
56+
let mut keys = Vec::from_fn(n, |_| rng.gen::<uint>() % n);
57+
58+
for k in keys.iter() {
59+
insert(map, *k);
60+
}
61+
62+
rng.shuffle(keys.as_mut_slice());
63+
64+
// measure
65+
let mut i = 0;
66+
b.iter(|| {
67+
let t = find(map, keys[i]);
68+
i = (i + 1) % n;
69+
t
70+
})
71+
}
72+
73+
pub fn find_seq_n<M, T>(n: uint, map: &mut M, b: &mut Bencher,
74+
insert: |&mut M, uint|,
75+
find: |&M, uint| -> T) {
76+
// setup
77+
for i in range(0u, n) {
78+
insert(map, i);
79+
}
80+
81+
// measure
82+
let mut i = 0;
83+
b.iter(|| {
84+
let x = find(map, i);
85+
i = (i + 1) % n;
86+
x
87+
})
88+
}

src/libcollections/bitv.rs

+62-52
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ use core::slice;
7070
use core::u32;
7171
use std::hash;
7272

73-
use {Mutable, Set, MutableSet, MutableSeq};
7473
use vec::Vec;
7574

7675
type MatchWords<'a> = Chain<MaskWords<'a>, Skip<Take<Enumerate<Repeat<u32>>>>>;
@@ -755,6 +754,20 @@ impl Bitv {
755754
}
756755
self.set(insert_pos, elem);
757756
}
757+
758+
/// Return the total number of bits in this vector
759+
#[inline]
760+
pub fn len(&self) -> uint { self.nbits }
761+
762+
/// Returns true if there are no bits in this vector
763+
#[inline]
764+
pub fn is_empty(&self) -> bool { self.len() == 0 }
765+
766+
/// Clears all bits in this vector.
767+
#[inline]
768+
pub fn clear(&mut self) {
769+
for w in self.storage.iter_mut() { *w = 0u32; }
770+
}
758771
}
759772

760773
/// Transforms a byte-vector into a `Bitv`. Each byte becomes eight bits,
@@ -804,18 +817,6 @@ impl Default for Bitv {
804817
fn default() -> Bitv { Bitv::new() }
805818
}
806819

807-
impl Collection for Bitv {
808-
#[inline]
809-
fn len(&self) -> uint { self.nbits }
810-
}
811-
812-
impl Mutable for Bitv {
813-
#[inline]
814-
fn clear(&mut self) {
815-
for w in self.storage.iter_mut() { *w = 0u32; }
816-
}
817-
}
818-
819820
impl FromIterator<bool> for Bitv {
820821
fn from_iter<I:Iterator<bool>>(iterator: I) -> Bitv {
821822
let mut ret = Bitv::new();
@@ -1466,61 +1467,45 @@ impl BitvSet {
14661467
pub fn symmetric_difference_with(&mut self, other: &BitvSet) {
14671468
self.other_op(other, |w1, w2| w1 ^ w2);
14681469
}
1469-
}
1470-
1471-
impl fmt::Show for BitvSet {
1472-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1473-
try!(write!(fmt, "{{"));
1474-
let mut first = true;
1475-
for n in self.iter() {
1476-
if !first {
1477-
try!(write!(fmt, ", "));
1478-
}
1479-
try!(write!(fmt, "{}", n));
1480-
first = false;
1481-
}
1482-
write!(fmt, "}}")
1483-
}
1484-
}
14851470

1486-
impl<S: hash::Writer> hash::Hash<S> for BitvSet {
1487-
fn hash(&self, state: &mut S) {
1488-
for pos in self.iter() {
1489-
pos.hash(state);
1490-
}
1471+
/// Return the number of set bits in this set.
1472+
#[inline]
1473+
pub fn len(&self) -> uint {
1474+
let &BitvSet(ref bitv) = self;
1475+
bitv.storage.iter().fold(0, |acc, &n| acc + n.count_ones())
14911476
}
1492-
}
14931477

1494-
impl Collection for BitvSet {
1478+
/// Returns whether there are no bits set in this set
14951479
#[inline]
1496-
fn len(&self) -> uint {
1480+
pub fn is_empty(&self) -> bool {
14971481
let &BitvSet(ref bitv) = self;
1498-
bitv.storage.iter().fold(0, |acc, &n| acc + n.count_ones())
1482+
bitv.storage.iter().all(|&n| n == 0)
14991483
}
1500-
}
15011484

1502-
impl Mutable for BitvSet {
1485+
/// Clears all bits in this set
15031486
#[inline]
1504-
fn clear(&mut self) {
1487+
pub fn clear(&mut self) {
15051488
let &BitvSet(ref mut bitv) = self;
15061489
bitv.clear();
15071490
}
1508-
}
15091491

1510-
impl Set<uint> for BitvSet {
1492+
/// Returns `true` if this set contains the specified integer.
15111493
#[inline]
1512-
fn contains(&self, value: &uint) -> bool {
1494+
pub fn contains(&self, value: &uint) -> bool {
15131495
let &BitvSet(ref bitv) = self;
15141496
*value < bitv.nbits && bitv.get(*value)
15151497
}
15161498

1499+
/// Returns `true` if the set has no elements in common with `other`.
1500+
/// This is equivalent to checking for an empty intersection.
15171501
#[inline]
1518-
fn is_disjoint(&self, other: &BitvSet) -> bool {
1502+
pub fn is_disjoint(&self, other: &BitvSet) -> bool {
15191503
self.intersection(other).next().is_none()
15201504
}
15211505

1506+
/// Returns `true` if the set is a subset of another.
15221507
#[inline]
1523-
fn is_subset(&self, other: &BitvSet) -> bool {
1508+
pub fn is_subset(&self, other: &BitvSet) -> bool {
15241509
let &BitvSet(ref self_bitv) = self;
15251510
let &BitvSet(ref other_bitv) = other;
15261511

@@ -1531,14 +1516,15 @@ impl Set<uint> for BitvSet {
15311516
self_bitv.mask_words(other_bitv.storage.len()).all(|(_, w)| w == 0)
15321517
}
15331518

1519+
/// Returns `true` if the set is a superset of another.
15341520
#[inline]
1535-
fn is_superset(&self, other: &BitvSet) -> bool {
1521+
pub fn is_superset(&self, other: &BitvSet) -> bool {
15361522
other.is_subset(self)
15371523
}
1538-
}
15391524

1540-
impl MutableSet<uint> for BitvSet {
1541-
fn insert(&mut self, value: uint) -> bool {
1525+
/// Adds a value to the set. Returns `true` if the value was not already
1526+
/// present in the set.
1527+
pub fn insert(&mut self, value: uint) -> bool {
15421528
if self.contains(&value) {
15431529
return false;
15441530
}
@@ -1554,7 +1540,9 @@ impl MutableSet<uint> for BitvSet {
15541540
return true;
15551541
}
15561542

1557-
fn remove(&mut self, value: &uint) -> bool {
1543+
/// Removes a value from the set. Returns `true` if the value was
1544+
/// present in the set.
1545+
pub fn remove(&mut self, value: &uint) -> bool {
15581546
if !self.contains(value) {
15591547
return false;
15601548
}
@@ -1564,6 +1552,29 @@ impl MutableSet<uint> for BitvSet {
15641552
}
15651553
}
15661554

1555+
impl fmt::Show for BitvSet {
1556+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1557+
try!(write!(fmt, "{{"));
1558+
let mut first = true;
1559+
for n in self.iter() {
1560+
if !first {
1561+
try!(write!(fmt, ", "));
1562+
}
1563+
try!(write!(fmt, "{}", n));
1564+
first = false;
1565+
}
1566+
write!(fmt, "}}")
1567+
}
1568+
}
1569+
1570+
impl<S: hash::Writer> hash::Hash<S> for BitvSet {
1571+
fn hash(&self, state: &mut S) {
1572+
for pos in self.iter() {
1573+
pos.hash(state);
1574+
}
1575+
}
1576+
}
1577+
15671578
/// An iterator for `BitvSet`.
15681579
pub struct BitPositions<'a> {
15691580
set: &'a BitvSet,
@@ -1643,7 +1654,6 @@ mod tests {
16431654
use std::rand::Rng;
16441655
use test::Bencher;
16451656

1646-
use {Set, Mutable, MutableSet, MutableSeq};
16471657
use bitv::{Bitv, BitvSet, from_fn, from_bytes};
16481658
use bitv;
16491659
use vec::Vec;

0 commit comments

Comments
 (0)