2023-02-21 08:37:10 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use rustc_index::vec::Idx;
|
|
|
|
|
2023-03-14 12:11:56 +00:00
|
|
|
#[derive(Default)]
|
2023-03-14 11:51:00 +00:00
|
|
|
pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
|
2023-02-21 08:37:10 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
vec: elsa::vec::FrozenVec<T>,
|
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
vec: elsa::sync::LockFreeFrozenVec<T>,
|
|
|
|
_marker: PhantomData<fn(&I)>,
|
|
|
|
}
|
|
|
|
|
2023-03-14 11:51:00 +00:00
|
|
|
impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
|
2023-02-21 08:37:10 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 11:51:00 +00:00
|
|
|
|
2023-03-14 12:11:56 +00:00
|
|
|
#[derive(Default)]
|
2023-03-14 11:51:00 +00:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 12:11:56 +00:00
|
|
|
pub fn push(&self, val: T) -> usize {
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
let i = self.vec.len();
|
2023-03-14 11:51:00 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
self.vec.push(val);
|
|
|
|
#[cfg(parallel_compiler)]
|
2023-03-14 12:11:56 +00:00
|
|
|
let i = self.vec.push(val);
|
|
|
|
i
|
2023-03-14 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 12:16:19 +00:00
|
|
|
|
|
|
|
impl<T: Copy + PartialEq> AppendOnlyVec<T> {
|
|
|
|
pub fn contains(&self, val: T) -> bool {
|
|
|
|
for i in 0.. {
|
|
|
|
match self.get(i) {
|
|
|
|
None => return false,
|
|
|
|
Some(v) => {
|
|
|
|
if val == v {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|