Skip to content

Commit

Permalink
Move common options outside the subcommands.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelLarkin committed Mar 31, 2022
1 parent b9a5853 commit 332bbd1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
4 changes: 2 additions & 2 deletions benches/hyperfine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function speedtest {
--style full \
--parameter-list sample_size $sample_sizes \
"$python unweighted --size {sample_size} $tmp_src" \
"cargo run --release unweighted --size {sample_size} < $tmp_src" \
"cargo run --release -- --size {sample_size} unweighted < $tmp_src" \
"$python weighted --size {sample_size} $tmp_src <(cut -f 8 < $tmp_src)" \
"cargo run --release weighted --size {sample_size} $tmp_src <(cut -f 8 < $tmp_src)"
"cargo run --release -- --size {sample_size} weighted $tmp_src <(cut -f 8 < $tmp_src)"
}


Expand Down
35 changes: 13 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ use reservoir_sampling::{
struct Cli {
#[clap(subcommand)]
command: Commands,

/// Seed for reproducibility
#[clap(long, parse(try_from_str))]
seed: Option<u64>,

/// Sample size
#[clap(short, long, default_value_t=10)]
size: usize,
}


Expand All @@ -44,14 +52,6 @@ enum Commands {
#[clap(arg_required_else_help=false, visible_alias="uw")]
/// Unweighted resevoir sampling
Unweighted {
/// Seed for reproducibility
#[clap(long, parse(try_from_str))]
seed: Option<u64>,

/// Sample size
#[clap(short, long, default_value_t=10)]
size: usize,

/// Population file name.
#[clap(name="population's file name")]
population_fn: Option<String>,
Expand All @@ -60,14 +60,6 @@ enum Commands {
#[clap(arg_required_else_help=false, visible_alias="w")]
/// Weighted reservoir sampling
Weighted {
/// Seed for reproducibility
#[clap(long, parse(try_from_str))]
seed: Option<u64>,

/// Sample size
#[clap(short, long, default_value_t=10)]
size: usize,

/// Population file name.
#[clap(name="population's file name")]
population_fn: String,
Expand Down Expand Up @@ -121,21 +113,20 @@ fn get_rng(seed: &Option<u64>) -> SmallRng

fn main() {
let args = Cli::parse();
let mut rng = get_rng(&args.seed);
match &args.command {
Commands::Unweighted { size, seed, population_fn } => {
let mut rng = get_rng(seed);
Commands::Unweighted { population_fn } => {
let population = get_reader(population_fn);

let samples = l(
&mut population.lines().map(|v| v.unwrap()),
*size,
args.size,
&mut rng);
for sample in samples {
println!("{}", sample);
}
}
Commands::Weighted { size, seed, weight_fn, population_fn } => {
let mut rng = get_rng(seed);
Commands::Weighted { weight_fn, population_fn } => {
let weights = read_lines(weight_fn)
.unwrap()
.map(Result::unwrap)
Expand All @@ -147,7 +138,7 @@ fn main() {

let samples = a_exp_j(
&mut weighted_samples,
*size,
args.size,
&mut rng);
for sample in samples {
println!("{}", sample);
Expand Down

0 comments on commit 332bbd1

Please sign in to comment.