133 /// Verbosity depends on the [`AlgIterator`] that produced this state. |
133 /// Verbosity depends on the [`AlgIterator`] that produced this state. |
134 /// |
134 /// |
135 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted |
135 /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted |
136 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the |
136 /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the |
137 /// [module documentation][self]. |
137 /// [module documentation][self]. |
138 fn if_verbose<V, E : Error>(self, calc_objective : impl FnMut() -> V) -> Step<V, Self, E>; |
138 fn if_verbose<V, E : Error>(self, calc_objective : impl FnOnce() -> V) -> Step<V, Self, E>; |
139 |
139 |
140 /// Returns the current iteration count. |
140 /// Returns the current iteration count. |
141 fn iteration(&self) -> usize; |
141 fn iteration(&self) -> usize; |
142 |
142 |
143 /// Indicates whether the iterator is quiet |
143 /// Indicates whether the iterator is quiet |
258 where F : FnMut(Self::State) -> Step<V, Self::State, E>, |
258 where F : FnMut(Self::State) -> Step<V, Self::State, E>, |
259 E : Error { |
259 E : Error { |
260 self.prepare().iterate(step) |
260 self.prepare().iterate(step) |
261 } |
261 } |
262 |
262 |
263 /// Iterate the the closure `step`. |
263 /// Iterate the closure `step`. |
264 /// |
264 /// |
265 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
265 /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
266 /// It should return the output of |
266 /// It should return the output of |
267 /// `state.`[`if_verbose`][AlgIteratorState::if_verbose]. |
267 /// `state.`[`if_verbose`][AlgIteratorState::if_verbose]. |
268 /// |
268 /// |
404 fn iter(self) -> AlgIteratorIterator<Self::Iter> { |
404 fn iter(self) -> AlgIteratorIterator<Self::Iter> { |
405 AlgIteratorIterator { |
405 AlgIteratorIterator { |
406 algi : Rc::new(RefCell::new(self.prepare())), |
406 algi : Rc::new(RefCell::new(self.prepare())), |
407 } |
407 } |
408 } |
408 } |
|
409 |
|
410 /// Returns an an [`std::iter::Iterator`] that can be used in a `for`-loop, |
|
411 /// also inputting an initial iteration status calculated by `f` if needed. |
|
412 fn iter_init(self, f : impl FnOnce() -> <Self::Iter as AlgIterator>::Input) |
|
413 -> AlgIteratorIterator<Self::Iter> { |
|
414 let mut i = self.prepare(); |
|
415 let st = i.state(); |
|
416 let step : Step<<Self::Iter as AlgIterator>::Input, Self::State> = st.if_verbose(f); |
|
417 i.poststep(step); |
|
418 AlgIteratorIterator { |
|
419 algi : Rc::new(RefCell::new(i)), |
|
420 } |
|
421 } |
409 } |
422 } |
410 |
423 |
411 /// Options for [`BasicAlgIteratorFactory`]. |
424 /// Options for [`BasicAlgIteratorFactory`]. |
412 /// |
425 /// |
413 /// Use as: |
426 /// Use as: |
596 } |
609 } |
597 } |
610 } |
598 |
611 |
599 impl AlgIteratorState for BasicState { |
612 impl AlgIteratorState for BasicState { |
600 #[inline] |
613 #[inline] |
601 fn if_verbose<V, E : Error>(self, mut calc_objective : impl FnMut() -> V) -> Step<V, Self, E> { |
614 fn if_verbose<V, E : Error>(self, calc_objective : impl FnOnce() -> V) -> Step<V, Self, E> { |
602 if self.calc { |
615 if self.calc { |
603 Step::Result(calc_objective(), self) |
616 Step::Result(calc_objective(), self) |
604 } else { |
617 } else { |
605 Step::Quiet |
618 Step::Quiet |
606 } |
619 } |
1060 /// [module documentation][self]. |
1073 /// [module documentation][self]. |
1061 /// |
1074 /// |
1062 /// This function may panic if result reporting is not ordered correctly (an unlikely mistake |
1075 /// This function may panic if result reporting is not ordered correctly (an unlikely mistake |
1063 /// if using this facility correctly). For a version that propagates errors, see |
1076 /// if using this facility correctly). For a version that propagates errors, see |
1064 /// [`Self::if_verbose_check`]. |
1077 /// [`Self::if_verbose_check`]. |
1065 pub fn if_verbose(self, calc_objective : impl FnMut() -> I::Input) { |
1078 pub fn if_verbose(self, calc_objective : impl FnOnce() -> I::Input) { |
1066 self.if_verbose_check(calc_objective).unwrap() |
1079 self.if_verbose_check(calc_objective).unwrap() |
1067 } |
1080 } |
1068 |
1081 |
1069 /// Version of [`Self::if_verbose`] that propagates errors instead of panicking. |
1082 /// Version of [`Self::if_verbose`] that propagates errors instead of panicking. |
1070 pub fn if_verbose_check(self, calc_objective : impl FnMut() -> I::Input) |
1083 pub fn if_verbose_check(self, calc_objective : impl FnOnce() -> I::Input) |
1071 -> Result<(), IterationError> { |
1084 -> Result<(), IterationError> { |
1072 let mut algi = match RefCell::try_borrow_mut(&self.algi) { |
1085 let mut algi = match RefCell::try_borrow_mut(&self.algi) { |
1073 Err(_) => return Err(IterationError::ReportingOrderingError), |
1086 Err(_) => return Err(IterationError::ReportingOrderingError), |
1074 Ok(algi) => algi |
1087 Ok(algi) => algi |
1075 }; |
1088 }; |