moirai-iter 0.7.0

Parallel and async iterator combinators for Moirai concurrency library
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Advanced iterator patterns with unified memory management.
//!
//! This module implements sophisticated iterator patterns that leverage
//! the unified channel system for zero-copy operations and optimal
//! memory utilization, based on modern iterator design principles.

#![cfg_attr(test, allow(clippy::unwrap_used, reason = "test scope"))]

/// Transfer granularity used to derive chunk widths (see `moirai-utils`,
/// which owns the per-target table).
use moirai_utils::CACHE_LINE_SIZE;

/// Default ring buffer capacity (power of 2)
const DEFAULT_RING_BUFFER_CAPACITY: usize = 1024;
use moirai_core::channel::{ChannelConfig, ChannelError, UnifiedReceiver, UnifiedSender};
use moirai_core::memory::MemoryPool;

use std::marker::PhantomData;
use std::sync::Arc;

/// Zero-copy iterator that operates directly on channel streams
pub struct StreamingIterator<T> {
    receiver: UnifiedReceiver<T>,
    buffer: std::vec::IntoIter<T>,
    batch_size: usize,
    finished: bool,
}

impl<T> StreamingIterator<T> {
    /// Create a new streaming iterator from a channel receiver
    pub fn new(receiver: UnifiedReceiver<T>, batch_size: usize) -> Self {
        Self {
            receiver,
            buffer: Vec::new().into_iter(),
            batch_size,
            finished: false,
        }
    }

    /// Try to fill buffer from channel
    fn fill_buffer(&mut self) -> bool {
        if self.finished {
            return false;
        }

        // Try to fill buffer with batch receive
        let new_items = self.receiver.recv_batch(self.batch_size);

        if new_items.is_empty() {
            // Check if channel is closed
            if self.receiver.is_closed() {
                self.finished = true;
                return false;
            }
            // Channel is empty but not closed - try single receive
            match self.receiver.try_recv() {
                Ok(item) => {
                    self.buffer = vec![item].into_iter();
                    true
                }
                Err(_) => false,
            }
        } else {
            self.buffer = new_items.into_iter();
            true
        }
    }
}

impl<T> Iterator for StreamingIterator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        // Check if we have items in buffer
        if let Some(item) = self.buffer.next() {
            return Some(item);
        }

        // Buffer is empty, try to refill
        if self.fill_buffer() {
            self.buffer.next()
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // We can't know the exact size since it depends on the channel
        (self.buffer.len(), None)
    }
}

/// Producer-consumer iterator pair for parallel processing
pub struct ProducerConsumerPair<T> {
    sender: UnifiedSender<T>,
    receiver: UnifiedReceiver<T>,
    config: ChannelConfig,
}

impl<T> ProducerConsumerPair<T> {
    /// Create a new producer-consumer pair with specified capacity
    pub fn new(capacity: usize) -> Result<Self, ChannelError> {
        let config = ChannelConfig {
            capacity,
            enable_batching: true,
            batch_size: capacity.min(64),
            ..Default::default()
        };

        let (sender, receiver) = moirai_core::channel::unified_channel_with_config(config.clone())?;

        Ok(Self {
            sender,
            receiver,
            config,
        })
    }

    /// Get the producer (sender) side
    pub fn producer(&self) -> &UnifiedSender<T> {
        &self.sender
    }

    /// Get the consumer (receiver) side  
    pub fn consumer(&self) -> &UnifiedReceiver<T> {
        &self.receiver
    }

    /// Create a streaming iterator from the consumer side
    pub fn into_streaming_iter(self) -> StreamingIterator<T> {
        StreamingIterator::new(self.receiver, self.config.batch_size)
    }

    /// Split into producer and streaming iterator
    pub fn split(self) -> (UnifiedSender<T>, StreamingIterator<T>) {
        let iter = StreamingIterator::new(self.receiver, self.config.batch_size);
        (self.sender, iter)
    }
}

/// Pipeline stage for composable iterator processing
pub trait PipelineStage<Input, Output>: Send + Sync {
    /// Process a batch of inputs and produce outputs
    fn process_batch(&self, inputs: Vec<Input>) -> Vec<Output>;

    /// Get preferred batch size for this stage
    fn preferred_batch_size(&self) -> usize {
        64
    }
}

