From 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:39 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_data_structures/src/sync/vec.rs | 68 +++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_data_structures/src/sync/vec.rs') diff --git a/compiler/rustc_data_structures/src/sync/vec.rs b/compiler/rustc_data_structures/src/sync/vec.rs index cbea4f059..1783b4b35 100644 --- a/compiler/rustc_data_structures/src/sync/vec.rs +++ b/compiler/rustc_data_structures/src/sync/vec.rs @@ -2,7 +2,8 @@ use std::marker::PhantomData; use rustc_index::vec::Idx; -pub struct AppendOnlyVec { +#[derive(Default)] +pub struct AppendOnlyIndexVec { #[cfg(not(parallel_compiler))] vec: elsa::vec::FrozenVec, #[cfg(parallel_compiler)] @@ -10,7 +11,7 @@ pub struct AppendOnlyVec { _marker: PhantomData, } -impl AppendOnlyVec { +impl AppendOnlyIndexVec { pub fn new() -> Self { Self { #[cfg(not(parallel_compiler))] @@ -39,3 +40,66 @@ impl AppendOnlyVec { 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 + } +} -- cgit v1.2.3