Mon, 03 Feb 2025 19:22:16 -0500
merge dev to default
| 0 | 1 | /*! |
| 2 | Logging routines for intermediate computational results. | |
| 3 | */ | |
| 4 | ||
| 5 | use serde::Serialize; | |
| 6 | use crate::tabledump::TableDump; | |
| 7 | ||
| 8 | /// A log of items of type `T` along with log configuration. | |
| 9 | /// The constructor takes no arguments; create the log with `Log{T}()` for `T` your | |
| 10 | /// data type, e.g., `Float64`. | |
| 11 | #[derive(Debug, Clone)] | |
| 12 | pub struct Logger<V> { | |
| 13 | data : Vec<V>, | |
| 14 | } | |
| 15 | ||
| 16 | impl<V> Logger<V> { | |
| 17 | /// Create new log | |
| 18 | pub fn new() -> Logger<V> { | |
| 19 | Logger{ data : Vec::new() } | |
| 20 | } | |
| 21 | ||
| 5 | 22 | /// Store the value `v` in the log. |
| 0 | 23 | pub fn log(&mut self, v : V) -> () { |
| 24 | self.data.push(v); | |
| 25 | } | |
| 26 | ||
| 5 | 27 | /// Get the logged data as a [`Vec`] array. |
| 0 | 28 | pub fn data(&self) -> &Vec<V> { |
| 29 | &self.data | |
| 30 | } | |
| 50 | 31 | |
| 32 | /// Map the log with `g`. | |
| 33 | pub fn map<W>(self, g : impl FnMut(V) -> W) -> Logger<W> { | |
| 34 | Logger { data : self.data.into_iter().map(g).collect() } | |
| 35 | } | |
| 0 | 36 | } |
| 37 | ||
| 38 | impl<'a, V : Serialize + 'a> TableDump<'a> for Logger<V> { | |
| 39 | type Iter = std::slice::Iter<'a, V>; | |
| 40 | ||
| 41 | fn tabledump_entries(&'a self) -> Self::Iter { | |
| 42 | self.data.iter() | |
| 43 | } | |
| 44 | } |