iter_log/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use rayon::iter::plumbing::{Consumer, Folder, UnindexedConsumer};
use rayon::prelude::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

/// Extension trait to add a `log_progress` method to regular iterators.
pub trait LogProgressExt: Iterator + Sized {
    /// Wraps the iterator with progress logging.
    ///
    /// This method will print progress updates every `step_percent`% of the way through the iteration.
    ///
    /// # Arguments
    ///
    /// * `step_percent` - The percentage of items to be processed before logging a progress update.
    ///
    /// # Returns
    ///
    /// Returns a new `LogProgress` iterator which tracks and logs progress.
    fn log_progress(self, step_percent: usize) -> LogProgress<Self>;
}

impl<I> LogProgressExt for I
where
    I: Iterator, // Ensure `I` is an iterator
{
    fn log_progress(self, step_percent: usize) -> LogProgress<Self> {
        let total = self.size_hint().1.unwrap_or(0); // Get the total number of items (if possible)

        LogProgress {
            iter: self,
            progress: 0,
            total,
            step_percent,
        }
    }
}

/// A struct that wraps an iterator and tracks progress.
pub struct LogProgress<I> {
    iter: I,
    progress: usize,
    total: usize,
    step_percent: usize,
}

impl<I: Iterator> Iterator for LogProgress<I> {
    type Item = I::Item;

    /// Returns the next item in the iteration, while logging progress at intervals.
    ///
    /// The method increments the progress count and checks if the progress reaches a new milestone
    /// (i.e., a multiple of `step_percent`). If so, it logs the progress to the console.
    ///
    /// # Returns
    ///
    /// Returns the next item in the iterator, or `None` if the iterator is finished.
    fn next(&mut self) -> Option<Self::Item> {
        let item = self.iter.next()?;

        self.progress += 1;
        let old_percent = (self.progress * 100) / self.total;
        let new_percent = ((self.progress + 1) * 100) / self.total;

        // Log the progress if we hit a new milestone
        if new_percent / self.step_percent > old_percent / self.step_percent {
            let rounded_percent = new_percent - (new_percent % self.step_percent);
            println!("Progress: {}%", rounded_percent);
        }

        Some(item)
    }
}

/// A struct to handle ordered progress logging during parallel iteration.
struct OrderedLogger {
    last_logged: AtomicUsize,
    pending_logs: Mutex<Vec<usize>>,
}

impl OrderedLogger {
    /// Creates a new `OrderedLogger` instance.
    ///
    /// This function initializes the logger with a fresh `AtomicUsize` and an empty vector for
    /// pending progress updates.
    ///
    /// # Returns
    ///
    /// Returns an `Arc` (thread-safe reference) of the `OrderedLogger`.
    fn new() -> Arc<Self> {
        Arc::new(Self {
            last_logged: AtomicUsize::new(0),
            pending_logs: Mutex::new(Vec::new()),
        })
    }

    /// Logs the progress if it matches the expected step and ensures ordered output.
    ///
    /// # Arguments
    ///
    /// * `progress` - The current progress percentage.
    /// * `step_percent` - The percentage step at which to log progress.
    ///
    /// # Notes
    ///
    /// This method ensures that progress logs are printed in the correct order, even when the
    /// parallel tasks report progress asynchronously.
    fn log_progress(&self, progress: usize, step_percent: usize) {
        let mut pending = self.pending_logs.lock().unwrap();

        if progress == self.last_logged.load(Ordering::Relaxed) + step_percent {
            // Print the progress immediately if it's the next expected one
            println!("Progress: {}%", progress);
            self.last_logged.fetch_add(step_percent, Ordering::Relaxed);

            // Print any pending logs that can now be processed in order
            while let Some(&next) = pending.first() {
                if next == self.last_logged.load(Ordering::Relaxed) + step_percent {
                    println!("Progress: {}%", next);
                    self.last_logged.fetch_add(step_percent, Ordering::Relaxed);
                    pending.remove(0);
                } else {
                    break;
                }
            }
        } else {
            // If progress is not expected yet, store it for later
            pending.push(progress);
            pending.sort_unstable(); // Sort pending progress updates
        }
    }
}

/// A struct that wraps a parallel iterator and tracks progress.
pub struct LogProgressPar<I> {
    iter: I,
    progress: Arc<AtomicUsize>,
    total: usize,
    step_percent: usize,
    logger: Arc<OrderedLogger>,
}

impl<I> ParallelIterator for LogProgressPar<I>
where
    I: ParallelIterator,
{
    type Item = I::Item;

    /// Drives the parallel iteration using a custom consumer.
    ///
    /// This method wraps the original consumer with the `LogProgressConsumer`, which tracks the
    /// progress and logs it at the specified intervals.
    ///
    /// # Arguments
    ///
    /// * `consumer` - The base consumer that will process the items of the parallel iterator.
    ///
    /// # Returns
    ///
    /// Returns the result of the parallel iteration after it is consumed.
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        let wrapped_consumer = LogProgressConsumer {
            base: consumer,
            progress: self.progress,
            total: self.total,
            step_percent: self.step_percent,
            logger: self.logger,
        };
        self.iter.drive_unindexed(wrapped_consumer)
    }
}

