pipeline-dsl 0.0.3

Pipeline DSL types and re-exports of #[pipeline]/#[stage].
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
use crate::Error;
use crate::Reset;
use bitvec::prelude::*;
use std::vec::Vec;

pub struct Vector<V> {
    data: Vec<V>,         // Stores the actual values
    update_flags: BitVec, // Bit vector to track which values are updated
    indices: Vec<usize>,  // Indices of updated elements for efficient iteration
    is_updated: bool,     // Flag to indicate if any element is updated
}

impl<V> Default for Vector<V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<V> Vector<V> {
    /// Creates a new, empty `Vector`.
    pub fn new() -> Self {
        Self {
            data: Vec::new(),
            update_flags: BitVec::new(),
            indices: Vec::new(),
            is_updated: false,
        }
    }

    /// Creates a `Vector` with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            update_flags: bitvec![0; capacity],
            indices: Vec::with_capacity(capacity),
            is_updated: false,
        }
    }

    /// Returns `true` if any element in the `Vector` is updated.
    pub fn is_updated(&self) -> bool {
        self.is_updated
    }

    /// Returns `true` if all elements in the `Vector` are updated.
    pub fn all_updated(&self) -> bool {
        self.is_updated && self.indices.is_empty()
    }

    /// Clears the `Vector`, removing all values.
    pub fn clear(&mut self) {
        self.data.clear();
        self.update_flags.clear();
        self.indices.clear();
        self.is_updated = false;
    }

    /// Returns the number of elements in the `Vector`.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns `true` if the `Vector` contains no elements.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Returns an iterator over the elements of the `Vector`.
    pub fn iter(&self) -> std::slice::Iter<'_, V> {
        self.data.iter()
    }

    /// Returns a mutable iterator over the elements of the `Vector`.
    /// Marks all elements as updated.
    pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, V> {
        if !self.all_updated() {
            self.is_updated = true;
            self.indices.clear();
            self.update_flags.clear();
        }
        self.data.iter_mut()
    }

    /// Returns a reference to the element at the given index, or `None` if out of bounds.
    pub fn get(&self, index: usize) -> Option<&V> {
        self.data.get(index)
    }

    /// Returns a reference to the updated element at the given index, or `None` if not updated or out of bounds.
    pub fn get_updated(&self, index: usize) -> Option<&V> {
        if self.all_updated() || (self.update_flags.get(index).as_deref() == Some(&true)) {
            self.data.get(index)
        } else {
            None
        }
    }

    /// Returns a mutable reference to the element at the given index, marking it as updated.
    pub fn get_mut(&mut self, index: usize) -> Option<&mut V> {
        if index >= self.data.len() {
            return None;
        }

        if self.all_updated() {
            // All elements are already updated
        } else {
            // Ensure `update_flags` is large enough
            if index >= self.update_flags.len() {
                self.update_flags.resize(self.data.len(), false);
            }

            // Mark this index as updated
            if self.update_flags.get(index).as_deref() != Some(&true) {
                self.update_flags.set(index, true);
                self.indices.push(index);
                self.is_updated = true;

                // Check if all elements are now updated
                if self.indices.len() == self.data.len() {
                    // All elements are updated; optimize storage
                    self.indices.clear();
                    self.update_flags.clear();
                }
            }
        }

        self.data.get_mut(index)
    }

    /// Appends an element to the back of the `Vector`, marking it as updated.
    pub fn push(&mut self, value: V) {
        self.data.push(value);
        if self.all_updated() {
            // All elements are updated; no need to track individually
        } else {
            // Mark new element as updated
            self.update_flags.push(true);
            self.indices.push(self.data.len() - 1);
            self.is_updated = true;

            // Check if all elements are now updated
            if self.indices.len() == self.data.len() {
                // All elements are updated; optimize storage
                self.indices.clear();
                self.update_flags.clear();
            }
        }
    }

    /// Removes the last element from the `Vector` and returns it, or `None` if empty.
    pub fn pop(&mut self) -> Option<V> {
        let value = self.data.pop();
        if value.is_some() {
            let index = self.data.len(); // Index of the removed element

            if self.all_updated() {
                // All elements are updated; no need to modify `indices` or `update_flags`
            } else {
                self.update_flags.pop();

                // Remove index from `indices` if present
                if let Some(pos) = self.indices.iter().position(|&i| i == index) {
                    let last = self.indices.len() - 1;
                    if pos != last {
                        // Swap the element at 'pos' with the last element
                        self.indices.swap(pos, last);
                    }
                    // Remove the last element
                    self.indices.pop();
                }
            }

            // **Update `is_updated` flag appropriately**
            if self.data.is_empty() {
                // If the vector is empty, there are no updates
                self.is_updated = false;
            } else {
                // Otherwise, update based on remaining elements
                self.is_updated = self.all_updated() || !self.indices.is_empty();
            }
        }
        value
    }
}

