| 39:dd144a972722 | 40:daf0e3a70c79 |
|---|---|
| 50 use serde::{Serialize, Deserialize}; | 50 use serde::{Serialize, Deserialize}; |
| 51 use cpu_time::ProcessTime; | 51 use cpu_time::ProcessTime; |
| 52 use std::marker::PhantomData; | 52 use std::marker::PhantomData; |
| 53 use std::time::Duration; | 53 use std::time::Duration; |
| 54 use std::error::Error; | 54 use std::error::Error; |
| 55 use std::rc::Rc; | |
| 56 use std::cell::Cell; | |
| 55 use crate::types::*; | 57 use crate::types::*; |
| 56 use crate::logger::*; | 58 use crate::logger::*; |
| 57 | 59 |
| 58 /// Create the displayed presentation for log items. | 60 /// Create the displayed presentation for log items. |
| 59 pub trait LogRepr : Debug { | 61 pub trait LogRepr : Debug { |
| 112 | 114 |
| 113 /// State of an [`AlgIterator`]. | 115 /// State of an [`AlgIterator`]. |
| 114 /// | 116 /// |
| 115 /// This is the parameter obtained by the closure passed to [`AlgIterator::iterate`] or | 117 /// This is the parameter obtained by the closure passed to [`AlgIterator::iterate`] or |
| 116 /// [`AlgIteratorFactory::iterate`]. | 118 /// [`AlgIteratorFactory::iterate`]. |
| 117 pub trait AlgIteratorState { | 119 pub trait AlgIteratorState : Sized { |
| 118 /// Call `call_objective` if this is a verbose iteration. | 120 /// Call `call_objective` if this is a verbose iteration. |
| 119 /// | 121 /// |
| 120 /// Verbosity depends on the [`AlgIterator`] that produced this state. | 122 /// Verbosity depends on the [`AlgIterator`] that produced this state. |
| 121 /// | 123 /// |
| 122 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted | 124 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted |
| 123 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the | 125 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the |
| 124 /// [module documentation][self]. | 126 /// [module documentation][self]. |
| 125 fn if_verbose<V, E : Error>(&self, calc_objective : impl FnMut() -> V) -> Step<V, E>; | 127 fn if_verbose<V, E : Error>(self, calc_objective : impl FnMut() -> V) -> Step<V, Self, E>; |
| 126 | 128 |
| 127 /// Returns the current iteration count. | 129 /// Returns the current iteration count. |
| 128 fn iteration(&self) -> usize; | 130 fn iteration(&self) -> usize; |
| 129 | 131 |
| 130 /// Indicates whether the iterator is quiet | 132 /// Indicates whether the iterator is quiet |
| 131 fn is_quiet(&self) -> bool; | 133 fn is_quiet(&self) -> bool; |
| 132 } | 134 } |
| 133 | 135 |
| 134 /// Result of a step of an [`AlgIterator`] | 136 /// Result of a step of an [`AlgIterator`] |
| 135 #[derive(Debug, Serialize)] | 137 #[derive(Debug, Serialize)] |
| 136 pub enum Step<V, Fail : Error = std::convert::Infallible> { | 138 pub enum Step<V, S, Fail : Error = std::convert::Infallible> { |
| 137 /// Iteration should be terminated | 139 /// Iteration should be terminated |
| 138 Terminated, | 140 Terminated, |
| 139 /// Iteration should be terminated due to failure | 141 /// Iteration should be terminated due to failure |
| 140 Failure(Fail), | 142 Failure(Fail), |
| 141 /// No result this iteration (due to verbosity settings) | 143 /// No result this iteration (due to verbosity settings) |
| 142 Quiet, | 144 Quiet, |
| 143 /// Result of this iteration (due to verbosity settings) | 145 /// Result of this iteration (due to verbosity settings) |
| 144 Result(V), | 146 Result(V, S), |
| 145 } | 147 } |
| 146 | 148 |
| 147 impl<V, E : Error> Step<V, E> { | 149 impl<V, S, E : Error> Step<V, S, E> { |
| 148 /// Maps the value contained within the `Step`, if any, by the closure `f`. | 150 /// Maps the value contained within the `Step`, if any, by the closure `f`. |
| 149 pub fn map<U>(self, mut f : impl FnMut(V) -> U) -> Step<U, E> { | 151 pub fn map<U>(self, mut f : impl FnMut(V) -> U) -> Step<U, S, E> { |
| 150 match self { | 152 match self { |
| 151 Step::Result(v) => Step::Result(f(v)), | 153 Step::Result(v, s) => Step::Result(f(v), s), |
| 152 Step::Failure(e) => Step::Failure(e), | 154 Step::Failure(e) => Step::Failure(e), |
| 153 Step::Quiet => Step::Quiet, | 155 Step::Quiet => Step::Quiet, |
| 154 Step::Terminated => Step::Terminated, | 156 Step::Terminated => Step::Terminated, |
| 155 } | 157 } |
| 158 } | |
| 159 } | |
| 160 | |
| 161 impl<V, S, E : Error> Default for Step<V, S, E> { | |
| 162 fn default() -> Self { | |
| 163 Step::Quiet | |
| 156 } | 164 } |
| 157 } | 165 } |
| 158 | 166 |
| 159 /// An iterator for algorithms, produced by [`AlgIteratorFactory::prepare`]. | 167 /// An iterator for algorithms, produced by [`AlgIteratorFactory::prepare`]. |
| 160 /// | 168 /// |
| 161 /// Typically not accessed directly, but transparently produced by an [`AlgIteratorFactory`]. | 169 /// Typically not accessed directly, but transparently produced by an [`AlgIteratorFactory`]. |
| 162 /// Every [`AlgIteratorFactory`] has to implement a corresponding `AlgIterator`. | 170 /// Every [`AlgIteratorFactory`] has to implement a corresponding `AlgIterator`. |
| 163 pub trait AlgIterator : Sized { | 171 pub trait AlgIterator : Sized { |
| 164 /// The state type | 172 /// The state type |
| 165 type State : AlgIteratorState; | 173 type State : AlgIteratorState; |
| 166 /// The output type for [`Self::step`]. | 174 /// The output type for [`Self::poststep`] and [`Self::step`]. |
| 167 type Output; | 175 type Output; |
| 168 /// The error type for [`Self::step`] and [`Self::iterate`]. | 176 /// The input type for [`Self::poststep`]. |
| 169 type Err : Error; | 177 type Input; |
| 170 | 178 |
| 171 /// Advance the iterator. | 179 /// Advance the iterator, performing `step_fn` with the state |
| 172 fn step(&mut self) -> Step<Self::Output, Self::Err>; | 180 fn step<F, E>(&mut self, step_fn : &mut F) -> Step<Self::Output, Self::State, E> |
| 181 where F : FnMut(Self::State) -> Step<Self::Input, Self::State, E>, | |
| 182 E : Error { | |
| 183 self.prestep().map_or(Step::Terminated, | |
| 184 |state| self.poststep(step_fn(state))) | |
| 185 } | |
| 186 | |
| 187 /// Initial stage of advancing the iterator, before the actual step | |
| 188 fn prestep(&mut self) -> Option<Self::State>; | |
| 189 | |
| 190 /// Handle step result | |
| 191 fn poststep<E>(&mut self, result : Step<Self::Input, Self::State, E>) | |
| 192 -> Step<Self::Output, Self::State, E> | |
| 193 where E : Error; | |
| 173 | 194 |
| 174 /// Return current iteration count. | 195 /// Return current iteration count. |
| 175 fn iteration(&self) -> usize { | 196 fn iteration(&self) -> usize { |
| 176 self.state().iteration() | 197 self.state().iteration() |
| 177 } | 198 } |
| 178 | 199 |
| 179 /// Return current state. | 200 /// Return current state. |
| 180 fn state(&self) -> Self::State; | 201 fn state(&self) -> Self::State; |
| 181 | 202 |
| 182 /// Iterate the `AlgIterator` until termination. | 203 /// Iterate the `AlgIterator` until termination, erforming `step_fn` on each step. |
| 183 /// | 204 /// |
| 184 /// Returns either `()` or an error if the step closure terminated in [`Step::Failure“]. | 205 /// Returns either `()` or an error if the step closure terminated in [`Step::Failure“]. |
| 185 #[inline] | 206 #[inline] |
| 186 fn iterate(&mut self) -> Result<(), Self::Err> { | 207 fn iterate<F, E>(&mut self, mut step_fn : F) -> Result<(), E> |
| 208 where F : FnMut(Self::State) -> Step<Self::Input, Self::State, E>, | |
| 209 E : Error { | |
| 187 loop { | 210 loop { |
| 188 match self.step() { | 211 match self.step(&mut step_fn) { |
| 189 Step::Terminated => return Ok(()), | 212 Step::Terminated => return Ok(()), |
| 190 Step::Failure(e) => return Err(e), | 213 Step::Failure(e) => return Err(e), |
| 191 _ => {}, | 214 _ => {}, |
| 192 } | 215 } |
| 193 } | 216 } |
| 194 } | 217 } |
| 195 | |
| 196 /// Converts the `AlgIterator` into a plain [`Iterator`]. | |
| 197 /// | |
| 198 /// [`Step::Quiet`] results are discarded, and [`Step::Failure`] results **panic**. | |
| 199 fn downcast(self) -> AlgIteratorI<Self> { | |
| 200 AlgIteratorI(self) | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 /// Conversion of an `AlgIterator` into a plain [`Iterator`]. | |
| 205 /// | |
| 206 /// The conversion discards [`Step::Quiet`] and **panics** on [`Step::Failure`]. | |
| 207 pub struct AlgIteratorI<A>(A); | |
| 208 | |
| 209 impl<A> Iterator for AlgIteratorI<A> | |
| 210 where A : AlgIterator { | |
| 211 type Item = A::Output; | |
| 212 | |
| 213 fn next(&mut self) -> Option<A::Output> { | |
| 214 loop { | |
| 215 match self.0.step() { | |
| 216 Step::Result(v) => return Some(v), | |
| 217 Step::Failure(e) => panic!("{e:?}"), | |
| 218 Step::Terminated => return None, | |
| 219 Step::Quiet => continue, | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 } | 218 } |
| 224 | 219 |
| 225 /// A factory for producing an [`AlgIterator`]. | 220 /// A factory for producing an [`AlgIterator`]. |
| 226 /// | 221 /// |
| 227 /// For usage instructions see the [module documentation][self]. | 222 /// For usage instructions see the [module documentation][self]. |
| 228 pub trait AlgIteratorFactory<V> : Sized { | 223 pub trait AlgIteratorFactory<V> : Sized { |
| 229 type Iter<F, E> : AlgIterator<State = Self::State, Output = Self::Output, Err = E> | 224 type Iter : AlgIterator<State = Self::State, Input = V, Output = Self::Output>; |
| 230 where F : FnMut(&Self::State) -> Step<V, E>, | 225 |
| 231 E : Error; | |
| 232 /// The state type of the corresponding [`AlgIterator`]. | 226 /// The state type of the corresponding [`AlgIterator`]. |
| 233 /// A reference to this is passed to the closures passed to methods such as [`Self::iterate`]. | 227 /// A reference to this is passed to the closures passed to methods such as [`Self::iterate`]. |
| 234 type State : AlgIteratorState; | 228 type State : AlgIteratorState; |
| 235 /// The output type of the corresponding [`AlgIterator`]. | 229 /// The output type of the corresponding [`AlgIterator`]. |
| 236 /// This is the output of the closures passed to methods such as [`Self::iterate`] after | 230 /// This is the output of the closures passed to methods such as [`Self::iterate`] after |
| 237 /// mappings performed by each [`AlgIterator`] implementation. | 231 /// mappings performed by each [`AlgIterator`] implementation. |
| 238 type Output; | 232 type Output; |
| 239 | 233 |
| 240 /// Prepare an [`AlgIterator`], consuming the factory. | 234 /// Prepare an [`AlgIterator`], consuming the factory. |
| 241 /// | 235 fn prepare(self) -> Self::Iter; |
| 242 /// The function `step_fn` should accept a `state` ([`AlgIteratorState`] parameter, and return | |
| 243 /// a [`Step`]. | |
| 244 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | |
| 245 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 246 E : Error; | |
| 247 | 236 |
| 248 /// Iterate the the closure `step`. | 237 /// Iterate the the closure `step`. |
| 249 /// | 238 /// |
| 250 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]). | 239 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]). |
| 251 /// It should return the output of | 240 /// It should return the output of |
| 253 /// completion for other reason, or [`Step::Failure`] for termination for failure. | 242 /// completion for other reason, or [`Step::Failure`] for termination for failure. |
| 254 /// | 243 /// |
| 255 /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`]. | 244 /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`]. |
| 256 #[inline] | 245 #[inline] |
| 257 fn iterate_fallible<F, E>(self, step : F) -> Result<(), E> | 246 fn iterate_fallible<F, E>(self, step : F) -> Result<(), E> |
| 258 where F : FnMut(&Self::State) -> Step<V, E>, | 247 where F : FnMut(Self::State) -> Step<V, Self::State, E>, |
| 259 E : Error { | 248 E : Error { |
| 260 self.prepare(step).iterate() | 249 self.prepare().iterate(step) |
| 261 } | 250 } |
| 262 | 251 |
| 263 /// Iterate the the closure `step`. | 252 /// Iterate the the closure `step`. |
| 264 /// | 253 /// |
| 265 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), | 254 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
| 270 /// | 259 /// |
| 271 /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`] | 260 /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`] |
| 272 /// with the error type `E=`[`std::convert::Infallible`]. | 261 /// with the error type `E=`[`std::convert::Infallible`]. |
| 273 #[inline] | 262 #[inline] |
| 274 fn iterate<F>(self, step : F) | 263 fn iterate<F>(self, step : F) |
| 275 where F : FnMut(&Self::State) -> Step<V> { | 264 where F : FnMut(Self::State) -> Step<V, Self::State> { |
| 276 self.iterate_fallible(step).unwrap_or_default() | 265 self.iterate_fallible(step).unwrap_or_default() |
| 277 } | 266 } |
| 278 | 267 |
| 279 /// Iterate the closure `step` with data produced by `datasource`. | 268 /// Iterate the closure `step` with data produced by `datasource`. |
| 280 /// | 269 /// |
| 286 /// If the `datasource` runs out of data, the iterator is considered having terminated | 275 /// If the `datasource` runs out of data, the iterator is considered having terminated |
| 287 /// successsfully. | 276 /// successsfully. |
| 288 /// | 277 /// |
| 289 /// For usage instructions see the [module documentation][self]. | 278 /// For usage instructions see the [module documentation][self]. |
| 290 #[inline] | 279 #[inline] |
| 291 fn iterate_data_fallible<F, D, I, E>(self, mut datasource : I, mut step : F) -> Result<(), E> | 280 fn iterate_data_fallible<F, D, I, E>(self, mut datasource : I, mut step : F) |
| 292 where F : FnMut(&Self::State, D) -> Step<V, E>, | 281 -> Result<(), E> |
| 282 where F : FnMut(Self::State, D) -> Step<V, Self::State, E>, | |
| 293 I : Iterator<Item = D>, | 283 I : Iterator<Item = D>, |
| 294 E : Error { | 284 E : Error { |
| 295 self.prepare(move |state| { | 285 self.prepare().iterate(move |state| { |
| 296 datasource.next().map_or(Step::Terminated, |d| step(state, d)) | 286 datasource.next().map_or(Step::Terminated, |d| step(state, d)) |
| 297 }).iterate() | 287 }) |
| 298 } | 288 } |
| 299 | 289 |
| 300 /// Iterate the closure `step` with data produced by `datasource`. | 290 /// Iterate the closure `step` with data produced by `datasource`. |
| 301 /// | 291 /// |
| 302 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), | 292 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
| 307 /// successsfully. | 297 /// successsfully. |
| 308 /// | 298 /// |
| 309 /// For usage instructions see the [module documentation][self]. | 299 /// For usage instructions see the [module documentation][self]. |
| 310 #[inline] | 300 #[inline] |
| 311 fn iterate_data<F, D, I>(self, datasource : I, step : F) | 301 fn iterate_data<F, D, I>(self, datasource : I, step : F) |
| 312 where F : FnMut(&Self::State, D) -> Step<V>, | 302 where F : FnMut(Self::State, D) -> Step<V, Self::State>, |
| 313 I : Iterator<Item = D> { | 303 I : Iterator<Item = D> { |
| 314 self.iterate_data_fallible(datasource, step).unwrap_or_default() | 304 self.iterate_data_fallible(datasource, step).unwrap_or_default() |
| 315 } | 305 } |
| 316 | 306 |
| 317 // fn make_iterate<'own>(self) | 307 // fn make_iterate<'own>(self) |
| 396 StallIteratorFactory { base_options : self, stall : stall } | 386 StallIteratorFactory { base_options : self, stall : stall } |
| 397 } | 387 } |
| 398 | 388 |
| 399 /// Is the iterator quiet, i.e., on-verbose? | 389 /// Is the iterator quiet, i.e., on-verbose? |
| 400 fn is_quiet(&self) -> bool { false } | 390 fn is_quiet(&self) -> bool { false } |
| 391 | |
| 392 /// Returns an an [`std::iter::Iterator`] that can be used in a `for`-loop. | |
| 393 fn iter(self) -> AlgIteratorIterator<Self::Iter> { | |
| 394 AlgIteratorIterator { | |
| 395 algi : self.prepare(), | |
| 396 result : Rc::new(AlgIteratorResponse{ | |
| 397 expected_iter : 0, | |
| 398 response : Cell::new(Step::Quiet) | |
| 399 }) | |
| 400 } | |
| 401 } | |
| 401 } | 402 } |
| 402 | 403 |
| 403 /// Options for [`BasicAlgIteratorFactory`]. | 404 /// Options for [`BasicAlgIteratorFactory`]. |
| 404 /// | 405 /// |
| 405 /// Use as: | 406 /// Use as: |
| 481 _phantoms : PhantomData<V>, | 482 _phantoms : PhantomData<V>, |
| 482 } | 483 } |
| 483 | 484 |
| 484 /// The simplest [`AlgIterator`], created by [`BasicAlgIteratorFactory`] | 485 /// The simplest [`AlgIterator`], created by [`BasicAlgIteratorFactory`] |
| 485 #[derive(Clone,Debug)] | 486 #[derive(Clone,Debug)] |
| 486 pub struct BasicAlgIterator<F, V, E : Error> { | 487 pub struct BasicAlgIterator<V> { |
| 487 options : AlgIteratorOptions, | 488 options : AlgIteratorOptions, |
| 488 iter : usize, | 489 iter : usize, |
| 489 step_fn : F, | 490 _phantoms : PhantomData<V>, |
| 490 _phantoms : PhantomData<(V, E)>, | |
| 491 } | 491 } |
| 492 | 492 |
| 493 impl AlgIteratorOptions { | 493 impl AlgIteratorOptions { |
| 494 /// [`AlgIteratorOptions`] is directly a factory for [`BasicAlgIterator`], | 494 /// [`AlgIteratorOptions`] is directly a factory for [`BasicAlgIterator`], |
| 495 /// however, due to type inference issues, it may become convenient to instantiate | 495 /// however, due to type inference issues, it may become convenient to instantiate |
| 503 } | 503 } |
| 504 | 504 |
| 505 impl<V> AlgIteratorFactory<V> for AlgIteratorOptions | 505 impl<V> AlgIteratorFactory<V> for AlgIteratorOptions |
| 506 where V : LogRepr { | 506 where V : LogRepr { |
| 507 type State = BasicState; | 507 type State = BasicState; |
| 508 type Iter<F, E> = BasicAlgIterator<F, V, E> | 508 type Iter = BasicAlgIterator<V>; |
| 509 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 510 E : Error; | |
| 511 type Output = V; | 509 type Output = V; |
| 512 | 510 |
| 513 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 511 fn prepare(self) -> Self::Iter { |
| 514 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 515 E : Error { | |
| 516 BasicAlgIterator{ | 512 BasicAlgIterator{ |
| 517 options : self, | 513 options : self, |
| 518 iter : 0, | 514 iter : 0, |
| 519 step_fn, | |
| 520 _phantoms : PhantomData, | 515 _phantoms : PhantomData, |
| 521 } | 516 } |
| 522 } | 517 } |
| 523 | 518 |
| 524 #[inline] | 519 #[inline] |
| 528 } | 523 } |
| 529 | 524 |
| 530 impl<V> AlgIteratorFactory<V> for BasicAlgIteratorFactory<V> | 525 impl<V> AlgIteratorFactory<V> for BasicAlgIteratorFactory<V> |
| 531 where V : LogRepr { | 526 where V : LogRepr { |
| 532 type State = BasicState; | 527 type State = BasicState; |
| 533 type Iter<F, E> = BasicAlgIterator<F, V, E> | 528 type Iter = BasicAlgIterator<V>; |
| 534 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 535 E : Error; | |
| 536 type Output = V; | 529 type Output = V; |
| 537 | 530 |
| 538 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 531 fn prepare(self) -> Self::Iter { |
| 539 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 540 E : Error { | |
| 541 BasicAlgIterator { | 532 BasicAlgIterator { |
| 542 options : self.options, | 533 options : self.options, |
| 543 iter : 0, | 534 iter : 0, |
| 544 step_fn, | |
| 545 _phantoms : PhantomData | 535 _phantoms : PhantomData |
| 546 } | 536 } |
| 547 } | 537 } |
| 548 | 538 |
| 549 #[inline] | 539 #[inline] |
| 550 fn is_quiet(&self) -> bool { | 540 fn is_quiet(&self) -> bool { |
| 551 self.options.quiet | 541 self.options.quiet |
| 552 } | 542 } |
| 553 } | 543 } |
| 554 | 544 |
| 555 impl<F, V, E> AlgIterator for BasicAlgIterator<F, V, E> | 545 impl<V> AlgIterator for BasicAlgIterator<V> |
| 556 where V : LogRepr, | 546 where V : LogRepr { |
| 557 E : Error, | |
| 558 F : FnMut(&BasicState) -> Step<V, E> { | |
| 559 type State = BasicState; | 547 type State = BasicState; |
| 560 type Output = V; | 548 type Output = V; |
| 561 type Err = E; | 549 type Input = V; |
| 562 | 550 |
| 563 #[inline] | 551 #[inline] |
| 564 fn step(&mut self) -> Step<V, E> { | 552 fn prestep(&mut self) -> Option<Self::State> { |
| 565 if self.iter >= self.options.max_iter { | 553 if self.iter >= self.options.max_iter { |
| 566 Step::Terminated | 554 None |
| 567 } else { | 555 } else { |
| 568 self.iter += 1; | 556 self.iter += 1; |
| 569 let state = self.state(); | 557 Some(self.state()) |
| 570 let res = (self.step_fn)(&state); | 558 } |
| 571 if let Step::Result(ref val) = res { | 559 } |
| 572 if state.verbose && !self.options.quiet { | 560 |
| 573 println!("{}{}/{} {}{}", "".dimmed(), | 561 fn poststep<E : Error>(&mut self, res : Step<V, Self::State, E>) -> Step<V, Self::State, E> { |
| 574 state.iter, | 562 if let Step::Result(ref val, ref state) = res { |
| 575 self.options.max_iter, | 563 if state.verbose && !self.options.quiet { |
| 576 val.logrepr(), | 564 println!("{}{}/{} {}{}", "".dimmed(), |
| 577 "".clear()); | 565 state.iter, |
| 578 } | 566 self.options.max_iter, |
| 567 val.logrepr(), | |
| 568 "".clear()); | |
| 579 } | 569 } |
| 580 res | 570 } |
| 581 } | 571 res |
| 582 } | 572 } |
| 583 | 573 |
| 584 #[inline] | 574 #[inline] |
| 585 fn iteration(&self) -> usize { | 575 fn iteration(&self) -> usize { |
| 586 self.iter | 576 self.iter |
| 599 } | 589 } |
| 600 } | 590 } |
| 601 | 591 |
| 602 impl AlgIteratorState for BasicState { | 592 impl AlgIteratorState for BasicState { |
| 603 #[inline] | 593 #[inline] |
| 604 fn if_verbose<V, E : Error>(&self, mut calc_objective : impl FnMut() -> V) -> Step<V, E> { | 594 fn if_verbose<V, E : Error>(self, mut calc_objective : impl FnMut() -> V) -> Step<V, Self, E> { |
| 605 if self.calc { | 595 if self.calc { |
| 606 Step::Result(calc_objective()) | 596 Step::Result(calc_objective(), self) |
| 607 } else { | 597 } else { |
| 608 Step::Quiet | 598 Step::Quiet |
| 609 } | 599 } |
| 610 } | 600 } |
| 611 | 601 |
| 645 | 635 |
| 646 impl<V, U, BaseFactory> AlgIteratorFactory<V> | 636 impl<V, U, BaseFactory> AlgIteratorFactory<V> |
| 647 for StallIteratorFactory<U, BaseFactory> | 637 for StallIteratorFactory<U, BaseFactory> |
| 648 where BaseFactory : AlgIteratorFactory<V, Output=U>, | 638 where BaseFactory : AlgIteratorFactory<V, Output=U>, |
| 649 U : SignedNum + PartialOrd { | 639 U : SignedNum + PartialOrd { |
| 650 type Iter<F, E> = StallIterator<U, BaseFactory::Iter<F, E>> | 640 type Iter = StallIterator<U, BaseFactory::Iter>; |
| 651 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 652 E : Error; | |
| 653 type State = BaseFactory::State; | 641 type State = BaseFactory::State; |
| 654 type Output = BaseFactory::Output; | 642 type Output = BaseFactory::Output; |
| 655 | 643 |
| 656 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 644 fn prepare(self) -> Self::Iter { |
| 657 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 658 E : Error { | |
| 659 StallIterator { | 645 StallIterator { |
| 660 base_iterator : self.base_options.prepare(step_fn), | 646 base_iterator : self.base_options.prepare(), |
| 661 stall : self.stall, | 647 stall : self.stall, |
| 662 previous_value : None, | 648 previous_value : None, |
| 663 } | 649 } |
| 664 } | 650 } |
| 665 | 651 |
| 671 impl<U, BaseIterator> AlgIterator | 657 impl<U, BaseIterator> AlgIterator |
| 672 for StallIterator<U, BaseIterator> | 658 for StallIterator<U, BaseIterator> |
| 673 where BaseIterator : AlgIterator<Output=U>, | 659 where BaseIterator : AlgIterator<Output=U>, |
| 674 U : SignedNum + PartialOrd { | 660 U : SignedNum + PartialOrd { |
| 675 type State = BaseIterator::State; | 661 type State = BaseIterator::State; |
| 676 type Output = BaseIterator::Output; | 662 type Output = U; |
| 677 type Err = BaseIterator::Err; | 663 type Input = BaseIterator::Input; |
| 678 | 664 |
| 679 #[inline] | 665 #[inline] |
| 680 fn step(&mut self) -> Step<U, Self::Err> { | 666 fn prestep(&mut self) -> Option<Self::State> { |
| 681 match self.base_iterator.step() { | 667 self.base_iterator.prestep() |
| 682 Step::Result(nv) => { | 668 } |
| 669 | |
| 670 #[inline] | |
| 671 fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<U, Self::State, E> | |
| 672 where E : Error { | |
| 673 match self.base_iterator.poststep(res) { | |
| 674 Step::Result(nv, state) => { | |
| 683 let previous_v = self.previous_value; | 675 let previous_v = self.previous_value; |
| 684 self.previous_value = Some(nv); | 676 self.previous_value = Some(nv); |
| 685 match previous_v { | 677 match previous_v { |
| 686 Some(pv) if (nv - pv).abs() <= self.stall * pv.abs() => Step::Terminated, | 678 Some(pv) if (nv - pv).abs() <= self.stall * pv.abs() => Step::Terminated, |
| 687 _ => Step::Result(nv), | 679 _ => Step::Result(nv, state), |
| 688 } | 680 } |
| 689 }, | 681 }, |
| 690 val => val, | 682 val => val, |
| 691 } | 683 } |
| 692 } | 684 } |
| 720 | 712 |
| 721 impl<V, U, BaseFactory> AlgIteratorFactory<V> | 713 impl<V, U, BaseFactory> AlgIteratorFactory<V> |
| 722 for ValueIteratorFactory<U, BaseFactory> | 714 for ValueIteratorFactory<U, BaseFactory> |
| 723 where BaseFactory : AlgIteratorFactory<V, Output=U>, | 715 where BaseFactory : AlgIteratorFactory<V, Output=U>, |
| 724 U : SignedNum + PartialOrd { | 716 U : SignedNum + PartialOrd { |
| 725 type Iter<F, E> = ValueIterator<U, BaseFactory::Iter<F, E>> | 717 type Iter = ValueIterator<U, BaseFactory::Iter>; |
| 726 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 727 E : Error; | |
| 728 type State = BaseFactory::State; | 718 type State = BaseFactory::State; |
| 729 type Output = BaseFactory::Output; | 719 type Output = BaseFactory::Output; |
| 730 | 720 |
| 731 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 721 fn prepare(self) -> Self::Iter { |
| 732 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 733 E : Error { | |
| 734 ValueIterator { | 722 ValueIterator { |
| 735 base_iterator : self.base_options.prepare(step_fn), | 723 base_iterator : self.base_options.prepare(), |
| 736 target : self.target | 724 target : self.target |
| 737 } | 725 } |
| 738 } | 726 } |
| 739 | 727 |
| 740 fn is_quiet(&self) -> bool { | 728 fn is_quiet(&self) -> bool { |
| 745 impl<U, BaseIterator> AlgIterator | 733 impl<U, BaseIterator> AlgIterator |
| 746 for ValueIterator<U, BaseIterator> | 734 for ValueIterator<U, BaseIterator> |
| 747 where BaseIterator : AlgIterator<Output=U>, | 735 where BaseIterator : AlgIterator<Output=U>, |
| 748 U : SignedNum + PartialOrd { | 736 U : SignedNum + PartialOrd { |
| 749 type State = BaseIterator::State; | 737 type State = BaseIterator::State; |
| 750 type Output = BaseIterator::Output; | 738 type Output = U; |
| 751 type Err = BaseIterator::Err; | 739 type Input = BaseIterator::Input; |
| 752 | 740 |
| 753 #[inline] | 741 #[inline] |
| 754 fn step(&mut self) -> Step<U, Self::Err> { | 742 fn prestep(&mut self) -> Option<Self::State> { |
| 755 match self.base_iterator.step() { | 743 self.base_iterator.prestep() |
| 756 Step::Result(v) => { | 744 } |
| 745 | |
| 746 #[inline] | |
| 747 fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<U, Self::State, E> where E : Error{ | |
| 748 match self.base_iterator.poststep(res) { | |
| 749 Step::Result(v, state) => { | |
| 757 if v <= self.target { | 750 if v <= self.target { |
| 758 Step::Terminated | 751 Step::Terminated |
| 759 } else { | 752 } else { |
| 760 Step::Result(v) | 753 Step::Result(v, state) |
| 761 } | 754 } |
| 762 }, | 755 }, |
| 763 val => val, | 756 val => val, |
| 764 } | 757 } |
| 765 } | 758 } |
| 801 impl<'log, V, BaseFactory> AlgIteratorFactory<V> | 794 impl<'log, V, BaseFactory> AlgIteratorFactory<V> |
| 802 for LoggingIteratorFactory<'log, BaseFactory::Output, BaseFactory> | 795 for LoggingIteratorFactory<'log, BaseFactory::Output, BaseFactory> |
| 803 where BaseFactory : AlgIteratorFactory<V>, | 796 where BaseFactory : AlgIteratorFactory<V>, |
| 804 BaseFactory::Output : 'log { | 797 BaseFactory::Output : 'log { |
| 805 type State = BaseFactory::State; | 798 type State = BaseFactory::State; |
| 806 type Iter<F, E> = LoggingIterator<'log, BaseFactory::Output, BaseFactory::Iter<F, E>> | 799 type Iter = LoggingIterator<'log, BaseFactory::Output, BaseFactory::Iter>; |
| 807 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 808 E : Error; | |
| 809 type Output = (); | 800 type Output = (); |
| 810 | 801 |
| 811 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 802 fn prepare(self) -> Self::Iter { |
| 812 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 813 E : Error { | |
| 814 LoggingIterator { | 803 LoggingIterator { |
| 815 base_iterator : self.base_options.prepare(step_fn), | 804 base_iterator : self.base_options.prepare(), |
| 816 logger : self.logger, | 805 logger : self.logger, |
| 817 } | 806 } |
| 818 } | 807 } |
| 819 | 808 |
| 820 #[inline] | 809 #[inline] |
| 827 for LoggingIterator<'log, BaseIterator::Output, BaseIterator> | 816 for LoggingIterator<'log, BaseIterator::Output, BaseIterator> |
| 828 where BaseIterator : AlgIterator, | 817 where BaseIterator : AlgIterator, |
| 829 BaseIterator::Output : 'log { | 818 BaseIterator::Output : 'log { |
| 830 type State = BaseIterator::State; | 819 type State = BaseIterator::State; |
| 831 type Output = (); | 820 type Output = (); |
| 832 type Err = BaseIterator::Err; | 821 type Input = BaseIterator::Input; |
| 833 | 822 |
| 834 #[inline] | 823 #[inline] |
| 835 fn step(&mut self) -> Step<Self::Output, Self::Err> { | 824 fn prestep(&mut self) -> Option<Self::State> { |
| 836 match self.base_iterator.step() { | 825 self.base_iterator.prestep() |
| 837 Step::Result(v) => { | 826 } |
| 827 | |
| 828 #[inline] | |
| 829 fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<(), Self::State, E> where E : Error { | |
| 830 match self.base_iterator.poststep(res) { | |
| 831 Step::Result(v, _) => { | |
| 838 self.logger.log(v); | 832 self.logger.log(v); |
| 839 Step::Quiet | 833 Step::Quiet |
| 840 }, | 834 }, |
| 841 Step::Quiet => Step::Quiet, | 835 Step::Quiet => Step::Quiet, |
| 842 Step::Terminated => Step::Terminated, | 836 Step::Terminated => Step::Terminated, |
| 854 self.base_iterator.state() | 848 self.base_iterator.state() |
| 855 } | 849 } |
| 856 } | 850 } |
| 857 | 851 |
| 858 /// This [`AlgIteratorFactory`] allows output mapping. | 852 /// This [`AlgIteratorFactory`] allows output mapping. |
| 859 /// | 853 /// |
| 860 /// Typically produced with [`AlgIteratorFactory::mapped`]. | 854 /// Typically produced with [`AlgIteratorFactory::mapped`]. |
| 861 #[derive(Debug)] | 855 #[derive(Debug)] |
| 862 pub struct MappingIteratorFactory<G, BaseFactory> { | 856 pub struct MappingIteratorFactory<G, BaseFactory> { |
| 863 /// Base [`AlgIteratorFactory`] on which to build | 857 /// Base [`AlgIteratorFactory`] on which to build |
| 864 base_options : BaseFactory, | 858 base_options : BaseFactory, |
| 877 impl<V, U, G, BaseFactory> AlgIteratorFactory<V> | 871 impl<V, U, G, BaseFactory> AlgIteratorFactory<V> |
| 878 for MappingIteratorFactory<G, BaseFactory> | 872 for MappingIteratorFactory<G, BaseFactory> |
| 879 where BaseFactory : AlgIteratorFactory<V>, | 873 where BaseFactory : AlgIteratorFactory<V>, |
| 880 G : Fn(usize, BaseFactory::Output) -> U { | 874 G : Fn(usize, BaseFactory::Output) -> U { |
| 881 type State = BaseFactory::State; | 875 type State = BaseFactory::State; |
| 882 type Iter<F, E> = MappingIterator<G, BaseFactory::Iter<F, E>> | 876 type Iter = MappingIterator<G, BaseFactory::Iter>; |
| 883 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 884 E : Error; | |
| 885 type Output = U; | 877 type Output = U; |
| 886 | 878 |
| 887 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 879 fn prepare(self) -> Self::Iter { |
| 888 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 889 E : Error { | |
| 890 MappingIterator { | 880 MappingIterator { |
| 891 base_iterator : self.base_options.prepare(step_fn), | 881 base_iterator : self.base_options.prepare(), |
| 892 map : self.map | 882 map : self.map |
| 893 } | 883 } |
| 894 } | 884 } |
| 895 | 885 |
| 896 #[inline] | 886 #[inline] |
| 903 for MappingIterator<G, BaseIterator> | 893 for MappingIterator<G, BaseIterator> |
| 904 where BaseIterator : AlgIterator, | 894 where BaseIterator : AlgIterator, |
| 905 G : Fn(usize, BaseIterator::Output) -> U { | 895 G : Fn(usize, BaseIterator::Output) -> U { |
| 906 type State = BaseIterator::State; | 896 type State = BaseIterator::State; |
| 907 type Output = U; | 897 type Output = U; |
| 908 type Err = BaseIterator::Err; | 898 type Input = BaseIterator::Input; |
| 909 | 899 |
| 910 #[inline] | 900 #[inline] |
| 911 fn step(&mut self) -> Step<Self::Output, Self::Err> { | 901 fn prestep(&mut self) -> Option<Self::State> { |
| 912 match self.base_iterator.step() { | 902 self.base_iterator.prestep() |
| 913 Step::Result(v) => Step::Result((self.map)(self.iteration(), v)), | 903 } |
| 904 | |
| 905 #[inline] | |
| 906 fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<Self::Output, Self::State, E> where E : Error { | |
| 907 match self.base_iterator.poststep(res) { | |
| 908 Step::Result(v, state) => Step::Result((self.map)(self.iteration(), v), state), | |
| 914 Step::Quiet => Step::Quiet, | 909 Step::Quiet => Step::Quiet, |
| 915 Step::Terminated => Step::Terminated, | 910 Step::Terminated => Step::Terminated, |
| 916 Step::Failure(e) => Step::Failure(e), | 911 Step::Failure(e) => Step::Failure(e), |
| 917 } | 912 } |
| 918 } | 913 } |
| 960 | 955 |
| 961 impl<V, BaseFactory> AlgIteratorFactory<V> | 956 impl<V, BaseFactory> AlgIteratorFactory<V> |
| 962 for TimingIteratorFactory<BaseFactory> | 957 for TimingIteratorFactory<BaseFactory> |
| 963 where BaseFactory : AlgIteratorFactory<V> { | 958 where BaseFactory : AlgIteratorFactory<V> { |
| 964 type State = BaseFactory::State; | 959 type State = BaseFactory::State; |
| 965 type Iter<F, E> = TimingIterator<BaseFactory::Iter<F, E>> | 960 type Iter = TimingIterator<BaseFactory::Iter>; |
| 966 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 967 E : Error; | |
| 968 type Output = Timed<BaseFactory::Output>; | 961 type Output = Timed<BaseFactory::Output>; |
| 969 | 962 |
| 970 fn prepare<F, E>(self, step_fn : F) -> Self::Iter<F, E> | 963 fn prepare(self) -> Self::Iter { |
| 971 where F : FnMut(&Self::State) -> Step<V, E>, | |
| 972 E : Error { | |
| 973 TimingIterator { | 964 TimingIterator { |
| 974 base_iterator : self.0.prepare(step_fn), | 965 base_iterator : self.0.prepare(), |
| 975 start_time : ProcessTime::now() | 966 start_time : ProcessTime::now() |
| 976 } | 967 } |
| 977 } | 968 } |
| 978 | 969 |
| 979 #[inline] | 970 #[inline] |
| 985 impl<BaseIterator> AlgIterator | 976 impl<BaseIterator> AlgIterator |
| 986 for TimingIterator<BaseIterator> | 977 for TimingIterator<BaseIterator> |
| 987 where BaseIterator : AlgIterator { | 978 where BaseIterator : AlgIterator { |
| 988 type State = BaseIterator::State; | 979 type State = BaseIterator::State; |
| 989 type Output = Timed<BaseIterator::Output>; | 980 type Output = Timed<BaseIterator::Output>; |
| 990 type Err = BaseIterator::Err; | 981 type Input = BaseIterator::Input; |
| 991 | 982 |
| 992 #[inline] | 983 #[inline] |
| 993 fn step(&mut self) -> Step<Self::Output, Self::Err> { | 984 fn prestep(&mut self) -> Option<Self::State> { |
| 994 match self.base_iterator.step() { | 985 self.base_iterator.prestep() |
| 995 Step::Result(data) => { | 986 } |
| 987 | |
| 988 #[inline] | |
| 989 fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<Self::Output, Self::State, E> where E : Error { | |
| 990 match self.base_iterator.poststep(res) { | |
| 991 Step::Result(data, state) => { | |
| 996 Step::Result(Timed{ | 992 Step::Result(Timed{ |
| 997 cpu_time : self.start_time.elapsed(), | 993 cpu_time : self.start_time.elapsed(), |
| 998 data | 994 data |
| 999 }) | 995 }, state) |
| 1000 }, | 996 }, |
| 1001 Step::Quiet => Step::Quiet, | 997 Step::Quiet => Step::Quiet, |
| 1002 Step::Terminated => Step::Terminated, | 998 Step::Terminated => Step::Terminated, |
| 1003 Step::Failure(e) => Step::Failure(e), | 999 Step::Failure(e) => Step::Failure(e), |
| 1004 } | 1000 } |
| 1010 } | 1006 } |
| 1011 | 1007 |
| 1012 #[inline] | 1008 #[inline] |
| 1013 fn state(&self) -> Self::State { | 1009 fn state(&self) -> Self::State { |
| 1014 self.base_iterator.state() | 1010 self.base_iterator.state() |
| 1011 } | |
| 1012 } | |
| 1013 | |
| 1014 // | |
| 1015 // New for-loop interface | |
| 1016 // | |
| 1017 | |
| 1018 struct AlgIteratorResponse<I : AlgIterator> { | |
| 1019 expected_iter : usize, | |
| 1020 response : Cell<Step<I::Input, I::State>>, | |
| 1021 } | |
| 1022 pub struct AlgIteratorIterator<I : AlgIterator> { | |
| 1023 algi : I, | |
| 1024 result : Rc<AlgIteratorResponse<I>>, | |
| 1025 } | |
| 1026 | |
| 1027 pub struct AlgIteratorIteration<I : AlgIterator> { | |
| 1028 state : I::State, | |
| 1029 result : Rc<AlgIteratorResponse<I>>, | |
| 1030 } | |
| 1031 | |
| 1032 impl<I : AlgIterator> AlgIteratorIterator<I> { | |
| 1033 fn poststep(&mut self, even_if_quiet : bool) { | |
| 1034 let res = self.result.response.take(); | |
| 1035 if !even_if_quiet { | |
| 1036 if let Step::Quiet = res { | |
| 1037 return | |
| 1038 } | |
| 1039 } | |
| 1040 self.algi.poststep(res); | |
| 1041 } | |
| 1042 } | |
| 1043 | |
| 1044 impl<I : AlgIterator> std::iter::Iterator for AlgIteratorIterator<I> { | |
| 1045 type Item = AlgIteratorIteration<I>; | |
| 1046 | |
| 1047 fn next(&mut self) -> Option<Self::Item> { | |
| 1048 self.poststep(true); | |
| 1049 self.algi.prestep().map(|state| AlgIteratorIteration { | |
| 1050 state, | |
| 1051 result : self.result.clone(), | |
| 1052 }) | |
| 1053 } | |
| 1054 } | |
| 1055 | |
| 1056 impl<I : AlgIterator> Drop for AlgIteratorIterator<I> { | |
| 1057 fn drop(&mut self) { | |
| 1058 self.poststep(false) | |
| 1059 } | |
| 1060 } | |
| 1061 | |
| 1062 /// Types of errors that may occur | |
| 1063 #[derive(Debug,PartialEq,Eq)] | |
| 1064 pub enum IterationError { | |
| 1065 /// [`AlgIteratorIteration::if_verbose_check`] is not called in iteration order. | |
| 1066 ReportingOrderingError | |
| 1067 } | |
| 1068 | |
| 1069 impl<I : AlgIterator> AlgIteratorIteration<I> { | |
| 1070 /// Call `call_objective` if this is a verbose iteration. | |
| 1071 /// | |
| 1072 /// Verbosity depends on the [`AlgIterator`] that produced this state. | |
| 1073 /// | |
| 1074 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted | |
| 1075 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the | |
| 1076 /// [module documentation][self]. | |
| 1077 /// | |
| 1078 /// This function may panic if result reporting is not ordered correctly (an unlikely mistake | |
| 1079 /// if using this facility correctly). For a version that propagates errors, see | |
| 1080 /// [`Self::if_verbose_check`]. | |
| 1081 pub fn if_verbose(self, calc_objective : impl FnMut() -> I::Input) { | |
| 1082 self.if_verbose_check(calc_objective).unwrap() | |
| 1083 } | |
| 1084 | |
| 1085 /// Version of [`Self::if_verbose`] that propagates errors instead of panicking. | |
| 1086 pub fn if_verbose_check(self, mut calc_objective : impl FnMut() -> I::Input) | |
| 1087 -> Result<(), IterationError> { | |
| 1088 if self.result.expected_iter != self.state.iteration() { | |
| 1089 Err(IterationError::ReportingOrderingError) | |
| 1090 } else { | |
| 1091 let res = calc_objective(); | |
| 1092 self.result.response.replace(Step::Result(res, self.state)); | |
| 1093 Ok(()) | |
| 1094 } | |
| 1095 } | |
| 1096 | |
| 1097 /// Returns the current iteration count. | |
| 1098 pub fn iteration(&self) -> usize { | |
| 1099 self.state.iteration() | |
| 1100 } | |
| 1101 | |
| 1102 /// Indicates whether the iterator is quiet | |
| 1103 pub fn is_quiet(&self) -> bool { | |
| 1104 self.state.is_quiet() | |
| 1015 } | 1105 } |
| 1016 } | 1106 } |
| 1017 | 1107 |
| 1018 // | 1108 // |
| 1019 // Tests | 1109 // Tests |