use core::fmt;
use core::iter::FusedIterator;
use core::ops::{Index, IndexMut};
use super::append::AppendLog;
use crate::page::DEFAULT_PAGE;
use crate::paged_vec::PagedVec;
pub struct ConcurrentPagedVec<T, const PAGE: usize = DEFAULT_PAGE> {
log: AppendLog<T, PAGE>,
}
unsafe impl<T: Send, const PAGE: usize> Send for ConcurrentPagedVec<T, PAGE> {}
unsafe impl<T: Send + Sync, const PAGE: usize> Sync for ConcurrentPagedVec<T, PAGE> {}
impl<T, const PAGE: usize> ConcurrentPagedVec<T, PAGE> {
pub const PAGE: usize = PAGE;
pub fn new() -> Self {
Self {
log: AppendLog::new(),
}
}
pub fn push(&self, value: T) -> usize {
self.log
.push(value, usize::MAX)
.unwrap_or_else(|_| panic!("ConcurrentPagedVec length overflow"))
}
pub fn push_all(&self, iter: impl IntoIterator<Item = T>) {
for value in iter {
self.push(value);
}
}
pub fn get(&self, index: usize) -> Option<&T> {
self.log.get(index)
}
pub fn len(&self) -> usize {
self.log.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn reserved_len(&self) -> usize {
self.log.reserved_len()
}
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> + ExactSizeIterator + FusedIterator {
self.log.iter()
}
pub fn pages(
&self,
) -> impl DoubleEndedIterator<Item = &[T]> + ExactSizeIterator + FusedIterator {
self.log.pages()
}
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.log.get_mut(index)
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.log.iter_mut()
}
pub fn pages_mut(&mut self) -> impl Iterator<Item = &mut [T]> {
self.log.pages_mut()
}
pub fn pop(&mut self) -> Option<T> {
self.log.pop()
}
pub fn truncate(&mut self, len: usize) {
self.log.truncate(len);
}
pub fn clear(&mut self) {
self.log.clear();
}
pub fn into_inner(self) -> PagedVec<T, PAGE> {
self.log.into_inner()
}
}
impl<T, const PAGE: usize> Default for ConcurrentPagedVec<T, PAGE> {
fn default() -> Self {
Self::new()
}
}
impl<T: fmt::Debug, const PAGE: usize> fmt::Debug for ConcurrentPagedVec<T, PAGE> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<T, const PAGE: usize> Index<usize> for ConcurrentPagedVec<T, PAGE> {
type Output = T;
#[track_caller]
fn index(&self, index: usize) -> &T {
self.get(index)
.expect("index not visible in ConcurrentPagedVec")
}
}
impl<T, const PAGE: usize> IndexMut<usize> for ConcurrentPagedVec<T, PAGE> {
#[track_caller]
fn index_mut(&mut self, index: usize) -> &mut T {
self.get_mut(index)
.expect("index out of bounds in ConcurrentPagedVec")
}
}
impl<T, const PAGE: usize> Extend<T> for ConcurrentPagedVec<T, PAGE> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.push_all(iter);
}
}
impl<T, const PAGE: usize> FromIterator<T> for ConcurrentPagedVec<T, PAGE> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let result = Self::new();
result.push_all(iter);
result
}
}
impl<T, const PAGE: usize> From<PagedVec<T, PAGE>> for ConcurrentPagedVec<T, PAGE> {
fn from(inner: PagedVec<T, PAGE>) -> Self {
inner.into_iter().collect()
}
}