miden-utils-indexing 0.22.2

Type-safe u32-indexed vector utilities for Miden
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
//! Compressed Sparse Row (CSR) matrix for efficient sparse data storage.
//!
//! This module provides a generic [`CsrMatrix`] type that maps row indices to variable-length
//! data. It's commonly used for storing decorator IDs, assembly operation IDs, and similar
//! sparse mappings in the Miden VM.

use alloc::vec::Vec;

use miden_crypto::utils::{
    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{Idx, IndexVec, IndexedVecError};

// CSR MATRIX
// ================================================================================================

/// Compressed Sparse Row matrix mapping row indices to variable-length data.
///
/// For row `i`, its data is at `data[indptr[i]..indptr[i+1]]`.
///
/// # Type Parameters
///
/// - `I`: The row index type, must implement [`Idx`].
/// - `D`: The data type stored in each row.
///
/// # Example
///
/// ```ignore
/// use miden_utils_indexing::{CsrMatrix, newtype_id};
///
/// newtype_id!(NodeId);
///
/// let mut csr = CsrMatrix::<NodeId, u32>::new();
/// csr.push_row([1, 2, 3]);       // Row 0: [1, 2, 3]
/// csr.push_empty_row();          // Row 1: []
/// csr.push_row([4, 5]);          // Row 2: [4, 5]
///
/// assert_eq!(csr.row(NodeId::from(0)), Some(&[1, 2, 3][..]));
/// assert_eq!(csr.row(NodeId::from(1)), Some(&[][..]));
/// assert_eq!(csr.row(NodeId::from(2)), Some(&[4, 5][..]));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CsrMatrix<I: Idx, D> {
    /// Flat storage of all data values.
    data: Vec<D>,
    /// Row pointers: row i's data is at `data[indptr[i]..indptr[i+1]]`.
    indptr: IndexVec<I, usize>,
}

impl<I: Idx, D> Default for CsrMatrix<I, D> {
    fn default() -> Self {
        Self::new()
    }
}

impl<I: Idx, D> CsrMatrix<I, D> {
    // CONSTRUCTORS
    // --------------------------------------------------------------------------------------------

    /// Creates a new empty [`CsrMatrix`].
    pub fn new() -> Self {
        Self {
            data: Vec::new(),
            indptr: IndexVec::new(),
        }
    }

    /// Creates a [`CsrMatrix`] with pre-allocated capacity.
    ///
    /// # Arguments
    ///
    /// - `num_rows`: Expected number of rows.
    /// - `num_elements`: Expected total number of data elements across all rows.
    pub fn with_capacity(num_rows: usize, num_elements: usize) -> Self {
        Self {
            data: Vec::with_capacity(num_elements),
            indptr: IndexVec::with_capacity(num_rows + 1),
        }
    }

    // MUTATION
    // --------------------------------------------------------------------------------------------

    /// Appends a new row with the given data values and returns the index of the newly added row.
    ///
    /// Rows must be added in sequential order starting from row 0.
    ///
    /// # Errors
    ///
    /// Returns an error if the number of rows would exceed `u32::MAX`.
    pub fn push_row(&mut self, values: impl IntoIterator<Item = D>) -> Result<I, IndexedVecError> {
        // Initialize indptr with 0 if this is the first row
        if self.indptr.is_empty() {
            self.indptr.push(0)?;
        }

        // The row index is the current number of rows (before adding)
        let row_idx = self.num_rows();

        // Add data
        self.data.extend(values);

        // Add end pointer for this row
        self.indptr.push(self.data.len())?;

        Ok(I::from(row_idx as u32))
    }

    /// Appends an empty row (no data for this row index).
    ///
    /// # Errors
    ///
    /// Returns an error if the number of rows would exceed `u32::MAX`.
    pub fn push_empty_row(&mut self) -> Result<I, IndexedVecError> {
        self.push_row(core::iter::empty())
    }

