honeycomb_core/cmap/components/
unused.rs

1use std::{
2    ops::{Index, IndexMut},
3    slice::Iter,
4};
5
6#[cfg(feature = "par-internals")]
7use rayon::prelude::*;
8
9use crate::stm::TVar;
10
11use super::identifiers::DartIdType;
12
13/// Unused dart tracking structure.
14pub struct UnusedDarts(Vec<TVar<bool>>);
15
16#[allow(unused)]
17impl UnusedDarts {
18    #[cfg(not(feature = "par-internals"))]
19    /// Constructor
20    pub fn new(n_darts: usize) -> Self {
21        Self((0..n_darts).map(|_| TVar::new(false)).collect())
22    }
23
24    #[cfg(feature = "par-internals")]
25    /// Constructor
26    pub fn new(n_darts: usize) -> Self {
27        Self(
28            (0..n_darts)
29                .into_par_iter()
30                .map(|_| TVar::new(false))
31                .collect(),
32        )
33    }
34
35    #[cfg(not(feature = "par-internals"))]
36    /// Extend internal storage capacity
37    pub fn extend_with(&mut self, len: usize, val: bool) {
38        self.0.extend((0..len).map(|_| TVar::new(val)));
39    }
40
41    #[cfg(feature = "par-internals")]
42    /// Extend internal storage capacity
43    pub fn extend_with(&mut self, len: usize, val: bool) {
44        self.0
45            .par_extend((0..len).into_par_iter().map(|_| TVar::new(val)));
46    }
47
48    /// Return internal storage length
49    pub fn len(&self) -> usize {
50        self.0.len()
51    }
52
53    pub fn iter(&self) -> Iter<'_, TVar<bool>> {
54        self.0.iter()
55    }
56
57    #[cfg(feature = "par-internals")]
58    pub fn par_iter(&self) -> rayon::slice::Iter<'_, TVar<bool>> {
59        self.0.par_iter()
60    }
61}
62
63impl Index<DartIdType> for UnusedDarts {
64    type Output = TVar<bool>;
65
66    fn index(&self, dart_id: DartIdType) -> &Self::Output {
67        &self.0[dart_id as usize]
68    }
69}
70
71impl IndexMut<DartIdType> for UnusedDarts {
72    fn index_mut(&mut self, dart_id: DartIdType) -> &mut Self::Output {
73        &mut self.0[dart_id as usize]
74    }
75}