1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2018 Stefan Kroboth
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! TODO Documentation

/// This macro generates the `run` function for every solver which implements `ArgminSolver`.
#[macro_export]
macro_rules! make_run {
    ( $ProblemDefinition:ty, $StartingPoints:ty, $Parameter:ty, $CostValue:ty ) => {
        fn run(
            &mut self,
            operator: $ProblemDefinition,
            init_param: &$StartingPoints,
        ) -> Result<ArgminResult<$Parameter, $CostValue>> {
            self.init(operator, init_param)?;

            let mut res;
            loop {
                res = self.next_iter()?;
                if res.terminated {
                    break;
                }
            }
            Ok(res)
        }
    }
}

/// This macro generates the `terminate` function for every solver which implements `ArgminSolver`.
#[macro_export]
macro_rules! make_terminate {
    ($condition:expr, $reason:path;) => {
        if $condition {
            return $reason;
        }
    };
    ($condition:expr, $reason:path ; $($x: expr, $y:path;)*) => {
            make_terminate!( $condition, $reason; );
            make_terminate!( $($x, $y;)* );
    };
    ($self:ident, $($x: expr, $y:path;)*) => {
        fn terminate(&$self) -> TerminationReason {
            make_terminate!( $($x, $y;)* );
            TerminationReason::NotTerminated
        }
    };
}