/// Map stage implementation
pub struct MapStage<F, Input, Output> {
    func: F,
    batch_size: usize,
    _phantom: PhantomData<(Input, Output)>,
}

impl<F, Input, Output> MapStage<F, Input, Output>
where
    F: Fn(Input) -> Output + Send + Sync,
{
    /// Create a new map stage applying `func` to each input.
    pub fn new(func: F) -> Self {
        Self {
            func,
            batch_size: 64,
            _phantom: PhantomData,
        }
    }

    /// Set the preferred batch size for this stage.
    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = batch_size;
        self
    }
}

impl<F, Input, Output> PipelineStage<Input, Output> for MapStage<F, Input, Output>
where
    F: Fn(Input) -> Output + Send + Sync,
    Input: Send + Sync,
    Output: Send + Sync,
{
    fn process_batch(&self, inputs: Vec<Input>) -> Vec<Output> {
        inputs.into_iter().map(&self.func).collect()
    }

    fn preferred_batch_size(&self) -> usize {
        self.batch_size
    }
}

/// Filter stage implementation
pub struct FilterStage<F, T> {
    predicate: F,
    batch_size: usize,
    _phantom: PhantomData<T>,
}

impl<F, T> FilterStage<F, T>
where
    F: Fn(&T) -> bool + Send + Sync,
{
    /// Create a new filter stage keeping inputs for which `predicate` is true.
    pub fn new(predicate: F) -> Self {
        Self {
            predicate,
            batch_size: 64,
            _phantom: PhantomData,
        }
    }

    /// Set the preferred batch size for this stage.
    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = batch_size;
        self
    }
}

impl<F, T> PipelineStage<T, T> for FilterStage<F, T>
where
    F: Fn(&T) -> bool + Send + Sync,
    T: Send + Sync,
{
    fn process_batch(&self, inputs: Vec<T>) -> Vec<T> {
        inputs.into_iter().filter(|x| (self.predicate)(x)).collect()
    }

    fn preferred_batch_size(&self) -> usize {
        self.batch_size
    }
}

/// Advanced pipeline builder for complex iterator compositions
pub struct IteratorPipeline<T> {
    source: Option<Vec<T>>,
    channel_capacity: usize,
    memory_pool: Option<Arc<MemoryPool<T>>>,
}

impl<T> IteratorPipeline<T> {
    /// Create a new pipeline with a data source
    pub fn from_vec(data: Vec<T>) -> Self {
        Self {
            source: Some(data),
            channel_capacity: DEFAULT_RING_BUFFER_CAPACITY,
            memory_pool: None,
        }
    }

    /// Set channel capacity for pipeline stages
    pub fn with_channel_capacity(mut self, capacity: usize) -> Self {
        self.channel_capacity = capacity;
        self
    }

    /// Enable memory pooling for the pipeline
    pub fn with_memory_pool(mut self, pool: Arc<MemoryPool<T>>) -> Self {
        self.memory_pool = Some(pool);
        self
    }

    /// Add a map stage to the pipeline
    pub fn map<F, R>(self, func: F) -> MappedPipeline<T, R, F>
    where
        F: Fn(T) -> R + Send + Sync + 'static,
        T: Send + Sync + 'static,
        R: Send + Sync + 'static,
    {
        MappedPipeline {
            source: self.source.unwrap_or_default(),
            stage: MapStage::new(func),
            channel_capacity: self.channel_capacity,
            memory_pool: self.memory_pool,
        }
    }

    /// Add a filter stage to the pipeline
    pub fn filter<F>(self, predicate: F) -> FilteredPipeline<T, F>
    where
        F: Fn(&T) -> bool + Send + Sync + 'static,
        T: Send + Sync + 'static,
    {
        FilteredPipeline {
            source: self.source.unwrap_or_default(),
            stage: FilterStage::new(predicate),
            channel_capacity: self.channel_capacity,
            memory_pool: self.memory_pool,
        }
    }

    /// Execute pipeline and collect results
    pub fn collect(self) -> Vec<T> {
        self.source.unwrap_or_default()
    }
}

