binius_utils/
sparse_index.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
// Copyright 2024-2025 Irreducible Inc.

/// An index mapping positive integer IDs to optional values.
#[derive(Debug)]
pub struct SparseIndex<T> {
	entries: Vec<Option<T>>,
}

impl<T: Clone> SparseIndex<T> {
	pub fn new(id_bound: usize) -> Self {
		Self {
			entries: vec![None; id_bound],
		}
	}

	pub fn get(&self, id: usize) -> Option<&T> {
		self.entries.get(id)?.as_ref()
	}

	pub fn set(&mut self, id: usize, val: T) {
		if self.entries.len() <= id {
			self.entries.resize(id + 1, None);
		}
		self.entries[id] = Some(val);
	}
}