/// A consumer for parallel iterations that tracks progress and logs it.
struct LogProgressConsumer<C> {
    base: C,
    progress: Arc<AtomicUsize>,
    total: usize,
    step_percent: usize,
    logger: Arc<OrderedLogger>,
}

impl<C, T> Consumer<T> for LogProgressConsumer<C>
where
    C: Consumer<T>,
{
    type Folder = LogProgressFolder<C::Folder>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    /// Splits the consumer at the specified index and returns two new consumers.
    ///
    /// This method ensures that progress tracking is correctly propagated through both consumers.
    ///
    /// # Arguments
    ///
    /// * `index` - The index at which to split the consumer.
    ///
    /// # Returns
    ///
    /// Returns two new consumers and a reducer for parallel reduction.
    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            LogProgressConsumer {
                base: left,
                progress: Arc::clone(&self.progress),
                total: self.total,
                step_percent: self.step_percent,
                logger: Arc::clone(&self.logger),
            },
            LogProgressConsumer {
                base: right,
                progress: Arc::clone(&self.progress),
                total: self.total,
                step_percent: self.step_percent,
                logger: Arc::clone(&self.logger),
            },
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        LogProgressFolder {
            base: self.base.into_folder(),
            progress: self.progress,
            total: self.total,
            step_percent: self.step_percent,
            logger: Arc::clone(&self.logger),
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

impl<C, T> UnindexedConsumer<T> for LogProgressConsumer<C>
where
    C: UnindexedConsumer<T>,
{
    fn split_off_left(&self) -> Self {
        LogProgressConsumer {
            base: self.base.split_off_left(),
            progress: Arc::clone(&self.progress),
            total: self.total,
            step_percent: self.step_percent,
            logger: Arc::clone(&self.logger),
        }
    }

    fn to_reducer(&self) -> Self::Reducer {
        self.base.to_reducer()
    }
}

/// A folder for processing items in parallel while tracking progress.
struct LogProgressFolder<F> {
    base: F,
    progress: Arc<AtomicUsize>,
    total: usize,
    step_percent: usize,
    logger: Arc<OrderedLogger>,
}

impl<F, T> Folder<T> for LogProgressFolder<F>
where
    F: Folder<T>,
{
    type Result = F::Result;

    /// Consumes an item and tracks progress.
    ///
    /// This method updates the progress counter and logs progress at specified intervals.
    ///
    /// # Arguments
    ///
    /// * `item` - The item to consume and process.
    ///
    /// # Returns
    ///
    /// Returns a new `LogProgressFolder` with the updated state.
    fn consume(self, item: T) -> Self {
        let old_count = self.progress.fetch_add(1, Ordering::Relaxed);
        let old_percent = (old_count * 100) / self.total;
        let new_percent = ((old_count + 1) * 100) / self.total;

        if new_percent / self.step_percent > old_percent / self.step_percent {
            let rounded_percent = new_percent - (new_percent % self.step_percent);
            self.logger.log_progress(rounded_percent, self.step_percent);
        }

        LogProgressFolder {
            base: self.base.consume(item),
            progress: self.progress,
            total: self.total,
            step_percent: self.step_percent,
            logger: self.logger,
        }
    }

    fn complete(self) -> Self::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

/// Extension trait to add a `log_progress` method to parallel iterators.
pub trait LogProgressParExt: Sized + ParallelIterator {
    /// Wraps the parallel iterator with progress logging.
    ///
    /// This method will print progress updates every `step_percent`% of the way through the iteration.
    ///
    /// # Arguments
    ///
    /// * `step_percent` - The percentage of items to be processed before logging a progress update.
    ///
    /// # Returns
    ///
    /// Returns a new `LogProgressPar` iterator which tracks and logs progress.
    fn log_progress(self, step_percent: usize) -> LogProgressPar<Self>;
}

impl<I> LogProgressParExt for I
where
    I: ParallelIterator + IndexedParallelIterator,
{
    fn log_progress(self, step_percent: usize) -> LogProgressPar<Self> {
        let total = self.len(); // Get the total number of items
        let logger = OrderedLogger::new(); // Create the logger

        LogProgressPar {
            iter: self,
            progress: Arc::new(AtomicUsize::new(0)),
            total,
            step_percent,
            logger,
        }
    }
}

/// A long computation function to simulate more expensive work per item.
pub fn long_computation(x: u32) -> u32 {
    // Simulate a heavy calculation by introducing a delay
    let mut result = x;
    for _ in 0..1_000_000 {
        result = result.saturating_mul(2);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_long_computation() {
        assert_eq!(
            long_computation(2),
            2u32.saturating_mul(2).saturating_mul(2)
        ); // Expected result
    }
}