]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_data_structures/src/sync/vec.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / compiler / rustc_data_structures / src / sync / vec.rs
CommitLineData
9ffffee4
FG
1use std::marker::PhantomData;
2
49aad941 3use rustc_index::Idx;
9ffffee4 4
353b0b11
FG
5#[derive(Default)]
6pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
9ffffee4
FG
7 #[cfg(not(parallel_compiler))]
8 vec: elsa::vec::FrozenVec<T>,
9 #[cfg(parallel_compiler)]
10 vec: elsa::sync::LockFreeFrozenVec<T>,
11 _marker: PhantomData<fn(&I)>,
12}
13
353b0b11 14impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
9ffffee4
FG
15 pub fn new() -> Self {
16 Self {
17 #[cfg(not(parallel_compiler))]
18 vec: elsa::vec::FrozenVec::new(),
19 #[cfg(parallel_compiler)]
20 vec: elsa::sync::LockFreeFrozenVec::new(),
21 _marker: PhantomData,
22 }
23 }
24
25 pub fn push(&self, val: T) -> I {
26 #[cfg(not(parallel_compiler))]
27 let i = self.vec.len();
28 #[cfg(not(parallel_compiler))]
29 self.vec.push(val);
30 #[cfg(parallel_compiler)]
31 let i = self.vec.push(val);
32 I::new(i)
33 }
34
35 pub fn get(&self, i: I) -> Option<T> {
36 let i = i.index();
37 #[cfg(not(parallel_compiler))]
38 return self.vec.get_copy(i);
39 #[cfg(parallel_compiler)]
40 return self.vec.get(i);
41 }
42}
353b0b11
FG
43
44#[derive(Default)]
45pub struct AppendOnlyVec<T: Copy> {
46 #[cfg(not(parallel_compiler))]
47 vec: elsa::vec::FrozenVec<T>,
48 #[cfg(parallel_compiler)]
49 vec: elsa::sync::LockFreeFrozenVec<T>,
50}
51
52impl<T: Copy> AppendOnlyVec<T> {
53 pub fn new() -> Self {
54 Self {
55 #[cfg(not(parallel_compiler))]
56 vec: elsa::vec::FrozenVec::new(),
57 #[cfg(parallel_compiler)]
58 vec: elsa::sync::LockFreeFrozenVec::new(),
59 }
60 }
61
62 pub fn push(&self, val: T) -> usize {
63 #[cfg(not(parallel_compiler))]
64 let i = self.vec.len();
65 #[cfg(not(parallel_compiler))]
66 self.vec.push(val);
67 #[cfg(parallel_compiler)]
68 let i = self.vec.push(val);
69 i
70 }
71
72 pub fn get(&self, i: usize) -> Option<T> {
73 #[cfg(not(parallel_compiler))]
74 return self.vec.get_copy(i);
75 #[cfg(parallel_compiler)]
76 return self.vec.get(i);
77 }
78
79 pub fn iter_enumerated(&self) -> impl Iterator<Item = (usize, T)> + '_ {
80 (0..)
81 .map(|i| (i, self.get(i)))
82 .take_while(|(_, o)| o.is_some())
83 .filter_map(|(i, o)| Some((i, o?)))
84 }
85
86 pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
87 (0..).map(|i| self.get(i)).take_while(|o| o.is_some()).flatten()
88 }
89}
90
91impl<T: Copy + PartialEq> AppendOnlyVec<T> {
92 pub fn contains(&self, val: T) -> bool {
93 self.iter_enumerated().any(|(_, v)| v == val)
94 }
95}
96
97impl<A: Copy> FromIterator<A> for AppendOnlyVec<A> {
98 fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
99 let this = Self::new();
100 for val in iter {
101 this.push(val);
102 }
103 this
104 }
105}