    /// Appends empty rows to fill gaps up to (but not including) `target_row`.
    ///
    /// If `target_row` is less than or equal to the current number of rows, this is a no-op.
    ///
    /// # Errors
    ///
    /// Returns an error if the number of rows would exceed `u32::MAX`.
    pub fn fill_to_row(&mut self, target_row: I) -> Result<(), IndexedVecError> {
        let target = target_row.to_usize();
        while self.num_rows() < target {
            self.push_empty_row()?;
        }
        Ok(())
    }

    // ACCESSORS
    // --------------------------------------------------------------------------------------------

    /// Returns `true` if this matrix has no rows.
    pub fn is_empty(&self) -> bool {
        self.indptr.is_empty()
    }

    /// Returns the number of rows in this matrix.
    pub fn num_rows(&self) -> usize {
        if self.indptr.is_empty() {
            0
        } else {
            self.indptr.len() - 1
        }
    }

    /// Returns the total number of data elements across all rows.
    pub fn num_elements(&self) -> usize {
        self.data.len()
    }

    /// Returns the data slice for the given row, or `None` if the row doesn't exist.
    pub fn row(&self, row: I) -> Option<&[D]> {
        let row_idx = row.to_usize();
        if row_idx >= self.num_rows() {
            return None;
        }

        let start = self.indptr[row];
        let end = self.indptr[I::from((row_idx + 1) as u32)];
        Some(&self.data[start..end])
    }

    /// Returns the data slice for the given row, panicking if the row doesn't exist.
    ///
    /// # Panics
    ///
    /// Panics if `row` is out of bounds.
    pub fn row_expect(&self, row: I) -> &[D] {
        self.row(row).expect("row index out of bounds")
    }

    /// Returns an iterator over all `(row_index, data_slice)` pairs.
    pub fn iter(&self) -> impl Iterator<Item = (I, &[D])> {
        (0..self.num_rows()).map(move |i| {
            let row = I::from(i as u32);
            (row, self.row_expect(row))
        })
    }

    /// Returns an iterator over all data elements with their `(row_index, position_in_row, &data)`.
    pub fn iter_enumerated(&self) -> impl Iterator<Item = (I, usize, &D)> {
        self.iter()
            .flat_map(|(row, data)| data.iter().enumerate().map(move |(pos, d)| (row, pos, d)))
    }

    /// Returns the underlying data slice.
    pub fn data(&self) -> &[D] {
        &self.data
    }

    /// Returns the underlying indptr.
    pub fn indptr(&self) -> &IndexVec<I, usize> {
        &self.indptr
    }

    // VALIDATION
    // --------------------------------------------------------------------------------------------

    /// Validates the CSR structural invariants.
    ///
    /// Checks:
    /// - `indptr` starts at 0 (if non-empty)
    /// - `indptr` is monotonically increasing
    /// - `indptr` ends at `data.len()`
    ///
    /// For domain-specific validation of data values, use [`validate_with`](Self::validate_with).
    pub fn validate(&self) -> Result<(), CsrValidationError> {
        self.validate_with(|_| true)
    }

    /// Validates structural invariants plus domain-specific data constraints.
    ///
    /// The callback is invoked for each data element. Return `false` to indicate
    /// an invalid value.
    ///
    /// # Arguments
    ///
    /// - `f`: A function that returns `true` if the data value is valid.
    pub fn validate_with<F>(&self, f: F) -> Result<(), CsrValidationError>
    where
        F: Fn(&D) -> bool,
    {
        let indptr = self.indptr.as_slice();

        // Empty matrix is valid
        if indptr.is_empty() {
            return Ok(());
        }

        // Check indptr starts at 0
        if indptr[0] != 0 {
            return Err(CsrValidationError::IndptrStartNotZero(indptr[0]));
        }

        // Check indptr is monotonic
        for i in 1..indptr.len() {
            if indptr[i - 1] > indptr[i] {
                return Err(CsrValidationError::IndptrNotMonotonic {
                    index: i,
                    prev: indptr[i - 1],
                    curr: indptr[i],
                });
            }
        }

        // Check indptr ends at data.len()
        let last = *indptr.last().expect("indptr is non-empty");
        if last != self.data.len() {
            return Err(CsrValidationError::IndptrDataMismatch {
                indptr_end: last,
                data_len: self.data.len(),
            });
        }

        // Validate data values
        for (row, data) in self.iter() {
            for (pos, d) in data.iter().enumerate() {
                if !f(d) {
                    return Err(CsrValidationError::InvalidData {
                        row: row.to_usize(),
                        position: pos,
                    });
                }
            }
        }

        Ok(())
    }
}

