use std::marker::PhantomData; use rustc_index::vec::Idx; #[derive(Default)] pub struct AppendOnlyIndexVec { #[cfg(not(parallel_compiler))] vec: elsa::vec::FrozenVec, #[cfg(parallel_compiler)] vec: elsa::sync::LockFreeFrozenVec, _marker: PhantomData, } impl AppendOnlyIndexVec { pub fn new() -> Self { Self { #[cfg(not(parallel_compiler))] vec: elsa::vec::FrozenVec::new(), #[cfg(parallel_compiler)] vec: elsa::sync::LockFreeFrozenVec::new(), _marker: PhantomData, } } pub fn push(&self, val: T) -> I { #[cfg(not(parallel_compiler))] let i = self.vec.len(); #[cfg(not(parallel_compiler))] self.vec.push(val); #[cfg(parallel_compiler)] let i = self.vec.push(val); I::new(i) } pub fn get(&self, i: I) -> Option { let i = i.index(); #[cfg(not(parallel_compiler))] return self.vec.get_copy(i); #[cfg(parallel_compiler)] return self.vec.get(i); } } #[derive(Default)] pub struct AppendOnlyVec { #[cfg(not(parallel_compiler))] vec: elsa::vec::FrozenVec, #[cfg(parallel_compiler)] vec: elsa::sync::LockFreeFrozenVec, } impl AppendOnlyVec { pub fn new() -> Self { Self { #[cfg(not(parallel_compiler))] vec: elsa::vec::FrozenVec::new(), #[cfg(parallel_compiler)] vec: elsa::sync::LockFreeFrozenVec::new(), } } pub fn push(&self, val: T) -> usize { #[cfg(not(parallel_compiler))] let i = self.vec.len(); #[cfg(not(parallel_compiler))] self.vec.push(val); #[cfg(parallel_compiler)] let i = self.vec.push(val); i } pub fn get(&self, i: usize) -> Option { #[cfg(not(parallel_compiler))] return self.vec.get_copy(i); #[cfg(parallel_compiler)] return self.vec.get(i); } pub fn iter_enumerated(&self) -> impl Iterator + '_ { (0..) .map(|i| (i, self.get(i))) .take_while(|(_, o)| o.is_some()) .filter_map(|(i, o)| Some((i, o?))) } pub fn iter(&self) -> impl Iterator + '_ { (0..).map(|i| self.get(i)).take_while(|o| o.is_some()).flatten() } } impl AppendOnlyVec { pub fn contains(&self, val: T) -> bool { self.iter_enumerated().any(|(_, v)| v == val) } } impl FromIterator for AppendOnlyVec { fn from_iter>(iter: T) -> Self { let this = Self::new(); for val in iter { this.push(val); } this } }