use std::cell::UnsafeCell;
use std::ops::Index;
use std::sync::atomic::{AtomicUsize, Ordering};
pub struct AppendOnlyVec<T> {
count: AtomicUsize,
reserved: AtomicUsize,
data: [UnsafeCell<*mut T>; BITS_USED - 1 - 3],
}
unsafe impl<T: Send> Send for AppendOnlyVec<T> {}
unsafe impl<T: Sync + Send> Sync for AppendOnlyVec<T> {}
const BITS: usize = std::mem::size_of::<usize>() * 8;
#[cfg(target_arch = "x86_64")]
const BITS_USED: usize = 48;
#[cfg(all(not(target_arch = "x86_64"), target_pointer_width = "64"))]
const BITS_USED: usize = 64;
#[cfg(target_pointer_width = "32")]
const BITS_USED: usize = 32;
#[allow(clippy::cast_possible_truncation)]
const fn indices(i: usize) -> (u32, usize) {
let i = i + 8;
let bin = BITS as u32 - 1 - i.leading_zeros();
let bin = bin - 3;
let offset = i - bin_size(bin);
(bin, offset)
}
const fn bin_size(array: u32) -> usize {
(1 << 3) << array
}
#[test]
fn test_indices() {
for i in 0..32 {
println!("{:3}: {} {}", i, indices(i).0, indices(i).1);
}
let mut array = 0;
let mut offset = 0;
let mut index = 0;
while index < 1000 {
index += 1;
offset += 1;
if offset >= bin_size(array) {
offset = 0;
array += 1;
}
assert_eq!(indices(index), (array, offset));
}
}
fn layout<T>(array: u32) -> std::alloc::Layout {
std::alloc::Layout::array::<T>(bin_size(array)).unwrap()
}
impl<T> AppendOnlyVec<T> {
#[cfg(test)]
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> + ExactSizeIterator {
(0..self.len()).map(|i| unsafe { self.get_unchecked(i) })
}
#[cfg(test)]
unsafe fn get_unchecked(&self, idx: usize) -> &T {
let (array, offset) = indices(idx);
let ptr = *self.data[array as usize].get();
&*ptr.add(offset)
}
#[inline]
pub fn len(&self) -> usize {
self.count.load(Ordering::Acquire)
}
pub fn push(&self, val: T) -> usize {
let idx = self.reserved.fetch_add(1, Ordering::Relaxed);
let (array, offset) = indices(idx);
let ptr = if self.len() < 1 + idx - offset {
if offset == 0 {
let layout = layout::<T>(array);
let ptr = unsafe { std::alloc::alloc(layout) }.cast::<T>();
unsafe {
*self.data[array as usize].get() = ptr;
}
ptr
} else {
while self.len() < 1 + idx - offset {
std::hint::spin_loop();
}
unsafe { *self.data[array as usize].get() }
}
} else {
unsafe { *self.data[array as usize].get() }
};
unsafe { (ptr.add(offset)).write(val) };
while self.count.compare_exchange(idx, idx + 1, Ordering::Release, Ordering::Relaxed).is_err() {
std::hint::spin_loop();
}
idx
}
#[allow(clippy::declare_interior_mutable_const)] const EMPTY: UnsafeCell<*mut T> = UnsafeCell::new(std::ptr::null_mut());
pub const fn new() -> Self {
Self { count: AtomicUsize::new(0), reserved: AtomicUsize::new(0), data: [Self::EMPTY; BITS_USED - 1 - 3] }
}
}
impl<T> Index<usize> for AppendOnlyVec<T> {
type Output = T;
fn index(&self, idx: usize) -> &Self::Output {
assert!(idx < self.len()); let (array, offset) = indices(idx);
let ptr = unsafe { *self.data[array as usize].get() };
unsafe { &*ptr.add(offset) }
}
}
impl<T> Drop for AppendOnlyVec<T> {
fn drop(&mut self) {
for idx in 0..self.len() {
let (array, offset) = indices(idx);
let ptr = unsafe { *self.data[array as usize].get() };
unsafe {
std::ptr::drop_in_place(ptr.add(offset));
}
}
#[allow(clippy::cast_possible_truncation)]
for array in 0..self.data.len() as u32 {
let ptr = unsafe { *self.data[array as usize].get() };
if ptr.is_null() {
break;
}
let layout = layout::<T>(array);
unsafe { std::alloc::dealloc(ptr.cast::<u8>(), layout) };
}
}
}
#[test]
fn test_pushing_and_indexing() {
let v = AppendOnlyVec::<usize>::new();
for n in 0..50 {
v.push(n);
assert_eq!(v.len(), n + 1);
for i in 0..=n {
assert_eq!(v[i], i);
}
}
let vec: Vec<usize> = v.iter().copied().collect();
let ve2: Vec<usize> = (0..50).collect();
assert_eq!(vec, ve2);
}
#[test]
fn test_parallel_pushing() {
use std::sync::Arc;
const N: u64 = 100;
let v = Arc::new(AppendOnlyVec::<u64>::new());
let mut threads = Vec::new();
for thread_num in 0..N {
let v = v.clone();
threads.push(std::thread::spawn(move || {
let which1 = v.push(thread_num);
let which2 = v.push(thread_num);
assert_eq!(v[which1], thread_num);
assert_eq!(v[which2], thread_num);
}));
}
for t in threads {
t.join().ok();
}
for thread_num in 0..N {
assert_eq!(2, v.iter().copied().filter(|&x| x == thread_num).count());
}
}