use std::{
collections::TryReserveError,
fmt,
ops::{Deref, Index},
slice::{Iter, SliceIndex},
};
use crate::mem::{Root, ToRoot, Trace};
use super::Value;
pub struct Vector<'a> {
elements: Vec<Value<'a>>,
}
impl<'a> Vector<'a> {
#[must_use]
pub(crate) fn new(mut elements: Vec<Value<'a>>) -> Self {
for element in &mut elements {
element.unlock();
}
Self { elements }
}
#[must_use]
#[inline]
pub fn capacity(&self) -> usize {
self.elements.capacity()
}
#[inline]
pub fn clear(&mut self) {
self.elements.clear();
}
#[must_use]
#[inline]
pub fn get(&self, index: usize) -> Option<&Value<'a>> {
self.elements.get(index)
}
pub fn set(&mut self, index: usize, mut new_elem: Value<'a>) -> Option<Value<'a>> {
assert!(index <= self.elements.len(), "index out of bounds");
new_elem.unlock();
if let Some(element) = self.elements.get_mut(index) {
let mut old_elem = std::mem::replace(element, new_elem);
old_elem.lock();
Some(old_elem)
} else {
self.elements.insert(index, new_elem);
None
}
}
#[inline]
pub fn insert(&mut self, index: usize, mut element: Value<'a>) {
element.unlock();
self.elements.insert(index, element);
}
#[must_use]
#[inline]
pub fn is_empty(&self) -> bool {
self.elements.is_empty()
}
#[must_use]
#[inline]
pub fn len(&self) -> usize {
self.elements.len()
}
#[inline]
pub fn pop(&mut self) -> Option<Value<'a>> {
self.elements.pop().map(|mut element| {
element.lock();
element
})
}
#[inline]
pub fn push(&mut self, mut element: Value<'a>) {
element.unlock();
self.elements.push(element);
}
#[inline]
pub fn remove(&mut self, index: usize) -> Value<'a> {
let mut element = self.elements.remove(index);
element.lock();
element
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.elements.reserve(additional);
}
#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.elements.reserve_exact(additional);
}
#[inline]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&Value<'a>) -> bool,
{
self.elements.retain(f);
}
#[inline]
pub fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut Value<'a>) -> bool,
{
self.elements.retain_mut(f);
}
#[inline]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.elements.shrink_to(min_capacity);
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.elements.shrink_to_fit();
}
#[inline]
pub fn swap_remove(&mut self, index: usize) -> Value<'a> {
let mut element = self.elements.swap_remove(index);
element.lock();
element
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.elements.truncate(len);
}
#[inline]
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.elements.try_reserve(additional)
}
#[inline]
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.elements.try_reserve_exact(additional)
}
pub fn iter(&self) -> Iter<Value<'a>> {
self.elements.iter()
}
}
impl<'a> Deref for Vector<'a> {
type Target = [Value<'a>];
#[inline]
fn deref(&self) -> &Self::Target {
&self.elements
}
}
impl<'a, I: SliceIndex<[Value<'a>]>> Index<I> for Vector<'a> {
type Output = I::Output;
#[inline]
fn index(&self, index: I) -> &Self::Output {
self.elements.index(index)
}
}
impl<'a, 'b> IntoIterator for &'b Vector<'a> {
type Item = &'b Value<'a>;
type IntoIter = Iter<'b, Value<'a>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.elements.iter()
}
}
impl<'a> Trace<'a> for Vector<'a> {
fn trace(&self, traced: &mut Vec<Root<'a>>) {
for element in &self.elements {
if let Some(root) = element.to_root() {
traced.push(root);
}
}
}
}
impl<'a> PartialEq for Vector<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.elements == other.elements
}
}
impl<'a> fmt::Debug for Vector<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.elements.iter()).finish()
}
}