auto merge of #5005 : alexcrichton/rust/bitv++, r=catamorphism

These commits take the old bitv implementation and modernize it with an explicit self, some minor touchups, and using what I think is some more recent patterns (like `::new` instead of `Type()`).

Additionally, this adds an implementation of `container::Set` on top of a bit vector to have as a set of `uint`s. I initially tried to parameterize the type for the set to be `T: NumCast` but I was hitting build problems in stage0 which I think means that it's not in a snapshot yet, so it's just hardcoded as a set of `uint`s now. In the future perhaps it could be parameterized. I'm not sure if it would really add anything, though, so maybe it's nicer to be hardcoded anyway.

I also added some extra methods to do normal bit vector operations on the set in-place, but these aren't a part of the `Set` trait right now. I haven't benchmarked any of these operations just yet, but I imagine that there's quite a lot of room for optimization here and there.
This commit is contained in:
bors 2013-02-18 18:40:33 -08:00
commit 6351515d98
5 changed files with 895 additions and 197 deletions

File diff suppressed because it is too large Load Diff

View File

@ -43,13 +43,13 @@ use ext::base::ext_ctxt;
use ext::pipes::proto::protocol;
use core::str;
use std::bitv::{Bitv};
use std::bitv::Bitv;
pub fn analyze(proto: protocol, _cx: ext_ctxt) {
debug!("initializing colive analysis");
let num_states = proto.num_states();
let colive = do (copy proto.states).map_to_vec |state| {
let bv = ~Bitv(num_states, false);
let mut colive = do (copy proto.states).map_to_vec |state| {
let mut bv = ~Bitv::new(num_states, false);
for state.reachable |s| {
bv.set(s.id, true);
}
@ -61,15 +61,19 @@ pub fn analyze(proto: protocol, _cx: ext_ctxt) {
while changed {
changed = false;
debug!("colive iteration %?", i);
let mut new_colive = ~[];
for colive.eachi |i, this_colive| {
let mut result = ~this_colive.clone();
let this = proto.get_state_by_id(i);
for this_colive.ones |j| {
let next = proto.get_state_by_id(j);
if this.dir == next.dir {
changed = changed || this_colive.union(colive[j]);
changed = result.union(colive[j]) || changed;
}
}
new_colive.push(result)
}
colive = new_colive;
i += 1;
}

180
src/test/bench/core-set.rs Normal file
View File

@ -0,0 +1,180 @@
// Copyright 2013 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.
extern mod std;
use core::hashmap::linear::LinearSet;
use std::bitv::BitvSet;
use std::treemap::TreeSet;
use core::io::WriterUtil;
struct Results {
sequential_ints: float,
random_ints: float,
delete_ints: float,
sequential_strings: float,
random_strings: float,
delete_strings: float
}
fn timed(result: &mut float, op: fn()) {
let start = std::time::precise_time_s();
op();
let end = std::time::precise_time_s();
*result = (end - start);
}
impl Results {
fn bench_int<T: Set<uint>>(&mut self, rng: @rand::Rng, num_keys: uint,
rand_cap: uint, f: fn() -> T) {
{
let mut set = f();
do timed(&mut self.sequential_ints) {
for uint::range(0, num_keys) |i| {
set.insert(i);
}
for uint::range(0, num_keys) |i| {
assert set.contains(&i);
}
}
}
{
let mut set = f();
do timed(&mut self.random_ints) {
for num_keys.times {
set.insert((rng.next() as uint) % rand_cap);
}
}
}
{
let mut set = f();
for uint::range(0, num_keys) |i| {
set.insert(i);
}
do timed(&mut self.delete_ints) {
for uint::range(0, num_keys) |i| {
assert set.remove(&i);
}
}
}
}
fn bench_str<T: Set<~str>>(&mut self, rng: @rand::Rng, num_keys: uint,
f: fn() -> T) {
{
let mut set = f();
do timed(&mut self.sequential_strings) {
for uint::range(0, num_keys) |i| {
let s = uint::to_str(i);
set.insert(s);
}
for uint::range(0, num_keys) |i| {
let s = uint::to_str(i);
assert set.contains(&s);
}
}
}
{
let mut set = f();
do timed(&mut self.random_strings) {
for num_keys.times {
let s = uint::to_str(rng.next() as uint);
set.insert(s);
}
}
}
{
let mut set = f();
for uint::range(0, num_keys) |i| {
set.insert(uint::to_str(i));
}
do timed(&mut self.delete_strings) {
for uint::range(0, num_keys) |i| {
assert set.remove(&uint::to_str(i));
}
}
}
}
}
fn write_header(header: &str) {
io::stdout().write_str(header);
io::stdout().write_str("\n");
}
fn write_row(label: &str, value: float) {
io::stdout().write_str(fmt!("%30s %f s\n", label, value));
}
fn write_results(label: &str, results: &Results) {
write_header(label);
write_row("sequential_ints", results.sequential_ints);
write_row("random_ints", results.random_ints);
write_row("delete_ints", results.delete_ints);
write_row("sequential_strings", results.sequential_strings);
write_row("random_strings", results.random_strings);
write_row("delete_strings", results.delete_strings);
}
fn empty_results() -> Results {
Results {
sequential_ints: 0f,
random_ints: 0f,
delete_ints: 0f,
sequential_strings: 0f,
random_strings: 0f,
delete_strings: 0f,
}
}
fn main() {
let args = os::args();
let num_keys = {
if args.len() == 2 {
uint::from_str(args[1]).get()
} else {
100 // woefully inadequate for any real measurement
}
};
let seed = ~[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let max = 200000;
{
let rng = rand::seeded_rng(&seed);
let mut results = empty_results();
results.bench_int(rng, num_keys, max, || LinearSet::new::<uint>());
results.bench_str(rng, num_keys, || LinearSet::new::<~str>());
write_results("core::hashmap::LinearSet", &results);
}
{
let rng = rand::seeded_rng(&seed);
let mut results = empty_results();
results.bench_int(rng, num_keys, max, || TreeSet::new::<uint>());
results.bench_str(rng, num_keys, || TreeSet::new::<~str>());
write_results("std::treemap::TreeSet", &results);
}
{
let rng = rand::seeded_rng(&seed);
let mut results = empty_results();
results.bench_int(rng, num_keys, max, || BitvSet::new());
write_results("std::bitv::BitvSet", &results);
}
}

View File

@ -58,7 +58,7 @@ pub fn solve_grid(g: grid_t) {
fn next_color(mut g: grid, row: u8, col: u8, start_color: u8) -> bool {
if start_color < 10u8 {
// colors not yet used
let avail = bitv::Bitv(10u, false);
let mut avail = bitv::Bitv::new(10u, false);
for u8::range(start_color, 10u8) |color| {
avail.set(color as uint, true);
}
@ -80,7 +80,7 @@ pub fn solve_grid(g: grid_t) {
// find colors available in neighbourhood of (row, col)
fn drop_colors(g: grid, avail: bitv::Bitv, row: u8, col: u8) {
fn drop_color(g: grid, colors: bitv::Bitv, row: u8, col: u8) {
fn drop_color(g: grid, mut colors: bitv::Bitv, row: u8, col: u8) {
let color = g[row][col];
if color != 0u8 { colors.set(color as uint, false); }
}

View File

@ -14,8 +14,8 @@ extern mod std;
use std::bitv::*;
fn bitv_test() -> bool {
let v1 = ~Bitv(31, false);
let v2 = ~Bitv(31, true);
let mut v1 = ~Bitv::new(31, false);
let v2 = ~Bitv::new(31, true);
v1.union(v2);
true
}