/// Mapped pipeline stage
pub struct MappedPipeline<Input, Output, F> {
    source: Vec<Input>,
    stage: MapStage<F, Input, Output>,
    channel_capacity: usize,
    memory_pool: Option<Arc<MemoryPool<Input>>>,
}

impl<Input, Output, F> MappedPipeline<Input, Output, F>
where
    F: Fn(Input) -> Output + Send + Sync + 'static,
    Input: Send + Sync + 'static,
    Output: Send + Sync + 'static,
{
    /// Add another map stage
    pub fn map<G, R>(self, func: G) -> MappedPipeline<Output, R, G>
    where
        G: Fn(Output) -> R + Send + Sync + 'static,
        R: Send + 'static,
    {
        // Execute current stage first
        let intermediate_results = self.stage.process_batch(self.source);

        MappedPipeline {
            source: intermediate_results,
            stage: MapStage::new(func),
            channel_capacity: self.channel_capacity,
            memory_pool: self.memory_pool.map(|_| {
                // Create new pool for different type
                Arc::new(MemoryPool::new(256))
            }),
        }
    }

    /// Add a filter stage  
    pub fn filter<G>(self, predicate: G) -> FilteredPipeline<Output, G>
    where
        G: Fn(&Output) -> bool + Send + Sync + 'static,
    {
        // Execute current stage first
        let intermediate_results = self.stage.process_batch(self.source);

        FilteredPipeline {
            source: intermediate_results,
            stage: FilterStage::new(predicate),
            channel_capacity: self.channel_capacity,
            memory_pool: None, // Reset pool for different operations
        }
    }

    /// Execute pipeline and collect results
    pub fn collect(self) -> Vec<Output> {
        // Use memory pool if available for optimization hints
        if let Some(_pool) = &self.memory_pool {
            // In production, would use pool for allocations
        }
        self.stage.process_batch(self.source)
    }

    /// Execute pipeline with parallel processing
    pub async fn collect_parallel(self) -> Vec<Output> {
        // For simplicity, use sequential processing
        // In a full implementation, this would use the thread pool
        self.collect()
    }
}

/// Filtered pipeline stage
pub struct FilteredPipeline<T, F> {
    source: Vec<T>,
    stage: FilterStage<F, T>,
    channel_capacity: usize,
    memory_pool: Option<Arc<MemoryPool<T>>>,
}

impl<T, F> FilteredPipeline<T, F>
where
    F: Fn(&T) -> bool + Send + Sync + 'static,
    T: Send + Sync + 'static,
{
    /// Add a map stage
    pub fn map<G, R>(self, func: G) -> MappedPipeline<T, R, G>
    where
        G: Fn(T) -> R + Send + Sync + 'static,
        R: Send + 'static,
    {
        // Execute current stage first
        let intermediate_results = self.stage.process_batch(self.source);

        MappedPipeline {
            source: intermediate_results,
            stage: MapStage::new(func),
            channel_capacity: self.channel_capacity,
            memory_pool: self.memory_pool,
        }
    }

    /// Add another filter stage
    pub fn filter<G>(self, predicate: G) -> FilteredPipeline<T, G>
    where
        G: Fn(&T) -> bool + Send + Sync + 'static,
    {
        // Execute current stage first
        let intermediate_results = self.stage.process_batch(self.source);

        FilteredPipeline {
            source: intermediate_results,
            stage: FilterStage::new(predicate),
            channel_capacity: self.channel_capacity,
            memory_pool: self.memory_pool,
        }
    }

    /// Execute pipeline and collect results
    pub fn collect(self) -> Vec<T> {
        self.stage.process_batch(self.source)
    }

    /// Execute pipeline with parallel processing
    pub async fn collect_parallel(self) -> Vec<T> {
        // For simplicity, use sequential processing
        // In a full implementation, this would use the thread pool
        self.collect()
    }
}

/// Cache-aware iterator for optimal memory access patterns
pub struct CacheAwareIterator<T> {
    data: Vec<T>,
    chunk_size: usize,
    current_chunk: usize,
    current_pos: usize,
}

impl<T: Clone> CacheAwareIterator<T> {
    /// Create a new cache-aware iterator
    pub fn new(data: Vec<T>) -> Self {
        let chunk_size = CACHE_LINE_SIZE / std::mem::size_of::<T>().max(1);
        Self {
            data,
            chunk_size,
            current_chunk: 0,
            current_pos: 0,
        }
    }

