-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathvec.rs
74 lines (61 loc) · 1.71 KB
/
vec.rs
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
use std::marker::PhantomData;
use rustc_index::Idx;
#[derive(Default)]
pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
vec: elsa::sync::LockFreeFrozenVec<T>,
_marker: PhantomData<fn(&I)>,
}
impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
pub fn new() -> Self {
Self { vec: elsa::sync::LockFreeFrozenVec::new(), _marker: PhantomData }
}
pub fn push(&self, val: T) -> I {
let i = self.vec.push(val);
I::new(i)
}
pub fn get(&self, i: I) -> Option<T> {
let i = i.index();
self.vec.get(i)
}
}
#[derive(Default)]
pub struct AppendOnlyVec<T: Copy> {
vec: parking_lot::RwLock<Vec<T>>,
}
impl<T: Copy> AppendOnlyVec<T> {
pub fn new() -> Self {
Self { vec: Default::default() }
}
pub fn push(&self, val: T) -> usize {
let mut v = self.vec.write();
let n = v.len();
v.push(val);
n
}
pub fn get(&self, i: usize) -> Option<T> {
self.vec.read().get(i).copied()
}
pub fn iter_enumerated(&self) -> impl Iterator<Item = (usize, T)> {
(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<Item = T> {
(0..).map(|i| self.get(i)).take_while(|o| o.is_some()).flatten()
}
}
impl<T: Copy + PartialEq> AppendOnlyVec<T> {
pub fn contains(&self, val: T) -> bool {
self.iter_enumerated().any(|(_, v)| v == val)
}
}
impl<A: Copy> FromIterator<A> for AppendOnlyVec<A> {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
let this = Self::new();
for val in iter {
this.push(val);
}
this
}
}