impl<V> Reset for Vector<V> {
    type Error = Error;
    fn reset(&mut self) -> Result<(), Error> {
        self.update_flags.clear();
        self.update_flags.resize(self.data.len(), false);
        self.indices.clear();
        self.is_updated = false;
        Ok(())
    }
}

pub trait IterUpdated<'a, K: 'a, V: 'a> {
    type Iter: Iterator<Item = (K, &'a V)>;
    fn iter_updated(&'a self) -> Self::Iter;
}

impl<'a, V: 'a> IterUpdated<'a, usize, V> for Vector<V> {
    type Iter = IterUpdatedItems<'a, V>;

    fn iter_updated(&'a self) -> Self::Iter {
        if self.all_updated() {
            IterUpdatedItems {
                vector: self,
                indices_iter: None,
                current_index: 0,
                all_updated: true,
            }
        } else {
            IterUpdatedItems {
                vector: self,
                indices_iter: Some(self.indices.iter()),
                current_index: 0,
                all_updated: false,
            }
        }
    }
}

pub struct IterUpdatedItems<'a, V> {
    vector: &'a Vector<V>,
    indices_iter: Option<std::slice::Iter<'a, usize>>,
    current_index: usize,
    all_updated: bool,
}

impl<'a, V> Iterator for IterUpdatedItems<'a, V> {
    type Item = (usize, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        if self.all_updated {
            if self.current_index < self.vector.data.len() {
                let index = self.current_index;
                self.current_index += 1;
                Some((index, &self.vector.data[index]))
            } else {
                None
            }
        } else if let Some(ref mut indices_iter) = self.indices_iter {
            indices_iter
                .next()
                .map(|&index| (index, &self.vector.data[index]))
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_new_vector() {
        let vec: Vector<i32> = Vector::new();
        assert_eq!(vec.len(), 0);
        assert!(vec.is_empty());
        assert!(!vec.is_updated());
        assert!(!vec.all_updated());
    }

    #[test]
    fn test_with_capacity() {
        let vec: Vector<i32> = Vector::with_capacity(10);
        assert_eq!(vec.len(), 0);
        assert_eq!(vec.data.capacity(), 10);
    }

    #[test]
    fn test_get_mut() {
        let mut vec = Vector::new();
        vec.push(10);
        vec.push(20);
        vec.push(30);

        // Modify an element
        if let Some(val) = vec.get_mut(1) {
            *val = 200;
        }

        assert_eq!(vec.get(1), Some(&200));
        assert!(vec.is_updated());
        assert_eq!(vec.indices.len(), 0);
        assert!(vec.all_updated());

        // Check if the element is marked as updated
        assert_eq!(vec.get_updated(1), Some(&200));

        // Modify another element
        if let Some(val) = vec.get_mut(0) {
            *val = 100;
        }

        assert_eq!(vec.get(0), Some(&100));
        assert_eq!(vec.indices.len(), 0);
        assert!(vec.all_updated());
    }

    #[test]
    fn test_iter_updated() -> Result<(), Error> {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        // Modify an element
        if let Some(val) = vec.get_mut(1) {
            *val = 20;
        }

        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert_eq!(updated.len(), 3);
        assert_eq!(updated, vec![(0, 1), (1, 20), (2, 3)]);

        // Reset and check
        vec.reset()?;
        assert!(!vec.is_updated());
        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert!(updated.is_empty());

        // Modify an element
        if let Some(val) = vec.get_mut(1) {
            *val = 200;
        }
        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert_eq!(updated.len(), 1);
        assert_eq!(updated, vec![(1, 200)]);
        Ok(())
    }

    #[test]
    fn test_all_updated() {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        // Mark all as updated using iter_mut
        for val in vec.iter_mut() {
            *val += 10;
        }

        assert!(vec.all_updated());
        assert_eq!(vec.indices.len(), 0);
        assert_eq!(vec.update_flags.len(), 0);

        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert_eq!(updated.len(), 3);
        assert_eq!(updated, vec![(0, 11), (1, 12), (2, 13)]);
    }

    #[test]
    fn test_reset() -> Result<(), Error> {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        assert!(vec.is_updated());

        vec.reset()?;
        assert!(!vec.is_updated());
        assert!(!vec.all_updated());
        assert_eq!(vec.indices.len(), 0);
        assert_eq!(vec.update_flags.len(), 3);

        // After reset, get_updated should return None
        assert_eq!(vec.get_updated(0), None);

        // Modify an element
        if let Some(val) = vec.get_mut(1) {
            *val = 20;
        }

        assert!(vec.is_updated());
        assert!(!vec.all_updated());
        assert_eq!(vec.get_updated(1), Some(&20));
        Ok(())
    }

    #[test]
    fn test_clear() {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        vec.clear();
        assert_eq!(vec.len(), 0);
        assert!(vec.is_empty());
        assert!(!vec.is_updated());
        assert!(!vec.all_updated());
    }

    #[test]
    fn test_pop() {
        let mut vec = Vector::new();
        vec.push(10);
        vec.push(20);
        vec.push(30);

        let val = vec.pop();
        assert_eq!(val, Some(30));
        assert_eq!(vec.len(), 2);
        assert!(vec.is_updated());
        assert_eq!(vec.indices.len(), 0);
        assert!(vec.all_updated());

        // The removed index should not affect indices since they are cleared
        // Pop until empty
        vec.pop();
        vec.pop();
        assert!(vec.is_empty());
        assert!(!vec.is_updated());
    }

    #[test]
    fn test_get_out_of_bounds() {
        let mut vec = Vector::new();
        vec.push(1);

        assert_eq!(vec.get(1), None);
        assert_eq!(vec.get_mut(1), None);
        assert_eq!(vec.get_updated(1), None);
    }

    #[test]
    fn test_iter() {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        let collected: Vec<&i32> = vec.iter().collect();
        assert_eq!(collected, vec![&1, &2, &3]);

        for val in vec.iter_mut() {
            *val *= 2;
        }

        let collected: Vec<&i32> = vec.iter().collect();
        assert_eq!(collected, vec![&2, &4, &6]);
    }

    #[test]
    fn test_push_after_all_updated() {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);

        // Mark all as updated
        vec.iter_mut().for_each(|_| {});

        // Now push a new element
        vec.push(3);
        assert!(vec.all_updated());

        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert_eq!(updated, vec![(0, 1), (1, 2), (2, 3)]);
    }

    #[test]
    fn test_multiple_updates() {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        // Update index 0
        if let Some(v) = vec.get_mut(0) {
            *v = 10;
        }

        assert_eq!(vec.indices.len(), 0);
        assert!(vec.all_updated());

        // Update index 1
        if let Some(v) = vec.get_mut(1) {
            *v = 20;
        }
        assert_eq!(vec.indices.len(), 0);

        // Update index 2
        if let Some(v) = vec.get_mut(2) {
            *v = 30
        }
        // All elements are updated
        assert!(vec.all_updated());
        assert_eq!(vec.indices.len(), 0);

        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert_eq!(updated, vec![(0, 10), (1, 20), (2, 30)]);
    }
    #[test]
    fn test_reset_after_all_updated() -> Result<(), Error> {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);

        // Mark all as updated
        vec.iter_mut().for_each(|v| *v += 1);

        assert!(vec.all_updated());

        // Reset
        vec.reset()?;
        assert!(!vec.is_updated());
        assert!(!vec.all_updated());

        // Check that no elements are updated
        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert!(updated.is_empty());

        // Modify one element
        if let Some(v) = vec.get_mut(0) {
            *v = 10;
        }
        assert!(vec.is_updated());
        assert!(!vec.all_updated());

        let updated: Vec<(usize, i32)> = vec.iter_updated().map(|(i, &v)| (i, v)).collect();
        assert_eq!(updated, vec![(0, 10)]);
        Ok(())
    }

    #[test]
    fn test_push_and_get() {
        let mut vec = Vector::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);

        assert_eq!(vec.len(), 3);
        assert_eq!(vec.get(0), Some(&1));
        assert_eq!(vec.get(1), Some(&2));
        assert_eq!(vec.get(2), Some(&3));
        assert_eq!(vec.get(3), None);

        assert!(vec.is_updated());
        assert!(vec.all_updated());

        // Test get_updated
        assert_eq!(vec.get_updated(0), Some(&1));
        assert_eq!(vec.get_updated(1), Some(&2));
        assert_eq!(vec.get_updated(2), Some(&3));
        assert_eq!(vec.get_updated(3), None);
    }
}