diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 50 | ||||
-rw-r--r-- | src/main.rs | 70 |
2 files changed, 120 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1fd07b4 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,50 @@ +extern crate chrono; + +use std::collections::LinkedList; + +use chrono::NaiveDate; +use chrono::Duration; + +pub fn partition (f: &Fn(u32) -> i64, v: &Vec<i64>) -> LinkedList<LinkedList<i64>> { + let mut term: LinkedList<i64> = LinkedList::new(); + let mut res: LinkedList<LinkedList<i64>> = LinkedList::new(); + let mut n : u32 = 1; + let mut a : i64 = v[0]; + + for &i in v.iter() { + while i >= a + f(n) { + res.push_back(term); + term = LinkedList::new(); + a += f(n); + n += 1; + } + term.push_back(i); + } + res.push_back(term); + + return res; +} + +pub fn partition_days (f: &Fn(u32) -> i64, days: &Vec<NaiveDate>) -> LinkedList<LinkedList<NaiveDate>> { + let day1 = days[0]; + let part; + + { + let mut v: Vec<i64> = Vec::with_capacity(days.len()); + + for &d in days.iter() { + v.push((day1-d).num_days()); + } + v.sort_unstable(); + v.dedup(); + + part = partition(f, &v); + } + + let res = part.into_iter().map( + |l| l.into_iter().map( + |d| day1 - Duration::days(d)).rev().collect() + ).collect(); + + return res +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ba7b145 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,70 @@ +extern crate chrono; +extern crate docopt; +extern crate serde; + +use std::process::exit; + +use chrono::NaiveDate; +use docopt::Docopt; +use serde::Deserialize; + +mod lib; +use lib::partition_days; + +const USAGE: &'static str = " +Usage: frotate [-k|-d] [-b <base>] <day>... + frotate --help + +Options: + -k --keep Print days to keep + -d --delete Print days to delete + -b --base <base> Base of the exponent [default: 1.1] + -h --help Show this help text +"; + +#[derive(Deserialize)] +struct Args { + flag_keep: bool, + flag_delete: bool, + flag_base: f32, + arg_day: Vec<NaiveDate>, +} + +fn exponent(b:f32, n:u32) -> i64 { + let i : i32 = n as i32 - 1; + return b.powi(i).ceil() as i64; +} + +fn main() { + let args: Args = Docopt::new(USAGE) + .and_then(|d| d.deserialize()) + .unwrap_or_else(|e| e.exit()); + + let parts = partition_days(&|n| exponent(args.flag_base, n), &args.arg_day); + + if args.flag_keep { + for days in parts.iter() { + match days.front() { + Some(d) => print!("{:?} ", d), + None => {} + } + } + println!(); + } else if args.flag_delete { + for days in parts.iter() { + for d in days.iter().skip(1) { + print!("{:?} ", d); + } + } + println!(); + } else { + for days in parts.iter() { + for d in days.iter() { + eprint!("{:?} ", d); + } + eprintln!(); + } + exit(1); + } +} + |