aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2019-10-05 21:54:27 +0200
committerIgor Pashev <pashev.igor@gmail.com>2019-10-05 22:02:27 +0200
commita65f7591894f67021df3490c07ab7e75627e476e (patch)
treea6137cfc9fa91c84c953e80e5a00a17b68a59640 /src/lib.rs
downloadfrotate.rs-a65f7591894f67021df3490c07ab7e75627e476e.tar.gz
Initial version 0.1.00.1.0
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs50
1 files changed, 50 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
+}