-
Notifications
You must be signed in to change notification settings - Fork 2
/
convec.rs
204 lines (185 loc) · 5.83 KB
/
convec.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
extern crate parking_lot;
use std::cell::UnsafeCell;
use std::ops::Index;
use std::{fmt, mem};
use parking_lot::RwLock;
const BASE: usize = 32;
/// A concurrent vector, only supporting push and indexed access
pub struct ConVec<T> {
len: RwLock<usize>,
allocations: [UnsafeCell<Vec<T>>; 64],
}
unsafe impl<T> Sync for ConVec<T> {}
unsafe impl<T> Send for ConVec<T> {}
impl<T> ConVec<T> {
pub fn new() -> Self {
ConVec {
len: RwLock::new(0),
allocations: [
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
UnsafeCell::new(vec![]),
],
}
}
// get the allocation and offset within it.
fn allocation(&self, mut offset: usize) -> (usize, usize) {
let mut compare = BASE;
let mut allocation = 0;
loop {
if compare > offset {
return (allocation, offset);
} else {
offset -= compare;
}
compare = compare << 1;
allocation += 1;
}
}
#[inline]
fn _get(&self, idx: usize) -> &T {
let (index, offset) = self.allocation(idx);
unsafe { &(*self.allocations[index].get())[offset] }
}
#[inline]
fn valid_index(&self, idx: usize) -> bool {
*self.len.read() > idx
}
pub fn len(&self) -> usize {
*self.len.read()
}
pub fn get(&self, idx: usize) -> Option<&T> {
if self.valid_index(idx) {
Some(self._get(idx))
} else {
None
}
}
pub unsafe fn get_unchecked(&self, idx: usize) -> &T {
self._get(idx)
}
pub fn push(&self, t: T) -> usize {
let mut guard = self.len.write();
let idx = *guard;
*guard += 1;
let (index, _) = self.allocation(idx);
unsafe {
let allocation = self.allocations[index].get();
if (*allocation).len() == 0 {
*allocation = Vec::with_capacity(BASE << index);
}
(*allocation).push(t);
}
idx
}
pub unsafe fn pop(&self) -> Option<T> {
let mut guard = self.len.write();
if *guard == 0 {
return None;
}
*guard -= 1;
let (index, _) = self.allocation(*guard);
let popped = (*self.allocations[index].get()).pop();
// deallocate
if (*self.allocations[index].get()).len() == 0 {
*self.allocations[index].get() = vec![];
}
popped
}
#[allow(unused)]
pub fn heap_size(&self) -> usize
where
T: ::std::fmt::Debug,
{
let mut heap_size = 0;
for i in 0..64 {
unsafe {
heap_size += (*self.allocations[i].get()).capacity() *
mem::size_of::<T>();
}
}
heap_size
}
}
impl<T: fmt::Debug> fmt::Debug for ConVec<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..64 {
let alloc = unsafe { &(*self.allocations[i].get()) };
if alloc.len() > 0 {
write!(f, "{:?} ", alloc)?;
}
}
Ok(())
}
}
impl<T> Index<usize> for ConVec<T> {
type Output = T;
fn index(&self, idx: usize) -> &Self::Output {
if self.valid_index(idx) {
self._get(idx)
} else {
panic!("Index out of range");
}
}
}