    /// Process in cache-friendly chunks
    pub fn process_chunks<F, R>(self, processor: F) -> Vec<R>
    where
        F: FnMut(&[T]) -> R,
    {
        self.data.chunks(self.chunk_size).map(processor).collect()
    }

    /// Apply function with cache prefetching hints
    pub fn map_with_prefetch<F, R>(self, func: F) -> Vec<R>
    where
        F: Fn(T) -> R + Send + Sync,
        T: Send,
        R: Send,
    {
        // In a real implementation, we'd add CPU prefetch instructions
        self.data.into_iter().map(func).collect()
    }
}

impl<T: Clone> Iterator for CacheAwareIterator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_pos >= self.data.len() {
            return None;
        }

        let item = self.data[self.current_pos].clone();
        self.current_pos += 1;

        // Update chunk tracking for prefetching hints
        if self.current_pos.is_multiple_of(self.chunk_size) {
            self.current_chunk += 1;
        }

        Some(item)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.data.len() - self.current_pos;
        (remaining, Some(remaining))
    }
}

impl<T: Clone> ExactSizeIterator for CacheAwareIterator<T> {}

/// Advanced iterator entry points
pub fn streaming_from_channel<T>(receiver: UnifiedReceiver<T>) -> StreamingIterator<T> {
    StreamingIterator::new(receiver, 64)
}

/// Create a bounded producer-consumer channel pair with the given capacity.
pub fn producer_consumer_channel<T>(
    capacity: usize,
) -> Result<ProducerConsumerPair<T>, ChannelError> {
    ProducerConsumerPair::new(capacity)
}

/// Create a cache-aware iterator over the given data.
pub fn cache_aware_iter<T: Clone>(data: Vec<T>) -> CacheAwareIterator<T> {
    CacheAwareIterator::new(data)
}

/// Create an iterator pipeline over the given data.
pub fn pipeline<T>(data: Vec<T>) -> IteratorPipeline<T> {
    IteratorPipeline::from_vec(data)
}

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

    #[test]
    fn test_streaming_iterator() {
        let (sender, receiver) = unified::unified_channel::<i32>(16).unwrap();

        // Send some data
        for i in 0..10 {
            sender.send(i).unwrap();
        }

        // Create streaming iterator
        let mut iter = StreamingIterator::new(receiver, 5);

        // Collect some items
        let mut collected = Vec::new();
        for _ in 0..5 {
            if let Some(item) = iter.next() {
                collected.push(item);
            }
        }

        assert_eq!(collected.len(), 5);
    }

    #[test]
    fn test_producer_consumer_pair() {
        let pair = ProducerConsumerPair::<i32>::new(32).unwrap();
        let producer = pair.producer();

        // Send some data
        for i in 0..5 {
            producer.send(i).unwrap();
        }

        // Need to close the channel so the iterator knows when to stop
        // For now, just use take() to limit the iteration
        let iter = pair.into_streaming_iter();
        let collected: Vec<_> = iter.take(5).collect();

        assert_eq!(collected, vec![0, 1, 2, 3, 4]);
    }

    #[test]
    fn test_pipeline_basic() {
        let data = vec![1, 2, 3, 4, 5];

        let result = pipeline(data).map(|x| x * 2).filter(|&x| x > 4).collect();

        assert_eq!(result, vec![6, 8, 10]);
    }

    #[test]
    fn test_cache_aware_iterator() {
        let data = (0..100).collect::<Vec<i32>>();
        let iter = CacheAwareIterator::new(data.clone());

        let collected: Vec<_> = iter.take(10).collect();
        assert_eq!(collected, (0..10).collect::<Vec<_>>());
    }

    #[test]
    fn test_pipeline_chaining() {
        let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        let result = pipeline(data)
            .filter(|&x| x % 2 == 0) // Keep even numbers: [2, 4, 6, 8, 10]
            .map(|x| x * x) // Square them: [4, 16, 36, 64, 100]
            .filter(|&x| x < 50) // Keep < 50: [4, 16, 36]
            .collect();

        assert_eq!(result, vec![4, 16, 36]);
    }
}