Skip to content

Commit

Permalink
collections: Remove all collections traits
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Nov 1, 2014
1 parent 1442235 commit 21ac985
Show file tree
Hide file tree
Showing 73 changed files with 2,462 additions and 1,569 deletions.
88 changes: 88 additions & 0 deletions src/libcollections/bench.rs
@@ -0,0 +1,88 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::prelude::*;
use std::rand;
use std::rand::Rng;
use test::Bencher;

pub fn insert_rand_n<M>(n: uint, map: &mut M, b: &mut Bencher,
insert: |&mut M, uint|,
remove: |&mut M, uint|) {
// setup
let mut rng = rand::weak_rng();

for _ in range(0, n) {
insert(map, rng.gen::<uint>() % n);
}

// measure
b.iter(|| {
let k = rng.gen::<uint>() % n;
insert(map, k);
remove(map, k);
})
}

pub fn insert_seq_n<M>(n: uint, map: &mut M, b: &mut Bencher,
insert: |&mut M, uint|,
remove: |&mut M, uint|) {
// setup
for i in range(0u, n) {
insert(map, i * 2);
}

// measure
let mut i = 1;
b.iter(|| {
insert(map, i);
remove(map, i);
i = (i + 2) % n;
})
}

pub fn find_rand_n<M, T>(n: uint, map: &mut M, b: &mut Bencher,
insert: |&mut M, uint|,
find: |&M, uint| -> T) {
// setup
let mut rng = rand::weak_rng();
let mut keys = Vec::from_fn(n, |_| rng.gen::<uint>() % n);

for k in keys.iter() {
insert(map, *k);
}

rng.shuffle(keys.as_mut_slice());

// measure
let mut i = 0;
b.iter(|| {
let t = find(map, keys[i]);
i = (i + 1) % n;
t
})
}

pub fn find_seq_n<M, T>(n: uint, map: &mut M, b: &mut Bencher,
insert: |&mut M, uint|,
find: |&M, uint| -> T) {
// setup
for i in range(0u, n) {
insert(map, i);
}

// measure
let mut i = 0;
b.iter(|| {
let x = find(map, i);
i = (i + 1) % n;
x
})
}
114 changes: 62 additions & 52 deletions src/libcollections/bitv.rs
Expand Up @@ -70,7 +70,6 @@ use core::slice;
use core::u32;
use std::hash;

use {Mutable, Set, MutableSet, MutableSeq};
use vec::Vec;

type MatchWords<'a> = Chain<MaskWords<'a>, Skip<Take<Enumerate<Repeat<u32>>>>>;
Expand Down Expand Up @@ -755,6 +754,20 @@ impl Bitv {
}
self.set(insert_pos, elem);
}

/// Return the total number of bits in this vector
#[inline]
pub fn len(&self) -> uint { self.nbits }

/// Returns true if there are no bits in this vector
#[inline]
pub fn is_empty(&self) -> bool { self.len() == 0 }

/// Clears all bits in this vector.
#[inline]
pub fn clear(&mut self) {
for w in self.storage.iter_mut() { *w = 0u32; }
}
}

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

impl Collection for Bitv {
#[inline]
fn len(&self) -> uint { self.nbits }
}

impl Mutable for Bitv {
#[inline]
fn clear(&mut self) {
for w in self.storage.iter_mut() { *w = 0u32; }
}
}

impl FromIterator<bool> for Bitv {
fn from_iter<I:Iterator<bool>>(iterator: I) -> Bitv {
let mut ret = Bitv::new();
Expand Down Expand Up @@ -1466,61 +1467,45 @@ impl BitvSet {
pub fn symmetric_difference_with(&mut self, other: &BitvSet) {
self.other_op(other, |w1, w2| w1 ^ w2);
}
}

impl fmt::Show for BitvSet {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
for n in self.iter() {
if !first {
try!(write!(fmt, ", "));
}
try!(write!(fmt, "{}", n));
first = false;
}
write!(fmt, "}}")
}
}

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

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

impl Mutable for BitvSet {
/// Clears all bits in this set
#[inline]
fn clear(&mut self) {
pub fn clear(&mut self) {
let &BitvSet(ref mut bitv) = self;
bitv.clear();
}
}

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

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

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

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

/// Returns `true` if the set is a superset of another.
#[inline]
fn is_superset(&self, other: &BitvSet) -> bool {
pub fn is_superset(&self, other: &BitvSet) -> bool {
other.is_subset(self)
}
}

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

fn remove(&mut self, value: &uint) -> bool {
/// Removes a value from the set. Returns `true` if the value was
/// present in the set.
pub fn remove(&mut self, value: &uint) -> bool {
if !self.contains(value) {
return false;
}
Expand All @@ -1564,6 +1552,29 @@ impl MutableSet<uint> for BitvSet {
}
}

impl fmt::Show for BitvSet {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
for n in self.iter() {
if !first {
try!(write!(fmt, ", "));
}
try!(write!(fmt, "{}", n));
first = false;
}
write!(fmt, "}}")
}
}

impl<S: hash::Writer> hash::Hash<S> for BitvSet {
fn hash(&self, state: &mut S) {
for pos in self.iter() {
pos.hash(state);
}
}
}

/// An iterator for `BitvSet`.
pub struct BitPositions<'a> {
set: &'a BitvSet,
Expand Down Expand Up @@ -1643,7 +1654,6 @@ mod tests {
use std::rand::Rng;
use test::Bencher;

use {Set, Mutable, MutableSet, MutableSeq};
use bitv::{Bitv, BitvSet, from_fn, from_bytes};
use bitv;
use vec::Vec;
Expand Down

5 comments on commit 21ac985

@bors
Copy link
Contributor

@bors bors commented on 21ac985 Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from aturon
at alexcrichton@21ac985

@bors
Copy link
Contributor

@bors bors commented on 21ac985 Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/no-more-traits = 21ac985 into auto

@bors
Copy link
Contributor

@bors bors commented on 21ac985 Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/no-more-traits = 21ac985 merged ok, testing candidate = 39f90ae

@bors
Copy link
Contributor

@bors bors commented on 21ac985 Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 21ac985 Nov 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 39f90ae

Please sign in to comment.