Convert an iterator of Into<LinearExpr> to a LinearExpr
This commit is contained in:
committed by
Guillaume P
parent
1c60e72683
commit
442e542347
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cp_sat"
|
name = "cp_sat"
|
||||||
version = "0.3.1"
|
version = "0.3.2"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Rust bindings to the Google CP-SAT constraint programming solver."
|
description = "Rust bindings to the Google CP-SAT constraint programming solver."
|
||||||
documentation = "https://docs.rs/cp_sat"
|
documentation = "https://docs.rs/cp_sat"
|
||||||
|
|||||||
@@ -877,6 +877,11 @@ pub struct Constraint(usize);
|
|||||||
/// _ => expr -= LinearExpr::from([(42, v), (1337, y1)]) + 5,
|
/// _ => expr -= LinearExpr::from([(42, v), (1337, y1)]) + 5,
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
|
///
|
||||||
|
/// // an iterator of `Into<LinearExpr>` can be collected or extended,
|
||||||
|
/// // meaning summing the elements
|
||||||
|
/// model.maximize(vars.iter().copied().collect::<LinearExpr>()); // means sum(vars)
|
||||||
|
/// expr.extend(vars.iter().map(|&v| (2, v))); // means expr += sum_vars(2 * v)
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone, Default, Debug)]
|
#[derive(Clone, Default, Debug)]
|
||||||
pub struct LinearExpr {
|
pub struct LinearExpr {
|
||||||
@@ -974,3 +979,18 @@ impl From<LinearExpr> for proto::LinearExpressionProto {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Into<LinearExpr>> std::iter::Extend<T> for LinearExpr {
|
||||||
|
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
||||||
|
for e in iter {
|
||||||
|
*self += e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: Into<LinearExpr>> std::iter::FromIterator<T> for LinearExpr {
|
||||||
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
||||||
|
let mut res = LinearExpr::default();
|
||||||
|
res.extend(iter);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user