src/iterate.rs

Wed, 22 Apr 2026 23:41:59 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Apr 2026 23:41:59 -0500
branch
dev
changeset 197
1f301affeae3
parent 87
72968cf30033
permissions
-rw-r--r--

Fix internal links in documentation

0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 /*!
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
2 Tools for separating the computational steps of an iterative algorithm from stopping rules
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
3 and reporting.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
5 The computational step is to be implemented as a closure. That closure gets passed a `state`
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
6 parameter that implements an [`if_verbose`][AlgIteratorState::if_verbose] method that is to
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
7 be called to determine whether function values or other potentially costly things need to be
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
8 calculated on that iteration. The parameter of [`if_verbose`][AlgIteratorState::if_verbose] is
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
9 another closure that does the necessary computation.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 ## Simple example
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 ```rust
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
14 # use alg_tools::types::*;
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
15 # use alg_tools::iterate::*;
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
16 let mut iter = AlgIteratorOptions{
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
17 max_iter : 100,
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
18 verbose_iter : Verbose::Every(10),
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
19 .. Default::default()
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
20 };
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 let mut x = 1 as float;
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
22 # let mut iter_clone = iter.clone();
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 iter.iterate(|state|{
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 // This is our computational step
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 x = x + x.sqrt();
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 state.if_verbose(||{
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 // return current value when requested
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 return x
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 })
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
30 });
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
31 // or alternatively (avoiding problems with moves)
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
32 # iter = iter_clone;
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
33 for state in iter.iter() {
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
34 // This is our computational step
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
35 x = x + x.sqrt();
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
36 state.if_verbose(||{
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
37 // return current value when requested
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
38 return x
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
39 })
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
40 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 ```
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 There is no colon after `state.if_verbose`, because we need to return its value. If you do something after the step, you need to store the result in a variable and return it from the main computational step afterwards.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 This will print out
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 ```output
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 10/100 J=31.051164
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 20/100 J=108.493699
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 30/100 J=234.690039
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 40/100 J=410.056327
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 50/100 J=634.799262
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 60/100 J=909.042928
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 70/100 J=1232.870172
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 80/100 J=1606.340254
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 90/100 J=2029.497673
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 100/100 J=2502.377071
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 ```
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 */
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
59 use crate::logger::*;
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
60 use crate::types::*;
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
61 use colored::{ColoredString, Colorize};
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 use core::fmt::Debug;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 use cpu_time::ProcessTime;
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
64 use serde::{Deserialize, Serialize};
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
65 use std::cell::RefCell;
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
66 use std::error::Error;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 use std::marker::PhantomData;
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
68 use std::rc::Rc;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 use std::time::Duration;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
71 /// Create the displayed presentation for log items.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
72 pub trait LogRepr: Debug {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
73 fn logrepr(&self) -> ColoredString {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
74 format!("« {self:?} »").as_str().into()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
75 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 impl LogRepr for str {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
79 fn logrepr(&self) -> ColoredString {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
80 self.into()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
81 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 impl LogRepr for String {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
85 fn logrepr(&self) -> ColoredString {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
86 self.as_str().into()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
87 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
90 impl<T> LogRepr for T
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
91 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
92 T: Num,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
93 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
94 fn logrepr(&self) -> ColoredString {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
95 format!("J={self}").as_str().into()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
96 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
99 impl<V> LogRepr for Option<V>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
100 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
101 V: LogRepr,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
102 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103 fn logrepr(&self) -> ColoredString {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 match self {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
105 None => "===missing value===".red(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
106 Some(v) => v.logrepr(),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
111 /// Helper struct for returning results annotated with an additional string to
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
112 /// [`if_verbose`][AlgIteratorState::if_verbose]. The [`LogRepr`] implementation will
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
113 /// display that string when so decided by the specific [`AlgIterator`] in use.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
114 #[derive(Debug, Clone)]
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 pub struct Annotated<F>(pub F, pub String);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
117 impl<V> LogRepr for Annotated<V>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
118 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
119 V: LogRepr,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
120 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 fn logrepr(&self) -> ColoredString {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
122 format!("{}\t| {}", self.0.logrepr(), self.1)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
123 .as_str()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
124 .into()
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128 /// Basic log item.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130 pub struct LogItem<V> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
131 pub iter: usize,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 // This causes [`csv`] to crash.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
133 //#[serde(flatten)]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
134 pub data: V,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
137 impl<V> LogItem<V> {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
138 /// Creates a new log item
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
139 fn new(iter: usize, data: V) -> Self {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
140 LogItem { iter, data }
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
141 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
144 /// State of an [`AlgIterator`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
145 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
146 /// This is the parameter obtained by the closure passed to [`AlgIterator::iterate`] or
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
147 /// [`AlgIteratorFactory::iterate`].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
148 pub trait AlgIteratorState: Sized {
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
149 /// Call `call_objective` if this is a verbose iteration.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
150 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
151 /// Verbosity depends on the [`AlgIterator`] that produced this state.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
152 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
153 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
154 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
155 /// [module documentation][self].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
156 fn if_verbose<V, E: Error>(self, calc_objective: impl FnOnce() -> V) -> Step<V, Self, E>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
158 /// Returns the current iteration count.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 fn iteration(&self) -> usize;
31
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
160
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
161 /// Indicates whether the iterator is quiet
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
162 fn is_quiet(&self) -> bool;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 /// Result of a step of an [`AlgIterator`]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166 #[derive(Debug, Serialize)]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
167 pub enum Step<V, S, Fail: Error = std::convert::Infallible> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 /// Iteration should be terminated
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 Terminated,
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
170 /// Iteration should be terminated due to failure
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
171 Failure(Fail),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 /// No result this iteration (due to verbosity settings)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 Quiet,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 /// Result of this iteration (due to verbosity settings)
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
175 Result(V, S),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
178 impl<V, S, E: Error> Step<V, S, E> {
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
179 /// Maps the value contained within the `Step`, if any, by the closure `f`.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
180 pub fn map<U>(self, mut f: impl FnMut(V) -> U) -> Step<U, S, E> {
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
181 match self {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
182 Step::Result(v, s) => Step::Result(f(v), s),
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
183 Step::Failure(e) => Step::Failure(e),
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
184 Step::Quiet => Step::Quiet,
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
185 Step::Terminated => Step::Terminated,
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
186 }
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
187 }
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
188 }
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
189
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
190 impl<V, S, E: Error> Default for Step<V, S, E> {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
191 fn default() -> Self {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
192 Step::Quiet
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
193 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
194 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
195
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
196 /// An iterator for algorithms, produced by [`AlgIteratorFactory::prepare`].
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
197 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
198 /// Typically not accessed directly, but transparently produced by an [`AlgIteratorFactory`].
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
199 /// Every [`AlgIteratorFactory`] has to implement a corresponding `AlgIterator`.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
200 pub trait AlgIterator: Sized {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
201 /// The state type
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
202 type State: AlgIteratorState;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
203 /// The output type for [`Self::poststep`] and [`Self::step`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204 type Output;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
205 /// The input type for [`Self::poststep`].
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
206 type Input;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
208 /// Advance the iterator, performing `step_fn` with the state
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
209 fn step<F, E>(&mut self, step_fn: &mut F) -> Step<Self::Output, Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
210 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
211 F: FnMut(Self::State) -> Step<Self::Input, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
212 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
213 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
214 self.prestep()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
215 .map_or(Step::Terminated, |state| self.poststep(step_fn(state)))
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
216 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
217
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
218 /// Initial stage of advancing the iterator, before the actual step
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
219 fn prestep(&mut self) -> Option<Self::State>;
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
220
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
221 /// Handle step result
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
222 fn poststep<E>(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
223 &mut self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
224 result: Step<Self::Input, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
225 ) -> Step<Self::Output, Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
226 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
227 E: Error;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
228
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229 /// Return current iteration count.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231 self.state().iteration()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
233
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
234 /// Return current state.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235 fn state(&self) -> Self::State;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
237 /// Iterate the `AlgIterator` until termination, erforming `step_fn` on each step.
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
238 ///
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
239 /// Returns either `()` or an error if the step closure terminated in [`Step::Failure`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
241 fn iterate<F, E>(&mut self, mut step_fn: F) -> Result<(), E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
242 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
243 F: FnMut(Self::State) -> Step<Self::Input, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
244 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
245 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
246 loop {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
247 match self.step(&mut step_fn) {
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
248 Step::Terminated => return Ok(()),
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
249 Step::Failure(e) => return Err(e),
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
250 _ => {}
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
251 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
252 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
253 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
254 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
255
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
256 /// A factory for producing an [`AlgIterator`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
257 ///
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
258 /// For usage instructions see the [module documentation][self].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
259 pub trait AlgIteratorFactory<V>: Sized {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
260 type Iter: AlgIterator<State = Self::State, Input = V, Output = Self::Output>;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
261
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
262 /// The state type of the corresponding [`AlgIterator`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
263 /// A reference to this is passed to the closures passed to methods such as [`Self::iterate`].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
264 type State: AlgIteratorState;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
265 /// The output type of the corresponding [`AlgIterator`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
266 /// This is the output of the closures passed to methods such as [`Self::iterate`] after
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
267 /// mappings performed by each [`AlgIterator`] implementation.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
268 type Output;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
269
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 /// Prepare an [`AlgIterator`], consuming the factory.
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
271 fn prepare(self) -> Self::Iter;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
272
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
273 /// Iterate the the closure `step`.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
274 ///
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
275 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]).
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
276 /// It should return the output of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
277 /// `state.`[`if_verbose`][AlgIteratorState::if_verbose], [`Step::Terminated`] to indicate
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
278 /// completion for other reason, or [`Step::Failure`] for termination for failure.
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
279 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
280 /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`].
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
281 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
282 fn iterate_fallible<F, E>(self, step: F) -> Result<(), E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
283 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
284 F: FnMut(Self::State) -> Step<V, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
285 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
286 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
287 self.prepare().iterate(step)
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
288 }
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
289
46
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
290 /// Iterate the closure `step`.
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
291 ///
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
292 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]),
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
293 /// It should return the output of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
294 /// `state.`[`if_verbose`][AlgIteratorState::if_verbose].
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
295 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
296 /// For usage instructions see the [module documentation][self].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
297 ///
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
298 /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`]
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
299 /// with the error type `E=`[`std::convert::Infallible`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
300 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
301 fn iterate<F>(self, step: F)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
302 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
303 F: FnMut(Self::State) -> Step<V, Self::State>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
304 {
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
305 self.iterate_fallible(step).unwrap_or_default()
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
306 }
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
307
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
308 /// Iterate the closure `step` with data produced by `datasource`.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
309 ///
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
310 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]),
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
311 /// and a data parameter taken from `datasource`. It should return the output of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
312 /// `state.`[`if_verbose`][AlgIteratorState::if_verbose], [`Step::Terminated`] to indicate
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
313 /// completion for other reason, or [`Step::Failure`] for termination for failure.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
314 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
315 /// If the `datasource` runs out of data, the iterator is considered having terminated
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
316 /// successsfully.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
317 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
318 /// For usage instructions see the [module documentation][self].
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
319 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
320 fn iterate_data_fallible<F, D, I, E>(self, mut datasource: I, mut step: F) -> Result<(), E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
321 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
322 F: FnMut(Self::State, D) -> Step<V, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
323 I: Iterator<Item = D>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
324 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
325 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
326 self.prepare().iterate(move |state| {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
327 datasource
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
328 .next()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
329 .map_or(Step::Terminated, |d| step(state, d))
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
330 })
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
331 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
332
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
333 /// Iterate the closure `step` with data produced by `datasource`.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
334 ///
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
335 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]),
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
336 /// and a data parameter taken from `datasource`. It should return the output of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
337 /// `state.`[`if_verbose`][AlgIteratorState::if_verbose].
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
338 ///
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
339 /// If the `datasource` runs out of data, the iterator is considered having terminated
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
340 /// successsfully.
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
341 ///
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
342 /// For usage instructions see the [module documentation][self].
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
343 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
344 fn iterate_data<F, D, I>(self, datasource: I, step: F)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
345 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
346 F: FnMut(Self::State, D) -> Step<V, Self::State>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
347 I: Iterator<Item = D>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
348 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
349 self.iterate_data_fallible(datasource, step)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
350 .unwrap_or_default()
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
351 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
352
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
353 // fn make_iterate<'own>(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
354 // -> Box<dyn (FnMut(dyn FnMut(&Self::State) -> Option<V>) -> ()) + 'own> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
355 // Box::new(move |step| self.iterate(step))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
356 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
357
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
358 // fn make_iterate_data<'own, D, I>(self, datasource : I)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
359 // -> Box<dyn (FnMut(dyn FnMut(&Self::State, D) -> Option<V>) -> ()) + 'own>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
360 // where I : Iterator<Item = D> + 'own {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
361 // Box::new(move |step| self.iterate_data(step, datasource))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
362 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
363
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
364 /// Add logging to the iterator produced by the factory.
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
365 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
366 /// Returns a new factory whose corresponding [`AlgIterator`] only inserts that data into the
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
367 /// log without passing it onwards.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
368 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
369 /// Use as:
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
370 /// ```rust
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
371 /// # use alg_tools::iterate::*;
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
372 /// # use alg_tools::logger::*;
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
373 /// let iter = AlgIteratorOptions::default();
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
374 /// let mut log = Logger::new();
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
375 /// iter.into_log(&mut log).iterate(|state|{
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
376 /// // perform iterations
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
377 /// state.if_verbose(||{
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
378 /// // calculate and return function value or other displayed data v
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
379 /// return 0
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
380 /// })
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
381 /// })
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
382 /// ```
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
383 fn into_log<'log>(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
384 self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
385 logger: &'log mut Logger<Self::Output>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
386 ) -> LoggingIteratorFactory<'log, Self::Output, Self>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
387 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
388 Self: Sized,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
389 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
390 LoggingIteratorFactory { base_options: self, logger }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
391 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
392
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
393 /// Map the output of the iterator produced by the factory.
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
394 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
395 /// Returns a new factory.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
396 fn mapped<U, G>(self, map: G) -> MappingIteratorFactory<G, Self>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
397 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
398 Self: Sized,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
399 G: Fn(usize, Self::Output) -> U,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
400 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
401 MappingIteratorFactory { base_options: self, map }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
402 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
403
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
404 /// Adds iteration number to the output.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
405 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
406 /// Returns a new factory.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
407 /// Typically followed by [`Self::into_log`].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
408 fn with_iteration_number(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
409 self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
410 ) -> MappingIteratorFactory<fn(usize, Self::Output) -> LogItem<Self::Output>, Self>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
411 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
412 Self: Sized,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
413 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
414 self.mapped(LogItem::new)
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
415 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
416
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
417 /// Add timing to the iterator produced by the factory.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
418 fn timed(self) -> TimingIteratorFactory<Self>
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
419 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
420 Self: Sized,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
421 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
422 TimingIteratorFactory(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
423 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
424
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
425 /// Add value stopping threshold to the iterator produce by the factory
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
426 fn stop_target(self, target: Self::Output) -> ValueIteratorFactory<Self::Output, Self>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
427 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
428 Self: Sized,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
429 Self::Output: Num,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
430 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
431 ValueIteratorFactory { base_options: self, target: target }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
432 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
433
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
434 /// Add stall stopping to the iterator produce by the factory
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
435 fn stop_stall(self, stall: Self::Output) -> StallIteratorFactory<Self::Output, Self>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
436 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
437 Self: Sized,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
438 Self::Output: Num,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
439 {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
440 StallIteratorFactory { base_options: self, stall: stall }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
441 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
442
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
443 /// Is the iterator quiet, i.e., on-verbose?
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
444 fn is_quiet(&self) -> bool {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
445 false
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
446 }
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
447
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
448 /// Returns an an [`std::iter::Iterator`] that can be used in a `for`-loop.
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
449 fn iter(self) -> AlgIteratorIterator<Self::Iter> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
450 AlgIteratorIterator { algi: Rc::new(RefCell::new(self.prepare())) }
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
451 }
46
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
452
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
453 /// Returns an an [`std::iter::Iterator`] that can be used in a `for`-loop,
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
454 /// also inputting an initial iteration status calculated by `f` if needed.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
455 fn iter_init(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
456 self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
457 f: impl FnOnce() -> <Self::Iter as AlgIterator>::Input,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
458 ) -> AlgIteratorIterator<Self::Iter> {
46
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
459 let mut i = self.prepare();
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
460 let st = i.state();
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
461 let step: Step<<Self::Iter as AlgIterator>::Input, Self::State> = st.if_verbose(f);
46
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
462 i.poststep(step);
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
463 AlgIteratorIterator { algi: Rc::new(RefCell::new(i)) }
46
bd924d62d952 Add iter_init
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
464 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
465 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
466
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
467 /// Options for [`BasicAlgIteratorFactory`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
468 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
469 /// Use as:
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
470 /// ```
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
471 /// # use alg_tools::iterate::*;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
472 /// let iter = AlgIteratorOptions{ max_iter : 10000, .. Default::default() };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
473 /// ```
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
474 #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
475 pub struct AlgIteratorOptions {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
476 /// Maximum number of iterations
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
477 pub max_iter: usize,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
478 /// Number of iterations between verbose iterations that display state.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
479 pub verbose_iter: Verbose,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
480 /// Whether verbose iterations are displayed, or just passed onwards to a containing
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
481 /// `AlgIterator`.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
482 pub quiet: bool,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
483 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
484
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
485 #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
486 pub enum Verbose {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
487 /// Be verbose every $n$ iterations.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
488 Every(usize),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
489 /// Be verbose every $n$ iterations and initial $m$ iterations.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
490 EveryAndInitial { every: usize, initial: usize },
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
491 /// Be verbose if iteration number $n$ divides by $b^{\text{floor}(\log_b(n))}$, where
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
492 /// $b$ is indicated logarithmic base. So, with $b=10$,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
493 /// * every iteration for first 10 iterations,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
494 /// * every 10 iterations from there on until 100 iterations,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
495 /// * every 100 iteartinos frmo there on until 1000 iterations, etc.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
496 Logarithmic(usize),
87
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
497 /// Same as `Logarithmic`, but $\log_b(n)$ is replaced by $min\{c, \log_b(n)\}$ where $c$
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
498 /// is the given `cap`. For example, with `base=10` and `cap=2`, the first ten iterations
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
499 /// will be output, then every tenth iteration, and after 100 iterations, every 100th iteration,
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
500 /// without further logarithmic progression.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
501 LogarithmicCap { base: usize, cap: u32 },
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
502 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
503
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
504 impl Verbose {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
505 /// Indicates whether given iteration number is verbose
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
506 pub fn is_verbose(&self, iter: usize) -> bool {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
507 match self {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
508 &Verbose::Every(every) => every != 0 && iter % every == 0,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
509 &Verbose::EveryAndInitial { every, initial } => {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
510 iter <= initial || (every != 0 && iter % every == 0)
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
511 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
512 &Verbose::Logarithmic(base) => {
25
d14c877e14b7 Logarithmic logging base correction
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
513 let every = base.pow((iter as float).log(base as float).floor() as u32);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
514 iter % every == 0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
515 }
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
516 &Verbose::LogarithmicCap { base, cap } => {
87
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
517 let every = base.pow(((iter as float).log(base as float).floor() as u32).min(cap));
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
518 iter % every == 0
72968cf30033 LogarithmicCap verbosity option
Tuomo Valkonen <tuomov@iki.fi>
parents: 51
diff changeset
519 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
520 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
521 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
522 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
523
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
524 impl Default for AlgIteratorOptions {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
525 fn default() -> AlgIteratorOptions {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
526 AlgIteratorOptions {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
527 max_iter: 1000,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
528 verbose_iter: Verbose::EveryAndInitial { every: 100, initial: 10 },
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
529 quiet: false,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
530 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
531 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
532 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
533
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
534 /// State of a `BasicAlgIterator`
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
535 #[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)]
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
536 pub struct BasicState {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
537 /// Current iteration
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
538 iter: usize,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
539 /// Whether the iteration is verbose, i.e., results should be displayed.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
540 /// Requires `calc` to be `true`.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
541 verbose: bool,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
542 /// Whether results should be calculated.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
543 calc: bool,
31
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
544 /// Indicates whether the iteration is quiet
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
545 quiet: bool,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
546 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
547
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
548 /// [`AlgIteratorFactory`] for [`BasicAlgIterator`]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
549 #[derive(Clone, Debug)]
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
550 pub struct BasicAlgIteratorFactory<V> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
551 options: AlgIteratorOptions,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
552 _phantoms: PhantomData<V>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
553 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
554
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
555 /// The simplest [`AlgIterator`], created by [`BasicAlgIteratorFactory`]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
556 #[derive(Clone, Debug)]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
557 pub struct BasicAlgIterator<V> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
558 options: AlgIteratorOptions,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
559 iter: usize,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
560 _phantoms: PhantomData<V>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
561 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
562
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
563 impl AlgIteratorOptions {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
564 /// [`AlgIteratorOptions`] is directly a factory for [`BasicAlgIterator`],
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
565 /// however, due to type inference issues, it may become convenient to instantiate
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
566 /// it to a specific return type for the inner step function. This method does that.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
567 pub fn instantiate<V>(&self) -> BasicAlgIteratorFactory<V> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
568 BasicAlgIteratorFactory { options: self.clone(), _phantoms: PhantomData }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
569 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
570 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
571
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
572 impl<V> AlgIteratorFactory<V> for AlgIteratorOptions
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
573 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
574 V: LogRepr,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
575 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
576 type State = BasicState;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
577 type Iter = BasicAlgIterator<V>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
578 type Output = V;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
579
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
580 fn prepare(self) -> Self::Iter {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
581 BasicAlgIterator { options: self, iter: 0, _phantoms: PhantomData }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
582 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
583
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
584 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
585 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
586 self.quiet
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
587 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
588 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
589
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
590 impl<V> AlgIteratorFactory<V> for BasicAlgIteratorFactory<V>
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
591 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
592 V: LogRepr,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
593 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
594 type State = BasicState;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
595 type Iter = BasicAlgIterator<V>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
596 type Output = V;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
597
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
598 fn prepare(self) -> Self::Iter {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
599 BasicAlgIterator { options: self.options, iter: 0, _phantoms: PhantomData }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
600 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
601
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
602 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
603 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
604 self.options.quiet
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
605 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
606 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
607
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
608 impl<V> AlgIterator for BasicAlgIterator<V>
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
609 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
610 V: LogRepr,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
611 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
612 type State = BasicState;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
613 type Output = V;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
614 type Input = V;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
615
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
616 #[inline]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
617 fn prestep(&mut self) -> Option<Self::State> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
618 if self.iter >= self.options.max_iter {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
619 None
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
620 } else {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
621 self.iter += 1;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
622 Some(self.state())
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
623 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
624 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
625
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
626 fn poststep<E: Error>(&mut self, res: Step<V, Self::State, E>) -> Step<V, Self::State, E> {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
627 if let Step::Result(ref val, ref state) = res {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
628 if state.verbose && !self.options.quiet {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
629 println!(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
630 "{}{}/{} {}{}",
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
631 "".dimmed(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
632 state.iter,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
633 self.options.max_iter,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
634 val.logrepr(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
635 "".clear()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
636 );
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
637 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
638 }
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
639 res
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
640 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
641
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
642 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
643 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
644 self.iter
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
645 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
646
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
647 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
648 fn state(&self) -> BasicState {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
649 let iter = self.iter;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
650 let verbose = self.options.verbose_iter.is_verbose(iter);
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
651 BasicState { iter: iter, verbose: verbose, calc: verbose, quiet: self.options.quiet }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
652 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
653 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
654
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
655 impl AlgIteratorState for BasicState {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
656 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
657 fn if_verbose<V, E: Error>(self, calc_objective: impl FnOnce() -> V) -> Step<V, Self, E> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
658 if self.calc {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
659 Step::Result(calc_objective(), self)
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
660 } else {
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
661 Step::Quiet
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
662 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
663 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
664
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
665 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
666 fn iteration(&self) -> usize {
31
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
667 self.iter
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
668 }
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
669
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
670 #[inline]
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
671 fn is_quiet(&self) -> bool {
50a77e4efcbb Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
672 self.quiet
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
673 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
674 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
675
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
676 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
677 // Stall detecting iteration function.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
678 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
679
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
680 /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that detects “stall”.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
681 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
682 /// We define stall as $(v_{k+n}-v_k)/v_k ≤ θ$, where $n$ the distance between
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
683 /// [`Step::Result`] iterations, and $θ$ is the provided `stall` parameter.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
684 #[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)]
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
685 pub struct StallIteratorFactory<U: Num, BaseFactory> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
686 /// An [`AlgIteratorFactory`] on which to build on
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
687 pub base_options: BaseFactory,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
688 /// Stalling threshold $θ$.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
689 pub stall: U,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
690 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
691
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
692 /// Iterator produced by [`StallIteratorFactory`].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
693 pub struct StallIterator<U: Num, BaseIterator> {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
694 base_iterator: BaseIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
695 stall: U,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
696 previous_value: Option<U>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
697 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
698
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
699 impl<V, U, BaseFactory> AlgIteratorFactory<V> for StallIteratorFactory<U, BaseFactory>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
700 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
701 BaseFactory: AlgIteratorFactory<V, Output = U>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
702 U: SignedNum + PartialOrd,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
703 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
704 type Iter = StallIterator<U, BaseFactory::Iter>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
705 type State = BaseFactory::State;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
706 type Output = BaseFactory::Output;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
707
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
708 fn prepare(self) -> Self::Iter {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
709 StallIterator {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
710 base_iterator: self.base_options.prepare(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
711 stall: self.stall,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
712 previous_value: None,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
713 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
714 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
715
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
716 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
717 self.base_options.is_quiet()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
718 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
719 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
720
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
721 impl<U, BaseIterator> AlgIterator for StallIterator<U, BaseIterator>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
722 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
723 BaseIterator: AlgIterator<Output = U>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
724 U: SignedNum + PartialOrd,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
725 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
726 type State = BaseIterator::State;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
727 type Output = U;
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
728 type Input = BaseIterator::Input;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
729
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
730 #[inline]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
731 fn prestep(&mut self) -> Option<Self::State> {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
732 self.base_iterator.prestep()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
733 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
734
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
735 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
736 fn poststep<E>(&mut self, res: Step<Self::Input, Self::State, E>) -> Step<U, Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
737 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
738 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
739 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
740 match self.base_iterator.poststep(res) {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
741 Step::Result(nv, state) => {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
742 let previous_v = self.previous_value;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
743 self.previous_value = Some(nv);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
744 match previous_v {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
745 Some(pv) if (nv - pv).abs() <= self.stall * pv.abs() => Step::Terminated,
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
746 _ => Step::Result(nv, state),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
747 }
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
748 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
749 val => val,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
750 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
751 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
752
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
753 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
754 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
755 self.base_iterator.iteration()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
756 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
757
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
758 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
759 fn state(&self) -> Self::State {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
760 self.base_iterator.state()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
761 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
762 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
763
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
764 /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that detect whether step function
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
765 /// return value is less than `target`, and terminates if it is.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
766 #[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)]
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
767 pub struct ValueIteratorFactory<U: Num, BaseFactory> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
768 /// An [`AlgIteratorFactory`] on which to build on
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
769 pub base_options: BaseFactory,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
770 /// Target value
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
771 pub target: U,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
772 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
773
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
774 /// Iterator produced by [`ValueIteratorFactory`].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
775 pub struct ValueIterator<U: Num, BaseIterator> {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
776 base_iterator: BaseIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
777 target: U,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
778 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
779
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
780 impl<V, U, BaseFactory> AlgIteratorFactory<V> for ValueIteratorFactory<U, BaseFactory>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
781 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
782 BaseFactory: AlgIteratorFactory<V, Output = U>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
783 U: SignedNum + PartialOrd,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
784 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
785 type Iter = ValueIterator<U, BaseFactory::Iter>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
786 type State = BaseFactory::State;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
787 type Output = BaseFactory::Output;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
788
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
789 fn prepare(self) -> Self::Iter {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
790 ValueIterator { base_iterator: self.base_options.prepare(), target: self.target }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
791 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
792
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
793 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
794 self.base_options.is_quiet()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
795 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
796 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
797
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
798 impl<U, BaseIterator> AlgIterator for ValueIterator<U, BaseIterator>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
799 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
800 BaseIterator: AlgIterator<Output = U>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
801 U: SignedNum + PartialOrd,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
802 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
803 type State = BaseIterator::State;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
804 type Output = U;
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
805 type Input = BaseIterator::Input;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
806
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
807 #[inline]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
808 fn prestep(&mut self) -> Option<Self::State> {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
809 self.base_iterator.prestep()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
810 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
811
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
812 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
813 fn poststep<E>(&mut self, res: Step<Self::Input, Self::State, E>) -> Step<U, Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
814 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
815 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
816 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
817 match self.base_iterator.poststep(res) {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
818 Step::Result(v, state) => {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
819 if v <= self.target {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
820 Step::Terminated
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
821 } else {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
822 Step::Result(v, state)
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
823 }
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
824 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
825 val => val,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
826 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
827 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
828
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
829 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
830 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
831 self.base_iterator.iteration()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
832 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
833
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
834 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
835 fn state(&self) -> Self::State {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
836 self.base_iterator.state()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
837 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
838 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
839
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
840 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
841 // Logging iterator
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
842 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
843
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
844 /// [`AlgIteratorFactory`] for a logging [`AlgIterator`].
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
845 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
846 /// Typically produced with [`AlgIteratorFactory::into_log`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
847 /// The `Output` of the corresponding [`LoggingIterator`] is `()`:
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
848 #[derive(Debug)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
849 pub struct LoggingIteratorFactory<'log, U, BaseFactory> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
850 /// Base [`AlgIteratorFactory`] on which to build
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
851 base_options: BaseFactory,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
852 /// The `Logger` to use.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
853 logger: &'log mut Logger<U>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
854 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
855
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
856 /// Iterator produced by `LoggingIteratorFactory`.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
857 pub struct LoggingIterator<'log, U, BaseIterator> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
858 base_iterator: BaseIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
859 logger: &'log mut Logger<U>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
860 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
861
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
862 impl<'log, V, BaseFactory> AlgIteratorFactory<V>
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
863 for LoggingIteratorFactory<'log, BaseFactory::Output, BaseFactory>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
864 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
865 BaseFactory: AlgIteratorFactory<V>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
866 BaseFactory::Output: 'log,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
867 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
868 type State = BaseFactory::State;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
869 type Iter = LoggingIterator<'log, BaseFactory::Output, BaseFactory::Iter>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
870 type Output = ();
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
871
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
872 fn prepare(self) -> Self::Iter {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
873 LoggingIterator { base_iterator: self.base_options.prepare(), logger: self.logger }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
874 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
875
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
876 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
877 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
878 self.base_options.is_quiet()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
879 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
880 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
881
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
882 impl<'log, BaseIterator> AlgIterator for LoggingIterator<'log, BaseIterator::Output, BaseIterator>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
883 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
884 BaseIterator: AlgIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
885 BaseIterator::Output: 'log,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
886 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
887 type State = BaseIterator::State;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
888 type Output = ();
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
889 type Input = BaseIterator::Input;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
890
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
891 #[inline]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
892 fn prestep(&mut self) -> Option<Self::State> {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
893 self.base_iterator.prestep()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
894 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
895
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
896 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
897 fn poststep<E>(&mut self, res: Step<Self::Input, Self::State, E>) -> Step<(), Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
898 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
899 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
900 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
901 match self.base_iterator.poststep(res) {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
902 Step::Result(v, _) => {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
903 self.logger.log(v);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
904 Step::Quiet
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
905 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
906 Step::Quiet => Step::Quiet,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
907 Step::Terminated => Step::Terminated,
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
908 Step::Failure(e) => Step::Failure(e),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
909 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
910 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
911
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
912 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
913 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
914 self.base_iterator.iteration()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
915 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
916
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
917 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
918 fn state(&self) -> Self::State {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
919 self.base_iterator.state()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
920 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
921 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
922
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
923 /// This [`AlgIteratorFactory`] allows output mapping.
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
924 ///
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
925 /// Typically produced with [`AlgIteratorFactory::mapped`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
926 #[derive(Debug)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
927 pub struct MappingIteratorFactory<G, BaseFactory> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
928 /// Base [`AlgIteratorFactory`] on which to build
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
929 base_options: BaseFactory,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
930 /// A closure `G : Fn(usize, BaseFactory::Output) -> U` that gets the current iteration
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
931 /// and the output of the base factory as input, and produces a new output.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
932 map: G,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
933 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
934
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
935 /// [`AlgIterator`] produced by [`MappingIteratorFactory`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
936 pub struct MappingIterator<G, BaseIterator> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
937 base_iterator: BaseIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
938 map: G,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
939 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
940
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
941 impl<V, U, G, BaseFactory> AlgIteratorFactory<V> for MappingIteratorFactory<G, BaseFactory>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
942 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
943 BaseFactory: AlgIteratorFactory<V>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
944 G: Fn(usize, BaseFactory::Output) -> U,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
945 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
946 type State = BaseFactory::State;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
947 type Iter = MappingIterator<G, BaseFactory::Iter>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
948 type Output = U;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
949
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
950 fn prepare(self) -> Self::Iter {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
951 MappingIterator { base_iterator: self.base_options.prepare(), map: self.map }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
952 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
953
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
954 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
955 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
956 self.base_options.is_quiet()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
957 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
958 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
959
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
960 impl<U, G, BaseIterator> AlgIterator for MappingIterator<G, BaseIterator>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
961 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
962 BaseIterator: AlgIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
963 G: Fn(usize, BaseIterator::Output) -> U,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
964 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
965 type State = BaseIterator::State;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
966 type Output = U;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
967 type Input = BaseIterator::Input;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
968
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
969 #[inline]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
970 fn prestep(&mut self) -> Option<Self::State> {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
971 self.base_iterator.prestep()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
972 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
973
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
974 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
975 fn poststep<E>(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
976 &mut self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
977 res: Step<Self::Input, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
978 ) -> Step<Self::Output, Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
979 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
980 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
981 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
982 match self.base_iterator.poststep(res) {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
983 Step::Result(v, state) => Step::Result((self.map)(self.iteration(), v), state),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
984 Step::Quiet => Step::Quiet,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
985 Step::Terminated => Step::Terminated,
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
986 Step::Failure(e) => Step::Failure(e),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
987 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
988 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
989
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
990 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
991 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
992 self.base_iterator.iteration()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
993 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
994
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
995 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
996 fn state(&self) -> Self::State {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
997 self.base_iterator.state()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
998 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
999 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1000
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1001 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1002 // Timing iterator
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1003 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1004
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
1005 /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that adds spent CPU time to verbose events.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1006 #[derive(Debug)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1007 pub struct TimingIteratorFactory<BaseFactory>(pub BaseFactory);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1008
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1009 /// Iterator produced by [`TimingIteratorFactory`]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1010 #[derive(Debug)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1011 pub struct TimingIterator<BaseIterator> {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1012 base_iterator: BaseIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1013 start_time: ProcessTime,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1014 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1015
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1016 /// Data `U` with production time attached
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1017 #[derive(Copy, Clone, Debug, Serialize)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1018 pub struct Timed<U> {
51
92ef745ec8db Add iteration to Timed log
Tuomo Valkonen <tuomov@iki.fi>
parents: 46
diff changeset
1019 /// CPU time taken
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1020 pub cpu_time: Duration,
51
92ef745ec8db Add iteration to Timed log
Tuomo Valkonen <tuomov@iki.fi>
parents: 46
diff changeset
1021 /// Iteration number
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1022 pub iter: usize,
51
92ef745ec8db Add iteration to Timed log
Tuomo Valkonen <tuomov@iki.fi>
parents: 46
diff changeset
1023 /// User data
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1024 //#[serde(flatten)]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1025 pub data: U,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1026 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1027
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1028 impl<T> LogRepr for Timed<T>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1029 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1030 T: LogRepr,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1031 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1032 fn logrepr(&self) -> ColoredString {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1033 format!(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1034 "[{:.3}s] {}",
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1035 self.cpu_time.as_secs_f64(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1036 self.data.logrepr()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1037 )
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1038 .as_str()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1039 .into()
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1040 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1041 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1042
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1043 impl<V, BaseFactory> AlgIteratorFactory<V> for TimingIteratorFactory<BaseFactory>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1044 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1045 BaseFactory: AlgIteratorFactory<V>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1046 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1047 type State = BaseFactory::State;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1048 type Iter = TimingIterator<BaseFactory::Iter>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1049 type Output = Timed<BaseFactory::Output>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1050
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1051 fn prepare(self) -> Self::Iter {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1052 TimingIterator { base_iterator: self.0.prepare(), start_time: ProcessTime::now() }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1053 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1054
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1055 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1056 fn is_quiet(&self) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1057 self.0.is_quiet()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1058 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1059 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1060
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1061 impl<BaseIterator> AlgIterator for TimingIterator<BaseIterator>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1062 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1063 BaseIterator: AlgIterator,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1064 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1065 type State = BaseIterator::State;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1066 type Output = Timed<BaseIterator::Output>;
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1067 type Input = BaseIterator::Input;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1068
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1069 #[inline]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1070 fn prestep(&mut self) -> Option<Self::State> {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1071 self.base_iterator.prestep()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1072 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1073
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1074 #[inline]
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1075 fn poststep<E>(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1076 &mut self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1077 res: Step<Self::Input, Self::State, E>,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1078 ) -> Step<Self::Output, Self::State, E>
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1079 where
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1080 E: Error,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1081 {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1082 match self.base_iterator.poststep(res) {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1083 Step::Result(data, state) => Step::Result(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1084 Timed { cpu_time: self.start_time.elapsed(), iter: self.iteration(), data },
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1085 state,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1086 ),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1087 Step::Quiet => Step::Quiet,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1088 Step::Terminated => Step::Terminated,
3
20db884b7028 Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
1089 Step::Failure(e) => Step::Failure(e),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1090 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1091 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1092
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1093 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1094 fn iteration(&self) -> usize {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1095 self.base_iterator.iteration()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1096 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1097
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1098 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1099 fn state(&self) -> Self::State {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1100 self.base_iterator.state()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1101 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1102 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1103
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1104 //
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1105 // New for-loop interface
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1106 //
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1107
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1108 pub struct AlgIteratorIterator<I: AlgIterator> {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1109 algi: Rc<RefCell<I>>,
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1110 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1111
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1112 pub struct AlgIteratorIteration<I: AlgIterator> {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1113 state: I::State,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1114 algi: Rc<RefCell<I>>,
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1115 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1116
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1117 impl<I: AlgIterator> std::iter::Iterator for AlgIteratorIterator<I> {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1118 type Item = AlgIteratorIteration<I>;
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1119
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1120 fn next(&mut self) -> Option<Self::Item> {
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1121 let algi = self.algi.clone();
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1122 RefCell::borrow_mut(&self.algi)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1123 .prestep()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1124 .map(|state| AlgIteratorIteration { state, algi })
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1125 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1126 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1127
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1128 /// Types of errors that may occur
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1129 #[derive(Debug, PartialEq, Eq)]
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1130 pub enum IterationError {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1131 /// [`AlgIteratorIteration::if_verbose_check`] is not called in iteration order.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1132 ReportingOrderingError,
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1133 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1134
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1135 impl<I: AlgIterator> AlgIteratorIteration<I> {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1136 /// Call `call_objective` if this is a verbose iteration.
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1137 ///
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1138 /// Verbosity depends on the [`AlgIterator`] that produced this state.
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1139 ///
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1140 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1141 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1142 /// [module documentation][self].
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1143 ///
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1144 /// This function may panic if result reporting is not ordered correctly (an unlikely mistake
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1145 /// if using this facility correctly). For a version that propagates errors, see
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1146 /// [`Self::if_verbose_check`].
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1147 pub fn if_verbose(self, calc_objective: impl FnOnce() -> I::Input) {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1148 self.if_verbose_check(calc_objective).unwrap()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1149 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1150
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1151 /// Version of [`Self::if_verbose`] that propagates errors instead of panicking.
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1152 pub fn if_verbose_check(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1153 self,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1154 calc_objective: impl FnOnce() -> I::Input,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1155 ) -> Result<(), IterationError> {
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1156 let mut algi = match RefCell::try_borrow_mut(&self.algi) {
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1157 Err(_) => return Err(IterationError::ReportingOrderingError),
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1158 Ok(algi) => algi,
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1159 };
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1160 if self.state.iteration() != algi.iteration() {
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1161 Err(IterationError::ReportingOrderingError)
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1162 } else {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1163 let res: Step<I::Input, I::State, std::convert::Infallible> =
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1164 self.state.if_verbose(calc_objective);
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1165 algi.poststep(res);
40
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1166 Ok(())
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1167 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1168 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1169
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1170 /// Returns the current iteration count.
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1171 pub fn iteration(&self) -> usize {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1172 self.state.iteration()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1173 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1174
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1175 /// Indicates whether the iterator is quiet
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1176 pub fn is_quiet(&self) -> bool {
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1177 self.state.is_quiet()
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1178 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1179 }
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1180
daf0e3a70c79 New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents: 38
diff changeset
1181 //
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1182 // Tests
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1183 //
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1184
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1185 #[cfg(test)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1186 mod tests {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1187 use super::*;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1188 use crate::logger::Logger;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1189 #[test]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1190 fn iteration() {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1191 let options = AlgIteratorOptions {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1192 max_iter: 10,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1193 verbose_iter: Verbose::Every(3),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1194 ..Default::default()
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1195 };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1196
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1197 {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1198 let mut start = 1 as int;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1199 options.iterate(|state| {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1200 start = start * 2;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1201 state.if_verbose(|| start)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1202 });
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1203 assert_eq!(start, (2 as int).pow(10));
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1204 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1205
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1206 {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1207 let mut start = 1 as int;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1208 let mut log = Logger::new();
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1209 let factory = options
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1210 .instantiate()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1211 .with_iteration_number()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1212 .into_log(&mut log);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1213 factory.iterate(|state| {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1214 start = start * 2;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1215 state.if_verbose(|| start)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1216 });
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1217 assert_eq!(start, (2 as int).pow(10));
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1218 assert_eq!(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1219 log.data()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1220 .iter()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1221 .map(|LogItem { data: v, iter: _ }| v.clone())
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1222 .collect::<Vec<int>>(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1223 (1..10)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1224 .map(|i| (2 as int).pow(i))
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1225 .skip(2)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1226 .step_by(3)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1227 .collect::<Vec<int>>()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1228 )
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1229 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1230 }
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1231
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1232 #[test]
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1233 fn iteration_for_loop() {
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1234 let options = AlgIteratorOptions {
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1235 max_iter: 10,
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1236 verbose_iter: Verbose::Every(3),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1237 ..Default::default()
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1238 };
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1239
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1240 {
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1241 let mut start = 1 as int;
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1242 for state in options.iter() {
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1243 start = start * 2;
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1244 state.if_verbose(|| start)
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1245 }
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1246 assert_eq!(start, (2 as int).pow(10));
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1247 }
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1248
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1249 {
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1250 let mut start = 1 as int;
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1251 let mut log = Logger::new();
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1252 let factory = options
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1253 .instantiate()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1254 .with_iteration_number()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1255 .into_log(&mut log);
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1256 for state in factory.iter() {
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1257 start = start * 2;
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1258 state.if_verbose(|| start)
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1259 }
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1260 assert_eq!(start, (2 as int).pow(10));
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1261 assert_eq!(
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1262 log.data()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1263 .iter()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1264 .map(|LogItem { data: v, iter: _ }| v.clone())
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1265 .collect::<Vec<int>>(),
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1266 (1..10)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1267 .map(|i| (2 as int).pow(i))
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1268 .skip(2)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1269 .step_by(3)
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1270 .collect::<Vec<int>>()
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
1271 )
41
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1272 }
121cf065e9ed Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents: 40
diff changeset
1273 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1274 }

mercurial