about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/sync/vec.rs
blob: 99de33685f6e0a739bb729caf7d86760c2e5ceee (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::marker::PhantomData;

use rustc_index::vec::Idx;

#[derive(Default)]
pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
    #[cfg(not(parallel_compiler))]
    vec: elsa::vec::FrozenVec<T>,
    #[cfg(parallel_compiler)]
    vec: elsa::sync::LockFreeFrozenVec<T>,
    _marker: PhantomData<fn(&I)>,
}

impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
    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<T> {
        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<T: Copy> {
    #[cfg(not(parallel_compiler))]
    vec: elsa::vec::FrozenVec<T>,
    #[cfg(parallel_compiler)]
    vec: elsa::sync::LockFreeFrozenVec<T>,
}

impl<T: Copy> AppendOnlyVec<T> {
    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<T> {
        #[cfg(not(parallel_compiler))]
        return self.vec.get_copy(i);
        #[cfg(parallel_compiler)]
        return self.vec.get(i);
    }
}