// CSR VALIDATION ERROR
// ================================================================================================

/// Errors that can occur during CSR validation.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum CsrValidationError {
    /// The indptr array must start at 0.
    #[error("indptr must start at 0, got {0}")]
    IndptrStartNotZero(usize),

    /// The indptr array must be monotonically increasing.
    #[error("indptr not monotonic at index {index}: {prev} > {curr}")]
    IndptrNotMonotonic { index: usize, prev: usize, curr: usize },

    /// The last indptr value must equal data.len().
    #[error("indptr ends at {indptr_end}, but data.len() is {data_len}")]
    IndptrDataMismatch { indptr_end: usize, data_len: usize },

    /// A data value failed domain-specific validation.
    #[error("invalid data value at row {row}, position {position}")]
    InvalidData { row: usize, position: usize },
}

// SERIALIZATION
// ================================================================================================

impl<I, D> Serializable for CsrMatrix<I, D>
where
    I: Idx,
    D: Serializable,
{
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        // Write data
        target.write_usize(self.data.len());
        for item in &self.data {
            item.write_into(target);
        }

        // Write indptr
        target.write_usize(self.indptr.len());
        for &ptr in self.indptr.as_slice() {
            target.write_usize(ptr);
        }
    }
}

impl<I, D> Deserializable for CsrMatrix<I, D>
where
    I: Idx,
    D: Deserializable,
{
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        // Read data using read_many_iter for BudgetedReader integration
        let data_len = source.read_usize()?;
        let data: Vec<D> = source.read_many_iter(data_len)?.collect::<Result<_, _>>()?;

        // Read indptr using read_many_iter for BudgetedReader integration
        let indptr_len = source.read_usize()?;
        let indptr_vec: Vec<usize> =
            source.read_many_iter(indptr_len)?.collect::<Result<_, _>>()?;
        let indptr = IndexVec::try_from(indptr_vec).map_err(|_| {
            DeserializationError::InvalidValue("indptr too large for IndexVec".into())
        })?;

        Ok(Self { data, indptr })
    }

    /// Returns the minimum serialized size for a CsrMatrix.
    ///
    /// A CsrMatrix serializes as:
    /// - data_len (vint, minimum 1 byte)
    /// - data elements (minimum 0 if empty)
    /// - indptr_len (vint, minimum 1 byte)
    /// - indptr elements (minimum 0 if empty)
    ///
    /// Total minimum: 2 bytes (two vint length prefixes for empty matrix)
    fn min_serialized_size() -> usize {
        2
    }
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
    use alloc::vec;

    use super::*;
    use crate::newtype_id;

    newtype_id!(TestRowId);

    #[test]
    fn test_new_is_empty() {
        let csr = CsrMatrix::<TestRowId, u32>::new();
        assert!(csr.is_empty());
        assert_eq!(csr.num_rows(), 0);
        assert_eq!(csr.num_elements(), 0);
    }

    #[test]
    fn test_push_row() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();

        let id0 = csr.push_row([1, 2, 3]).unwrap();
        assert_eq!(id0, TestRowId::from(0));
        assert_eq!(csr.num_rows(), 1);
        assert_eq!(csr.num_elements(), 3);
        assert_eq!(csr.row(TestRowId::from(0)), Some(&[1, 2, 3][..]));

        let id1 = csr.push_row([4, 5]).unwrap();
        assert_eq!(id1, TestRowId::from(1));
        assert_eq!(csr.num_rows(), 2);
        assert_eq!(csr.num_elements(), 5);
        assert_eq!(csr.row(TestRowId::from(1)), Some(&[4, 5][..]));
    }

    #[test]
    fn test_push_empty_row() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();

        csr.push_row([1, 2]).unwrap();
        csr.push_empty_row().unwrap();
        csr.push_row([3]).unwrap();

        assert_eq!(csr.num_rows(), 3);
        assert_eq!(csr.row(TestRowId::from(0)), Some(&[1, 2][..]));
        assert_eq!(csr.row(TestRowId::from(1)), Some(&[][..]));
        assert_eq!(csr.row(TestRowId::from(2)), Some(&[3][..]));
    }

    #[test]
    fn test_fill_to_row() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();

        csr.push_row([1]).unwrap();
        csr.fill_to_row(TestRowId::from(3)).unwrap();
        csr.push_row([2]).unwrap();

        assert_eq!(csr.num_rows(), 4);
        assert_eq!(csr.row(TestRowId::from(0)), Some(&[1][..]));
        assert_eq!(csr.row(TestRowId::from(1)), Some(&[][..]));
        assert_eq!(csr.row(TestRowId::from(2)), Some(&[][..]));
        assert_eq!(csr.row(TestRowId::from(3)), Some(&[2][..]));
    }

    #[test]
    fn test_row_out_of_bounds() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();
        csr.push_row([1]).unwrap();

        assert_eq!(csr.row(TestRowId::from(0)), Some(&[1][..]));
        assert_eq!(csr.row(TestRowId::from(1)), None);
        assert_eq!(csr.row(TestRowId::from(100)), None);
    }

    #[test]
    fn test_iter() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();
        csr.push_row([1, 2]).unwrap();
        csr.push_empty_row().unwrap();
        csr.push_row([3]).unwrap();

        let items: alloc::vec::Vec<_> = csr.iter().collect();
        assert_eq!(items.len(), 3);
        assert_eq!(items[0], (TestRowId::from(0), &[1, 2][..]));
        assert_eq!(items[1], (TestRowId::from(1), &[][..]));
        assert_eq!(items[2], (TestRowId::from(2), &[3][..]));
    }

    #[test]
    fn test_iter_enumerated() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();
        csr.push_row([10, 20]).unwrap();
        csr.push_row([30]).unwrap();

        let items: alloc::vec::Vec<_> = csr.iter_enumerated().collect();
        assert_eq!(items.len(), 3);
        assert_eq!(items[0], (TestRowId::from(0), 0, &10));
        assert_eq!(items[1], (TestRowId::from(0), 1, &20));
        assert_eq!(items[2], (TestRowId::from(1), 0, &30));
    }

    #[test]
    fn test_validate_empty() {
        let csr = CsrMatrix::<TestRowId, u32>::new();
        assert!(csr.validate().is_ok());
    }

    #[test]
    fn test_validate_valid() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();
        csr.push_row([1, 2, 3]).unwrap();
        csr.push_empty_row().unwrap();
        csr.push_row([4]).unwrap();

        assert!(csr.validate().is_ok());
    }

    #[test]
    fn test_validate_with_callback() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();
        csr.push_row([1, 2, 3]).unwrap();
        csr.push_row([4, 5]).unwrap();

        // All values < 10: valid
        assert!(csr.validate_with(|&v| v < 10).is_ok());

        // All values < 4: invalid (first failure is 4 at row 1, position 0)
        let result = csr.validate_with(|&v| v < 4);
        assert!(matches!(result, Err(CsrValidationError::InvalidData { row: 1, position: 0 })));
    }

    #[test]
    fn test_serialization_roundtrip() {
        let mut csr = CsrMatrix::<TestRowId, u32>::new();
        csr.push_row([1, 2, 3]).unwrap();
        csr.push_empty_row().unwrap();
        csr.push_row([4, 5]).unwrap();

        // Serialize
        let mut bytes = vec![];
        csr.write_into(&mut bytes);

        // Deserialize
        let mut reader = miden_crypto::utils::SliceReader::new(&bytes);
        let restored: CsrMatrix<TestRowId, u32> = CsrMatrix::read_from(&mut reader).unwrap();

        assert_eq!(csr, restored);
    }
}