Permalink
Please sign in to comment.
Browse files
Bump to 0.2
This switches the CurvatureBounds enum to a set of traits, with derive macros. Also updates slog.
- Loading branch information...
Showing
with
177 additions
and 41 deletions.
- +4 −3 Cargo.toml
- +11 −0 avarice-derive/Cargo.toml
- +57 −0 avarice-derive/src/lib.rs
- +11 −14 src/greedy.rs
- +61 −0 src/lib.rs
- +33 −24 src/objective.rs
| @@ -0,0 +1,11 @@ | ||
| +[package] | ||
| +authors = ["J David Smith <emallson@atlanis.net>"] | ||
| +name = "avarice-derive" | ||
| +version = "0.1.0" | ||
| + | ||
| +[lib] | ||
| +proc-macro = true | ||
| + | ||
| +[dependencies] | ||
| +quote = "0.3.15" | ||
| +syn = "0.11.10" |
| @@ -0,0 +1,57 @@ | ||
| +extern crate proc_macro; | ||
| +extern crate syn; | ||
| +#[macro_use] | ||
| +extern crate quote; | ||
| + | ||
| +use proc_macro::TokenStream; | ||
| +use quote::Ident; | ||
| + | ||
| +#[proc_macro_derive(Submodular)] | ||
| +pub fn submodular(input: TokenStream) -> TokenStream { | ||
| + let s = input.to_string(); | ||
| + let ast = syn::parse_derive_input(&s).unwrap(); | ||
| + let expanded = expand_bounds(&ast, | ||
| + Ident::new("Submodular"), | ||
| + Ident::new("None"), | ||
| + Ident::new("Some(1.0)")); | ||
| + | ||
| + expanded.parse().unwrap() | ||
| +} | ||
| + | ||
| +#[proc_macro_derive(Supermodular)] | ||
| +pub fn supermodular(input: TokenStream) -> TokenStream { | ||
| + let s = input.to_string(); | ||
| + let ast = syn::parse_derive_input(&s).unwrap(); | ||
| + let expanded = expand_bounds(&ast, | ||
| + Ident::new("Supermodular"), | ||
| + Ident::new("Some(1.0)"), | ||
| + Ident::new("None")); | ||
| + | ||
| + expanded.parse().unwrap() | ||
| +} | ||
| + | ||
| +#[proc_macro_derive(Modular)] | ||
| +pub fn modular(input: TokenStream) -> TokenStream { | ||
| + let s = input.to_string(); | ||
| + let ast = syn::parse_derive_input(&s).unwrap(); | ||
| + let expanded = expand_bounds(&ast, | ||
| + Ident::new("Modular"), | ||
| + Ident::new("Some(1.0)"), | ||
| + Ident::new("Some(1.0)")); | ||
| + | ||
| + expanded.parse().unwrap() | ||
| +} | ||
| + | ||
| +fn expand_bounds(ast: &syn::DeriveInput, ty: Ident, low: Ident, high: Ident) -> quote::Tokens { | ||
| + let name = &ast.ident; | ||
| + let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); | ||
| + | ||
| + quote! { | ||
| + impl #impl_generics ::objective::curvature::#ty for #name #ty_generics #where_clause {} | ||
| + impl #impl_generics ::objective::curvature::Bounded for #name #ty_generics #where_clause { | ||
| + fn bounds() -> (Option<f64>, Option<f64>) { | ||
| + (#low, #high) | ||
| + } | ||
| + } | ||
| + } | ||
| +} |
61
src/lib.rs
0 comments on commit
5baf300